summaryrefslogtreecommitdiff
path: root/components/cplusplus/cpp11/armclang/thread.cpp
diff options
context:
space:
mode:
authorguo <[email protected]>2021-09-28 10:36:54 +0800
committerGitHub <[email protected]>2021-09-28 10:36:54 +0800
commit55a302cd092758a44b589d65470a69d9e8ae4ced (patch)
treee60fc53994c426007e9eb4877e5bd123f124ef39 /components/cplusplus/cpp11/armclang/thread.cpp
parent00727d6b6db92dc1cf98f028f26fd6168594b905 (diff)
parent00d42ef73cff61ecda7ff006b077a73e1a4ff669 (diff)
Merge branch 'master' into usbh_construct
Diffstat (limited to 'components/cplusplus/cpp11/armclang/thread.cpp')
-rw-r--r--components/cplusplus/cpp11/armclang/thread.cpp109
1 files changed, 109 insertions, 0 deletions
diff --git a/components/cplusplus/cpp11/armclang/thread.cpp b/components/cplusplus/cpp11/armclang/thread.cpp
new file mode 100644
index 0000000000..f7f0cb4077
--- /dev/null
+++ b/components/cplusplus/cpp11/armclang/thread.cpp
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2021-04-27 flybreak the first version.
+ */
+
+#include <arm-tpl.h>
+#include "tpl.h"
+#include <cstdio>
+#include <pthread.h>
+
+extern "C" int __ARM_TPL_thread_create(__ARM_TPL_thread_t *__t,
+ void *(*__func)(void *),
+ void *__arg)
+{
+ int ret = 0;
+ /* TODO memory leek */
+ pthread_t *pid = (pthread_t *)rt_malloc(sizeof(pthread_t));
+ if (pid == nullptr)
+ return -1;
+ ret = pthread_create(pid, RT_NULL, __func, __arg);
+ if (ret == 0)
+ {
+ __t->data = (std::uintptr_t)pid;
+ return 0;
+ }
+ return -1;
+}
+
+extern "C" int __ARM_TPL_thread_id_compare(__ARM_TPL_thread_id __tid1,
+ __ARM_TPL_thread_id __tid2)
+{
+ if (__tid1 > __tid2)
+ return 1;
+ else if (__tid1 < __tid2)
+ return -1;
+ else
+ return 0;
+}
+
+extern "C" __ARM_TPL_thread_id __ARM_TPL_thread_get_current_id()
+{
+ return (__ARM_TPL_thread_id)pthread_self();
+}
+
+extern "C" __ARM_TPL_thread_id __ARM_TPL_thread_get_id(
+ const __ARM_TPL_thread_t *__t)
+{
+ return (__ARM_TPL_thread_id)((*(pthread_t *)__t->data));
+}
+
+extern "C" int __ARM_TPL_thread_join(__ARM_TPL_thread_t *__t)
+{
+ pthread_join((*(pthread_t *)__t->data), RT_NULL);
+ return 0;
+}
+
+extern "C" int __ARM_TPL_thread_detach(__ARM_TPL_thread_t *__t)
+{
+ pthread_detach((*(pthread_t *)__t->data));
+ return 0;
+}
+
+extern "C" void __ARM_TPL_thread_yield()
+{
+ rt_thread_yield();
+}
+
+extern "C" int __ARM_TPL_thread_nanosleep(const __ARM_TPL_timespec_t *__req,
+ __ARM_TPL_timespec_t *__rem)
+{
+ return nanosleep(__req, rem);
+}
+
+extern "C" unsigned __ARM_TPL_thread_hw_concurrency()
+{
+ return 1;
+}
+
+extern "C" int __ARM_TPL_tls_create(__ARM_TPL_tls_key *__key,
+ void (*__at_exit)(void *))
+{
+ pthread_key_t key;
+
+ if (pthread_key_create(&key, __at_exit) == 0)
+ {
+ *__key = key;
+ return 0;
+ }
+ return -1;
+}
+
+extern "C" void *__ARM_TPL_tls_get(__ARM_TPL_tls_key __key)
+{
+ return pthread_getspecific(__key);
+}
+
+extern "C" int __ARM_TPL_tls_set(__ARM_TPL_tls_key __key, void *__p)
+{
+ if (pthread_setspecific(__key, (void*)__p) != 0)
+ {
+ return -1;
+ }
+ return 0;
+}