]> pd.if.org Git - nbds/commitdiff
Fix initialization ordering problem from last commit.
authorjdybnis <jdybnis@9ec2166a-aeea-11dd-8830-69e4bb380a4a>
Wed, 12 Nov 2008 09:44:35 +0000 (09:44 +0000)
committerjdybnis <jdybnis@9ec2166a-aeea-11dd-8830-69e4bb380a4a>
Wed, 12 Nov 2008 09:44:35 +0000 (09:44 +0000)
runtime/lwt.c
runtime/mem.c
runtime/rcu.c
runtime/runtime.c
runtime/runtime_local.h
runtime/tls.h
struct/ht_test.c

index 42e79bff9ae89651447e05490e0dca5572af409a..8f12974247136e4cd7327967cf917b2b43518417 100644 (file)
@@ -5,9 +5,8 @@
  * lightweight tracing 
  */
 #include <stdio.h>
-
 #include "common.h"
-#include "tls.h"
+#include "runtime_local.h"
 #include "lwt.h"
 #include "mem.h"
 
@@ -27,26 +26,16 @@ typedef struct lwt_buffer {
     lwt_record_t x[0];
 } lwt_buffer_t;
 
-DECLARE_THREAD_LOCAL(tb_, int);
-
 lwt_buffer_t *lwt_buf_[MAX_NUM_THREADS] = {};
 uint64_t flag_mask_ = 0;
-static int buf_count_ = 0;
 static const char *flags_ = "";
 
-void lwt_init (void)
-{
-    INIT_THREAD_LOCAL(tb_, NULL);
-}
-
 void lwt_thread_init (int thread_id)
 {
     assert(thread_id < MAX_NUM_THREADS);
     if (lwt_buf_[thread_id] == NULL) {
         lwt_buf_[thread_id] = (lwt_buffer_t *)nbd_malloc(sizeof(lwt_buffer_t) + sizeof(lwt_record_t) * LWT_BUFFER_SIZE);
-        SYNC_ADD(&buf_count_, 1);
         memset(lwt_buf_[thread_id], 0, sizeof(lwt_buffer_t));
-        SET_THREAD_LOCAL(tb_, lwt_buf_[thread_id]);
     }
 }
 
