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