diff options
| author | 2023-02-06 10:53:29 +0100 | |
|---|---|---|
| committer | 2023-02-06 10:53:29 +0100 | |
| commit | 66d974a0c6d0b4ca1116ed6dd91ad4a7bc35306c (patch) | |
| tree | fe9a15285d7f3a729fb8f4451441ea2b7d3dfff7 /httpd.c | |
| parent | Input connections cannot be non-threaded. (diff) | |
| download | snac2-66d974a0c6d0b4ca1116ed6dd91ad4a7bc35306c.tar.gz snac2-66d974a0c6d0b4ca1116ed6dd91ad4a7bc35306c.tar.xz snac2-66d974a0c6d0b4ca1116ed6dd91ad4a7bc35306c.zip | |
Started work towards the pool of threads.
Diffstat (limited to 'httpd.c')
| -rw-r--r-- | httpd.c | 34 |
1 files changed, 27 insertions, 7 deletions
| @@ -252,8 +252,6 @@ static void *purge_thread(void *arg) | |||
| 252 | static void *background_thread(void *arg) | 252 | static void *background_thread(void *arg) |
| 253 | /* background thread (queue management and other things) */ | 253 | /* background thread (queue management and other things) */ |
| 254 | { | 254 | { |
| 255 | pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 256 | pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER; | ||
| 257 | time_t purge_time; | 255 | time_t purge_time; |
| 258 | 256 | ||
| 259 | /* first purge time */ | 257 | /* first purge time */ |
| @@ -295,7 +293,10 @@ static void *background_thread(void *arg) | |||
| 295 | } | 293 | } |
| 296 | 294 | ||
| 297 | /* sleep 3 seconds */ | 295 | /* sleep 3 seconds */ |
| 296 | pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 297 | pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER; | ||
| 298 | struct timespec ts; | 298 | struct timespec ts; |
| 299 | |||
| 299 | clock_gettime(CLOCK_REALTIME, &ts); | 300 | clock_gettime(CLOCK_REALTIME, &ts); |
| 300 | ts.tv_sec += 3; | 301 | ts.tv_sec += 3; |
| 301 | 302 | ||
| @@ -318,13 +319,18 @@ static void *connection_thread(void *arg) | |||
| 318 | } | 319 | } |
| 319 | 320 | ||
| 320 | 321 | ||
| 322 | #ifndef MAX_THREADS | ||
| 323 | #define MAX_THREADS 256 | ||
| 324 | #endif | ||
| 325 | |||
| 321 | void httpd(void) | 326 | void httpd(void) |
| 322 | /* starts the server */ | 327 | /* starts the server */ |
| 323 | { | 328 | { |
| 324 | char *address; | 329 | char *address; |
| 325 | int port; | 330 | int port; |
| 326 | int rs; | 331 | int rs; |
| 327 | pthread_t htid; | 332 | pthread_t threads[MAX_THREADS]; |
| 333 | int n_threads = 0; | ||
| 328 | 334 | ||
| 329 | address = xs_dict_get(srv_config, "address"); | 335 | address = xs_dict_get(srv_config, "address"); |
| 330 | port = xs_number_get(xs_dict_get(srv_config, "port")); | 336 | port = xs_number_get(xs_dict_get(srv_config, "port")); |
| @@ -338,11 +344,25 @@ void httpd(void) | |||
| 338 | 344 | ||
| 339 | signal(SIGPIPE, SIG_IGN); | 345 | signal(SIGPIPE, SIG_IGN); |
| 340 | signal(SIGTERM, term_handler); | 346 | signal(SIGTERM, term_handler); |
| 341 | signal(SIGINT, term_handler); | 347 | signal(SIGINT, term_handler); |
| 342 | 348 | ||
| 343 | srv_log(xs_fmt("httpd start %s:%d %s", address, port, USER_AGENT)); | 349 | srv_log(xs_fmt("httpd start %s:%d %s", address, port, USER_AGENT)); |
| 344 | 350 | ||
| 345 | pthread_create(&htid, NULL, background_thread, NULL); | 351 | /* thread creation */ |
| 352 | #ifdef _SC_NPROCESSORS_ONLN | ||
| 353 | n_threads = sysconf(_SC_NPROCESSORS_ONLN); | ||
| 354 | #endif | ||
| 355 | |||
| 356 | if (n_threads < 4) | ||
| 357 | n_threads = 4; | ||
| 358 | |||
| 359 | if (n_threads > MAX_THREADS) | ||
| 360 | n_threads = MAX_THREADS; | ||
| 361 | |||
| 362 | srv_debug(0, xs_fmt("using %d threads", n_threads)); | ||
| 363 | |||
| 364 | /* thread #0 is the background thread */ | ||
| 365 | pthread_create(&threads[0], NULL, background_thread, NULL); | ||
| 346 | 366 | ||
| 347 | if (setjmp(on_break) == 0) { | 367 | if (setjmp(on_break) == 0) { |
| 348 | for (;;) { | 368 | for (;;) { |
| @@ -357,8 +377,8 @@ void httpd(void) | |||
| 357 | 377 | ||
| 358 | srv_running = 0; | 378 | srv_running = 0; |
| 359 | 379 | ||
| 360 | /* wait for the helper thread to end */ | 380 | /* wait for the background thread to end */ |
| 361 | pthread_join(htid, NULL); | 381 | pthread_join(threads[0], NULL); |
| 362 | 382 | ||
| 363 | srv_log(xs_fmt("httpd stop %s:%d", address, port)); | 383 | srv_log(xs_fmt("httpd stop %s:%d", address, port)); |
| 364 | } | 384 | } |