diff options
| author | 2024-01-08 08:10:57 +0100 | |
|---|---|---|
| committer | 2024-01-08 08:10:57 +0100 | |
| commit | 93e7138e53628a76bed9583c5eb45ccf17b97e21 (patch) | |
| tree | fe2b503c3843ec0c836b01821b0210e654a4de34 /httpd.c | |
| parent | Merge pull request 'Added compatibility with NetBSD' (#99) from draga79/snac2... (diff) | |
| download | snac2-93e7138e53628a76bed9583c5eb45ccf17b97e21.tar.gz snac2-93e7138e53628a76bed9583c5eb45ccf17b97e21.tar.xz snac2-93e7138e53628a76bed9583c5eb45ccf17b97e21.zip | |
Rewritten part of the job threads to be leaner and faster.
Diffstat (limited to 'httpd.c')
| -rw-r--r-- | httpd.c | 84 |
1 files changed, 45 insertions, 39 deletions
| @@ -31,16 +31,22 @@ | |||
| 31 | srv_stat s_stat = {0}; | 31 | srv_stat s_stat = {0}; |
| 32 | srv_stat *p_stat = NULL; | 32 | srv_stat *p_stat = NULL; |
| 33 | 33 | ||
| 34 | |||
| 34 | /** job control **/ | 35 | /** job control **/ |
| 35 | 36 | ||
| 36 | /* mutex to access the lists of jobs */ | 37 | /* mutex to access the lists of jobs */ |
| 37 | static pthread_mutex_t job_mutex; | 38 | static pthread_mutex_t job_mutex; |
| 38 | 39 | ||
| 39 | /* semaphre to trigger job processing */ | 40 | /* semaphore to trigger job processing */ |
| 40 | static sem_t *job_sem; | 41 | static sem_t *job_sem; |
| 41 | 42 | ||
| 42 | /* fifo of jobs */ | 43 | typedef struct job_fifo_item { |
| 43 | xs_list *job_fifo = NULL; | 44 | struct job_fifo_item *next; |
| 45 | xs_val *job; | ||
| 46 | } job_fifo_item; | ||
| 47 | |||
| 48 | static job_fifo_item *job_fifo_first = NULL; | ||
| 49 | static job_fifo_item *job_fifo_last = NULL; | ||
| 44 | 50 | ||
| 45 | 51 | ||
| 46 | /* nodeinfo 2.0 template */ | 52 | /* nodeinfo 2.0 template */ |
| @@ -418,24 +424,6 @@ void httpd_connection(FILE *f) | |||
| 418 | } | 424 | } |
| 419 | 425 | ||
| 420 | 426 | ||
| 421 | static jmp_buf on_break; | ||
| 422 | |||
| 423 | |||
| 424 | void term_handler(int s) | ||
| 425 | { | ||
| 426 | (void)s; | ||
| 427 | |||
| 428 | longjmp(on_break, 1); | ||
| 429 | } | ||
| 430 | |||
| 431 | |||
| 432 | int job_fifo_ready(void) | ||
| 433 | /* returns true if the job fifo is ready */ | ||
| 434 | { | ||
| 435 | return job_fifo != NULL; | ||
| 436 | } | ||
| 437 | |||
| 438 | |||
| 439 | void job_post(const xs_val *job, int urgent) | 427 | void job_post(const xs_val *job, int urgent) |
| 440 | /* posts a job for the threads to process it */ | 428 | /* posts a job for the threads to process it */ |
| 441 | { | 429 | { |
| @@ -443,19 +431,25 @@ void job_post(const xs_val *job, int urgent) | |||
| 443 | /* lock the mutex */ | 431 | /* lock the mutex */ |
| 444 | pthread_mutex_lock(&job_mutex); | 432 | pthread_mutex_lock(&job_mutex); |
| 445 | 433 | ||
| 446 | /* add to the fifo */ | 434 | job_fifo_item *i = xs_realloc(NULL, sizeof(job_fifo_item)); |
| 447 | if (job_fifo != NULL) { | 435 | *i = (job_fifo_item){ NULL, xs_dup(job) }; |
| 448 | if (urgent) | ||
| 449 | job_fifo = xs_list_insert(job_fifo, 0, job); | ||
| 450 | else | ||
| 451 | job_fifo = xs_list_append(job_fifo, job); | ||
| 452 | |||
| 453 | p_stat->job_fifo_size++; | ||
| 454 | 436 | ||
| 455 | srv_debug(2, xs_fmt( | 437 | if (job_fifo_first == NULL) |
| 456 | "job_fifo sizes: %d %08x", p_stat->job_fifo_size, xs_size(job_fifo))); | 438 | job_fifo_first = job_fifo_last = i; |
| 439 | else | ||
| 440 | if (urgent) { | ||
| 441 | /* prepend */ | ||
| 442 | i->next = job_fifo_first; | ||
| 443 | job_fifo_first = i; | ||
| 444 | } | ||
| 445 | else { | ||
| 446 | /* append */ | ||
| 447 | job_fifo_last->next = i; | ||
| 448 | job_fifo_last = i; | ||
| 457 | } | 449 | } |
| 458 | 450 | ||
| 451 | p_stat->job_fifo_size++; | ||
| 452 | |||
| 459 | /* unlock the mutex */ | 453 | /* unlock the mutex */ |
| 460 | pthread_mutex_unlock(&job_mutex); | 454 | pthread_mutex_unlock(&job_mutex); |
| 461 | 455 | ||
| @@ -475,8 +469,16 @@ void job_wait(xs_val **job) | |||
| 475 | pthread_mutex_lock(&job_mutex); | 469 | pthread_mutex_lock(&job_mutex); |
| 476 | 470 | ||
| 477 | /* dequeue */ | 471 | /* dequeue */ |
| 478 | if (job_fifo != NULL) { | 472 | job_fifo_item *i = job_fifo_first; |
| 479 | job_fifo = xs_list_shift(job_fifo, job); | 473 | |
| 474 | if (i != NULL) { | ||
| 475 | job_fifo_first = i->next; | ||
| 476 | |||
| 477 | if (job_fifo_first == NULL) | ||
| 478 | job_fifo_last = NULL; | ||
| 479 | |||
| 480 | *job = i->job; | ||
| 481 | xs_free(i); | ||
| 480 | 482 | ||
| 481 | p_stat->job_fifo_size--; | 483 | p_stat->job_fifo_size--; |
| 482 | } | 484 | } |
| @@ -604,6 +606,16 @@ static void *background_thread(void *arg) | |||
| 604 | } | 606 | } |
| 605 | 607 | ||
| 606 | 608 | ||
| 609 | static jmp_buf on_break; | ||
| 610 | |||
| 611 | void term_handler(int s) | ||
| 612 | { | ||
| 613 | (void)s; | ||
| 614 | |||
| 615 | longjmp(on_break, 1); | ||
| 616 | } | ||
| 617 | |||
| 618 | |||
| 607 | void httpd(void) | 619 | void httpd(void) |
| 608 | /* starts the server */ | 620 | /* starts the server */ |
| 609 | { | 621 | { |
| @@ -663,8 +675,6 @@ void httpd(void) | |||
| 663 | return; | 675 | return; |
| 664 | } | 676 | } |
| 665 | 677 | ||
| 666 | job_fifo = xs_list_new(); | ||
| 667 | |||
| 668 | /* initialize sleep control */ | 678 | /* initialize sleep control */ |
| 669 | pthread_mutex_init(&sleep_mutex, NULL); | 679 | pthread_mutex_init(&sleep_mutex, NULL); |
| 670 | pthread_cond_init(&sleep_cond, NULL); | 680 | pthread_cond_init(&sleep_cond, NULL); |
| @@ -717,10 +727,6 @@ void httpd(void) | |||
| 717 | for (n = 0; n < p_stat->n_threads; n++) | 727 | for (n = 0; n < p_stat->n_threads; n++) |
| 718 | pthread_join(threads[n], NULL); | 728 | pthread_join(threads[n], NULL); |
| 719 | 729 | ||
| 720 | pthread_mutex_lock(&job_mutex); | ||
| 721 | job_fifo = xs_free(job_fifo); | ||
| 722 | pthread_mutex_unlock(&job_mutex); | ||
| 723 | |||
| 724 | sem_close(job_sem); | 730 | sem_close(job_sem); |
| 725 | sem_unlink(sem_name); | 731 | sem_unlink(sem_name); |
| 726 | 732 | ||