Find a faster way to get currently executing thread's id
authorweiyu <weiyuluo1232@gmail.com>
Thu, 3 Sep 2020 23:46:17 +0000 (16:46 -0700)
committerweiyu <weiyuluo1232@gmail.com>
Thu, 3 Sep 2020 23:46:17 +0000 (16:46 -0700)
action.cc
cmodelint.cc
datarace.cc
librace.cc
model.cc
model.h
mutex.cc
schedule.cc
threads-model.h
threads.cc

index 65e1447f1a755cae8416df96aa330addff0f4711..4cdca0d7a7272b2f4c6eef265ef34ca613bc0c3f 100644 (file)
--- 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();
 }
 
 
index fe91d5373583bd337b5f2d8022f97e945243d216..0b9668d16a56c96f20322205e4a7a4cdb599fca4 100644 (file)
@@ -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
 }
index da5fa8ce6ea6434ebacf0201e36f44cbba376d7b..06de537546d90ca63f73d86e25fa747e5e5288eb 100644 (file)
@@ -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;
index 8ecf2afc1daa90db43fb692e76ed8c6c58a90210..214026d651b66328d0465e9d10a68588dfa4a897 100644 (file)
@@ -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);
 }
index c022e9e3baab9c716498992bc09e525fca61ce69..4cf5cc57fea4bb94c5998460c701a8b618264c3d 100644 (file)
--- 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 7b8a9c959ebf1f698e0c9a9cae0dbea585e055b8..03dafd11b1a0aa5493dcf8f1c62f549e788e7a74 100644 (file)
--- 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);
 
index 8b5d33b23e64f0c090633a498b330a9fd208eca4..fa751783dcc4418ddc8087e7ee5b24d566c412a5 100644 (file)
--- 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);
index 9bd87077af6801811ce0cc05fa9e39df8f70cabc..30af582dcc237991b690f606eb0c1e5ca11afa1b 100644 (file)
@@ -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;
 }
 
index b1aa9013bf6e7ee174976fafc8e286dca7183f39..15dd151577e908027f002565cf2855e95c85d787 100644 (file)
@@ -203,6 +203,7 @@ void tlsdestructor(void *v);
 #endif
 
 Thread * thread_current();
+thread_id_t thread_current_id();
 void thread_startup();
 void initMainThread();
 
index aef263ec4624418c2fc050cd3a998e8ad80bd878..f83433bc1a0da8696fd3f0d8497f76fcc1943c81 100644 (file)
@@ -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()));
 }