From f18d0bfc13aa2345e0a8695ee9b54d2c73403642 Mon Sep 17 00:00:00 2001 From: Pavel Afremov Date: Fri, 15 Jun 2007 18:38:57 +0400 Subject: [PATCH] Fix of Thread ID bug. Patch radices quantity of Thread IDs to 0x7FFF to match lock word format and add opportunity to reuse dead threads IDs in new threads. --- vm/thread/src/thread_native_basic.c | 26 +++++++++++++++++++++----- 1 files changed, 21 insertions(+), 5 deletions(-) diff --git a/vm/thread/src/thread_native_basic.c b/vm/thread/src/thread_native_basic.c index 228812b..73ee1d8 100644 --- a/vm/thread/src/thread_native_basic.c +++ b/vm/thread/src/thread_native_basic.c @@ -60,8 +60,9 @@ static IDATA register_to_group(hythread_t thread, hythread_group_t group); #endif #endif -#define MAX_ID 1000000 +#define MAX_ID 0x8000 hythread_t fast_thread_array[MAX_ID]; +short next_free_thread_id[MAX_ID]; int next_id = 1; /* @@ -574,6 +575,8 @@ IDATA VMCALL hythread_struct_init(hythread_t *ret_thread) { */ static IDATA register_to_group(hythread_t thread, hythread_group_t group) { IDATA status; + int free_slot_found = 0; + U_32 i; hythread_t cur, prev; assert(thread); @@ -591,14 +594,27 @@ static IDATA register_to_group(hythread_t thread, hythread_group_t group) { thread->state |= TM_THREAD_STATE_ALIVE | TM_THREAD_STATE_RUNNABLE; if (!thread->thread_id) { - ++next_id; - thread->thread_id = next_id; - if (next_id >= MAX_ID) { + for(i = 0; i < MAX_ID; i++) { + if (fast_thread_array[next_id] == NULL) { + assert(fast_thread_array[next_id] == NULL); + thread->thread_id = next_id; + free_slot_found = 1; + break; + } + next_id++; + + if (next_id == MAX_ID) { + next_id = 1; + } + } + + if (!free_slot_found) { hythread_global_unlock(NULL); return TM_ERROR_OUT_OF_MEMORY; } } - + + assert(thread->thread_id); fast_thread_array[thread->thread_id] = thread; thread->group = group; -- 1.5.0.3