diff options
Diffstat (limited to 'httpd.c')
| -rw-r--r-- | httpd.c | 55 |
1 files changed, 13 insertions, 42 deletions
| @@ -328,27 +328,18 @@ static pthread_mutex_t job_mutex; | |||
| 328 | /* semaphre to trigger job processing */ | 328 | /* semaphre to trigger job processing */ |
| 329 | static sem_t job_sem; | 329 | static sem_t job_sem; |
| 330 | 330 | ||
| 331 | /* list of input sockets */ | 331 | /* fifo of jobs */ |
| 332 | xs_list *job_sockets = NULL; | 332 | xs_list *job_fifo = NULL; |
| 333 | 333 | ||
| 334 | /* list of queue items */ | ||
| 335 | xs_list *job_qitems = NULL; | ||
| 336 | 334 | ||
| 337 | 335 | void job_post(const xs_val *job) | |
| 338 | void job_post(FILE *socket, const xs_dict *q_item) | 336 | /* posts a job for the threads to process it */ |
| 339 | /* posts a job, being an input connection or another queue item */ | ||
| 340 | { | 337 | { |
| 341 | /* lock the mutex */ | 338 | /* lock the mutex */ |
| 342 | pthread_mutex_lock(&job_mutex); | 339 | pthread_mutex_lock(&job_mutex); |
| 343 | 340 | ||
| 344 | /* add to the appropriate fifo */ | 341 | /* add to the fifo */ |
| 345 | if (socket != NULL) { | 342 | job_fifo = xs_list_append(job_fifo, job); |
| 346 | xs *d = xs_data_new(&socket, sizeof(FILE *)); | ||
| 347 | job_sockets = xs_list_append(job_sockets, d); | ||
| 348 | } | ||
| 349 | else | ||
| 350 | if (q_item != NULL) | ||
| 351 | job_qitems = xs_list_append(job_qitems, q_item); | ||
| 352 | 343 | ||
| 353 | /* unlock the mutex */ | 344 | /* unlock the mutex */ |
| 354 | pthread_mutex_unlock(&job_mutex); | 345 | pthread_mutex_unlock(&job_mutex); |
| @@ -358,39 +349,21 @@ void job_post(FILE *socket, const xs_dict *q_item) | |||
| 358 | } | 349 | } |
| 359 | 350 | ||
| 360 | 351 | ||
| 361 | int job_wait(FILE **socket, xs_dict **q_item) | 352 | void job_wait(xs_val **job) |
| 362 | /* waits for an available job; returns 0 if nothing left to do */ | 353 | /* waits for an available job */ |
| 363 | { | 354 | { |
| 364 | int done = 1; | 355 | *job = NULL; |
| 365 | |||
| 366 | *socket = NULL; | ||
| 367 | *q_item = NULL; | ||
| 368 | 356 | ||
| 369 | if (sem_wait(&job_sem) == 0) { | 357 | if (sem_wait(&job_sem) == 0) { |
| 370 | /* lock the mutex */ | 358 | /* lock the mutex */ |
| 371 | pthread_mutex_lock(&job_mutex); | 359 | pthread_mutex_lock(&job_mutex); |
| 372 | 360 | ||
| 373 | /* try first to get a socket to process */ | 361 | /* dequeue */ |
| 374 | xs *job_socket = NULL; | 362 | job_fifo = xs_list_shift(job_fifo, job); |
| 375 | job_sockets = xs_list_shift(job_sockets, &job_socket); | ||
| 376 | |||
| 377 | /* if empty, try a q_item */ | ||
| 378 | if (job_socket == NULL) | ||
| 379 | job_qitems = xs_list_shift(job_qitems, q_item); | ||
| 380 | 363 | ||
| 381 | /* unlock the mutex */ | 364 | /* unlock the mutex */ |
| 382 | pthread_mutex_unlock(&job_mutex); | 365 | pthread_mutex_unlock(&job_mutex); |
| 383 | |||
| 384 | if (job_socket != NULL) { | ||
| 385 | xs_data_get(job_socket, socket); | ||
| 386 | done = 0; | ||
| 387 | } | ||
| 388 | else | ||
| 389 | if (*q_item != NULL) | ||
| 390 | done = 0; | ||
| 391 | } | 366 | } |
| 392 | |||
| 393 | return done; | ||
| 394 | } | 367 | } |
| 395 | 368 | ||
| 396 | 369 | ||
| @@ -426,8 +399,7 @@ void httpd(void) | |||
| 426 | /* initialize the job control engine */ | 399 | /* initialize the job control engine */ |
| 427 | pthread_mutex_init(&job_mutex, NULL); | 400 | pthread_mutex_init(&job_mutex, NULL); |
| 428 | sem_init(&job_sem, 0, 0); | 401 | sem_init(&job_sem, 0, 0); |
| 429 | job_sockets = xs_list_new(); | 402 | job_fifo = xs_list_new(); |
| 430 | job_qitems = xs_list_new(); | ||
| 431 | 403 | ||
| 432 | #ifdef _SC_NPROCESSORS_ONLN | 404 | #ifdef _SC_NPROCESSORS_ONLN |
| 433 | /* get number of CPUs on the machine */ | 405 | /* get number of CPUs on the machine */ |
| @@ -461,8 +433,7 @@ void httpd(void) | |||
| 461 | /* wait for the background thread to end */ | 433 | /* wait for the background thread to end */ |
| 462 | pthread_join(threads[0], NULL); | 434 | pthread_join(threads[0], NULL); |
| 463 | 435 | ||
| 464 | job_sockets = xs_free(job_sockets); | 436 | job_fifo = xs_free(job_fifo); |
| 465 | job_qitems = xs_free(job_qitems); | ||
| 466 | 437 | ||
| 467 | srv_log(xs_fmt("httpd stop %s:%d", address, port)); | 438 | srv_log(xs_fmt("httpd stop %s:%d", address, port)); |
| 468 | } | 439 | } |