diff options
| author | 2023-02-06 20:07:29 +0100 | |
|---|---|---|
| committer | 2023-02-06 20:07:29 +0100 | |
| commit | b2d186cd0fc5451dcc336c3efc23450ce918656a (patch) | |
| tree | 10008c9889c7c1f6cffa12f40b7e78f662308b32 /httpd.c | |
| parent | Identify the job threads by number. (diff) | |
| download | snac2-b2d186cd0fc5451dcc336c3efc23450ce918656a.tar.gz snac2-b2d186cd0fc5451dcc336c3efc23450ce918656a.tar.xz snac2-b2d186cd0fc5451dcc336c3efc23450ce918656a.zip | |
The pool of threads now process q_items.
Also, the purge is commanded as a q_item.
Diffstat (limited to 'httpd.c')
| -rw-r--r-- | httpd.c | 140 |
1 files changed, 65 insertions, 75 deletions
| @@ -237,81 +237,6 @@ void term_handler(int s) | |||
| 237 | } | 237 | } |
| 238 | 238 | ||
| 239 | 239 | ||
| 240 | static void *purge_thread(void *arg) | ||
| 241 | /* spawned purge */ | ||
| 242 | { | ||
| 243 | srv_log(xs_dup("purge start")); | ||
| 244 | |||
| 245 | purge_all(); | ||
| 246 | |||
| 247 | srv_log(xs_dup("purge end")); | ||
| 248 | |||
| 249 | return NULL; | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | static void *background_thread(void *arg) | ||
| 254 | /* background thread (queue management and other things) */ | ||
| 255 | { | ||
| 256 | time_t purge_time; | ||
| 257 | |||
| 258 | /* first purge time */ | ||
| 259 | purge_time = time(NULL) + 10 * 60; | ||
| 260 | |||
| 261 | srv_log(xs_fmt("background thread started")); | ||
| 262 | |||
| 263 | while (srv_running) { | ||
| 264 | time_t t; | ||
| 265 | |||
| 266 | { | ||
| 267 | xs *list = user_list(); | ||
| 268 | char *p, *uid; | ||
| 269 | |||
| 270 | /* process queues for all users */ | ||
| 271 | p = list; | ||
| 272 | while (xs_list_iter(&p, &uid)) { | ||
| 273 | snac snac; | ||
| 274 | |||
| 275 | if (user_open(&snac, uid)) { | ||
| 276 | process_user_queue(&snac); | ||
| 277 | user_free(&snac); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | /* global queue */ | ||
| 283 | process_queue(); | ||
| 284 | |||
| 285 | /* time to purge? */ | ||
| 286 | if ((t = time(NULL)) > purge_time) { | ||
| 287 | pthread_t pth; | ||
| 288 | |||
| 289 | pthread_create(&pth, NULL, purge_thread, NULL); | ||
| 290 | pthread_detach(pth); | ||
| 291 | |||
| 292 | /* next purge time is tomorrow */ | ||
| 293 | purge_time = t + 24 * 60 * 60; | ||
| 294 | } | ||
| 295 | |||
| 296 | /* sleep 3 seconds */ | ||
| 297 | pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 298 | pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER; | ||
| 299 | struct timespec ts; | ||
| 300 | |||
| 301 | clock_gettime(CLOCK_REALTIME, &ts); | ||
| 302 | ts.tv_sec += 3; | ||
| 303 | |||
| 304 | pthread_mutex_lock(&dummy_mutex); | ||
| 305 | while (pthread_cond_timedwait(&dummy_cond, &dummy_mutex, &ts) == 0); | ||
| 306 | pthread_mutex_unlock(&dummy_mutex); | ||
| 307 | } | ||
| 308 | |||
| 309 | srv_log(xs_fmt("background thread stopped")); | ||
| 310 | |||
| 311 | return NULL; | ||
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | /** job control **/ | 240 | /** job control **/ |
| 316 | 241 | ||
| 317 | /* mutex to access the lists of jobs */ | 242 | /* mutex to access the lists of jobs */ |
| @@ -391,6 +316,10 @@ static void *job_thread(void *arg) | |||
| 391 | if (f != NULL) | 316 | if (f != NULL) |
| 392 | httpd_connection(f); | 317 | httpd_connection(f); |
| 393 | } | 318 | } |
| 319 | else { | ||
| 320 | /* it's a q_item */ | ||
| 321 | process_queue_item(job); | ||
| 322 | } | ||
| 394 | } | 323 | } |
| 395 | 324 | ||
| 396 | srv_debug(0, xs_fmt("job thread %ld stopped", pid)); | 325 | srv_debug(0, xs_fmt("job thread %ld stopped", pid)); |
| @@ -399,6 +328,67 @@ static void *job_thread(void *arg) | |||
| 399 | } | 328 | } |
| 400 | 329 | ||
| 401 | 330 | ||
| 331 | static void *background_thread(void *arg) | ||
| 332 | /* background thread (queue management and other things) */ | ||
| 333 | { | ||
| 334 | time_t purge_time; | ||
| 335 | |||
| 336 | /* first purge time */ | ||
| 337 | purge_time = time(NULL) + 10 * 60; | ||
| 338 | |||
| 339 | srv_log(xs_fmt("background thread started")); | ||
| 340 | |||
| 341 | while (srv_running) { | ||
| 342 | time_t t; | ||
| 343 | |||
| 344 | { | ||
| 345 | xs *list = user_list(); | ||
| 346 | char *p, *uid; | ||
| 347 | |||
| 348 | /* process queues for all users */ | ||
| 349 | p = list; | ||
| 350 | while (xs_list_iter(&p, &uid)) { | ||
| 351 | snac snac; | ||
| 352 | |||
| 353 | if (user_open(&snac, uid)) { | ||
| 354 | process_user_queue(&snac); | ||
| 355 | user_free(&snac); | ||
| 356 | } | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | /* global queue */ | ||
| 361 | process_queue(); | ||
| 362 | |||
| 363 | /* time to purge? */ | ||
| 364 | if ((t = time(NULL)) > purge_time) { | ||
| 365 | /* next purge time is tomorrow */ | ||
| 366 | purge_time = t + 24 * 60 * 60; | ||
| 367 | |||
| 368 | xs *q_item = xs_dict_new(); | ||
| 369 | q_item = xs_dict_append(q_item, "type", "purge"); | ||
| 370 | job_post(q_item); | ||
| 371 | } | ||
| 372 | |||
| 373 | /* sleep 3 seconds */ | ||
| 374 | pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 375 | pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER; | ||
| 376 | struct timespec ts; | ||
| 377 | |||
| 378 | clock_gettime(CLOCK_REALTIME, &ts); | ||
| 379 | ts.tv_sec += 3; | ||
| 380 | |||
| 381 | pthread_mutex_lock(&dummy_mutex); | ||
| 382 | while (pthread_cond_timedwait(&dummy_cond, &dummy_mutex, &ts) == 0); | ||
| 383 | pthread_mutex_unlock(&dummy_mutex); | ||
| 384 | } | ||
| 385 | |||
| 386 | srv_log(xs_fmt("background thread stopped")); | ||
| 387 | |||
| 388 | return NULL; | ||
| 389 | } | ||
| 390 | |||
| 391 | |||
| 402 | void httpd(void) | 392 | void httpd(void) |
| 403 | /* starts the server */ | 393 | /* starts the server */ |
| 404 | { | 394 | { |