lpereira / lwan

Experimental, scalable, high performance HTTP server
https://lwan.ws
GNU General Public License v2.0
5.94k stars 548 forks source link

quick quit optimize #196

Closed buf1024 closed 7 years ago

buf1024 commented 7 years ago

When I want to quit lwan every time, the program waits for secondst. That's because in the job_thread function(in file lwan-job.c) makes a system call nanosleep. That causes the job thread suspend, and wakes up after a few seconds. I think this may be optimized for quick quit.

Maybe lwan should use some mechanism to improve the quit speed. I use pthread_cond_timedwait to do that. Here is the diff patch.

28d27
< #include <sys/time.h>
45,47d43
< static pthread_mutex_t job_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
< static pthread_cond_t  job_wait_cond = PTHREAD_COND_INITIALIZER;
< 
51,52c47
<     pthread_mutex_lock(&job_wait_mutex);
<     int job_wait_sec = 1;
---
>     struct timespec rgtp = { 1, 0 };
67,80c62,69
<             job_wait_sec = 1;
<         else if (job_wait_sec <= 15)
<             job_wait_sec++;
<         
<         struct timeval now;
<         gettimeofday(&now, NULL);
<         struct timespec rgtp = { now.tv_sec + job_wait_sec, now.tv_usec * 1000 };
< 
<         lwan_status_debug("job_thread sleep for %d second\n", job_wait_sec);         
<         pthread_cond_timedwait(&job_wait_cond, &job_wait_mutex, &rgtp);
<         //if (UNLIKELY(nanosleep(&rgtp, NULL) < 0)) {
<         //    if (errno == EINTR)
<         //        sleep(1);
<         //}
---
>             rgtp.tv_sec = 1;
>         else if (rgtp.tv_sec <= 15)
>             rgtp.tv_sec++;
> 
>         if (UNLIKELY(nanosleep(&rgtp, NULL) < 0)) {
>             if (errno == EINTR)
>                 sleep(1);
>         }
120d108
<         pthread_cond_signal(&job_wait_cond);
lpereira commented 7 years ago

I can take the patch as is (just removing the commented-out code), but it would be best if you could make a pull request. Helps with giving you credit for the change as well as making my life a little bit easier. :)

lpereira commented 7 years ago

Closing as #199 has been merged.