From: jdybnis Date: Wed, 12 Nov 2008 09:44:35 +0000 (+0000) Subject: Fix initialization ordering problem from last commit. X-Git-Url: https://pd.if.org/git/?p=nbds;a=commitdiff_plain;h=fb536c12185fd1e339b5fd479d9ef84554b436df Fix initialization ordering problem from last commit. --- diff --git a/runtime/lwt.c b/runtime/lwt.c index 42e79bf..8f12974 100644 --- a/runtime/lwt.c +++ b/runtime/lwt.c @@ -5,9 +5,8 @@ * lightweight tracing */ #include - #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; } } diff --git a/runtime/mem.c b/runtime/mem.c index c65dfb0..f6fd318 100644 --- a/runtime/mem.c +++ b/runtime/mem.c @@ -8,8 +8,8 @@ #include #include #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 , 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; diff --git a/runtime/rcu.c b/runtime/rcu.c index 8378453..c2e465e 100644 --- a/runtime/rcu.c +++ b/runtime/rcu.c @@ -6,6 +6,7 @@ */ #include #include "common.h" +#include "runtime_local.h" #include "lwt.h" #include "mem.h" #include "tls.h" diff --git a/runtime/runtime.c b/runtime/runtime.c index 0731bfd..8056660 100644 --- a/runtime/runtime.c +++ b/runtime/runtime.c @@ -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; diff --git a/runtime/runtime_local.h b/runtime/runtime_local.h index 8b295d2..5b02076 100644 --- a/runtime/runtime_local.h +++ b/runtime/runtime_local.h @@ -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 diff --git a/runtime/tls.h b/runtime/tls.h index def2bec..865e6da 100644 --- a/runtime/tls.h +++ b/runtime/tls.h @@ -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__ @@ -20,15 +20,14 @@ #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 diff --git a/struct/ht_test.c b/struct/ht_test.c index e8fcc4c..c7e6d57 100644 --- a/struct/ht_test.c +++ b/struct/ht_test.c @@ -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);