|
@@ -5,6 +5,7 @@
|
|
|
|
|
|
|
|
#include "bh_log.h"
|
|
#include "bh_log.h"
|
|
|
#include "thread_manager.h"
|
|
#include "thread_manager.h"
|
|
|
|
|
+#include "tid_allocator.h"
|
|
|
|
|
|
|
|
#if WASM_ENABLE_INTERP != 0
|
|
#if WASM_ENABLE_INTERP != 0
|
|
|
#include "wasm_runtime.h"
|
|
#include "wasm_runtime.h"
|
|
@@ -15,16 +16,8 @@
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
static const char *THREAD_START_FUNCTION = "wasi_thread_start";
|
|
static const char *THREAD_START_FUNCTION = "wasi_thread_start";
|
|
|
-
|
|
|
|
|
static korp_mutex thread_id_lock;
|
|
static korp_mutex thread_id_lock;
|
|
|
-
|
|
|
|
|
-// Stack data structure to track available thread identifiers
|
|
|
|
|
-#define AVAIL_TIDS_INIT_SIZE CLUSTER_MAX_THREAD_NUM
|
|
|
|
|
-typedef struct {
|
|
|
|
|
- int32 *ids;
|
|
|
|
|
- uint32 pos, size;
|
|
|
|
|
-} AvailableThreadIds;
|
|
|
|
|
-static AvailableThreadIds avail_tids;
|
|
|
|
|
|
|
+static TidAllocator tid_allocator;
|
|
|
|
|
|
|
|
typedef struct {
|
|
typedef struct {
|
|
|
/* app's entry function */
|
|
/* app's entry function */
|
|
@@ -38,41 +31,10 @@ typedef struct {
|
|
|
static int32
|
|
static int32
|
|
|
allocate_thread_id()
|
|
allocate_thread_id()
|
|
|
{
|
|
{
|
|
|
- int32 id = -1;
|
|
|
|
|
-
|
|
|
|
|
os_mutex_lock(&thread_id_lock);
|
|
os_mutex_lock(&thread_id_lock);
|
|
|
- if (avail_tids.pos == 0) { // Resize stack and push new thread ids
|
|
|
|
|
- uint32 old_size = avail_tids.size;
|
|
|
|
|
- uint32 new_size = avail_tids.size * 2;
|
|
|
|
|
- if (new_size / 2 != avail_tids.size) {
|
|
|
|
|
- LOG_ERROR("Overflow detected during new size calculation");
|
|
|
|
|
- goto return_id;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- size_t realloc_size = new_size * sizeof(int32);
|
|
|
|
|
- if (realloc_size / sizeof(int32) != new_size) {
|
|
|
|
|
- LOG_ERROR("Overflow detected during realloc");
|
|
|
|
|
- goto return_id;
|
|
|
|
|
- }
|
|
|
|
|
- int32 *tmp =
|
|
|
|
|
- (int32 *)wasm_runtime_realloc(avail_tids.ids, realloc_size);
|
|
|
|
|
- if (tmp == NULL) {
|
|
|
|
|
- LOG_ERROR("Thread ID allocator realloc failed");
|
|
|
|
|
- goto return_id;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- avail_tids.size = new_size;
|
|
|
|
|
- avail_tids.pos = old_size;
|
|
|
|
|
- avail_tids.ids = tmp;
|
|
|
|
|
- for (uint32 i = 0; i < old_size; i++)
|
|
|
|
|
- avail_tids.ids[i] = new_size - i;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Pop available thread identifier from `avail_tids` stack
|
|
|
|
|
- id = avail_tids.ids[--avail_tids.pos];
|
|
|
|
|
-
|
|
|
|
|
-return_id:
|
|
|
|
|
|
|
+ int32 id = tid_allocator_get_tid(&tid_allocator);
|
|
|
os_mutex_unlock(&thread_id_lock);
|
|
os_mutex_unlock(&thread_id_lock);
|
|
|
|
|
+
|
|
|
return id;
|
|
return id;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -80,11 +42,7 @@ void
|
|
|
deallocate_thread_id(int32 thread_id)
|
|
deallocate_thread_id(int32 thread_id)
|
|
|
{
|
|
{
|
|
|
os_mutex_lock(&thread_id_lock);
|
|
os_mutex_lock(&thread_id_lock);
|
|
|
-
|
|
|
|
|
- // Release thread identifier by pushing it into `avail_tids` stack
|
|
|
|
|
- bh_assert(avail_tids.pos < avail_tids.size);
|
|
|
|
|
- avail_tids.ids[avail_tids.pos++] = thread_id;
|
|
|
|
|
-
|
|
|
|
|
|
|
+ tid_allocator_release_tid(&tid_allocator, thread_id);
|
|
|
os_mutex_unlock(&thread_id_lock);
|
|
os_mutex_unlock(&thread_id_lock);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -212,17 +170,10 @@ lib_wasi_threads_init(void)
|
|
|
if (0 != os_mutex_init(&thread_id_lock))
|
|
if (0 != os_mutex_init(&thread_id_lock))
|
|
|
return false;
|
|
return false;
|
|
|
|
|
|
|
|
- // Initialize stack to store thread identifiers
|
|
|
|
|
- avail_tids.size = AVAIL_TIDS_INIT_SIZE;
|
|
|
|
|
- avail_tids.pos = avail_tids.size;
|
|
|
|
|
- avail_tids.ids =
|
|
|
|
|
- (int32 *)wasm_runtime_malloc(avail_tids.size * sizeof(int32));
|
|
|
|
|
- if (avail_tids.ids == NULL) {
|
|
|
|
|
|
|
+ if (!tid_allocator_init(&tid_allocator)) {
|
|
|
os_mutex_destroy(&thread_id_lock);
|
|
os_mutex_destroy(&thread_id_lock);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
- for (uint32 i = 0; i < avail_tids.size; i++)
|
|
|
|
|
- avail_tids.ids[i] = avail_tids.size - i;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
@@ -230,7 +181,6 @@ lib_wasi_threads_init(void)
|
|
|
void
|
|
void
|
|
|
lib_wasi_threads_destroy(void)
|
|
lib_wasi_threads_destroy(void)
|
|
|
{
|
|
{
|
|
|
- wasm_runtime_free(avail_tids.ids);
|
|
|
|
|
- avail_tids.ids = NULL;
|
|
|
|
|
|
|
+ tid_allocator_deinit(&tid_allocator);
|
|
|
os_mutex_destroy(&thread_id_lock);
|
|
os_mutex_destroy(&thread_id_lock);
|
|
|
}
|
|
}
|