commit 06fc40e

default  ·  2023-02-06 17:43:27 +0000 UTC
parent 6bcc6bf
Simplified job_post() and job_wait().
1 files changed,  +13, -42
+13, -42
 1@@ -328,27 +328,18 @@ static pthread_mutex_t job_mutex;
 2 /* semaphre to trigger job processing */
 3 static sem_t job_sem;
 4 
 5-/* list of input sockets */
 6-xs_list *job_sockets = NULL;
 7+/* fifo of jobs */
 8+xs_list *job_fifo = NULL;
 9 
10-/* list of queue items */
11-xs_list *job_qitems = NULL;
12 
13-
14-void job_post(FILE *socket, const xs_dict *q_item)
15-/* posts a job, being an input connection or another queue item */
16+void job_post(const xs_val *job)
17+/* posts a job for the threads to process it */
18 {
19     /* lock the mutex */
20     pthread_mutex_lock(&job_mutex);
21 
22-    /* add to the appropriate fifo */
23-    if (socket != NULL) {
24-        xs *d = xs_data_new(&socket, sizeof(FILE *));
25-        job_sockets = xs_list_append(job_sockets, d);
26-    }
27-    else
28-    if (q_item != NULL)
29-        job_qitems = xs_list_append(job_qitems, q_item);
30+    /* add to the fifo */
31+    job_fifo = xs_list_append(job_fifo, job);
32 
33     /* unlock the mutex */
34     pthread_mutex_unlock(&job_mutex);
35@@ -358,39 +349,21 @@ void job_post(FILE *socket, const xs_dict *q_item)
36 }
37 
38 
39-int job_wait(FILE **socket, xs_dict **q_item)
40-/* waits for an available job; returns 0 if nothing left to do */
41+void job_wait(xs_val **job)
42+/* waits for an available job */
43 {
44-    int done = 1;
45-
46-    *socket = NULL;
47-    *q_item = NULL;
48+    *job = NULL;
49 
50     if (sem_wait(&job_sem) == 0) {
51         /* lock the mutex */
52         pthread_mutex_lock(&job_mutex);
53 
54-        /* try first to get a socket to process */
55-        xs *job_socket = NULL;
56-        job_sockets = xs_list_shift(job_sockets, &job_socket);
57-
58-        /* if empty, try a q_item */
59-        if (job_socket == NULL)
60-            job_qitems = xs_list_shift(job_qitems, q_item);
61+        /* dequeue */
62+        job_fifo = xs_list_shift(job_fifo, job);
63 
64         /* unlock the mutex */
65         pthread_mutex_unlock(&job_mutex);
66-
67-        if (job_socket != NULL) {
68-            xs_data_get(job_socket, socket);
69-            done = 0;
70-        }
71-        else
72-        if (*q_item != NULL)
73-            done = 0;
74     }
75-
76-    return done;
77 }
78 
79 
80@@ -426,8 +399,7 @@ void httpd(void)
81     /* initialize the job control engine */
82     pthread_mutex_init(&job_mutex, NULL);
83     sem_init(&job_sem, 0, 0);
84-    job_sockets = xs_list_new();
85-    job_qitems = xs_list_new();
86+    job_fifo = xs_list_new();
87 
88 #ifdef _SC_NPROCESSORS_ONLN
89     /* get number of CPUs on the machine */
90@@ -461,8 +433,7 @@ void httpd(void)
91     /* wait for the background thread to end */
92     pthread_join(threads[0], NULL);
93 
94-    job_sockets = xs_free(job_sockets);
95-    job_qitems  = xs_free(job_qitems);
96+    job_fifo = xs_free(job_fifo);
97 
98     srv_log(xs_fmt("httpd stop %s:%d", address, port));
99 }