From e258bf67f51a09a67b7928b53e478fa243b013ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 15 Oct 2022 09:49:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.c | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/server.c b/server.c index 91b06dc..7170d34 100644 --- a/server.c +++ b/server.c @@ -627,7 +627,6 @@ static void cleanup_thread(thread_timer_t* timer) { timer->isbusy = 0; pthread_cond_destroy(&timer->c); pthread_mutex_destroy(&timer->mc); - pthread_rwlock_destroy(&timer->mb); setdicts[timer->index].data[0] = 0; puts("Finish cleaning"); } @@ -650,27 +649,26 @@ static void handle_pipe(int signo) { static void handle_accept(void *p) { pthread_cond_init(&timer_pointer_of(p)->c, NULL); pthread_mutex_init(&timer_pointer_of(p)->mc, NULL); - pthread_rwlock_init(&timer_pointer_of(p)->mb, NULL); - pthread_t thread = timer_pointer_of(p)->timerthread; - if(!thread || pthread_kill(thread, 0)) { - pthread_cond_init(&timer_pointer_of(p)->tc, NULL); - pthread_mutex_init(&timer_pointer_of(p)->tmc, NULL); - if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) { - perror("Error creating timer thread"); - cleanup_thread(timer_pointer_of(p)); - return; - } - timer_pointer_of(p)->timerthread = thread; - puts("Creating timer thread succeeded"); - } else { - pthread_mutex_lock(&timer_pointer_of(p)->tmc); - pthread_cond_signal(&timer_pointer_of(p)->tc); // wakeup thread - pthread_mutex_unlock(&timer_pointer_of(p)->tmc); - puts("Waking up timer thread succeeded"); - } pthread_cleanup_push((void*)&cleanup_thread, p); puts("Handling accept..."); while(1) { + pthread_t thread = timer_pointer_of(p)->timerthread; + if(!thread || pthread_kill(thread, 0)) { + pthread_cond_init(&timer_pointer_of(p)->tc, NULL); + pthread_mutex_init(&timer_pointer_of(p)->tmc, NULL); + if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) { + perror("Error creating timer thread"); + cleanup_thread(timer_pointer_of(p)); + return; + } + timer_pointer_of(p)->timerthread = thread; + puts("Creating timer thread succeeded"); + } else { + pthread_mutex_lock(&timer_pointer_of(p)->tmc); + pthread_cond_signal(&timer_pointer_of(p)->tc); // wakeup thread + pthread_mutex_unlock(&timer_pointer_of(p)->tmc); + puts("Waking up timer thread succeeded"); + } int accept_fd = timer_pointer_of(p)->accept_fd; uint32_t index = timer_pointer_of(p)->index; uint8_t *buff = timer_pointer_of(p)->buf; @@ -762,7 +760,6 @@ static void handle_accept(void *p) { #endif } CONV_END: puts("Conversation end"); - puts("Thread job finished normally"); pthread_rwlock_wrlock(&timer_pointer_of(p)->mb); timer_pointer_of(p)->isbusy = 0; pthread_mutex_lock(&timer_pointer_of(p)->mc); @@ -771,11 +768,6 @@ static void handle_accept(void *p) { pthread_cond_wait(&timer_pointer_of(p)->c, &timer_pointer_of(p)->mc); pthread_mutex_unlock(&timer_pointer_of(p)->mc); puts("Thread wakeup"); - if(!pthread_kill(thread, 0)) { - pthread_mutex_lock(&timer_pointer_of(p)->tmc); - pthread_cond_signal(&timer_pointer_of(p)->tc); // wakeup thread - pthread_mutex_unlock(&timer_pointer_of(p)->tmc); - } } pthread_cleanup_pop(1); } @@ -802,6 +794,7 @@ static void accept_client(int fd) { init_crypto(); init_dict_pool(open_dict(0, 1)); close_dict(0); + for(int i = 0; i < THREADCNT; i++) pthread_rwlock_init(&timers[i].mb, NULL); while(1) { puts("Ready for accept, waitting..."); int p = 0; @@ -836,7 +829,7 @@ static void accept_client(int fd) { inet_ntop(AF_INET, &in, str, sizeof(str)); #endif time_t t = time(NULL); - printf("\n> %sAccept client %s:%u at slot No.%d\n", ctime(&t), str, port, p); + printf("\n> %sAccept client %s:%u at slot No.%d, ", ctime(&t), str, port, p); thread_timer_t* timer = &timers[p]; timer->accept_fd = accept_fd; timer->index = p; @@ -846,7 +839,7 @@ static void accept_client(int fd) { pthread_rwlock_wrlock(&timer->mb); timer->isbusy = 1; pthread_rwlock_unlock(&timer->mb); - puts("Set thread status to busy"); + printf("Set thread status to busy, "); if(timer->thread) { pthread_mutex_lock(&timer->mc); pthread_cond_signal(&timer->c); // wakeup thread @@ -855,8 +848,8 @@ static void accept_client(int fd) { } else if (pthread_create(&timer->thread, &attr, (void *)&handle_accept, timer)) { perror("Error creating thread"); cleanup_thread(timer); - continue; - } else puts("Creating thread succeeded"); + putchar('\n'); + } else puts("Thread created"); } }