mirror of
https://github.com/fumiama/simple-dict.git
synced 2026-06-05 02:00:25 +08:00
优化 timer mutex
This commit is contained in:
22
server.c
22
server.c
@@ -20,10 +20,6 @@
|
||||
#include "crypto.h"
|
||||
#include "config.h"
|
||||
|
||||
#if !__APPLE__
|
||||
#include <sys/sendfile.h>
|
||||
#endif
|
||||
|
||||
#ifdef LISTEN_ON_IPV6
|
||||
static socklen_t struct_len = sizeof(struct sockaddr_in6);
|
||||
static struct sockaddr_in6 server_addr;
|
||||
@@ -46,7 +42,7 @@ struct thread_timer_t {
|
||||
pthread_mutex_t tmc;
|
||||
pthread_rwlock_t mb;
|
||||
uint8_t isbusy; // lock by mb
|
||||
uint8_t hastimerslept; // lock by mb
|
||||
uint8_t hastimerslept; // lock by tmc
|
||||
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;
|
||||
@@ -595,16 +591,12 @@ static void accept_timer(void *p) {
|
||||
pthread_rwlock_unlock(&timer->mb);
|
||||
if(!isbusy) {
|
||||
pthread_mutex_lock(&timer->tmc);
|
||||
pthread_rwlock_wrlock(&timer->mb);
|
||||
timer->hastimerslept = 1;
|
||||
pthread_rwlock_unlock(&timer->mb);
|
||||
puts("Timer sleep");
|
||||
pthread_cond_wait(&timer->tc, &timer->tmc);
|
||||
timer->hastimerslept = 0;
|
||||
pthread_mutex_unlock(&timer->tmc);
|
||||
puts("Timer wake up");
|
||||
pthread_rwlock_wrlock(&timer->mb);
|
||||
timer->hastimerslept = 0;
|
||||
pthread_rwlock_unlock(&timer->mb);
|
||||
sleep(MAXWAITSEC / 4);
|
||||
}
|
||||
if(is_dict_opening) touch_timer(p);
|
||||
@@ -620,10 +612,10 @@ static void accept_timer(void *p) {
|
||||
}
|
||||
sleep(MAXWAITSEC / 4);
|
||||
}
|
||||
pthread_rwlock_wrlock(&timer->mb);
|
||||
pthread_mutex_lock(&timer->tmc);
|
||||
timer->timerthread = NULL;
|
||||
timer->hastimerslept = 0;
|
||||
pthread_rwlock_unlock(&timer->mb);
|
||||
pthread_mutex_unlock(&timer->tmc);
|
||||
}
|
||||
|
||||
static void cleanup_thread(thread_timer_t* timer) {
|
||||
@@ -882,9 +874,7 @@ static void accept_client(int fd) {
|
||||
}
|
||||
// start or wakeup timer thread
|
||||
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)) {
|
||||
printf("Creating timer thread...");
|
||||
pthread_cond_init(&timer->tc, NULL);
|
||||
@@ -896,9 +886,9 @@ static void accept_client(int fd) {
|
||||
continue;
|
||||
}
|
||||
puts("succeeded");
|
||||
pthread_rwlock_wrlock(&timer->mb);
|
||||
pthread_mutex_lock(&timer->tmc);
|
||||
timer->hastimerslept = 0;
|
||||
pthread_rwlock_unlock(&timer->mb);
|
||||
pthread_mutex_unlock(&timer->tmc);
|
||||
} else if(hastimerslept) {
|
||||
printf("Waking up timer thread...");
|
||||
pthread_mutex_lock(&timer->tmc);
|
||||
|
||||
Reference in New Issue
Block a user