diff --git a/libraries/wutstdc++/CMakeLists.txt b/libraries/wutstdc++/CMakeLists.txt index 0362850..d807475 100644 --- a/libraries/wutstdc++/CMakeLists.txt +++ b/libraries/wutstdc++/CMakeLists.txt @@ -2,8 +2,13 @@ cmake_minimum_required(VERSION 3.2) project(wutstdc++ CXX) add_library(wutstdc++ - thread.cc + gthread.cc + gthread_cond.cc gthread_keys.cc + gthread_mutex.cc + gthread_once.cc + gthread_recursive_mutex.cc + thread.cc include/bits/gthr-default.h) target_compile_definitions(wutstdc++ diff --git a/libraries/wutstdc++/gthread.cc b/libraries/wutstdc++/gthread.cc new file mode 100644 index 0000000..2aa0c3b --- /dev/null +++ b/libraries/wutstdc++/gthread.cc @@ -0,0 +1,78 @@ +#include "include/bits/gthr-default.h" + +static void +__gthread_thread_deallocator(OSThread *thread, void *stack) +{ + MEMExpandedHeap *heap = (MEMExpandedHeap *)MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); + MEMFreeToExpHeap(heap, thread); + MEMFreeToExpHeap(heap, stack); +} + +static void +__gthread_thread_cleanup(OSThread *thread, void *stack) +{ + __gthread_key_cleanup(thread); +} + +int +__gthread_create (__gthread_t *__threadid, void *(*__func) (void*), void *__args) +{ + MEMExpandedHeap *heap = (MEMExpandedHeap *)MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); + OSThread *thread = (OSThread *)MEMAllocFromExpHeapEx(heap, sizeof(OSThread), 8); + char *stack = (char *)MEMAllocFromExpHeapEx(heap, __GTHREAD_STACK_SIZE, 8); + memset(thread, 0, sizeof(OSThread)); + + if (!OSCreateThread(thread, + (OSThreadEntryPointFn)__func, + (int)__args, + NULL, + stack + __GTHREAD_STACK_SIZE, + __GTHREAD_STACK_SIZE, + 16, + OS_THREAD_ATTRIB_AFFINITY_ANY)) { + MEMFreeToExpHeap((MEMExpandedHeap*)MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2), thread); + return EINVAL; + } + + *__threadid = thread; + OSSetThreadDeallocator(thread, &__gthread_thread_deallocator); + OSSetThreadCleanupCallback(thread, &__gthread_thread_cleanup); + OSResumeThread(thread); + return 0; +} + +int +__gthread_join (__gthread_t __threadid, void **__value_ptr) +{ + if (!OSJoinThread(__threadid, (int *)__value_ptr)) { + return EINVAL; + } + + return 0; +} + +int +__gthread_detach (__gthread_t __threadid) +{ + OSDetachThread(__threadid); + return 0; +} + +int +__gthread_equal (__gthread_t __t1, __gthread_t __t2) +{ + return __t1 == __t2; +} + +__gthread_t +__gthread_self (void) +{ + return OSGetCurrentThread(); +} + +int +__gthread_yield (void) +{ + OSYieldThread(); + return 0; +} diff --git a/libraries/wutstdc++/gthread_cond.cc b/libraries/wutstdc++/gthread_cond.cc new file mode 100644 index 0000000..5ce1254 --- /dev/null +++ b/libraries/wutstdc++/gthread_cond.cc @@ -0,0 +1,42 @@ +#include "include/bits/gthr-default.h" + +void +__gthread_cond_init_function (__gthread_cond_t *__cond) +{ + OSInitCond(__cond); +} + +int +__gthread_cond_broadcast (__gthread_cond_t *__cond) +{ + OSSignalCond(__cond); + return 0; +} + +int +__gthread_cond_signal (__gthread_cond_t *__cond) +{ + OSSignalCond(__cond); + return 0; +} + +int +__gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex) +{ + OSWaitCond(__cond, __mutex); + return 0; +} + +int +__gthread_cond_wait_recursive (__gthread_cond_t *__cond, + __gthread_recursive_mutex_t *__mutex) +{ + OSWaitCond(__cond, __mutex); + return 0; +} + +int +__gthread_cond_destroy (__gthread_cond_t* __cond) +{ + return 0; +} diff --git a/libraries/wutstdc++/gthread_mutex.cc b/libraries/wutstdc++/gthread_mutex.cc new file mode 100644 index 0000000..266250f --- /dev/null +++ b/libraries/wutstdc++/gthread_mutex.cc @@ -0,0 +1,37 @@ +#include "include/bits/gthr-default.h" + +void +__gthread_mutex_init_function (__gthread_mutex_t *__mutex) +{ + OSInitMutex(__mutex); +} + +int +__gthread_mutex_lock (__gthread_mutex_t *__mutex) +{ + OSLockMutex(__mutex); + return 0; +} + +int +__gthread_mutex_trylock (__gthread_mutex_t *__mutex) +{ + if (!OSTryLockMutex(__mutex)) { + return -1; + } + + return 0; +} + +int +__gthread_mutex_unlock (__gthread_mutex_t *__mutex) +{ + OSUnlockMutex(__mutex); + return 0; +} + +int +__gthread_mutex_destroy (__gthread_mutex_t *__mutex) +{ + return 0; +} diff --git a/libraries/wutstdc++/gthread_once.cc b/libraries/wutstdc++/gthread_once.cc new file mode 100644 index 0000000..7823dfe --- /dev/null +++ b/libraries/wutstdc++/gthread_once.cc @@ -0,0 +1,23 @@ +#include "include/bits/gthr-default.h" + +int +__gthread_once (__gthread_once_t *__once, void (*__func) (void)) +{ + uint32_t value = 0; + + if (OSCompareAndSwapAtomicEx(__once, + __GTHREAD_ONCE_VALUE_INIT, + __GTHREAD_ONCE_VALUE_STARTED, + &value)) { + __func(); + OSCompareAndSwapAtomic(__once, + __GTHREAD_ONCE_VALUE_STARTED, + __GTHREAD_ONCE_VALUE_DONE); + } else if (value != __GTHREAD_ONCE_VALUE_DONE) { + while (!OSCompareAndSwapAtomic(__once, + __GTHREAD_ONCE_VALUE_DONE, + __GTHREAD_ONCE_VALUE_DONE)); + } + + return 0; +} diff --git a/libraries/wutstdc++/gthread_recursive_mutex.cc b/libraries/wutstdc++/gthread_recursive_mutex.cc new file mode 100644 index 0000000..8a1011e --- /dev/null +++ b/libraries/wutstdc++/gthread_recursive_mutex.cc @@ -0,0 +1,38 @@ +#include "include/bits/gthr-default.h" + +int +__gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex) +{ + OSInitMutex(__mutex); + return 0; +} + +int +__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex) +{ + OSLockMutex(__mutex); + return 0; +} + +int +__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex) +{ + if (!OSTryLockMutex(__mutex)) { + return -1; + } + + return 0; +} + +int +__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex) +{ + OSUnlockMutex(__mutex); + return 0; +} + +int +__gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex) +{ + return 0; +} diff --git a/libraries/wutstdc++/include/bits/gthr-default.h b/libraries/wutstdc++/include/bits/gthr-default.h index 737dad7..578d112 100644 --- a/libraries/wutstdc++/include/bits/gthr-default.h +++ b/libraries/wutstdc++/include/bits/gthr-default.h @@ -72,108 +72,31 @@ __gthread_active_p (void) return 1; } -static inline void -__gthread_thread_deallocator(OSThread *thread, void *stack) -{ - MEMExpandedHeap *heap = (MEMExpandedHeap *)MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); - MEMFreeToExpHeap(heap, thread); - MEMFreeToExpHeap(heap, stack); -} +int +__gthread_create (__gthread_t *__threadid, void *(*__func) (void*), + void *__args); + +int +__gthread_join (__gthread_t __threadid, void **__value_ptr); + +int +__gthread_detach (__gthread_t __threadid); + +int +__gthread_equal (__gthread_t __t1, __gthread_t __t2); + +__gthread_t +__gthread_self (void); + +int +__gthread_yield (void); + +int +__gthread_once (__gthread_once_t *__once, void (*__func) (void)); void __gthread_key_cleanup (OSThread *thread); -static inline void -__gthread_thread_cleanup(OSThread *thread, void *stack) -{ - __gthread_key_cleanup(thread); -} - -static inline int -__gthread_create (__gthread_t *__threadid, void *(*__func) (void*), - void *__args) -{ - MEMExpandedHeap *heap = (MEMExpandedHeap *)MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); - OSThread *thread = (OSThread *)MEMAllocFromExpHeapEx(heap, sizeof(OSThread), 8); - char *stack = (char *)MEMAllocFromExpHeapEx(heap, __GTHREAD_STACK_SIZE, 8); - memset(thread, 0, sizeof(OSThread)); - - if (!OSCreateThread(thread, - (OSThreadEntryPointFn)__func, - (int)__args, - NULL, - stack + __GTHREAD_STACK_SIZE, - __GTHREAD_STACK_SIZE, - 16, - OS_THREAD_ATTRIB_AFFINITY_ANY)) { - MEMFreeToExpHeap((MEMExpandedHeap*)MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2), thread); - return EINVAL; - } - - *__threadid = thread; - OSSetThreadDeallocator(thread, &__gthread_thread_deallocator); - OSSetThreadCleanupCallback(thread, &__gthread_thread_cleanup); - OSResumeThread(thread); - return 0; -} - -static inline int -__gthread_join (__gthread_t __threadid, void **__value_ptr) -{ - if (!OSJoinThread(__threadid, (int *)__value_ptr)) { - return EINVAL; - } - return 0; -} - -static inline int -__gthread_detach (__gthread_t __threadid) -{ - OSDetachThread(__threadid); - return 0; -} - -static inline int -__gthread_equal (__gthread_t __t1, __gthread_t __t2) -{ - return __t1 == __t2; -} - -static inline __gthread_t -__gthread_self (void) -{ - return OSGetCurrentThread(); -} - -static inline int -__gthread_yield (void) -{ - OSYieldThread(); - return 0; -} - -static inline int -__gthread_once (__gthread_once_t *__once, void (*__func) (void)) -{ - uint32_t value = 0; - - if (OSCompareAndSwapAtomicEx(__once, - __GTHREAD_ONCE_VALUE_INIT, - __GTHREAD_ONCE_VALUE_STARTED, - &value)) { - __func(); - OSCompareAndSwapAtomic(__once, - __GTHREAD_ONCE_VALUE_STARTED, - __GTHREAD_ONCE_VALUE_DONE); - } else if (value != __GTHREAD_ONCE_VALUE_DONE) { - while (!OSCompareAndSwapAtomic(__once, - __GTHREAD_ONCE_VALUE_DONE, - __GTHREAD_ONCE_VALUE_DONE)); - } - - return 0; -} - int __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *)); @@ -186,118 +109,53 @@ __gthread_getspecific (__gthread_key_t __key); int __gthread_setspecific (__gthread_key_t __key, const void *__ptr); -static inline void -__gthread_mutex_init_function (__gthread_mutex_t *__mutex) -{ - OSInitMutex(__mutex); -} +void +__gthread_mutex_init_function (__gthread_mutex_t *__mutex); -static inline int -__gthread_mutex_destroy (__gthread_mutex_t *__mutex) -{ - return 0; -} +int +__gthread_mutex_destroy (__gthread_mutex_t *__mutex); -static inline int -__gthread_mutex_lock (__gthread_mutex_t *__mutex) -{ - OSLockMutex(__mutex); - return 0; -} +int +__gthread_mutex_lock (__gthread_mutex_t *__mutex); -static inline int -__gthread_mutex_trylock (__gthread_mutex_t *__mutex) -{ - if (!OSTryLockMutex(__mutex)) { - return -1; - } +int +__gthread_mutex_trylock (__gthread_mutex_t *__mutex); - return 0; -} +int +__gthread_mutex_unlock (__gthread_mutex_t *__mutex); -static inline int -__gthread_mutex_unlock (__gthread_mutex_t *__mutex) -{ - OSUnlockMutex(__mutex); - return 0; -} +int +__gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex); -static inline int -__gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex) -{ - OSInitMutex(__mutex); - return 0; -} +int +__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex); -static inline int -__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex) -{ - OSLockMutex(__mutex); - return 0; -} +int +__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex); -static inline int -__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex) -{ - if (!OSTryLockMutex(__mutex)) { - return -1; - } +int +__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex); - return 0; -} +int +__gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex); -static inline int -__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex) -{ - OSUnlockMutex(__mutex); - return 0; -} +void +__gthread_cond_init_function (__gthread_cond_t *__cond); -static inline int -__gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex) -{ - return 0; -} +int +__gthread_cond_broadcast (__gthread_cond_t *__cond); -static inline void -__gthread_cond_init_function (__gthread_cond_t *__cond) -{ - OSInitCond(__cond); -} +int +__gthread_cond_signal (__gthread_cond_t *__cond); -static inline int -__gthread_cond_broadcast (__gthread_cond_t *__cond) -{ - OSSignalCond(__cond); - return 0; -} +int +__gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex); -static inline int -__gthread_cond_signal (__gthread_cond_t *__cond) -{ - OSSignalCond(__cond); - return 0; -} - -static inline int -__gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex) -{ - OSWaitCond(__cond, __mutex); - return 0; -} - -static inline int +int __gthread_cond_wait_recursive (__gthread_cond_t *__cond, - __gthread_recursive_mutex_t *__mutex) -{ - OSWaitCond(__cond, __mutex); - return 0; -} + __gthread_recursive_mutex_t *__mutex); -static inline int -__gthread_cond_destroy (__gthread_cond_t* __cond) -{ - return 0; -} +int +__gthread_cond_destroy (__gthread_cond_t* __cond); #endif /* ! _GLIBCXX_GCC_GTHR_SINGLE_H */