mirror of
https://github.com/fumiama/simple-dict.git
synced 2026-06-23 04:30:35 +08:00
优化
This commit is contained in:
60
server.c
60
server.c
@@ -584,8 +584,10 @@ static void handle_quit(int signo) {
|
|||||||
uint32_t index = (uint32_t)(pthread_getspecific(pthread_key_index));
|
uint32_t index = (uint32_t)(pthread_getspecific(pthread_key_index));
|
||||||
printf("Handle sigquit@%d\n", index-1);
|
printf("Handle sigquit@%d\n", index-1);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
signal(SIGQUIT, handle_quit);
|
if(index) {
|
||||||
if(index) longjmp(jmp2convend[index-1], signo);
|
signal(SIGQUIT, handle_quit);
|
||||||
|
longjmp(jmp2convend[index-1], signo);
|
||||||
|
}
|
||||||
else pthread_exit(NULL);
|
else pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -593,8 +595,10 @@ static void handle_segv(int signo) {
|
|||||||
uint32_t index = (uint32_t)(pthread_getspecific(pthread_key_index));
|
uint32_t index = (uint32_t)(pthread_getspecific(pthread_key_index));
|
||||||
printf("Handle sigsegv@%d\n", index-1);
|
printf("Handle sigsegv@%d\n", index-1);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
signal(SIGSEGV, handle_segv);
|
if(index) {
|
||||||
if(index) longjmp(jmp2convend[index-1], signo);
|
signal(SIGSEGV, handle_segv);
|
||||||
|
longjmp(jmp2convend[index-1], signo);
|
||||||
|
}
|
||||||
else pthread_exit(NULL);
|
else pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -614,21 +618,18 @@ static void handle_pipe(int signo) {
|
|||||||
uint32_t index = (uint32_t)(pthread_getspecific(pthread_key_index));
|
uint32_t index = (uint32_t)(pthread_getspecific(pthread_key_index));
|
||||||
printf("Pipe error@%d, break loop...\n", index-1);
|
printf("Pipe error@%d, break loop...\n", index-1);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
signal(SIGPIPE, handle_pipe);
|
if(index) {
|
||||||
if(index) longjmp(jmp2convend[index-1], signo);
|
signal(SIGPIPE, handle_pipe);
|
||||||
|
longjmp(jmp2convend[index-1], signo);
|
||||||
|
}
|
||||||
else pthread_exit(NULL);
|
else pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void accept_timer(void *p) {
|
static void accept_timer(void *p) {
|
||||||
thread_timer_t *timer = timer_pointer_of(p);
|
thread_timer_t *timer = timer_pointer_of(p);
|
||||||
uint32_t index = timer->index;
|
uint32_t index = timer->index;
|
||||||
sigset_t mask;
|
|
||||||
pthread_t thread = timer->thread;
|
pthread_t thread = timer->thread;
|
||||||
|
|
||||||
sigemptyset(&mask);
|
|
||||||
sigaddset(&mask, SIGPIPE); // 防止处理嵌套
|
|
||||||
pthread_sigmask(SIG_BLOCK, &mask, NULL);
|
|
||||||
|
|
||||||
sleep(MAXWAITSEC / 4);
|
sleep(MAXWAITSEC / 4);
|
||||||
while(thread && !pthread_kill(thread, 0)) {
|
while(thread && !pthread_kill(thread, 0)) {
|
||||||
pthread_rwlock_rdlock(&timer->mb);
|
pthread_rwlock_rdlock(&timer->mb);
|
||||||
@@ -665,11 +666,6 @@ static void accept_timer(void *p) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void cleanup_thread(thread_timer_t* timer) {
|
static void cleanup_thread(thread_timer_t* timer) {
|
||||||
sigset_t mask;
|
|
||||||
sigemptyset(&mask);
|
|
||||||
sigaddset(&mask, SIGPIPE); // 防止处理嵌套
|
|
||||||
pthread_sigmask(SIG_BLOCK, &mask, NULL);
|
|
||||||
|
|
||||||
printf("Start cleaning@%d, ", timer->index);
|
printf("Start cleaning@%d, ", timer->index);
|
||||||
|
|
||||||
if(timer->accept_fd) {
|
if(timer->accept_fd) {
|
||||||
@@ -705,7 +701,10 @@ static void handle_accept(void *p) {
|
|||||||
pthread_cleanup_push((void*)&cleanup_thread, p);
|
pthread_cleanup_push((void*)&cleanup_thread, p);
|
||||||
puts("Handling accept...");
|
puts("Handling accept...");
|
||||||
pthread_setspecific(pthread_key_index, (void*)((uintptr_t)timer_pointer_of(p)->index+1));
|
pthread_setspecific(pthread_key_index, (void*)((uintptr_t)timer_pointer_of(p)->index+1));
|
||||||
if(setjmp(jmp2convend[timer_pointer_of(p)->index])) goto CONV_END;
|
if(setjmp(jmp2convend[timer_pointer_of(p)->index])) {
|
||||||
|
printf("Long Jump@%d\n", timer_pointer_of(p)->index);
|
||||||
|
goto CONV_END;
|
||||||
|
}
|
||||||
while(1) {
|
while(1) {
|
||||||
int accept_fd = timer_pointer_of(p)->accept_fd;
|
int accept_fd = timer_pointer_of(p)->accept_fd;
|
||||||
uint32_t index = timer_pointer_of(p)->index;
|
uint32_t index = timer_pointer_of(p)->index;
|
||||||
@@ -834,12 +833,21 @@ static void accept_client(int fd) {
|
|||||||
perror("Error when forking a subprocess");
|
perror("Error when forking a subprocess");
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}*/
|
}*/
|
||||||
signal(SIGINT, handle_int);
|
#ifdef __APPLE__
|
||||||
signal(SIGQUIT, handle_quit);
|
sigaction(SIGINT , &(const struct sigaction){handle_int , 0, 0}, NULL);
|
||||||
signal(SIGKILL, handle_kill);
|
sigaction(SIGQUIT, &(const struct sigaction){handle_quit, 0, 0}, NULL);
|
||||||
signal(SIGSEGV, handle_segv);
|
sigaction(SIGKILL, &(const struct sigaction){handle_kill, 0, 0}, NULL);
|
||||||
signal(SIGPIPE, handle_pipe);
|
sigaction(SIGSEGV, &(const struct sigaction){handle_segv, 0, 0}, NULL);
|
||||||
signal(SIGTERM, handle_kill);
|
sigaction(SIGPIPE, &(const struct sigaction){handle_pipe, 0, 0}, NULL);
|
||||||
|
sigaction(SIGTERM, &(const struct sigaction){handle_kill, 0, 0}, NULL);
|
||||||
|
#else
|
||||||
|
sigaction(SIGINT , &(const struct sigaction){handle_int , NULL, 0, 0, NULL}, NULL);
|
||||||
|
sigaction(SIGQUIT, &(const struct sigaction){handle_quit, NULL, 0, 0, NULL}, NULL);
|
||||||
|
sigaction(SIGKILL, &(const struct sigaction){handle_kill, NULL, 0, 0, NULL}, NULL);
|
||||||
|
sigaction(SIGSEGV, &(const struct sigaction){handle_segv, NULL, 0, 0, NULL}, NULL);
|
||||||
|
sigaction(SIGPIPE, &(const struct sigaction){handle_pipe, NULL, 0, 0, NULL}, NULL);
|
||||||
|
sigaction(SIGTERM, &(const struct sigaction){handle_kill, NULL, 0, 0, NULL}, NULL);
|
||||||
|
#endif
|
||||||
pthread_attr_init(&attr);
|
pthread_attr_init(&attr);
|
||||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||||
init_crypto();
|
init_crypto();
|
||||||
@@ -863,7 +871,7 @@ static void accept_client(int fd) {
|
|||||||
sleep(1);
|
sleep(1);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
printf("Ready for accept on slot No.%d, ", p);
|
printf("Ready for accept on slot No.%d\n", p);
|
||||||
thread_timer_t* timer = &timers[p];
|
thread_timer_t* timer = &timers[p];
|
||||||
pthread_rwlock_unlock(&timer->mb);
|
pthread_rwlock_unlock(&timer->mb);
|
||||||
#ifdef LISTEN_ON_IPV6
|
#ifdef LISTEN_ON_IPV6
|
||||||
@@ -872,7 +880,7 @@ static void accept_client(int fd) {
|
|||||||
struct sockaddr_in client_addr;
|
struct sockaddr_in client_addr;
|
||||||
#endif
|
#endif
|
||||||
int accept_fd;
|
int accept_fd;
|
||||||
while((accept_fd=accept(fd, (struct sockaddr *)&client_addr, &struct_len))<=0) {
|
if((accept_fd=accept(fd, (struct sockaddr *)&client_addr, &struct_len))<=0) {
|
||||||
perror("Accept client error");
|
perror("Accept client error");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -900,7 +908,7 @@ static void accept_client(int fd) {
|
|||||||
reset_seq(p);
|
reset_seq(p);
|
||||||
// start or wakeup accept thread
|
// start or wakeup accept thread
|
||||||
pthread_t thread = timer->thread;
|
pthread_t thread = timer->thread;
|
||||||
if(thread) {
|
if(thread && !pthread_kill(thread, 0)) {
|
||||||
pthread_mutex_lock(&timer->mc);
|
pthread_mutex_lock(&timer->mc);
|
||||||
pthread_cond_signal(&timer->c); // wakeup thread
|
pthread_cond_signal(&timer->c); // wakeup thread
|
||||||
pthread_mutex_unlock(&timer->mc);
|
pthread_mutex_unlock(&timer->mc);
|
||||||
|
|||||||
Reference in New Issue
Block a user