mirror of
https://github.com/fumiama/simple-dict.git
synced 2026-06-23 20:50:27 +08:00
优化 timer
This commit is contained in:
28
server.c
28
server.c
@@ -42,11 +42,12 @@ struct thread_timer_t {
|
|||||||
pthread_t timerthread;
|
pthread_t timerthread;
|
||||||
pthread_cond_t c;
|
pthread_cond_t c;
|
||||||
pthread_mutex_t mc;
|
pthread_mutex_t mc;
|
||||||
pthread_rwlock_t mb;
|
|
||||||
pthread_cond_t tc;
|
pthread_cond_t tc;
|
||||||
pthread_mutex_t tmc;
|
pthread_mutex_t tmc;
|
||||||
uint8_t isbusy;
|
pthread_rwlock_t mb;
|
||||||
uint8_t buf[BUFSIZ-sizeof(uint32_t)-sizeof(int)-sizeof(time_t)-sizeof(ssize_t)-sizeof(char*)-2*sizeof(pthread_t)-2*sizeof(pthread_cond_t)-2*sizeof(pthread_mutex_t)-sizeof(pthread_rwlock_t)-sizeof(uint8_t)];
|
uint8_t isbusy; // lock by mb
|
||||||
|
uint8_t hastimerslept; // lock by mb
|
||||||
|
uint8_t buf[BUFSIZ-sizeof(uint32_t)-sizeof(int)-sizeof(time_t)-sizeof(ssize_t)-sizeof(char*)-2*sizeof(pthread_t)-2*sizeof(pthread_cond_t)-2*sizeof(pthread_mutex_t)-sizeof(pthread_rwlock_t)-2*sizeof(uint8_t)];
|
||||||
};
|
};
|
||||||
typedef struct thread_timer_t thread_timer_t;
|
typedef struct thread_timer_t thread_timer_t;
|
||||||
static thread_timer_t timers[THREADCNT];
|
static thread_timer_t timers[THREADCNT];
|
||||||
@@ -594,10 +595,16 @@ static void accept_timer(void *p) {
|
|||||||
pthread_rwlock_unlock(&timer->mb);
|
pthread_rwlock_unlock(&timer->mb);
|
||||||
if(!isbusy) {
|
if(!isbusy) {
|
||||||
pthread_mutex_lock(&timer->tmc);
|
pthread_mutex_lock(&timer->tmc);
|
||||||
|
pthread_rwlock_wrlock(&timer->mb);
|
||||||
|
timer->hastimerslept = 1;
|
||||||
|
pthread_rwlock_unlock(&timer->mb);
|
||||||
puts("Timer sleep");
|
puts("Timer sleep");
|
||||||
pthread_cond_wait(&timer->tc, &timer->tmc);
|
pthread_cond_wait(&timer->tc, &timer->tmc);
|
||||||
pthread_mutex_unlock(&timer->tmc);
|
pthread_mutex_unlock(&timer->tmc);
|
||||||
puts("Timer wake up");
|
puts("Timer wake up");
|
||||||
|
pthread_rwlock_wrlock(&timer->mb);
|
||||||
|
timer->hastimerslept = 0;
|
||||||
|
pthread_rwlock_unlock(&timer->mb);
|
||||||
sleep(MAXWAITSEC / 4);
|
sleep(MAXWAITSEC / 4);
|
||||||
}
|
}
|
||||||
if(is_dict_opening) touch_timer(p);
|
if(is_dict_opening) touch_timer(p);
|
||||||
@@ -613,6 +620,10 @@ static void accept_timer(void *p) {
|
|||||||
}
|
}
|
||||||
sleep(MAXWAITSEC / 4);
|
sleep(MAXWAITSEC / 4);
|
||||||
}
|
}
|
||||||
|
pthread_rwlock_wrlock(&timer->mb);
|
||||||
|
timer->timerthread = NULL;
|
||||||
|
timer->hastimerslept = 0;
|
||||||
|
pthread_rwlock_unlock(&timer->mb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cleanup_thread(thread_timer_t* timer) {
|
static void cleanup_thread(thread_timer_t* timer) {
|
||||||
@@ -866,10 +877,14 @@ static void accept_client(int fd) {
|
|||||||
cleanup_thread(timer);
|
cleanup_thread(timer);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
continue;
|
continue;
|
||||||
} else puts("Thread created");
|
}
|
||||||
|
puts("Thread created");
|
||||||
}
|
}
|
||||||
// start or wakeup timer thread
|
// start or wakeup timer thread
|
||||||
pthread_t thread = timer->timerthread;
|
pthread_t thread = timer->timerthread;
|
||||||
|
pthread_rwlock_rdlock(&timer->mb);
|
||||||
|
uint8_t hastimerslept = timer->hastimerslept;
|
||||||
|
pthread_rwlock_unlock(&timer->mb);
|
||||||
if(!thread || pthread_kill(thread, 0)) {
|
if(!thread || pthread_kill(thread, 0)) {
|
||||||
printf("Creating timer thread...");
|
printf("Creating timer thread...");
|
||||||
pthread_cond_init(&timer->tc, NULL);
|
pthread_cond_init(&timer->tc, NULL);
|
||||||
@@ -881,7 +896,10 @@ static void accept_client(int fd) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
puts("succeeded");
|
puts("succeeded");
|
||||||
} else {
|
pthread_rwlock_wrlock(&timer->mb);
|
||||||
|
timer->hastimerslept = 0;
|
||||||
|
pthread_rwlock_unlock(&timer->mb);
|
||||||
|
} else if(hastimerslept) {
|
||||||
printf("Waking up timer thread...");
|
printf("Waking up timer thread...");
|
||||||
pthread_mutex_lock(&timer->tmc);
|
pthread_mutex_lock(&timer->tmc);
|
||||||
pthread_cond_signal(&timer->tc); // wakeup thread
|
pthread_cond_signal(&timer->tc); // wakeup thread
|
||||||
|
|||||||
Reference in New Issue
Block a user