mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-22 13:25:04 +00:00
More thread work.
This commit is contained in:
parent
06fc40e1cd
commit
451d964c0c
1 changed files with 47 additions and 8 deletions
55
httpd.c
55
httpd.c
|
@ -335,14 +335,16 @@ xs_list *job_fifo = NULL;
|
||||||
void job_post(const xs_val *job)
|
void job_post(const xs_val *job)
|
||||||
/* posts a job for the threads to process it */
|
/* posts a job for the threads to process it */
|
||||||
{
|
{
|
||||||
/* lock the mutex */
|
if (job != NULL) {
|
||||||
pthread_mutex_lock(&job_mutex);
|
/* lock the mutex */
|
||||||
|
pthread_mutex_lock(&job_mutex);
|
||||||
|
|
||||||
/* add to the fifo */
|
/* add to the fifo */
|
||||||
job_fifo = xs_list_append(job_fifo, job);
|
job_fifo = xs_list_append(job_fifo, job);
|
||||||
|
|
||||||
/* unlock the mutex */
|
/* unlock the mutex */
|
||||||
pthread_mutex_unlock(&job_mutex);
|
pthread_mutex_unlock(&job_mutex);
|
||||||
|
}
|
||||||
|
|
||||||
/* ask for someone to attend it */
|
/* ask for someone to attend it */
|
||||||
sem_post(&job_sem);
|
sem_post(&job_sem);
|
||||||
|
@ -371,6 +373,33 @@ void job_wait(xs_val **job)
|
||||||
#define MAX_THREADS 256
|
#define MAX_THREADS 256
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static void *job_thread(void *arg)
|
||||||
|
/* job thread */
|
||||||
|
{
|
||||||
|
// httpd_connection((FILE *)arg);
|
||||||
|
srv_debug(0, xs_fmt("job thread started"));
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
xs *job = NULL;
|
||||||
|
|
||||||
|
job_wait(&job);
|
||||||
|
|
||||||
|
srv_debug(0, xs_fmt("job thread wake up"));
|
||||||
|
|
||||||
|
if (job == NULL)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (xs_type(job) == XSTYPE_DATA) {
|
||||||
|
/* it's a socket */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
srv_debug(0, xs_fmt("job thread stopped"));
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void httpd(void)
|
void httpd(void)
|
||||||
/* starts the server */
|
/* starts the server */
|
||||||
{
|
{
|
||||||
|
@ -379,6 +408,7 @@ void httpd(void)
|
||||||
int rs;
|
int rs;
|
||||||
pthread_t threads[MAX_THREADS];
|
pthread_t threads[MAX_THREADS];
|
||||||
int n_threads = 0;
|
int n_threads = 0;
|
||||||
|
int n;
|
||||||
|
|
||||||
address = xs_dict_get(srv_config, "address");
|
address = xs_dict_get(srv_config, "address");
|
||||||
port = xs_number_get(xs_dict_get(srv_config, "port"));
|
port = xs_number_get(xs_dict_get(srv_config, "port"));
|
||||||
|
@ -417,6 +447,10 @@ void httpd(void)
|
||||||
/* thread #0 is the background thread */
|
/* thread #0 is the background thread */
|
||||||
pthread_create(&threads[0], NULL, background_thread, NULL);
|
pthread_create(&threads[0], NULL, background_thread, NULL);
|
||||||
|
|
||||||
|
/* the rest of threads are for job processing */
|
||||||
|
for (n = 1; n < n_threads; n++)
|
||||||
|
pthread_create(&threads[n], NULL, job_thread, NULL);
|
||||||
|
|
||||||
if (setjmp(on_break) == 0) {
|
if (setjmp(on_break) == 0) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
FILE *f = xs_socket_accept(rs);
|
FILE *f = xs_socket_accept(rs);
|
||||||
|
@ -430,8 +464,13 @@ void httpd(void)
|
||||||
|
|
||||||
srv_running = 0;
|
srv_running = 0;
|
||||||
|
|
||||||
/* wait for the background thread to end */
|
/* send as many empty jobs as working threads */
|
||||||
pthread_join(threads[0], NULL);
|
for (n = 1; n < n_threads; n++)
|
||||||
|
job_post(NULL);
|
||||||
|
|
||||||
|
/* wait for all the threads to exit */
|
||||||
|
for (n = 0; n < n_threads; n++)
|
||||||
|
pthread_join(threads[n], NULL);
|
||||||
|
|
||||||
job_fifo = xs_free(job_fifo);
|
job_fifo = xs_free(job_fifo);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue