commit 66d974a
default
·
2023-02-06 09:53:29 +0000 UTC
parent 307e0aa
Started work towards the pool of threads.
1 files changed,
+27,
-7
M
httpd.c
M
httpd.c
+27,
-7
1@@ -252,8 +252,6 @@ static void *purge_thread(void *arg)
2 static void *background_thread(void *arg)
3 /* background thread (queue management and other things) */
4 {
5- pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
6- pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER;
7 time_t purge_time;
8
9 /* first purge time */
10@@ -295,7 +293,10 @@ static void *background_thread(void *arg)
11 }
12
13 /* sleep 3 seconds */
14+ pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
15+ pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER;
16 struct timespec ts;
17+
18 clock_gettime(CLOCK_REALTIME, &ts);
19 ts.tv_sec += 3;
20
21@@ -318,13 +319,18 @@ static void *connection_thread(void *arg)
22 }
23
24
25+#ifndef MAX_THREADS
26+#define MAX_THREADS 256
27+#endif
28+
29 void httpd(void)
30 /* starts the server */
31 {
32 char *address;
33 int port;
34 int rs;
35- pthread_t htid;
36+ pthread_t threads[MAX_THREADS];
37+ int n_threads = 0;
38
39 address = xs_dict_get(srv_config, "address");
40 port = xs_number_get(xs_dict_get(srv_config, "port"));
41@@ -338,11 +344,25 @@ void httpd(void)
42
43 signal(SIGPIPE, SIG_IGN);
44 signal(SIGTERM, term_handler);
45- signal(SIGINT, term_handler);
46+ signal(SIGINT, term_handler);
47
48 srv_log(xs_fmt("httpd start %s:%d %s", address, port, USER_AGENT));
49
50- pthread_create(&htid, NULL, background_thread, NULL);
51+ /* thread creation */
52+#ifdef _SC_NPROCESSORS_ONLN
53+ n_threads = sysconf(_SC_NPROCESSORS_ONLN);
54+#endif
55+
56+ if (n_threads < 4)
57+ n_threads = 4;
58+
59+ if (n_threads > MAX_THREADS)
60+ n_threads = MAX_THREADS;
61+
62+ srv_debug(0, xs_fmt("using %d threads", n_threads));
63+
64+ /* thread #0 is the background thread */
65+ pthread_create(&threads[0], NULL, background_thread, NULL);
66
67 if (setjmp(on_break) == 0) {
68 for (;;) {
69@@ -357,8 +377,8 @@ void httpd(void)
70
71 srv_running = 0;
72
73- /* wait for the helper thread to end */
74- pthread_join(htid, NULL);
75+ /* wait for the background thread to end */
76+ pthread_join(threads[0], NULL);
77
78 srv_log(xs_fmt("httpd stop %s:%d", address, port));
79 }