| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #include "platform_api_vmcore.h"
- #include "platform_api_extension.h"
- #define bh_assert(v) assert(v)
- #define BH_SEM_COUNT_MAX 0xFFFF
- struct os_thread_data;
- typedef struct os_thread_wait_node {
- korp_sem sem;
- void *retval;
- os_thread_wait_list next;
- } os_thread_wait_node;
- typedef struct os_thread_data {
- /* Next thread data */
- struct os_thread_data *next;
- /* Thread data of parent thread */
- struct os_thread_data *parent;
- /* Thread Id */
- DWORD thread_id;
- /* Thread start routine */
- thread_start_routine_t start_routine;
- /* Thread start routine argument */
- void *arg;
- /* Wait node of current thread */
- os_thread_wait_node wait_node;
- /* Wait cond */
- korp_cond wait_cond;
- /* Wait lock */
- korp_mutex wait_lock;
- /* Waiting list of other threads who are joining this thread */
- os_thread_wait_list thread_wait_list;
- } os_thread_data;
- static bool is_thread_sys_inited = false;
- /* Thread data of supervisor thread */
- static os_thread_data supervisor_thread_data;
- /* Thread data key */
- static DWORD thread_data_key;
- /* The GetCurrentThreadStackLimits API from "kernel32" */
- static void(WINAPI *GetCurrentThreadStackLimits_Kernel32)(PULONG_PTR,
- PULONG_PTR) = NULL;
- int
- os_sem_init(korp_sem *sem);
- int
- os_sem_destroy(korp_sem *sem);
- int
- os_sem_wait(korp_sem *sem);
- int
- os_sem_reltimed_wait(korp_sem *sem, uint64 useconds);
- int
- os_sem_signal(korp_sem *sem);
- int
- os_thread_sys_init()
- {
- HMODULE module;
- if (is_thread_sys_inited)
- return BHT_OK;
- if ((thread_data_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
- return BHT_ERROR;
- /* Initialize supervisor thread data */
- memset(&supervisor_thread_data, 0, sizeof(os_thread_data));
- supervisor_thread_data.thread_id = GetCurrentThreadId();
- if (os_sem_init(&supervisor_thread_data.wait_node.sem) != BHT_OK)
- goto fail1;
- if (os_mutex_init(&supervisor_thread_data.wait_lock) != BHT_OK)
- goto fail2;
- if (os_cond_init(&supervisor_thread_data.wait_cond) != BHT_OK)
- goto fail3;
- if (!TlsSetValue(thread_data_key, &supervisor_thread_data))
- goto fail4;
- if ((module = GetModuleHandle((LPSTR) "kernel32"))) {
- *(void **)&GetCurrentThreadStackLimits_Kernel32 =
- GetProcAddress(module, "GetCurrentThreadStackLimits");
- }
- is_thread_sys_inited = true;
- return BHT_OK;
- fail4:
- os_cond_destroy(&supervisor_thread_data.wait_cond);
- fail3:
- os_mutex_destroy(&supervisor_thread_data.wait_lock);
- fail2:
- os_sem_destroy(&supervisor_thread_data.wait_node.sem);
- fail1:
- TlsFree(thread_data_key);
- return BHT_ERROR;
- }
- void
- os_thread_sys_destroy()
- {
- if (is_thread_sys_inited) {
- os_cond_destroy(&supervisor_thread_data.wait_cond);
- os_mutex_destroy(&supervisor_thread_data.wait_lock);
- os_sem_destroy(&supervisor_thread_data.wait_node.sem);
- memset(&supervisor_thread_data, 0, sizeof(os_thread_data));
- TlsFree(thread_data_key);
- thread_data_key = 0;
- is_thread_sys_inited = false;
- }
- }
- static os_thread_data *
- thread_data_current()
- {
- return (os_thread_data *)TlsGetValue(thread_data_key);
- }
- static void
- os_thread_cleanup(void *retval)
- {
- os_thread_data *thread_data = thread_data_current();
- bh_assert(thread_data != NULL);
- os_mutex_lock(&thread_data->wait_lock);
- if (thread_data->thread_wait_list) {
- /* Signal each joining thread */
- os_thread_wait_list head = thread_data->thread_wait_list;
- while (head) {
- os_thread_wait_list next = head->next;
- head->retval = retval;
- os_sem_signal(&head->sem);
- head = next;
- }
- thread_data->thread_wait_list = NULL;
- }
- os_mutex_unlock(&thread_data->wait_lock);
- /* Destroy resources */
- os_cond_destroy(&thread_data->wait_cond);
- os_sem_destroy(&thread_data->wait_node.sem);
- os_mutex_destroy(&thread_data->wait_lock);
- BH_FREE(thread_data);
- }
- static unsigned __stdcall os_thread_wrapper(void *arg)
- {
- os_thread_data *thread_data = arg;
- os_thread_data *parent = thread_data->parent;
- void *retval;
- bool result;
- #if 0
- os_printf("THREAD CREATED %p\n", thread_data);
- #endif
- os_mutex_lock(&parent->wait_lock);
- thread_data->thread_id = GetCurrentThreadId();
- result = TlsSetValue(thread_data_key, thread_data);
- #ifdef OS_ENABLE_HW_BOUND_CHECK
- if (result)
- result = os_thread_signal_init() == 0 ? true : false;
- #endif
- /* Notify parent thread */
- os_cond_signal(&parent->wait_cond);
- os_mutex_unlock(&parent->wait_lock);
- if (!result)
- return -1;
- retval = thread_data->start_routine(thread_data->arg);
- os_thread_cleanup(retval);
- return 0;
- }
- int
- os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
- void *arg, unsigned int stack_size, int prio)
- {
- os_thread_data *parent = thread_data_current();
- os_thread_data *thread_data;
- if (!p_tid || !start)
- return BHT_ERROR;
- if (stack_size < BH_APPLET_PRESERVED_STACK_SIZE)
- stack_size = BH_APPLET_PRESERVED_STACK_SIZE;
- if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
- return BHT_ERROR;
- memset(thread_data, 0, sizeof(os_thread_data));
- thread_data->parent = parent;
- thread_data->start_routine = start;
- thread_data->arg = arg;
- if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
- goto fail1;
- if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
- goto fail2;
- if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
- goto fail3;
- os_mutex_lock(&parent->wait_lock);
- if (!_beginthreadex(NULL, stack_size, os_thread_wrapper, thread_data, 0,
- NULL)) {
- os_mutex_unlock(&parent->wait_lock);
- goto fail4;
- }
- /* Wait for the thread routine to set thread_data's tid
- and add thread_data to thread data list */
- os_cond_wait(&parent->wait_cond, &parent->wait_lock);
- os_mutex_unlock(&parent->wait_lock);
- *p_tid = (korp_tid)thread_data;
- return BHT_OK;
- fail4:
- os_cond_destroy(&thread_data->wait_cond);
- fail3:
- os_mutex_destroy(&thread_data->wait_lock);
- fail2:
- os_sem_destroy(&thread_data->wait_node.sem);
- fail1:
- BH_FREE(thread_data);
- return BHT_ERROR;
- }
- int
- os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
- unsigned int stack_size)
- {
- return os_thread_create_with_prio(tid, start, arg, stack_size,
- BH_THREAD_DEFAULT_PRIORITY);
- }
- korp_tid
- os_self_thread()
- {
- return (korp_tid)TlsGetValue(thread_data_key);
- }
- int
- os_thread_join(korp_tid thread, void **p_retval)
- {
- os_thread_data *thread_data, *curr_thread_data;
- /* Get thread data of current thread */
- curr_thread_data = thread_data_current();
- curr_thread_data->wait_node.next = NULL;
- /* Get thread data of thread to join */
- thread_data = (os_thread_data *)thread;
- bh_assert(thread_data);
- os_mutex_lock(&thread_data->wait_lock);
- if (!thread_data->thread_wait_list)
- thread_data->thread_wait_list = &curr_thread_data->wait_node;
- else {
- /* Add to end of waiting list */
- os_thread_wait_node *p = thread_data->thread_wait_list;
- while (p->next)
- p = p->next;
- p->next = &curr_thread_data->wait_node;
- }
- os_mutex_unlock(&thread_data->wait_lock);
- /* Wait the sem */
- os_sem_wait(&curr_thread_data->wait_node.sem);
- if (p_retval)
- *p_retval = curr_thread_data->wait_node.retval;
- return BHT_OK;
- }
- int
- os_thread_detach(korp_tid thread)
- {
- /* Do nothing */
- return BHT_OK;
- (void)thread;
- }
- void
- os_thread_exit(void *retval)
- {
- os_thread_cleanup(retval);
- _endthreadex(0);
- }
- int
- os_thread_env_init()
- {
- os_thread_data *thread_data = TlsGetValue(thread_data_key);
- if (thread_data)
- /* Already created */
- return BHT_OK;
- if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
- return BHT_ERROR;
- memset(thread_data, 0, sizeof(os_thread_data));
- thread_data->thread_id = GetCurrentThreadId();
- if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
- goto fail1;
- if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
- goto fail2;
- if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
- goto fail3;
- if (!TlsSetValue(thread_data_key, thread_data))
- goto fail4;
- return BHT_OK;
- fail4:
- os_cond_destroy(&thread_data->wait_cond);
- fail3:
- os_mutex_destroy(&thread_data->wait_lock);
- fail2:
- os_sem_destroy(&thread_data->wait_node.sem);
- fail1:
- BH_FREE(thread_data);
- return BHT_ERROR;
- }
- void
- os_thread_env_destroy()
- {
- os_thread_data *thread_data = TlsGetValue(thread_data_key);
- /* Note that supervisor_thread_data's resources will be destroyed
- by os_thread_sys_destroy() */
- if (thread_data && thread_data != &supervisor_thread_data) {
- TlsSetValue(thread_data_key, NULL);
- os_cond_destroy(&thread_data->wait_cond);
- os_mutex_destroy(&thread_data->wait_lock);
- os_sem_destroy(&thread_data->wait_node.sem);
- BH_FREE(thread_data);
- }
- }
- bool
- os_thread_env_inited()
- {
- os_thread_data *thread_data = TlsGetValue(thread_data_key);
- return thread_data ? true : false;
- }
- int
- os_sem_init(korp_sem *sem)
- {
- bh_assert(sem);
- *sem = CreateSemaphore(NULL, 0, BH_SEM_COUNT_MAX, NULL);
- return (*sem != NULL) ? BHT_OK : BHT_ERROR;
- }
- int
- os_sem_destroy(korp_sem *sem)
- {
- bh_assert(sem);
- CloseHandle(*sem);
- return BHT_OK;
- }
- int
- os_sem_wait(korp_sem *sem)
- {
- DWORD ret;
- bh_assert(sem);
- ret = WaitForSingleObject(*sem, INFINITE);
- if (ret == WAIT_OBJECT_0)
- return BHT_OK;
- else if (ret == WAIT_TIMEOUT)
- return (int)WAIT_TIMEOUT;
- else /* WAIT_FAILED or others */
- return BHT_ERROR;
- }
- int
- os_sem_reltimed_wait(korp_sem *sem, uint64 useconds)
- {
- uint64 mseconds_64;
- DWORD ret, mseconds;
- bh_assert(sem);
- if (useconds == BHT_WAIT_FOREVER)
- mseconds = INFINITE;
- else {
- mseconds_64 = useconds / 1000;
- if (mseconds_64 < (uint64)(UINT32_MAX - 1)) {
- mseconds = (uint32)mseconds_64;
- }
- else {
- mseconds = UINT32_MAX - 1;
- os_printf("Warning: os_sem_reltimed_wait exceeds limit, "
- "set to max timeout instead\n");
- }
- }
- ret = WaitForSingleObject(*sem, mseconds);
- if (ret == WAIT_OBJECT_0)
- return BHT_OK;
- else if (ret == WAIT_TIMEOUT)
- return (int)WAIT_TIMEOUT;
- else /* WAIT_FAILED or others */
- return BHT_ERROR;
- }
- int
- os_sem_signal(korp_sem *sem)
- {
- bh_assert(sem);
- return ReleaseSemaphore(*sem, 1, NULL) != FALSE ? BHT_OK : BHT_ERROR;
- }
- int
- os_mutex_init(korp_mutex *mutex)
- {
- bh_assert(mutex);
- *mutex = CreateMutex(NULL, FALSE, NULL);
- return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
- }
- int
- os_recursive_mutex_init(korp_mutex *mutex)
- {
- bh_assert(mutex);
- *mutex = CreateMutex(NULL, FALSE, NULL);
- return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
- }
- int
- os_mutex_destroy(korp_mutex *mutex)
- {
- assert(mutex);
- return CloseHandle(*mutex) ? BHT_OK : BHT_ERROR;
- }
- int
- os_mutex_lock(korp_mutex *mutex)
- {
- int ret;
- assert(mutex);
- ret = WaitForSingleObject(*mutex, INFINITE);
- return ret != WAIT_FAILED ? BHT_OK : BHT_ERROR;
- }
- int
- os_mutex_unlock(korp_mutex *mutex)
- {
- bh_assert(mutex);
- return ReleaseMutex(*mutex) ? BHT_OK : BHT_ERROR;
- }
- int
- os_cond_init(korp_cond *cond)
- {
- bh_assert(cond);
- if (os_mutex_init(&cond->wait_list_lock) != BHT_OK)
- return BHT_ERROR;
- cond->thread_wait_list = NULL;
- return BHT_OK;
- }
- int
- os_cond_destroy(korp_cond *cond)
- {
- bh_assert(cond);
- os_mutex_destroy(&cond->wait_list_lock);
- return BHT_OK;
- }
- static int
- os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed,
- uint64 useconds)
- {
- os_thread_wait_node *node = &thread_data_current()->wait_node;
- node->next = NULL;
- bh_assert(cond);
- bh_assert(mutex);
- os_mutex_lock(&cond->wait_list_lock);
- if (!cond->thread_wait_list)
- cond->thread_wait_list = node;
- else {
- /* Add to end of wait list */
- os_thread_wait_node *p = cond->thread_wait_list;
- while (p->next)
- p = p->next;
- p->next = node;
- }
- os_mutex_unlock(&cond->wait_list_lock);
- /* Unlock mutex, wait sem and lock mutex again */
- os_mutex_unlock(mutex);
- int wait_result;
- if (timed)
- wait_result = os_sem_reltimed_wait(&node->sem, useconds);
- else
- wait_result = os_sem_wait(&node->sem);
- os_mutex_lock(mutex);
- /* Remove wait node from wait list */
- os_mutex_lock(&cond->wait_list_lock);
- if (cond->thread_wait_list == node)
- cond->thread_wait_list = node->next;
- else {
- /* Remove from the wait list */
- os_thread_wait_node *p = cond->thread_wait_list;
- while (p->next != node)
- p = p->next;
- p->next = node->next;
- }
- os_mutex_unlock(&cond->wait_list_lock);
- return wait_result;
- }
- int
- os_cond_wait(korp_cond *cond, korp_mutex *mutex)
- {
- return os_cond_wait_internal(cond, mutex, false, 0);
- }
- int
- os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
- {
- if (useconds == BHT_WAIT_FOREVER) {
- return os_cond_wait_internal(cond, mutex, false, 0);
- }
- else {
- return os_cond_wait_internal(cond, mutex, true, useconds);
- }
- }
- int
- os_cond_signal(korp_cond *cond)
- {
- /* Signal the head wait node of wait list */
- os_mutex_lock(&cond->wait_list_lock);
- if (cond->thread_wait_list)
- os_sem_signal(&cond->thread_wait_list->sem);
- os_mutex_unlock(&cond->wait_list_lock);
- return BHT_OK;
- }
- int
- os_cond_broadcast(korp_cond *cond)
- {
- /* Signal all of the wait node of wait list */
- os_mutex_lock(&cond->wait_list_lock);
- if (cond->thread_wait_list) {
- os_thread_wait_node *p = cond->thread_wait_list;
- while (p) {
- os_sem_signal(&p->sem);
- p = p->next;
- }
- }
- os_mutex_unlock(&cond->wait_list_lock);
- return BHT_OK;
- }
- static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
- static ULONG
- GetCurrentThreadStackLimits_Win7(PULONG_PTR p_low_limit,
- PULONG_PTR p_high_limit)
- {
- MEMORY_BASIC_INFORMATION mbi;
- NT_TIB *tib = (NT_TIB *)NtCurrentTeb();
- if (!tib) {
- os_printf("warning: NtCurrentTeb() failed\n");
- return -1;
- }
- *p_high_limit = (ULONG_PTR)tib->StackBase;
- if (VirtualQuery(tib->StackLimit, &mbi, sizeof(mbi))) {
- *p_low_limit = (ULONG_PTR)mbi.AllocationBase;
- return 0;
- }
- os_printf("warning: VirtualQuery() failed\n");
- return GetLastError();
- }
- uint8 *
- os_thread_get_stack_boundary()
- {
- ULONG_PTR low_limit = 0, high_limit = 0;
- uint32 page_size;
- if (thread_stack_boundary)
- return thread_stack_boundary;
- page_size = os_getpagesize();
- if (GetCurrentThreadStackLimits_Kernel32) {
- GetCurrentThreadStackLimits_Kernel32(&low_limit, &high_limit);
- }
- else {
- if (0 != GetCurrentThreadStackLimits_Win7(&low_limit, &high_limit))
- return NULL;
- }
- /* 4 pages are set unaccessible by system, we reserved
- one more page at least for safety */
- thread_stack_boundary = (uint8 *)(uintptr_t)low_limit + page_size * 5;
- return thread_stack_boundary;
- }
- #ifdef OS_ENABLE_HW_BOUND_CHECK
- static os_thread_local_attribute bool thread_signal_inited = false;
- int
- os_thread_signal_init()
- {
- ULONG StackSizeInBytes = 16 * 1024;
- bool ret;
- if (thread_signal_inited)
- return 0;
- ret = SetThreadStackGuarantee(&StackSizeInBytes);
- if (ret)
- thread_signal_inited = true;
- return ret ? 0 : -1;
- }
- void
- os_thread_signal_destroy()
- {
- /* Do nothing */
- }
- bool
- os_thread_signal_inited()
- {
- return thread_signal_inited;
- }
- #endif
|