From: weiyu Date: Thu, 3 Sep 2020 23:46:17 +0000 (-0700) Subject: Find a faster way to get currently executing thread's id X-Git-Url: http://plrg.eecs.uci.edu/git/?p=c11tester.git;a=commitdiff_plain;h=7d107019dd0d32d0803fb802fc318a57101707a1 Find a faster way to get currently executing thread's id --- diff --git a/action.cc b/action.cc index 65e1447f..4cdca0d7 100644 --- a/action.cc +++ b/action.cc @@ -111,8 +111,7 @@ ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, /* References to NULL atomic variables can end up here */ ASSERT(loc); this->size = size; - Thread *t = thread_current(); - this->tid = t->get_id(); + this->tid = thread_current_id(); } @@ -146,8 +145,7 @@ ModelAction::ModelAction(action_type_t type, const char * position, memory_order /* References to NULL atomic variables can end up here */ ASSERT(loc); this->size = size; - Thread *t = thread_current(); - this->tid = t->get_id(); + this->tid = thread_current_id(); } diff --git a/cmodelint.cc b/cmodelint.cc index fe91d537..0b9668d1 100644 --- a/cmodelint.cc +++ b/cmodelint.cc @@ -62,7 +62,7 @@ VOLATILELOAD(64) ensureModel(); \ model->switch_thread(new ModelAction(ATOMIC_WRITE, position, memory_order_volatile_store, obj, (uint64_t) val)); \ *((volatile uint ## size ## _t *)obj) = val; \ - thread_id_t tid = thread_current()->get_id(); \ + thread_id_t tid = thread_current_id(); \ for(int i=0;i < size / 8;i++) { \ atomraceCheckWrite(tid, (void *)(((char *)obj)+i)); \ } \ @@ -79,7 +79,7 @@ VOLATILESTORE(64) ensureModel(); \ model->switch_thread(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)); \ *((volatile uint ## size ## _t *)obj) = val; \ - thread_id_t tid = thread_current()->get_id(); \ + thread_id_t tid = thread_current_id(); \ for(int i=0;i < size / 8;i++) { \ atomraceCheckWrite(tid, (void *)(((char *)obj)+i)); \ } \ @@ -96,7 +96,7 @@ CDSATOMICINT(64) ensureModel(); \ uint ## size ## _t val = (uint ## size ## _t)model->switch_thread( \ new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)); \ - thread_id_t tid = thread_current()->get_id(); \ + thread_id_t tid = thread_current_id(); \ for(int i=0;i < size / 8;i++) { \ atomraceCheckRead(tid, (void *)(((char *)obj)+i)); \ } \ @@ -114,7 +114,7 @@ CDSATOMICLOAD(64) ensureModel(); \ model->switch_thread(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)); \ *((volatile uint ## size ## _t *)obj) = val; \ - thread_id_t tid = thread_current()->get_id(); \ + thread_id_t tid = thread_current_id(); \ for(int i=0;i < size / 8;i++) { \ atomraceCheckWrite(tid, (void *)(((char *)obj)+i)); \ } \ @@ -134,7 +134,7 @@ CDSATOMICSTORE(64) _copy __op__ _val; \ model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position); \ *((volatile uint ## size ## _t *)addr) = _copy; \ - thread_id_t tid = thread_current()->get_id(); \ + thread_id_t tid = thread_current_id(); \ for(int i=0;i < size / 8;i++) { \ atomraceCheckRead(tid, (void *)(((char *)addr)+i)); \ recordWrite(tid, (void *)(((char *)addr)+i)); \ @@ -220,7 +220,7 @@ CDSATOMICXOR(64) if (_old == _expected) { \ model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); \ *((volatile uint ## size ## _t *)addr) = desired; \ - thread_id_t tid = thread_current()->get_id(); \ + thread_id_t tid = thread_current_id(); \ for(int i=0;i < size / 8;i++) { \ recordWrite(tid, (void *)(((char *)addr)+i)); \ } \ @@ -293,7 +293,7 @@ void cds_atomic_thread_fence(int atomic_index, const char * position) { void cds_func_entry(const char * funcName) { #ifdef NEWFUZZER ensureModel(); - Thread * th = thread_current(); + thread_id_t tid = thread_current_id(); uint32_t func_id; ModelHistory *history = model->get_history(); @@ -313,15 +313,14 @@ void cds_func_entry(const char * funcName) { func_id = history->getFuncMap()->get(funcName); } - history->enter_function(func_id, th->get_id()); + history->enter_function(func_id, tid); #endif } void cds_func_exit(const char * funcName) { #ifdef NEWFUZZER ensureModel(); - - Thread * th = thread_current(); + thread_id_t tid = thread_current_id(); uint32_t func_id; ModelHistory *history = model->get_history(); @@ -335,6 +334,6 @@ void cds_func_exit(const char * funcName) { if (func_id == 0) return; - history->exit_function(func_id, th->get_id()); + history->exit_function(func_id, tid); #endif } diff --git a/datarace.cc b/datarace.cc index da5fa8ce..06de5375 100644 --- a/datarace.cc +++ b/datarace.cc @@ -500,7 +500,7 @@ void recordWrite(thread_id_t thread, void *location) { /** This function just updates metadata on atomic write. */ void recordCalloc(void *location, size_t size) { - thread_id_t thread = thread_current()->get_id(); + thread_id_t thread = thread_current_id(); for(;size != 0;size--) { uint64_t *shadow = lookupAddressEntry(location); uint64_t shadowval = *shadow; diff --git a/librace.cc b/librace.cc index 8ecf2afc..214026d6 100644 --- a/librace.cc +++ b/librace.cc @@ -11,7 +11,7 @@ void store_8(void *addr, uint8_t val) { DEBUG("addr = %p, val = %" PRIu8 "\n", addr, val); - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckWrite(tid, addr); (*(uint8_t *)addr) = val; } @@ -19,7 +19,7 @@ void store_8(void *addr, uint8_t val) void store_16(void *addr, uint16_t val) { DEBUG("addr = %p, val = %" PRIu16 "\n", addr, val); - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckWrite(tid, addr); raceCheckWrite(tid, (void *)(((uintptr_t)addr) + 1)); (*(uint16_t *)addr) = val; @@ -28,7 +28,7 @@ void store_16(void *addr, uint16_t val) void store_32(void *addr, uint32_t val) { DEBUG("addr = %p, val = %" PRIu32 "\n", addr, val); - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckWrite(tid, addr); raceCheckWrite(tid, (void *)(((uintptr_t)addr) + 1)); raceCheckWrite(tid, (void *)(((uintptr_t)addr) + 2)); @@ -39,7 +39,7 @@ void store_32(void *addr, uint32_t val) void store_64(void *addr, uint64_t val) { DEBUG("addr = %p, val = %" PRIu64 "\n", addr, val); - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckWrite(tid, addr); raceCheckWrite(tid, (void *)(((uintptr_t)addr) + 1)); raceCheckWrite(tid, (void *)(((uintptr_t)addr) + 2)); @@ -54,7 +54,7 @@ void store_64(void *addr, uint64_t val) uint8_t load_8(const void *addr) { DEBUG("addr = %p\n", addr); - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckRead(tid, addr); return *((uint8_t *)addr); } @@ -62,7 +62,7 @@ uint8_t load_8(const void *addr) uint16_t load_16(const void *addr) { DEBUG("addr = %p\n", addr); - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckRead(tid, addr); raceCheckRead(tid, (const void *)(((uintptr_t)addr) + 1)); return *((uint16_t *)addr); @@ -71,7 +71,7 @@ uint16_t load_16(const void *addr) uint32_t load_32(const void *addr) { DEBUG("addr = %p\n", addr); - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckRead(tid, addr); raceCheckRead(tid, (const void *)(((uintptr_t)addr) + 1)); raceCheckRead(tid, (const void *)(((uintptr_t)addr) + 2)); @@ -82,7 +82,7 @@ uint32_t load_32(const void *addr) uint64_t load_64(const void *addr) { DEBUG("addr = %p\n", addr); - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckRead(tid, addr); raceCheckRead(tid, (const void *)(((uintptr_t)addr) + 1)); raceCheckRead(tid, (const void *)(((uintptr_t)addr) + 2)); @@ -106,7 +106,7 @@ void cds_store8(void *addr) //DEBUG("addr = %p, val = %" PRIu8 "\n", addr, val); if (!model) return; - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckWrite8(tid, addr); } @@ -115,7 +115,7 @@ void cds_store16(void *addr) //DEBUG("addr = %p, val = %" PRIu16 "\n", addr, val); if (!model) return; - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckWrite16(tid, addr); } @@ -124,7 +124,7 @@ void cds_store32(void *addr) //DEBUG("addr = %p, val = %" PRIu32 "\n", addr, val); if (!model) return; - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckWrite32(tid, addr); } @@ -133,34 +133,34 @@ void cds_store64(void *addr) //DEBUG("addr = %p, val = %" PRIu64 "\n", addr, val); if (!model) return; - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckWrite64(tid, addr); } void cds_load8(const void *addr) { if (!model) return; - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckRead8(tid, addr); } void cds_load16(const void *addr) { if (!model) return; - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckRead16(tid, addr); } void cds_load32(const void *addr) { if (!model) return; - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckRead32(tid, addr); } void cds_load64(const void *addr) { if (!model) return; - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); raceCheckRead64(tid, addr); } diff --git a/model.cc b/model.cc index c022e9e3..4cf5cc57 100644 --- a/model.cc +++ b/model.cc @@ -135,6 +135,18 @@ Thread * ModelChecker::get_current_thread() const return scheduler->get_current_thread(); } +/** + * Must be called from user-thread context (e.g., through the global + * thread_current_id() interface) + * + * @return The id of the currently executing Thread. + */ +thread_id_t ModelChecker::get_current_thread_id() const +{ + ASSERT(int_to_id(curr_thread_num) == get_current_thread()->get_id()); + return int_to_id(curr_thread_num); +} + /** * @brief Choose the next thread to execute. * diff --git a/model.h b/model.h index 7b8a9c95..03dafd11 100644 --- a/model.h +++ b/model.h @@ -44,6 +44,7 @@ public: Thread * get_thread(const ModelAction *act) const; Thread * get_current_thread() const; + thread_id_t get_current_thread_id() const; uint64_t switch_thread(ModelAction *act); diff --git a/mutex.cc b/mutex.cc index 8b5d33b2..fa751783 100644 --- a/mutex.cc +++ b/mutex.cc @@ -11,7 +11,7 @@ namespace cdsc { mutex::mutex(int type) { state.locked = NULL; - thread_id_t tid = thread_current()->get_id(); + thread_id_t tid = thread_current_id(); state.alloc_tid = tid; ClockVector *cv = model->get_execution()->get_cv(tid); state.alloc_clock = cv == NULL ? 0 : cv->getClock(tid); diff --git a/schedule.cc b/schedule.cc index 9bd87077..30af582d 100644 --- a/schedule.cc +++ b/schedule.cc @@ -234,7 +234,7 @@ Thread * Scheduler::select_next_thread() thread = execution->getFuzzer()->selectThread(thread_list, avail_threads); } - curr_thread_index = id_to_int(thread->get_id()); + //curr_thread_index = id_to_int(thread->get_id()); return thread; } diff --git a/threads-model.h b/threads-model.h index b1aa9013..15dd1515 100644 --- a/threads-model.h +++ b/threads-model.h @@ -203,6 +203,7 @@ void tlsdestructor(void *v); #endif Thread * thread_current(); +thread_id_t thread_current_id(); void thread_startup(); void initMainThread(); diff --git a/threads.cc b/threads.cc index aef263ec..f83433bc 100644 --- a/threads.cc +++ b/threads.cc @@ -60,6 +60,19 @@ Thread * thread_current(void) return model->get_current_thread(); } +/** + * @brief Get the current Thread id + * + * Must be called from a user context + * + * @return The id of the currently executing thread + */ +thread_id_t thread_current_id(void) +{ + ASSERT(model); + return model->get_current_thread_id(); +} + void modelexit() { model->switch_thread(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, thread_current())); }