commit 8b465a5

default  ·  2023-02-06 18:23:35 +0000 UTC
parent 451d964
Incoming connections are processed by the pool of threads.
1 files changed,  +13, -15
+13, -15
 1@@ -312,14 +312,6 @@ static void *background_thread(void *arg)
 2 }
 3 
 4 
 5-static void *connection_thread(void *arg)
 6-/* connection thread */
 7-{
 8-    httpd_connection((FILE *)arg);
 9-    return NULL;
10-}
11-
12-
13 /** job control **/
14 
15 /* mutex to access the lists of jobs */
16@@ -376,25 +368,32 @@ void job_wait(xs_val **job)
17 static void *job_thread(void *arg)
18 /* job thread */
19 {
20-//    httpd_connection((FILE *)arg);
21-    srv_debug(0, xs_fmt("job thread started"));
22+    int pid = pthread_self();
23+
24+    srv_debug(0, xs_fmt("job thread %x started", pid));
25 
26     for (;;) {
27         xs *job = NULL;
28 
29         job_wait(&job);
30 
31-        srv_debug(0, xs_fmt("job thread wake up"));
32+        srv_debug(0, xs_fmt("job thread %x wake up", pid));
33 
34         if (job == NULL)
35             break;
36 
37         if (xs_type(job) == XSTYPE_DATA) {
38             /* it's a socket */
39+            FILE *f = NULL;
40+
41+            xs_data_get(job, &f);
42+
43+            if (f != NULL)
44+                httpd_connection(f);
45         }
46     }
47 
48-    srv_debug(0, xs_fmt("job thread stopped"));
49+    srv_debug(0, xs_fmt("job thread %x stopped", pid));
50 
51     return NULL;
52 }
53@@ -455,10 +454,9 @@ void httpd(void)
54         for (;;) {
55             FILE *f = xs_socket_accept(rs);
56 
57-            pthread_t cth;
58+            xs *job = xs_data_new(&f, sizeof(FILE *));
59 
60-            pthread_create(&cth, NULL, connection_thread, f);
61-            pthread_detach(cth);
62+            job_post(job);
63         }
64     }
65