summaryrefslogtreecommitdiff
path: root/accel-pptpd/triton
diff options
context:
space:
mode:
authorKozlov Dmitry <dima@server>2010-09-01 19:29:43 +0400
committerKozlov Dmitry <dima@server>2010-09-01 19:29:43 +0400
commit2b63c6e6e368d6ea39584a70ddb81a88e0924c47 (patch)
tree790cc5d46e691b4b920170b7dfde40d3c6ec4be6 /accel-pptpd/triton
parent5bac5a2edb7bc7639c853fd0f7109dcddb7c4cee (diff)
downloadaccel-ppp-2b63c6e6e368d6ea39584a70ddb81a88e0924c47.tar.gz
accel-ppp-2b63c6e6e368d6ea39584a70ddb81a88e0924c47.zip
rewriting triton library ...
Diffstat (limited to 'accel-pptpd/triton')
-rw-r--r--accel-pptpd/triton/CMakeLists.txt5
-rw-r--r--accel-pptpd/triton/conf_file.c54
-rw-r--r--accel-pptpd/triton/conf_file.h16
-rw-r--r--accel-pptpd/triton/loader.c47
-rw-r--r--accel-pptpd/triton/log.c34
-rw-r--r--accel-pptpd/triton/md.c65
-rw-r--r--accel-pptpd/triton/spinlock.h6
-rw-r--r--accel-pptpd/triton/timer.c80
-rw-r--r--accel-pptpd/triton/triton.c104
-rw-r--r--accel-pptpd/triton/triton.h61
-rw-r--r--accel-pptpd/triton/triton_p.h45
11 files changed, 254 insertions, 263 deletions
diff --git a/accel-pptpd/triton/CMakeLists.txt b/accel-pptpd/triton/CMakeLists.txt
index f2de017..a531438 100644
--- a/accel-pptpd/triton/CMakeLists.txt
+++ b/accel-pptpd/triton/CMakeLists.txt
@@ -1,10 +1,11 @@
SET(target triton)
SET(sources_c
md.c
- conf_file.c
timer.c
- options.c
+ triton.c
+ conf_file.c
loader.c
+ log.c
)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
diff --git a/accel-pptpd/triton/conf_file.c b/accel-pptpd/triton/conf_file.c
index 3ddb649..026c4d3 100644
--- a/accel-pptpd/triton/conf_file.c
+++ b/accel-pptpd/triton/conf_file.c
@@ -3,14 +3,13 @@
#include <unistd.h>
#include <stdlib.h>
-#include "conf_file.h"
#include "triton_p.h"
struct sect_t
{
struct list_head entry;
- struct conf_file_sect_t *sect;
+ struct conf_sect_t *sect;
};
static LIST_HEAD(sections);
@@ -18,22 +17,22 @@ static LIST_HEAD(sections);
static char* skip_space(char *str);
static char* skip_word(char *str);
-static struct conf_file_sect_t *find_sect(const char *name);
-static struct conf_file_sect_t *create_sect(const char *name);
-static void sect_add_item(struct conf_file_sect_t *sect,const char *name,const char *val);
-static struct option_t *find_item(struct conf_file_sect_t *,const char *name);
+static struct conf_sect_t *find_sect(const char *name);
+static struct conf_sect_t *create_sect(const char *name);
+static void sect_add_item(struct conf_sect_t *sect,const char *name,const char *val);
+static struct conf_option_t *find_item(struct conf_sect_t *,const char *name);
-void conf_file_load(const char *fname)
+int conf_load(const char *fname)
{
char *buf,*str,*str2;
char *path0,*path;
int cur_line=0;
- static struct conf_file_sect_t *cur_sect=NULL;
+ static struct conf_sect_t *cur_sect=NULL;
FILE *f=fopen(fname,"r");
if (!f)
{
- perror("triton: open conf file");
- return;
+ perror("conf_file: open");
+ return -1;
}
buf=(char*)malloc(1024);
@@ -62,7 +61,7 @@ void conf_file_load(const char *fname)
strcat(path,str+1);
str=path;
}*/
- conf_file_load(str);
+ conf_load(str);
continue;
}
if (*str=='[')
@@ -71,8 +70,8 @@ void conf_file_load(const char *fname)
if (*str2!=']')
{
//L1:
- printf("triton: sintax error in conf file %s line %i\n",fname,cur_line);
- return;
+ fprintf(stderr,"conf_file:%s:%i: sintax error\n",fname,cur_line);
+ return -1;
}
*str2=0;
cur_sect=find_sect(str);
@@ -81,8 +80,8 @@ void conf_file_load(const char *fname)
}
if (!cur_sect)
{
- printf("triton: no section opened in conf file %s line %i\n",fname,cur_line);
- return;
+ fprintf(stderr,"conf_file:%s:%i: no section opened\n",fname,cur_line);
+ return -1;
}
str2=skip_word(str);
if (*str2==' ')
@@ -98,7 +97,7 @@ void conf_file_load(const char *fname)
if (*str2 && *(str2+1) && *str2=='$' && *(str2+1)=='{')
{
char *s;
- struct option_t *opt;
+ struct conf_option_t *opt;
for (s=str2+2; *s && *s!='}'; s++);
if (*s=='}')
{
@@ -108,8 +107,8 @@ void conf_file_load(const char *fname)
opt=find_item(cur_sect,str2);
if (!opt)
{
- printf("triton: parent option not found int conf file %s line %i\n",fname,cur_line);
- return;
+ fprintf(stderr,"conf_file:%s:%i: parent option not found\n",fname,cur_line);
+ return -1;
}
str2=opt->val;
}
@@ -121,6 +120,8 @@ void conf_file_load(const char *fname)
free(path);
free(path0);
fclose(f);
+
+ return 0;
}
static char* skip_space(char *str)
@@ -134,7 +135,7 @@ static char* skip_word(char *str)
return str;
}
-static struct conf_file_sect_t *find_sect(const char *name)
+static struct conf_sect_t *find_sect(const char *name)
{
struct sect_t *s;
list_for_each_entry(s,&sections,entry)
@@ -144,11 +145,11 @@ static struct conf_file_sect_t *find_sect(const char *name)
return NULL;
}
-static struct conf_file_sect_t *create_sect(const char *name)
+static struct conf_sect_t *create_sect(const char *name)
{
struct sect_t *s=(struct sect_t *)malloc(sizeof(struct sect_t));
- s->sect=(struct conf_file_sect_t*)malloc(sizeof(struct conf_file_sect_t));
+ s->sect=(struct conf_sect_t*)malloc(sizeof(struct conf_sect_t));
s->sect->name=(char*)strdup(name);
INIT_LIST_HEAD(&s->sect->items);
@@ -157,9 +158,9 @@ static struct conf_file_sect_t *create_sect(const char *name)
return s->sect;
}
-static void sect_add_item(struct conf_file_sect_t *sect,const char *name,const char *val)
+static void sect_add_item(struct conf_sect_t *sect,const char *name,const char *val)
{
- struct option_t *opt=(struct option_t *)malloc(sizeof(struct option_t));
+ struct conf_option_t *opt=(struct conf_option_t *)malloc(sizeof(struct conf_option_t));
opt->name=(char*)strdup(name);
opt->val=val?(char*)strdup(val):NULL;
@@ -167,9 +168,9 @@ static void sect_add_item(struct conf_file_sect_t *sect,const char *name,const c
list_add_tail(&opt->entry,&sect->items);
}
-static struct option_t *find_item(struct conf_file_sect_t *sect,const char *name)
+static struct conf_option_t *find_item(struct conf_sect_t *sect,const char *name)
{
- struct option_t *opt;
+ struct conf_option_t *opt;
list_for_each_entry(opt,&sect->items,entry)
{
if (strcmp(opt->name,name)==0)
@@ -179,7 +180,8 @@ static struct option_t *find_item(struct conf_file_sect_t *sect,const char *name
return NULL;
}
-struct conf_file_sect_t *conf_file_get_section(const char *name)
+struct conf_sect_t *conf_file_get_section(const char *name)
{
return find_sect(name);
}
+
diff --git a/accel-pptpd/triton/conf_file.h b/accel-pptpd/triton/conf_file.h
deleted file mode 100644
index 47ade31..0000000
--- a/accel-pptpd/triton/conf_file.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#ifndef CONF_FILE_H
-#define CONF_FILE_H
-
-#include "list.h"
-
-struct conf_file_sect_t
-{
- const char *name;
-
- struct list_head items;
-};
-
-void conf_file_load(const char *fname);
-struct conf_file_sect_t *conf_file_get_section(const char *name);
-
-#endif
diff --git a/accel-pptpd/triton/loader.c b/accel-pptpd/triton/loader.c
index 33d4831..a390f0a 100644
--- a/accel-pptpd/triton/loader.c
+++ b/accel-pptpd/triton/loader.c
@@ -4,52 +4,5 @@
#include <stdio.h>
#include <pthread.h>
-#include "conf_file.h"
#include "triton_p.h"
-void md_init(void);
-void event_init(void);
-void timer_init(void);
-
-struct thread_arg_t
-{
- int (*post_init)(void*);
- void *arg;
-};
-
-void *thread(struct thread_arg_t *arg)
-{
- printf("triton: starting new thread\n");
- #ifdef USE_CORO
- coroutine_init();
- #endif
- md_init();
- event_init();
- timer_init();
-
- arg->post_init(arg->arg);
-
- free(arg);
-
- //conf_file_load(cf_name);
- #ifdef USE_CORO
- schedule();
- #else
- md_run();
- #endif
-
- return NULL;
-}
-
-int triton_init(const char *conf_file)
-{
- return 0;
-}
-int triton_run(int (*post_init)(void*),void *arg)
-{
- pthread_t thr;
- struct thread_arg_t *thr_arg=malloc(sizeof(*thr_arg));
- thr_arg->post_init=post_init;
- thr_arg->arg=arg;
- return pthread_create(&thr,NULL,(void*(*)(void*))thread,thr_arg);
-}
diff --git a/accel-pptpd/triton/log.c b/accel-pptpd/triton/log.c
new file mode 100644
index 0000000..39a9cb1
--- /dev/null
+++ b/accel-pptpd/triton/log.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+
+#include "triton_p.h"
+
+static FILE *f_error;
+static FILE *f_debug;
+
+int log_init(void)
+{
+ char *log_error=conf_get_opt("core","log_error");
+ char *log_debug=conf_get_opt("core","log_debug");
+
+ if (log_error)
+ {
+ f_error=fopen(log_error,"a");
+ if (!f_error)
+ {
+ perror("log:log_error:open");
+ return -1;
+ }
+ }
+ if (log_debug)
+ {
+ f_debug=fopen(log_debug,"a");
+ if (!f_debug)
+ {
+ perror("log:log_debug:open");
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
diff --git a/accel-pptpd/triton/md.c b/accel-pptpd/triton/md.c
index 3080c5a..50e969c 100644
--- a/accel-pptpd/triton/md.c
+++ b/accel-pptpd/triton/md.c
@@ -3,6 +3,7 @@
#include <sys/time.h>
#include <signal.h>
#include <string.h>
+#include <errno.h>
#include "triton_p.h"
@@ -12,9 +13,9 @@ static int epoll_fd;
static struct epoll_event *epoll_events;
static pthread_t md_thr;
-static void* md_thread(void *arg)
+static void* md_thread(void *arg);
-int md_init()
+int md_init(void)
{
epoll_fd=epoll_create(1);
if (epoll_fd<0)
@@ -34,40 +35,42 @@ int md_init()
}
void md_run()
{
- pthread_create(&md_thr,md_thread,NULL);
+ pthread_create(&md_thr,NULL,md_thread,NULL);
}
void md_terminate()
{
- pthread_join(&md_thr);
+ pthread_cancel(md_thr);
+ pthread_join(md_thr,NULL);
}
static void* md_thread(void *arg)
{
int i,n,r;
struct triton_md_handler_t *h;
-
- n=epoll_wait(epoll_fd,epoll_events,MAX_EVENTS,-1);
- if (n<0)
- {
- if (errno!=EINTR)
- perror("epoll_wait");
- continue;
- }
- if (n==0)
- return;
-
- for(i=0; i<n; i++)
+
+ while(1)
{
- h=(struct triton_md_handler_t*)epoll_events[i].data.ptr;
- spin_lock(&h->ctx->lock);
- h->trig_epoll_events=epoll_events[i].events;
- list_add_tail(&h->entry2,&h->ctx->pending_handlers);
- h->pending=1;
- r=triton_queue_ctx(h->ctx);
- spin_unlock(&h->ctx->lock);
- if (r)
- triton_thread_wakeup(ctx->thread);
+ n=epoll_wait(epoll_fd,epoll_events,max_events,-1);
+ if (n<0)
+ {
+ if (errno!=EINTR)
+ perror("epoll_wait");
+ continue;
+ }
+
+ for(i=0; i<n; i++)
+ {
+ h=(struct triton_md_handler_t*)epoll_events[i].data.ptr;
+ spin_lock(&h->ctx->lock);
+ h->trig_epoll_events=epoll_events[i].events;
+ list_add_tail(&h->entry2,&h->ctx->pending_handlers);
+ h->pending=1;
+ r=triton_queue_ctx(h->ctx);
+ spin_unlock(&h->ctx->lock);
+ if (r)
+ triton_thread_wakeup(h->ctx->thread);
+ }
}
}
@@ -76,17 +79,17 @@ void triton_md_register_handler(struct triton_md_handler_t *h)
h->epoll_event.data.ptr=h;
if (!h->ctx)
h->ctx=default_ctx;
- pthread_mutex_lock(&h->ctx->lock);
+ spin_lock(&h->ctx->lock);
list_add_tail(&h->entry,&h->ctx->handlers);
- pthread_mutex_unlock(&h->ctx->lock);
+ spin_unlock(&h->ctx->lock);
}
void triton_md_unregister_handler(struct triton_md_handler_t *h)
{
- pthread_mutex_lock(&h->ctx->lock);
+ spin_lock(&h->ctx->lock);
list_del(&h->entry);
if (h->pending)
list_del(&h->entry2);
- pthread_lock_unlock(&h->ctx->lock);
+ spin_unlock(&h->ctx->lock);
}
int triton_md_enable_handler(struct triton_md_handler_t *h, int mode)
{
@@ -109,7 +112,9 @@ int triton_md_enable_handler(struct triton_md_handler_t *h, int mode)
}
int triton_md_disable_handler(struct triton_md_handler_t *h,int mode)
{
- if (h->epoll_events.events)
+ int r;
+
+ if (!h->epoll_event.events)
return -1;
if (mode&MD_MODE_READ)
diff --git a/accel-pptpd/triton/spinlock.h b/accel-pptpd/triton/spinlock.h
index a75e29c..7da93f8 100644
--- a/accel-pptpd/triton/spinlock.h
+++ b/accel-pptpd/triton/spinlock.h
@@ -2,15 +2,17 @@
#define __TRITON_SPINLOCK_H
#ifdef USE_SPINLOCK
-typedef spinlock_t unsigned char;
+typedef unsigned char spinlock_t;
#define spin_lock(l) {while(__sync_lock_test_and_set(l,1);}
#define spin_unlock(l) __sync_lock_release(l)
#define SPINLOCK_INITIALIZER 0
#else
-typedef spinlock_t pthread_mutex_t;
+#include <pthread.h>
+typedef pthread_mutex_t spinlock_t;
#define spin_lock(l) pthread_mutex_lock(l)
#define spin_unlock(l) pthread_mutex_unlock(l)
#define SPINLOCK_INITIALIZER PTHREAD_MUTEX_INITIALIZER
+#define spinlock_init(l) pthread_mutex_init(l,NULL)
#endif
#endif
diff --git a/accel-pptpd/triton/timer.c b/accel-pptpd/triton/timer.c
index 57270f6..3adbde5 100644
--- a/accel-pptpd/triton/timer.c
+++ b/accel-pptpd/triton/timer.c
@@ -4,22 +4,19 @@
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <string.h>
+#include <errno.h>
+#include <unistd.h>
#include "triton_p.h"
-static pthread_thread_t timer_thr;
-static void *timer_thread(void *arg);
-
-static spinlock_t timers_lock=SPINLOCK_INITIALIZER;
-static LIST_HEAD(timers);
-
-extern int max_events;
-static epoll_fd;
+int max_events=128;
+static int epoll_fd;
static struct epoll_event *epoll_events;
-static void tv_add(struct timeval *tv,int msec);
+static pthread_t timer_thr;
+static void *timer_thread(void *arg);
-void timer_init(void)
+int timer_init(void)
{
epoll_fd=epoll_create(1);
if (epoll_fd<0)
@@ -34,6 +31,8 @@ void timer_init(void)
fprintf(stderr,"cann't allocate memory\n");
return -1;
}
+
+ return 0;
}
void timer_run(void)
@@ -43,8 +42,8 @@ void timer_run(void)
void timer_terminate(void)
{
- pthread_cancel(&timer_thr);
- pthread_join(&timer_thr);
+ pthread_cancel(timer_thr);
+ pthread_join(timer_thr,NULL);
}
void *timer_thread(void *arg)
@@ -52,43 +51,44 @@ void *timer_thread(void *arg)
int i,n,r;
struct triton_timer_t *t;
- n=epoll_wait(epoll_fd,epoll_events,MAX_EVENTS,-1);
- if (n<0)
+ while(1)
{
- if (errno!=EINTR)
- perror("epoll_wait");
- continue;
- }
- if (n==0)
- return;
-
- for(i=0; i<n; i++)
- {
- t=(struct triton_md_handler_t*)epoll_events[i].data.ptr;
- spin_lock(&t->ctx->lock);
- list_add_tail(&t->entry2,&t->ctx->pending_timers);
- t->pending=1;
- r=triton_queue_ctx(t->ctx);
- spin_unlock(&t->ctx->lock);
- if (r)
- triton_thread_wakeup(ctx->thread);
+ n=epoll_wait(epoll_fd,epoll_events,max_events,-1);
+ if (n<0)
+ {
+ if (errno!=EINTR)
+ perror("epoll_wait");
+ continue;
+ }
+
+ for(i=0; i<n; i++)
+ {
+ t=(struct triton_timer_t*)epoll_events[i].data.ptr;
+ spin_lock(&t->ctx->lock);
+ list_add_tail(&t->entry2,&t->ctx->pending_timers);
+ t->pending=1;
+ r=triton_queue_ctx(t->ctx);
+ spin_unlock(&t->ctx->lock);
+ if (r)
+ triton_thread_wakeup(t->ctx->thread);
+ }
}
}
-int triton_timer_add(struct triton_timer_t *t)
+int triton_timer_add(struct triton_timer_t *t, int abs_time)
{
t->epoll_event.data.ptr=t;
t->epoll_event.events=EPOLLIN|EPOLLET;
if (!t->ctx)
t->ctx=default_ctx;
- t->fd=timerfd_create(CLOCK_MONOTONIC,0);
+ t->fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK);
if (t->fd<0)
{
fprintf(stderr,"timer: timerfd_create failed: %s\n",strerror(errno));
return -1;
}
- if (triton_timer_mod(t))
+ if (triton_timer_mod(t,abs_time))
{
close(t->fd);
return -1;
@@ -110,10 +110,8 @@ int triton_timer_add(struct triton_timer_t *t)
return 0;
}
-int triton_timer_mod(struct triton_timer_t *t)
+int triton_timer_mod(struct triton_timer_t *t,int abs_time)
{
- int flags;
-
struct itimerspec ts=
{
.it_value.tv_sec=t->expire_tv.tv_sec,
@@ -123,13 +121,9 @@ int triton_timer_mod(struct triton_timer_t *t)
};
if (t->expire_tv.tv_sec==0 && t->expire_tv.tv_usec==0)
- {
- ts.it_value=ts.interval;
- flags=0;
- }else
- flags=TFD_TIMER_ABSTIME;
+ ts.it_value=ts.it_interval;
- if (timerfd_settime(t->fd,flags,&ts,NULL))
+ if (timerfd_settime(t->fd,abs_time?TFD_TIMER_ABSTIME:0,&ts,NULL))
{
fprintf(stderr,"timer: timerfd_settime failed: %s\n",strerror(errno));
return -1;
diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c
index 23a69c6..e4fa117 100644
--- a/accel-pptpd/triton/triton.c
+++ b/accel-pptpd/triton/triton.c
@@ -1,4 +1,8 @@
#include <signal.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
#include "triton_p.h"
@@ -6,19 +10,19 @@ int thread_count=64;
static spinlock_t threads_lock=SPINLOCK_INITIALIZER;
static LIST_HEAD(threads);
-static int threads_count;
+static LIST_HEAD(sleep_threads);
-static spinlock_t ctx_queue_lock=SPINLOCK_INITIALIZER;
static LIST_HEAD(ctx_queue);
static spinlock_t ctx_list_lock=SPINLOCK_INITIALIZER;
static LIST_HEAD(ctx_list);
struct triton_ctx_t *default_ctx;
+static int terminate;
void triton_thread_wakeup(struct triton_thread_t *thread)
{
- pthread_kill(&thread->thread,SIGUSR1);
+ pthread_kill(thread->thread,SIGUSR1);
}
static void* triton_thread(struct triton_thread_t *thread)
@@ -36,19 +40,11 @@ static void* triton_thread(struct triton_thread_t *thread)
{
sigwait(&set,&sig);
- if (thread->terminate)
- return NULL;
-
cont:
- if (thread->ctx->close)
+ if (thread->ctx->need_close)
{
- list_for_each_entry(h,&thread->ctx->handlers,entry)
- if (h->close)
- h->close(h);
- list_for_each_entry(t,&thread->ctx->timers,entry)
- if (t->close)
- t->close(t);
- thread->ctx->close=0;
+ thread->ctx->close(thread->ctx);
+ thread->ctx->need_close=0;
}
while (1)
@@ -56,15 +52,15 @@ cont:
spin_lock(&thread->ctx->lock);
if (!list_empty(&thread->ctx->pending_timers))
{
- t=list_entry(thread->ctx->pending_timers.next);
+ t=list_entry(thread->ctx->pending_timers.next,typeof(*t),entry2);
list_del(&t->entry2);
spin_unlock(&thread->ctx->lock);
if (t->expire(t))
continue;
}
- if (!list_empty(&thread->ctx->pending_events))
+ if (!list_empty(&thread->ctx->pending_handlers))
{
- h=list_entry(thread->ctx->pending_events.next);
+ h=list_entry(thread->ctx->pending_handlers.next,typeof(*h),entry2);
list_del(&h->entry2);
h->pending=0;
spin_unlock(&thread->ctx->lock);
@@ -73,7 +69,7 @@ cont:
if (h->read)
if (h->read(h))
continue;
- if (h->trig_epoll_events&(EPOLLOUT|EPOLLERR|EPOLLHUP))
+ if (h->trig_epoll_events&(EPOLLOUT))
if (h->write)
if (h->write(h))
continue;
@@ -82,6 +78,8 @@ cont:
}
thread->ctx->thread=NULL;
spin_unlock(&thread->ctx->lock);
+ if (thread->ctx->need_free)
+ thread->ctx->free(thread->ctx);
thread->ctx=NULL;
break;
}
@@ -89,18 +87,21 @@ cont:
spin_lock(&threads_lock);
if (!list_empty(&ctx_queue))
{
- thread->ctx=list_entry(ctx_queue.next);
+ thread->ctx=list_entry(ctx_queue.next,typeof(*thread->ctx),entry2);
list_del(&thread->ctx->entry2);
spin_unlock(&threads_lock);
spin_lock(&thread->ctx->lock);
- ctx->thread=thread;
- ctx->queue=0;
+ thread->ctx->thread=thread;
+ thread->ctx->queued=0;
spin_unlock(&thread->ctx->lock);
goto cont;
}else
{
- list_add(&thread->entry,&threads);
+ if (!terminate)
+ list_add(&thread->entry2,&sleep_threads);
spin_unlock(&threads_lock);
+ if (terminate)
+ return NULL;
}
}
}
@@ -110,21 +111,18 @@ struct triton_thread_t *create_thread()
struct triton_thread_t *thread=malloc(sizeof(*thread));
memset(thread,0,sizeof(*thread));
- pthread_mutex_init(&thread->lock);
- pthread_cond_init(&thread->cond);
- pthread_create(&thread->thread,NULL,md_thread,thread);
- ++threads_count;
+ pthread_create(&thread->thread,NULL,(void*(*)(void*))triton_thread,thread);
return thread;
}
-void triton_queue_ctx(struct triton_ctx_t *ctx)
+int triton_queue_ctx(struct triton_ctx_t *ctx)
{
if (ctx->thread || ctx->queued)
return 0;
spin_lock(&threads_lock);
- if (list_empty(&threads))
+ if (list_empty(&sleep_threads))
{
list_add_tail(&ctx->entry2,&ctx_queue);
spin_unlock(&threads_lock);
@@ -132,8 +130,8 @@ void triton_queue_ctx(struct triton_ctx_t *ctx)
return 0;
}
- ctx->thread=list_entry(threads.next);
- list_del(&ctx->thread->entry);
+ ctx->thread=list_entry(sleep_threads.next,typeof(*ctx->thread),entry2);
+ list_del(&ctx->thread->entry2);
spin_unlock(&threads_lock);
return 1;
@@ -141,7 +139,7 @@ void triton_queue_ctx(struct triton_ctx_t *ctx)
void triton_register_ctx(struct triton_ctx_t *ctx)
{
- pthread_mutex_init(&ctx->lock);
+ spinlock_init(&ctx->lock);
INIT_LIST_HEAD(&ctx->handlers);
INIT_LIST_HEAD(&ctx->timers);
INIT_LIST_HEAD(&ctx->pending_handlers);
@@ -154,12 +152,13 @@ void triton_register_ctx(struct triton_ctx_t *ctx)
void triton_unregister_ctx(struct triton_ctx_t *ctx)
{
+ ctx->need_free=1;
spin_lock(&ctx_list_lock);
- list_add_tail(&ctx->entry,&ctx_list);
+ list_del(&ctx->entry);
spin_unlock(&ctx_list_lock);
}
-int triton_init()
+int triton_init(const char *conf_file)
{
default_ctx=malloc(sizeof(*default_ctx));
if (!default_ctx)
@@ -169,10 +168,19 @@ int triton_init()
}
triton_register_ctx(default_ctx);
+ if (conf_load(conf_file))
+ return -1;
+
+ if (log_init())
+ return -1;
+
if (md_init())
return -1;
+
if (timer_init())
return -1;
+
+ return 0;
}
void triton_run()
@@ -180,11 +188,13 @@ void triton_run()
struct triton_thread_t *t;
int i;
- for(i=0;i<max_threads;i++)
+ for(i=0;i<thread_count;i++)
{
t=create_thread();
list_add_tail(&t->entry,&threads);
+ list_add_tail(&t->entry2,&sleep_threads);
}
+
md_run();
timer_run();
}
@@ -192,17 +202,29 @@ void triton_run()
void triton_terminate()
{
struct triton_ctx_t *ctx;
- pthread_mutex_lock(&ctx_list_lock);
+ struct triton_thread_t *t;
+
+ md_terminate();
+ timer_terminate();
+
+ spin_lock(&ctx_list_lock);
list_for_each_entry(ctx,&ctx_list,entry)
{
- pthread_mutex_lock(&ctx->lock);
- ctx->close=1;
+ spin_lock(&ctx->lock);
+ ctx->need_close=1;
triton_queue_ctx(ctx);
- pthread_mutex_unlock(&ctx->lock);
+ spin_unlock(&ctx->lock);
}
- pthread_mutex_unlock(&ctx_list_lock);
+ spin_unlock(&ctx_list_lock);
- timer_terminate();
- md_terminate();
+ spin_lock(&threads_lock);
+ terminate=1;
+ spin_unlock(&threads_lock);
+
+ list_for_each_entry(t,&threads,entry)
+ triton_thread_wakeup(t);
+
+ list_for_each_entry(t,&threads,entry)
+ pthread_join(t->thread,NULL);
}
diff --git a/accel-pptpd/triton/triton.h b/accel-pptpd/triton/triton.h
index 0e9b73b..7364021 100644
--- a/accel-pptpd/triton/triton.h
+++ b/accel-pptpd/triton/triton.h
@@ -5,11 +5,13 @@
#include <pthread.h>
#include <sys/epoll.h>
-#include <triton.h>
+#include "list.h"
+#include "spinlock.h"
struct triton_thread_t
{
struct list_head entry;
+ struct list_head entry2;
pthread_t thread;
int terminate:1;
struct triton_ctx_t *ctx;
@@ -23,11 +25,15 @@ struct triton_ctx_t
struct list_head handlers;
struct list_head timers;
- triton_thread_t *thread;
+ struct triton_thread_t *thread;
struct list_head pending_handlers;
struct list_head pending_timers;
int queued:1;
- int close:1;
+ int need_close:1;
+ int need_free:1;
+
+ void (*close)(struct triton_ctx_t*);
+ void (*free)(struct triton_ctx_t*);
};
struct triton_md_handler_t
@@ -38,7 +44,7 @@ struct triton_md_handler_t
struct list_head entry2;
struct triton_ctx_t *ctx;
struct epoll_event epoll_event;
- uint32_t trig_epoll_event;
+ uint32_t trig_epoll_events;
int pending:1;
//=========
@@ -48,14 +54,16 @@ struct triton_md_handler_t
int (*read)(struct triton_md_handler_t *);
int (*write)(struct triton_md_handler_t *);
- void (*close)(struct triton_md_handler_t *);
//=========
};
struct triton_timer_t
{
struct list_head entry;
- int active;
+ struct list_head entry2;
+ struct epoll_event epoll_event;
+ struct triton_ctx_t *ctx;
+ int fd;
int pending:1;
struct timeval expire_tv;
@@ -63,28 +71,36 @@ struct triton_timer_t
int (*expire)(struct triton_timer_t *);
};
+struct conf_option_t
+{
+ struct list_head entry;
+
+ char *name;
+ char *val;
+};
+
+struct conf_sect_t
+{
+ const char *name;
+ struct list_head items;
+};
+
+void triton_register_ctx(struct triton_ctx_t *);
+void triton_unregister_ctx(struct triton_ctx_t *);
+
#define MD_MODE_READ 1
#define MD_MODE_WRITE 2
void triton_md_register_handler(struct triton_md_handler_t *h);
void triton_md_unregister_handler(struct triton_md_handler_t *h);
-void triton_md_enable_handler(struct triton_md_handler_t *h, int mode);
-void triton_md_disable_handler(struct triton_md_handler_t *h,int mode);
-void triton_md_set_timeout(struct triton_md_handler_t *h, int msec);
+int triton_md_enable_handler(struct triton_md_handler_t *h, int mode);
+int triton_md_disable_handler(struct triton_md_handler_t *h,int mode);
-void triton_timer_add(struct triton_timer_t*);
+int triton_timer_add(struct triton_timer_t*,int abs_time);
+int triton_timer_mod(struct triton_timer_t*,int abs_time);
void triton_timer_del(struct triton_timer_t*);
-typedef void (*triton_ss_func)(void);
-void triton_timer_single_shot1(int twait,triton_ss_func,int argc,...);
-void triton_timer_single_shot2(struct timeval *shot_tv,triton_ss_func,int argc,...);
-void triton_timer_single_shot3(int tv_sec,int tv_usec,triton_ss_func,int argc,...);
-
-int triton_get_int_option(const char *str);
-const char* triton_get_str_option(const char *str);
-double triton_get_double_option(const char *str);
-
-void triton_terminate(void);
-void triton_process_events(void);
+struct conf_sect_t *conf_get_section(const char *name);
+char *conf_get_opt(const char *sect, const char *name);
#define TRITON_OK 0
#define TRITON_ERR_NOCOMP -1
@@ -96,6 +112,7 @@ void triton_process_events(void);
#define TRITON_ERR_BUSY -5
int triton_init(const char *conf_file);
-int triton_run(int (*post_init)(void*),void *arg);
+void triton_run(void);
+void triton_terminate(void);
#endif
diff --git a/accel-pptpd/triton/triton_p.h b/accel-pptpd/triton/triton_p.h
index fae4848..e10c7a8 100644
--- a/accel-pptpd/triton/triton_p.h
+++ b/accel-pptpd/triton/triton_p.h
@@ -4,39 +4,16 @@
#include "triton.h"
#include "list.h"
-#include <stdarg.h>
-
-#define MAX_ARGS 32
-
-struct option_t
-{
- struct list_head entry;
-
- char *name;
- char *val;
-};
-
-struct timer_t
-{
- struct list_head entry;
- int del;
- struct triton_timer_t *timer;
-};
-
-struct timer_single_shot_t
-{
- struct list_head entry;
-
- struct timeval expire_tv;
- int arg_cnt;
- void *args;
- triton_ss_func ss_func;
-};
-
-extern void md_run();
-extern void md_terminate();
-extern void timer_run();
-extern void timer_terminate();
-extern struct triton_ctx_t *default_ctx;
+int log_init(void);
+int md_init();
+void md_run();
+void md_terminate();
+int timer_init();
+void timer_run();
+void timer_terminate();
+struct triton_ctx_t *default_ctx;
+int triton_queue_ctx(struct triton_ctx_t*);
+void triton_thread_wakeup(struct triton_thread_t*);
+int conf_load(const char *fname);
#endif