diff options
| author | 2023-02-06 18:59:20 +0100 | |
|---|---|---|
| committer | 2023-02-06 18:59:20 +0100 | |
| commit | 451d964c0c5d4b2d6b40c2d3d79bf502baea3500 (patch) | |
| tree | f69b82b0da892764dc2e9591421b6e68352713b1 /httpd.c | |
| parent | Simplified job_post() and job_wait(). (diff) | |
| download | snac2-451d964c0c5d4b2d6b40c2d3d79bf502baea3500.tar.gz snac2-451d964c0c5d4b2d6b40c2d3d79bf502baea3500.tar.xz snac2-451d964c0c5d4b2d6b40c2d3d79bf502baea3500.zip | |
More thread work.
Diffstat (limited to 'httpd.c')
| -rw-r--r-- | httpd.c | 55 |
1 files changed, 47 insertions, 8 deletions
| @@ -335,14 +335,16 @@ xs_list *job_fifo = NULL; | |||
| 335 | void job_post(const xs_val *job) | 335 | void job_post(const xs_val *job) |
| 336 | /* posts a job for the threads to process it */ | 336 | /* posts a job for the threads to process it */ |
| 337 | { | 337 | { |
| 338 | /* lock the mutex */ | 338 | if (job != NULL) { |
| 339 | pthread_mutex_lock(&job_mutex); | 339 | /* lock the mutex */ |
| 340 | pthread_mutex_lock(&job_mutex); | ||
| 340 | 341 | ||
| 341 | /* add to the fifo */ | 342 | /* add to the fifo */ |
| 342 | job_fifo = xs_list_append(job_fifo, job); | 343 | job_fifo = xs_list_append(job_fifo, job); |
| 343 | 344 | ||
| 344 | /* unlock the mutex */ | 345 | /* unlock the mutex */ |
| 345 | pthread_mutex_unlock(&job_mutex); | 346 | pthread_mutex_unlock(&job_mutex); |
| 347 | } | ||
| 346 | 348 | ||
| 347 | /* ask for someone to attend it */ | 349 | /* ask for someone to attend it */ |
| 348 | sem_post(&job_sem); | 350 | sem_post(&job_sem); |
| @@ -371,6 +373,33 @@ void job_wait(xs_val **job) | |||
| 371 | #define MAX_THREADS 256 | 373 | #define MAX_THREADS 256 |
| 372 | #endif | 374 | #endif |
| 373 | 375 | ||
| 376 | static void *job_thread(void *arg) | ||
| 377 | /* job thread */ | ||
| 378 | { | ||
| 379 | // httpd_connection((FILE *)arg); | ||
| 380 | srv_debug(0, xs_fmt("job thread started")); | ||
| 381 | |||
| 382 | for (;;) { | ||
| 383 | xs *job = NULL; | ||
| 384 | |||
| 385 | job_wait(&job); | ||
| 386 | |||
| 387 | srv_debug(0, xs_fmt("job thread wake up")); | ||
| 388 | |||
| 389 | if (job == NULL) | ||
| 390 | break; | ||
| 391 | |||
| 392 | if (xs_type(job) == XSTYPE_DATA) { | ||
| 393 | /* it's a socket */ | ||
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | srv_debug(0, xs_fmt("job thread stopped")); | ||
| 398 | |||
| 399 | return NULL; | ||
| 400 | } | ||
| 401 | |||
| 402 | |||
| 374 | void httpd(void) | 403 | void httpd(void) |
| 375 | /* starts the server */ | 404 | /* starts the server */ |
| 376 | { | 405 | { |
| @@ -379,6 +408,7 @@ void httpd(void) | |||
| 379 | int rs; | 408 | int rs; |
| 380 | pthread_t threads[MAX_THREADS]; | 409 | pthread_t threads[MAX_THREADS]; |
| 381 | int n_threads = 0; | 410 | int n_threads = 0; |
| 411 | int n; | ||
| 382 | 412 | ||
| 383 | address = xs_dict_get(srv_config, "address"); | 413 | address = xs_dict_get(srv_config, "address"); |
| 384 | port = xs_number_get(xs_dict_get(srv_config, "port")); | 414 | port = xs_number_get(xs_dict_get(srv_config, "port")); |
| @@ -417,6 +447,10 @@ void httpd(void) | |||
| 417 | /* thread #0 is the background thread */ | 447 | /* thread #0 is the background thread */ |
| 418 | pthread_create(&threads[0], NULL, background_thread, NULL); | 448 | pthread_create(&threads[0], NULL, background_thread, NULL); |
| 419 | 449 | ||
| 450 | /* the rest of threads are for job processing */ | ||
| 451 | for (n = 1; n < n_threads; n++) | ||
| 452 | pthread_create(&threads[n], NULL, job_thread, NULL); | ||
| 453 | |||
| 420 | if (setjmp(on_break) == 0) { | 454 | if (setjmp(on_break) == 0) { |
| 421 | for (;;) { | 455 | for (;;) { |
| 422 | FILE *f = xs_socket_accept(rs); | 456 | FILE *f = xs_socket_accept(rs); |
| @@ -430,8 +464,13 @@ void httpd(void) | |||
| 430 | 464 | ||
| 431 | srv_running = 0; | 465 | srv_running = 0; |
| 432 | 466 | ||
| 433 | /* wait for the background thread to end */ | 467 | /* send as many empty jobs as working threads */ |
| 434 | pthread_join(threads[0], NULL); | 468 | for (n = 1; n < n_threads; n++) |
| 469 | job_post(NULL); | ||
| 470 | |||
| 471 | /* wait for all the threads to exit */ | ||
| 472 | for (n = 0; n < n_threads; n++) | ||
| 473 | pthread_join(threads[n], NULL); | ||
| 435 | 474 | ||
| 436 | job_fifo = xs_free(job_fifo); | 475 | job_fifo = xs_free(job_fifo); |
| 437 | 476 | ||