diff options
Diffstat (limited to 'accel-pppd/triton/triton.c')
-rw-r--r-- | accel-pppd/triton/triton.c | 67 |
1 files changed, 57 insertions, 10 deletions
diff --git a/accel-pppd/triton/triton.c b/accel-pppd/triton/triton.c index 0740591..aa2183e 100644 --- a/accel-pppd/triton/triton.c +++ b/accel-pppd/triton/triton.c @@ -4,6 +4,7 @@ #include <stdio.h> #include <string.h> #include <unistd.h> +#include <limits.h> #include <sys/resource.h> #include "triton_p.h" @@ -21,6 +22,8 @@ static LIST_HEAD(ctx_queue); static spinlock_t ctx_list_lock = SPINLOCK_INITIALIZER; static LIST_HEAD(ctx_list); +static LIST_HEAD(init_list); + static int terminate; static int need_terminate; @@ -121,8 +124,12 @@ static void* triton_thread(struct _triton_thread_t *thread) } else spin_unlock(&threads_lock); - if (terminate) + if (terminate) { + spin_lock(&threads_lock); + list_del(&thread->entry); + spin_unlock(&threads_lock); return NULL; + } //printf("thread %p: enter sigwait\n", thread); sigwait(&set, &sig); @@ -188,7 +195,8 @@ static void ctx_thread(struct _triton_context_t *ctx) spin_unlock(&ctx->lock); __sync_sub_and_fetch(&triton_stat.timer_pending, 1); read(t->fd, &tt, sizeof(tt)); - t->ud->expire(t->ud); + if (t->ud) + t->ud->expire(t->ud); continue; } if (!list_empty(&ctx->pending_handlers)) { @@ -226,17 +234,21 @@ static void ctx_thread(struct _triton_context_t *ctx) struct _triton_thread_t *create_thread() { + pthread_attr_t attr; struct _triton_thread_t *thread = _malloc(sizeof(*thread)); if (!thread) { triton_log_error("out of memory"); return NULL; } + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN); + memset(thread, 0, sizeof(*thread)); pthread_mutex_init(&thread->sleep_lock, NULL); pthread_cond_init(&thread->sleep_cond, NULL); pthread_mutex_lock(&thread->sleep_lock); - if (pthread_create(&thread->thread, NULL, (void*(*)(void*))triton_thread, thread)) { + if (pthread_create(&thread->thread, &attr, (void*(*)(void*))triton_thread, thread)) { triton_log_error("pthread_create: %s", strerror(errno)); return NULL; } @@ -505,14 +517,31 @@ static void ru_update(struct triton_timer_t *t) val = (double)((rusage.ru_utime.tv_sec - ru_utime.tv_sec) * 1000000 + (rusage.ru_utime.tv_usec - ru_utime.tv_usec) + (rusage.ru_stime.tv_sec - ru_stime.tv_sec) * 1000000 + (rusage.ru_stime.tv_usec - ru_stime.tv_usec)) / dt * 100; - if (val <= 100) - triton_stat.cpu = val; + triton_stat.cpu = val; ru_timestamp = ts; ru_utime = rusage.ru_utime; ru_stime = rusage.ru_stime; } +void __export triton_register_init(int order, void (*func)(void)) +{ + struct _triton_init_t *i1, *i = _malloc(sizeof(*i)); + struct list_head *p = init_list.prev; + + + i->order = order; + i->func = func; + + while (p != &init_list) { + i1 = list_entry(p, typeof(*i1), entry); + if (order > i1->order) + break; + p = p->prev; + } + list_add(&i->entry, p); +} + int __export triton_init(const char *conf_file) { ctx_pool = mempool_create(sizeof(struct _triton_context_t)); @@ -533,13 +562,24 @@ int __export triton_init(const char *conf_file) if (event_init()) return -1; + triton_context_register(&default_ctx, NULL); + return 0; } int __export triton_load_modules(const char *mod_sect) { + struct _triton_init_t *i; + if (load_modules(mod_sect)) return -1; + + while (!list_empty(&init_list)) { + i = list_entry(init_list.next, typeof(*i), entry); + i->func(); + list_del(&i->entry); + _free(i); + } return 0; } @@ -561,6 +601,7 @@ void __export triton_run() struct _triton_thread_t *t; int i; char *opt; + struct timespec ts; opt = conf_get_opt("core", "thread-count"); if (opt && atoi(opt) > 0) @@ -575,19 +616,18 @@ void __export triton_run() pthread_mutex_unlock(&t->sleep_lock); } - time(&triton_stat.start_time); + clock_gettime(CLOCK_MONOTONIC, &ts); + triton_stat.start_time = ts.tv_sec; md_run(); timer_run(); - triton_context_register(&default_ctx, NULL); triton_context_wakeup(&default_ctx); } void __export triton_terminate() { struct _triton_context_t *ctx; - struct _triton_thread_t *t; int r; need_terminate = 1; @@ -603,8 +643,15 @@ void __export triton_terminate() } spin_unlock(&ctx_list_lock); - list_for_each_entry(t, &threads, entry) - pthread_join(t->thread, NULL); + while (1) { + spin_lock(&threads_lock); + if (list_empty(&threads)) { + spin_unlock(&threads_lock); + break; + } + spin_unlock(&threads_lock); + sleep(1); + } md_terminate(); timer_terminate(); |