@@ -77,17 +66,15 @@ static inline void dump_record (FILE *file, int thread_id, lwt_record_t *r, uint
 
 static void dump_buffer (FILE *file, int thread_id, uint64_t offset)
 {
-    assert(thread_id < buf_count_);
-
     lwt_buffer_t *tb = lwt_buf_[thread_id]; 
-    int i;
+    assert(tb);
     if (tb->head > LWT_BUFFER_SIZE) {
-        for (i = tb->head & LWT_BUFFER_MASK; i < LWT_BUFFER_SIZE; ++i) {
+        for (int i = tb->head & LWT_BUFFER_MASK; i < LWT_BUFFER_SIZE; ++i) {
             dump_record(file, thread_id, tb->x + i, offset);
         }
     }
 
-    for (i = 0; i < (tb->head & LWT_BUFFER_MASK); ++i) {
+    for (int i = 0; i < (tb->head & LWT_BUFFER_MASK); ++i) {
         dump_record(file, thread_id, tb->x + i, offset);
     }
 }
@@ -95,9 +82,8 @@ static void dump_buffer (FILE *file, int thread_id, uint64_t offset)
 void lwt_dump (const char *file_name)
 {
     uint64_t offset = (uint64_t)-1;
-    int i;
 
-    for (i = 0; i < buf_count_; ++i) {
+    for (int i = 0; i < MAX_NUM_THREADS; ++i) {
         if (lwt_buf_[i] != NULL && lwt_buf_[i]->head != 0) {
             uint64_t x = lwt_buf_[i]->x[0].timestamp;
             if (x < offset) {
@@ -116,7 +102,7 @@ void lwt_dump (const char *file_name)
     if (offset != (uint64_t)-1) {
         FILE *file = fopen(file_name, "w");
         assert(file);
-        for (i = 0; i < buf_count_; ++i) {
+        for (int i = 0; i < MAX_NUM_THREADS; ++i) {
             if (lwt_buf_[i] != NULL) {
                 dump_buffer(file, i, offset);
             }
@@ -127,12 +113,14 @@ void lwt_dump (const char *file_name)
 }
 
 void lwt_trace_i (const char *format, size_t value1, size_t value2) {
-    LOCALIZE_THREAD_LOCAL(tb_, lwt_buffer_t *);
-    if (tb_) {
+    LOCALIZE_THREAD_LOCAL(tid_, int);
+    lwt_buffer_t *tb = lwt_buf_[tid_];
+    if (tb != NULL) {
         unsigned int u, l;
         __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (u)); 
         uint64_t timestamp = ((uint64_t)u << 32) | l; 
         lwt_record_t temp = { timestamp, format, value1, value2 };
-        tb_->x[tb_->head++ & LWT_BUFFER_MASK] = temp;
+
+        tb->x[tb->head++ & LWT_BUFFER_MASK] = temp;
     }
 }
index c65dfb05c7705fce3e2900ef6d306d708ba153ad..f6fd318aef75f732c781b7d4a62585d6b382ea99 100644 (file)
@@ -8,8 +8,8 @@
 #include <stdio.h>
 #include <errno.h>
 #include "common.h"
+#include "runtime_local.h"
 #include "lwt.h"
-#include "tls.h"
 
 #define GET_SCALE(n) (sizeof(n)*8-__builtin_clzl((n)-1)) // log2 of <n>, rounded up
 #define MAX_SCALE 31 // allocate blocks up to 4GB in size (arbitrary, could be bigger)
@@ -61,7 +61,7 @@ void nbd_free (void *x) {
     assert(((size_t)b >> REGION_SCALE) < ((1 << HEADER_REGION_SCALE) / sizeof(header_t)));
     header_t *h = region_header_ + ((size_t)b >> REGION_SCALE);
     TRACE("m0", "nbd_free(): block %p scale %llu", x, h->scale);
-    block_t  *l = &free_list_[(int)h->owner][(int)h->scale][tid_];
+    block_t  *l = &free_list_[h->owner][h->scale][tid_];
     TRACE("m0", "nbd_free(): free list %p first block %p", l, l->next);
     b->next = l->next;
     l->next = b;
index 837845381c1b9c7fc0df50d9ea4748bf3eabc63a..c2e465e6cfcc3b0805e9d327f3430cd2ecee17be 100644 (file)
@@ -6,6 +6,7 @@
  */
 #include <string.h>
 #include "common.h"
+#include "runtime_local.h"
 #include "lwt.h"
 #include "mem.h"
 #include "tls.h"
index 0731bfdc70226d81ba2c87a95d9405a7dcfd500c..8056660694888b4b596e66a0c0c01655e2ac01ce 100644 (file)
@@ -9,9 +9,6 @@
 #include "mem.h"
 #include "tls.h"
 
-#undef malloc
-#undef free
-
 DECLARE_THREAD_LOCAL(tid_, int);
 
 typedef struct thread_info {
@@ -21,9 +18,9 @@ typedef struct thread_info {
 } thread_info_t;
 
 void nbd_init (void) {
-    INIT_THREAD_LOCAL(tid_, NULL);
+    INIT_THREAD_LOCAL(tid_);
+    SET_THREAD_LOCAL(tid_, 0);
     mem_init();
-    lwt_init();
     lwt_thread_init(0);
     rcu_thread_init(0);
 }
@@ -34,12 +31,12 @@ static void *worker (void *arg) {
     lwt_thread_init(ti->thread_id);
     rcu_thread_init(ti->thread_id);
     void *ret = ti->start_routine(ti->arg);
-    free(ti);
+    nbd_free(ti);
     return ret;
 }
 
 int nbd_thread_create (pthread_t *restrict thread, int thread_id, void *(*start_routine)(void *), void *restrict arg) {
-    thread_info_t *ti = (thread_info_t *)malloc(sizeof(thread_info_t));
+    thread_info_t *ti = (thread_info_t *)nbd_malloc(sizeof(thread_info_t));
     ti->thread_id = thread_id;
     ti->start_routine = start_routine;
     ti->arg = arg;
index 8b295d2e5dddfa319652dbf26f6e42767038fbc8..5b02076af70b81d1a9fd69b0348d46c8921d668c 100644 (file)
@@ -1,5 +1,10 @@
+#ifndef RUNTIME_LOCAL_H
+#define RUNTIME_LOCAL_H
+#include "tls.h"
+DECLARE_THREAD_LOCAL(tid_, int);
+
 void mem_init (void);
-void lwt_init (void);
 
 void rcu_thread_init (int thread_id);
 void lwt_thread_init (int thread_id);
+#endif//RUNTIME_LOCAL_H 
index def2becfd51ce352c5aa1449a652c225973b3281..865e6da26bed635a94493b53d9c72007856e7259 100644 (file)
@@ -12,7 +12,7 @@
 #define DECLARE_THREAD_LOCAL (name, type)  type name
 #define INIT_THREAD_LOCAL    (name, value) name = value
 #define SET_THREAD_LOCAL     (name, value) name = value
-#define LOCALIZE_THREAD_LOCAL(name, type)  extern __thread type name
+#define LOCALIZE_THREAD_LOCAL(name, type)
 
 #else//!__ELF__
 
 
 #define DECLARE_THREAD_LOCAL(name, type) pthread_key_t name##_KEY
 
-#define INIT_THREAD_LOCAL(name, value) \
+#define INIT_THREAD_LOCAL(name) \
     do { \
-        if (pthread_key_create(&name##_KEY, (void *)(size_t)value) != 0) { assert(FALSE); } \
+        if (pthread_key_create(&name##_KEY, NULL) != 0) { assert(FALSE); } \
     } while (0)
 
 #define SET_THREAD_LOCAL(name, value) pthread_setspecific(name##_KEY, (void *)(size_t)value);
 
-#define LOCALIZE_THREAD_LOCAL(name, type) \
-    extern pthread_key_t name##_KEY; type name = (type)(size_t)pthread_getspecific(name##_KEY)
+#define LOCALIZE_THREAD_LOCAL(name, type) type name = (type)(size_t)pthread_getspecific(name##_KEY)
 
 #endif//__ELF__
 #endif//TLS_H
index e8fcc4c299489355eb4cdb88b1e17eae9bf1308b..c7e6d57c1d34c59f9cd4ae2ce89750f1e544e0d1 100644 (file)
@@ -130,8 +130,8 @@ void simple_add_remove (CuTest* tc) {
         wd[i].tc = tc;
         wd[i].ht = ht;
         wd[i].wait = &wait;
-        int rc = pthread_create(thread + i, NULL, simple_worker, wd + i);
-        if (rc != 0) { perror("pthread_create"); return; }
+        int rc = nbd_thread_create(thread + i, i, simple_worker, wd + i);
+        if (rc != 0) { perror("nbd_thread_create"); return; }
     }
     for (i = 0; i < 2; ++i) {
         pthread_join(thread[i], NULL);