diff options
| author | 2023-02-22 10:35:26 +0100 | |
|---|---|---|
| committer | 2023-02-22 10:35:26 +0100 | |
| commit | 60c50c02f62844b9332872a9bd6d88416889f293 (patch) | |
| tree | 73d9ef86ec845c8d48f50a2290aabe2b7ed37847 | |
| parent | Close everything if xs_socket_accept() returns NULL. (diff) | |
| download | penes-snac2-60c50c02f62844b9332872a9bd6d88416889f293.tar.gz penes-snac2-60c50c02f62844b9332872a9bd6d88416889f293.tar.xz penes-snac2-60c50c02f62844b9332872a9bd6d88416889f293.zip | |
Create sleep mutex and cond variable only once.
| -rw-r--r-- | httpd.c | 15 |
1 files changed, 10 insertions, 5 deletions
| @@ -342,6 +342,9 @@ static void *job_thread(void *arg) | |||
| 342 | return NULL; | 342 | return NULL; |
| 343 | } | 343 | } |
| 344 | 344 | ||
| 345 | /* background thread sleep control */ | ||
| 346 | static pthread_mutex_t sleep_mutex; | ||
| 347 | static pthread_cond_t sleep_cond; | ||
| 345 | 348 | ||
| 346 | static void *background_thread(void *arg) | 349 | static void *background_thread(void *arg) |
| 347 | /* background thread (queue management and other things) */ | 350 | /* background thread (queue management and other things) */ |
| @@ -392,16 +395,14 @@ static void *background_thread(void *arg) | |||
| 392 | #ifdef USE_POLL_FOR_SLEEP | 395 | #ifdef USE_POLL_FOR_SLEEP |
| 393 | poll(NULL, 0, 3 * 1000); | 396 | poll(NULL, 0, 3 * 1000); |
| 394 | #else | 397 | #else |
| 395 | pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 396 | pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER; | ||
| 397 | struct timespec ts; | 398 | struct timespec ts; |
| 398 | 399 | ||
| 399 | clock_gettime(CLOCK_REALTIME, &ts); | 400 | clock_gettime(CLOCK_REALTIME, &ts); |
| 400 | ts.tv_sec += 3; | 401 | ts.tv_sec += 3; |
| 401 | 402 | ||
| 402 | pthread_mutex_lock(&dummy_mutex); | 403 | pthread_mutex_lock(&sleep_mutex); |
| 403 | while (pthread_cond_timedwait(&dummy_cond, &dummy_mutex, &ts) == 0); | 404 | while (pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &ts) == 0); |
| 404 | pthread_mutex_unlock(&dummy_mutex); | 405 | pthread_mutex_unlock(&sleep_mutex); |
| 405 | #endif | 406 | #endif |
| 406 | } | 407 | } |
| 407 | } | 408 | } |
| @@ -449,6 +450,10 @@ void httpd(void) | |||
| 449 | sem_init(&job_sem, 0, 0); | 450 | sem_init(&job_sem, 0, 0); |
| 450 | job_fifo = xs_list_new(); | 451 | job_fifo = xs_list_new(); |
| 451 | 452 | ||
| 453 | /* initialize sleep control */ | ||
| 454 | pthread_mutex_init(&sleep_mutex, NULL); | ||
| 455 | pthread_cond_init(&sleep_cond, NULL); | ||
| 456 | |||
| 452 | n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads")); | 457 | n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads")); |
| 453 | 458 | ||
| 454 | #ifdef _SC_NPROCESSORS_ONLN | 459 | #ifdef _SC_NPROCESSORS_ONLN |