commit 0289860
default
·
2024-01-08 07:21:22 +0000 UTC
parent 93e7138
Non-important variable renaming.
M
httpd.c
+35,
-34
1@@ -28,8 +28,8 @@
2
3 /** server stat **/
4
5-srv_stat s_stat = {0};
6-srv_stat *p_stat = NULL;
7+srv_state s_state = {0};
8+srv_state *p_state = NULL;
9
10
11 /** job control **/
12@@ -49,6 +49,13 @@ static job_fifo_item *job_fifo_first = NULL;
13 static job_fifo_item *job_fifo_last = NULL;
14
15
16+/** other global data **/
17+
18+static jmp_buf on_break;
19+
20+
21+/** code **/
22+
23 /* nodeinfo 2.0 template */
24 const char *nodeinfo_2_0_template = ""
25 "{\"version\":\"2.0\","
26@@ -234,9 +241,9 @@ int server_get_handler(xs_dict *req, const char *q_path,
27 *ctype = "text/plain";
28 *body = xs_str_new("UP\n");
29
30- xs *uptime = xs_str_time_diff(time(NULL) - p_stat->srv_start_time);
31+ xs *uptime = xs_str_time_diff(time(NULL) - p_state->srv_start_time);
32 srv_log(xs_fmt("status: uptime: %s", uptime));
33- srv_log(xs_fmt("status: job_fifo len: %d", p_stat->job_fifo_size));
34+ srv_log(xs_fmt("status: job_fifo len: %d", p_state->job_fifo_size));
35 }
36
37 if (status != 0)
38@@ -263,7 +270,7 @@ void httpd_connection(FILE *f)
39 char *p;
40 int fcgi_id;
41
42- if (p_stat->use_fcgi)
43+ if (p_state->use_fcgi)
44 req = xs_fcgi_request(f, &payload, &p_size, &fcgi_id);
45 else
46 req = xs_httpd_request(f, &payload, &p_size);
47@@ -401,7 +408,7 @@ void httpd_connection(FILE *f)
48 headers = xs_dict_append(headers, "access-control-allow-origin", "*");
49 headers = xs_dict_append(headers, "access-control-allow-headers", "*");
50
51- if (p_stat->use_fcgi)
52+ if (p_state->use_fcgi)
53 xs_fcgi_response(f, status, headers, body, b_size, fcgi_id);
54 else
55 xs_httpd_response(f, status, headers, body, b_size);
56@@ -448,7 +455,7 @@ void job_post(const xs_val *job, int urgent)
57 job_fifo_last = i;
58 }
59
60- p_stat->job_fifo_size++;
61+ p_state->job_fifo_size++;
62
63 /* unlock the mutex */
64 pthread_mutex_unlock(&job_mutex);
65@@ -480,7 +487,7 @@ void job_wait(xs_val **job)
66 *job = i->job;
67 xs_free(i);
68
69- p_stat->job_fifo_size--;
70+ p_state->job_fifo_size--;
71 }
72
73 /* unlock the mutex */
74@@ -489,10 +496,6 @@ void job_wait(xs_val **job)
75 }
76
77
78-#ifndef MAX_THREADS
79-#define MAX_THREADS 256
80-#endif
81-
82 static void *job_thread(void *arg)
83 /* job thread */
84 {
85@@ -549,7 +552,7 @@ static void *background_thread(void *arg)
86
87 srv_log(xs_fmt("background thread started"));
88
89- while (p_stat->srv_running) {
90+ while (p_state->srv_running) {
91 time_t t;
92 int cnt = 0;
93
94@@ -606,8 +609,6 @@ static void *background_thread(void *arg)
95 }
96
97
98-static jmp_buf on_break;
99-
100 void term_handler(int s)
101 {
102 (void)s;
103@@ -629,12 +630,12 @@ void httpd(void)
104
105 /* setup the server stat structure */
106 {
107- p_stat = &s_stat;
108+ p_state = &s_state;
109 }
110
111- p_stat->srv_start_time = time(NULL);
112+ p_state->srv_start_time = time(NULL);
113
114- p_stat->use_fcgi = xs_type(xs_dict_get(srv_config, "fastcgi")) == XSTYPE_TRUE;
115+ p_state->use_fcgi = xs_type(xs_dict_get(srv_config, "fastcgi")) == XSTYPE_TRUE;
116
117 address = xs_dict_get(srv_config, "address");
118 port = xs_number_str(xs_dict_get(srv_config, "port"));
119@@ -644,13 +645,13 @@ void httpd(void)
120 return;
121 }
122
123- p_stat->srv_running = 1;
124+ p_state->srv_running = 1;
125
126 signal(SIGPIPE, SIG_IGN);
127 signal(SIGTERM, term_handler);
128 signal(SIGINT, term_handler);
129
130- srv_log(xs_fmt("httpd%s start %s:%s %s", p_stat->use_fcgi ? " (FastCGI)" : "",
131+ srv_log(xs_fmt("httpd%s start %s:%s %s", p_state->use_fcgi ? " (FastCGI)" : "",
132 address, port, USER_AGENT));
133
134 /* show the number of usable file descriptors */
135@@ -679,29 +680,29 @@ void httpd(void)
136 pthread_mutex_init(&sleep_mutex, NULL);
137 pthread_cond_init(&sleep_cond, NULL);
138
139- p_stat->n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
140+ p_state->n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
141
142 #ifdef _SC_NPROCESSORS_ONLN
143- if (p_stat->n_threads == 0) {
144+ if (p_state->n_threads == 0) {
145 /* get number of CPUs on the machine */
146- p_stat->n_threads = sysconf(_SC_NPROCESSORS_ONLN);
147+ p_state->n_threads = sysconf(_SC_NPROCESSORS_ONLN);
148 }
149 #endif
150
151- if (p_stat->n_threads < 4)
152- p_stat->n_threads = 4;
153+ if (p_state->n_threads < 4)
154+ p_state->n_threads = 4;
155
156- if (p_stat->n_threads > MAX_THREADS)
157- p_stat->n_threads = MAX_THREADS;
158+ if (p_state->n_threads > MAX_THREADS)
159+ p_state->n_threads = MAX_THREADS;
160
161- srv_debug(0, xs_fmt("using %d threads", p_stat->n_threads));
162+ srv_debug(0, xs_fmt("using %d threads", p_state->n_threads));
163
164 /* thread #0 is the background thread */
165 pthread_create(&threads[0], NULL, background_thread, NULL);
166
167 /* the rest of threads are for job processing */
168 char *ptr = (char *) 0x1;
169- for (n = 1; n < p_stat->n_threads; n++)
170+ for (n = 1; n < p_state->n_threads; n++)
171 pthread_create(&threads[n], NULL, job_thread, ptr++);
172
173 if (setjmp(on_break) == 0) {
174@@ -717,22 +718,22 @@ void httpd(void)
175 }
176 }
177
178- p_stat->srv_running = 0;
179+ p_state->srv_running = 0;
180
181 /* send as many exit jobs as working threads */
182- for (n = 1; n < p_stat->n_threads; n++)
183+ for (n = 1; n < p_state->n_threads; n++)
184 job_post(xs_stock_false, 0);
185
186 /* wait for all the threads to exit */
187- for (n = 0; n < p_stat->n_threads; n++)
188+ for (n = 0; n < p_state->n_threads; n++)
189 pthread_join(threads[n], NULL);
190
191 sem_close(job_sem);
192 sem_unlink(sem_name);
193
194- xs *uptime = xs_str_time_diff(time(NULL) - p_stat->srv_start_time);
195+ xs *uptime = xs_str_time_diff(time(NULL) - p_state->srv_start_time);
196
197 srv_log(xs_fmt("httpd%s stop %s:%s (run time: %s)",
198- p_stat->use_fcgi ? " (FastCGI)" : "",
199+ p_state->use_fcgi ? " (FastCGI)" : "",
200 address, port, uptime));
201 }
M
snac.h
+5,
-1
1@@ -12,6 +12,10 @@
2
3 #define ISO_DATE_SPEC "%Y-%m-%dT%H:%M:%SZ"
4
5+#ifndef MAX_THREADS
6+#define MAX_THREADS 256
7+#endif
8+
9 extern double disk_layout;
10 extern xs_str *srv_basedir;
11 extern xs_dict *srv_config;
12@@ -47,7 +51,7 @@ typedef struct {
13 time_t srv_start_time; /* start time */
14 int job_fifo_size; /* job fifo size */
15 int n_threads; /* number of configured threads */
16-} srv_stat;
17+} srv_state;
18
19 void snac_log(snac *user, xs_str *str);
20 #define snac_debug(user, level, str) do { if (dbglevel >= (level)) \