Add mutex initializer for wasm-c-api engine operations (#1656)
The host embedder may new/delete wasm-c-api engine simultaneously in multiple threads, which requires lock for the operations. Since there isn't one time called global init/destroy APIs provided by wasm-c-api, we define a global lock and initialize it with thread mutex initializer if the platform supports that, and use it to lock the operations of engine. If the platform doesn't support thread mutex initializer, we require developer to create the lock by himself to ensure the thread-safe of the engine operations.
This commit is contained in:
@ -57,6 +57,8 @@ typedef pthread_cond_t korp_cond;
|
||||
typedef pthread_t korp_thread;
|
||||
typedef sem_t korp_sem;
|
||||
|
||||
#define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
||||
|
||||
#define os_thread_local_attribute __thread
|
||||
|
||||
#define bh_socket_t int
|
||||
|
||||
@ -60,6 +60,8 @@ typedef pthread_cond_t korp_cond;
|
||||
typedef pthread_t korp_thread;
|
||||
typedef sem_t korp_sem;
|
||||
|
||||
#define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
||||
|
||||
#define os_thread_local_attribute __thread
|
||||
|
||||
#define bh_socket_t int
|
||||
|
||||
@ -41,6 +41,8 @@ typedef pthread_cond_t korp_cond;
|
||||
typedef pthread_t korp_thread;
|
||||
typedef unsigned int korp_sem;
|
||||
|
||||
#define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
||||
|
||||
#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB)
|
||||
|
||||
/* Default thread priority */
|
||||
|
||||
@ -52,6 +52,10 @@ typedef pthread_mutex_t korp_mutex;
|
||||
typedef pthread_cond_t korp_cond;
|
||||
typedef unsigned int korp_sem;
|
||||
|
||||
#ifndef SGX_DISABLE_PTHREAD
|
||||
#define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
||||
#endif
|
||||
|
||||
typedef int (*os_print_function_t)(const char *message);
|
||||
void
|
||||
os_set_print_function(os_print_function_t pf);
|
||||
|
||||
@ -57,6 +57,8 @@ typedef pthread_cond_t korp_cond;
|
||||
typedef pthread_t korp_thread;
|
||||
typedef sem_t korp_sem;
|
||||
|
||||
#define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
||||
|
||||
#define os_thread_local_attribute __thread
|
||||
|
||||
#define bh_socket_t int
|
||||
|
||||
@ -42,6 +42,8 @@ typedef pthread_cond_t korp_cond;
|
||||
typedef pthread_t korp_thread;
|
||||
typedef sem_t korp_sem;
|
||||
|
||||
#define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
||||
|
||||
#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB)
|
||||
|
||||
/* Default thread priority */
|
||||
|
||||
@ -56,6 +56,8 @@ typedef pthread_cond_t korp_cond;
|
||||
typedef pthread_t korp_thread;
|
||||
typedef sem_t korp_sem;
|
||||
|
||||
#define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
||||
|
||||
#define os_thread_local_attribute __thread
|
||||
|
||||
#if WASM_DISABLE_HW_BOUND_CHECK == 0
|
||||
|
||||
@ -56,6 +56,17 @@ typedef void *korp_tid;
|
||||
typedef void *korp_mutex;
|
||||
typedef void *korp_sem;
|
||||
|
||||
/**
|
||||
* Create the mutex when os_mutex_lock is called, and no need to
|
||||
* CloseHandle() for the static lock's lifetime, since
|
||||
* "The system closes the handle automatically when the process
|
||||
* terminates. The mutex object is destroyed when its last
|
||||
* handle has been closed."
|
||||
* Refer to:
|
||||
* https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createmutexa
|
||||
*/
|
||||
#define OS_THREAD_MUTEX_INITIALIZER NULL
|
||||
|
||||
struct os_thread_wait_node;
|
||||
typedef struct os_thread_wait_node *os_thread_wait_list;
|
||||
typedef struct korp_cond {
|
||||
|
||||
@ -512,6 +512,21 @@ os_mutex_lock(korp_mutex *mutex)
|
||||
int ret;
|
||||
|
||||
assert(mutex);
|
||||
|
||||
if (*mutex == NULL) { /* static initializer? */
|
||||
HANDLE p = CreateMutex(NULL, FALSE, NULL);
|
||||
|
||||
if (!p) {
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
if (InterlockedCompareExchangePointer((PVOID *)mutex, (PVOID)p, NULL)
|
||||
!= NULL) {
|
||||
/* lock has been created by other threads */
|
||||
CloseHandle(p);
|
||||
}
|
||||
}
|
||||
|
||||
ret = WaitForSingleObject(*mutex, INFINITE);
|
||||
return ret != WAIT_FAILED ? BHT_OK : BHT_ERROR;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user