2 * work_thread.c - threads implementation for blocking worker child.
5 #include "ntp_workimpl.h"
16 #include "ntp_stdlib.h"
17 #include "ntp_malloc.h"
18 #include "ntp_syslog.h"
21 #include "ntp_assert.h"
22 #include "ntp_unixtime.h"
23 #include "timespecops.h"
24 #include "ntp_worker.h"
26 #define CHILD_EXIT_REQ ((blocking_pipe_header *)(intptr_t)-1)
27 #define CHILD_GONE_RESP CHILD_EXIT_REQ
28 /* Queue size increments:
29 * The request queue grows a bit faster than the response queue -- the
30 * deamon can push requests and pull results faster on avarage than the
31 * worker can process requests and push results... If this really pays
34 #define WORKITEMS_ALLOC_INC 16
35 #define RESPONSES_ALLOC_INC 4
37 /* Fiddle with min/max stack sizes. 64kB minimum seems to work, so we
38 * set the maximum to 256kB. If the minimum goes below the
39 * system-defined minimum stack size, we have to adjust accordingly.
41 #ifndef THREAD_MINSTACKSIZE
42 # define THREAD_MINSTACKSIZE (64U * 1024)
45 #if defined(PTHREAD_STACK_MIN) && THREAD_MINSTACKSIZE < PTHREAD_STACK_MIN
46 # undef THREAD_MINSTACKSIZE
47 # define THREAD_MINSTACKSIZE PTHREAD_STACK_MIN
51 #ifndef THREAD_MAXSTACKSIZE
52 # define THREAD_MAXSTACKSIZE (256U * 1024)
54 #if THREAD_MAXSTACKSIZE < THREAD_MINSTACKSIZE
55 # undef THREAD_MAXSTACKSIZE
56 # define THREAD_MAXSTACKSIZE THREAD_MINSTACKSIZE
62 # define thread_exit(c) _endthreadex(c)
63 # define tickle_sem(sh) ReleaseSemaphore((sh->shnd), 1, NULL)
64 u_int WINAPI blocking_thread(void *);
65 static BOOL same_os_sema(const sem_ref obj, void * osobj);
69 # define thread_exit(c) pthread_exit((void*)(size_t)(c))
70 # define tickle_sem sem_post
71 void * blocking_thread(void *);
72 static void block_thread_signals(sigset_t *);
77 addremove_io_fd_func addremove_io_fd;
79 addremove_io_semaphore_func addremove_io_semaphore;
82 static void start_blocking_thread(blocking_child *);
83 static void start_blocking_thread_internal(blocking_child *);
84 static void prepare_child_sems(blocking_child *);
85 static int wait_for_sem(sem_ref, struct timespec *);
86 static int ensure_workitems_empty_slot(blocking_child *);
87 static int ensure_workresp_empty_slot(blocking_child *);
88 static int queue_req_pointer(blocking_child *, blocking_pipe_header *);
89 static void cleanup_after_child(blocking_child *);
91 static sema_type worker_mmutex;
92 static sem_ref worker_memlock;
94 /* --------------------------------------------------------------------
95 * locking the global worker state table (and other global stuff)
101 if (worker_memlock) {
103 wait_for_sem(worker_memlock, NULL);
105 tickle_sem(worker_memlock);
109 /* --------------------------------------------------------------------
110 * implementation isolation wrapper
117 thread_exit(exitcode); /* see #define thread_exit */
120 /* --------------------------------------------------------------------
121 * sleep for a given time or until the wakup semaphore is tickled.
129 struct timespec until;
132 # ifdef HAVE_CLOCK_GETTIME
133 if (0 != clock_gettime(CLOCK_REALTIME, &until)) {
134 msyslog(LOG_ERR, "worker_sleep: clock_gettime() failed: %m");
138 if (0 != getclock(TIMEOFDAY, &until)) {
139 msyslog(LOG_ERR, "worker_sleep: getclock() failed: %m");
143 until.tv_sec += seconds;
144 rc = wait_for_sem(c->wake_scheduled_sleep, &until);
147 if (-1 == rc && ETIMEDOUT == errno)
149 msyslog(LOG_ERR, "worker_sleep: sem_timedwait: %m");
154 /* --------------------------------------------------------------------
155 * Wake up a worker that takes a nap.
158 interrupt_worker_sleep(void)
163 for (idx = 0; idx < blocking_children_alloc; idx++) {
164 c = blocking_children[idx];
165 if (NULL == c || NULL == c->wake_scheduled_sleep)
167 tickle_sem(c->wake_scheduled_sleep);
171 /* --------------------------------------------------------------------
172 * Make sure there is an empty slot at the head of the request
173 * queue. Tell if the queue is currently empty.
176 ensure_workitems_empty_slot(
181 ** !!! PRECONDITION: caller holds access lock!
183 ** This simply tries to increase the size of the buffer if it
184 ** becomes full. The resize operation does *not* maintain the
185 ** order of requests, but that should be irrelevant since the
186 ** processing is considered asynchronous anyway.
188 ** Return if the buffer is currently empty.
191 static const size_t each =
192 sizeof(blocking_children[0]->workitems[0]);
198 slots_used = c->head_workitem - c->tail_workitem;
199 if (slots_used >= c->workitems_alloc) {
200 new_alloc = c->workitems_alloc + WORKITEMS_ALLOC_INC;
201 c->workitems = erealloc(c->workitems, new_alloc * each);
202 for (sidx = c->workitems_alloc; sidx < new_alloc; ++sidx)
203 c->workitems[sidx] = NULL;
204 c->tail_workitem = 0;
205 c->head_workitem = c->workitems_alloc;
206 c->workitems_alloc = new_alloc;
208 INSIST(NULL == c->workitems[c->head_workitem % c->workitems_alloc]);
209 return (0 == slots_used);
212 /* --------------------------------------------------------------------
213 * Make sure there is an empty slot at the head of the response
214 * queue. Tell if the queue is currently empty.
217 ensure_workresp_empty_slot(
222 ** !!! PRECONDITION: caller holds access lock!
224 ** Works like the companion function above.
227 static const size_t each =
228 sizeof(blocking_children[0]->responses[0]);
234 slots_used = c->head_response - c->tail_response;
235 if (slots_used >= c->responses_alloc) {
236 new_alloc = c->responses_alloc + RESPONSES_ALLOC_INC;
237 c->responses = erealloc(c->responses, new_alloc * each);
238 for (sidx = c->responses_alloc; sidx < new_alloc; ++sidx)
239 c->responses[sidx] = NULL;
240 c->tail_response = 0;
241 c->head_response = c->responses_alloc;
242 c->responses_alloc = new_alloc;
244 INSIST(NULL == c->responses[c->head_response % c->responses_alloc]);
245 return (0 == slots_used);
249 /* --------------------------------------------------------------------
250 * queue_req_pointer() - append a work item or idle exit request to
251 * blocking_workitems[]. Employ proper locking.
256 blocking_pipe_header * hdr
261 /* >>>> ACCESS LOCKING STARTS >>>> */
262 wait_for_sem(c->accesslock, NULL);
263 ensure_workitems_empty_slot(c);
264 qhead = c->head_workitem;
265 c->workitems[qhead % c->workitems_alloc] = hdr;
266 c->head_workitem = 1 + qhead;
267 tickle_sem(c->accesslock);
268 /* <<<< ACCESS LOCKING ENDS <<<< */
270 /* queue consumer wake-up notification */
271 tickle_sem(c->workitems_pending);
276 /* --------------------------------------------------------------------
277 * API function to make sure a worker is running, a proper private copy
278 * of the data is made, the data eneterd into the queue and the worker
282 send_blocking_req_internal(
284 blocking_pipe_header * hdr,
288 blocking_pipe_header * threadcopy;
289 size_t payload_octets;
291 REQUIRE(hdr != NULL);
292 REQUIRE(data != NULL);
293 DEBUG_REQUIRE(BLOCKING_REQ_MAGIC == hdr->magic_sig);
295 if (hdr->octets <= sizeof(*hdr))
296 return 1; /* failure */
297 payload_octets = hdr->octets - sizeof(*hdr);
299 if (NULL == c->thread_ref)
300 start_blocking_thread(c);
301 threadcopy = emalloc(hdr->octets);
302 memcpy(threadcopy, hdr, sizeof(*hdr));
303 memcpy((char *)threadcopy + sizeof(*hdr), data, payload_octets);
305 return queue_req_pointer(c, threadcopy);
308 /* --------------------------------------------------------------------
309 * Wait for the 'incoming queue no longer empty' signal, lock the shared
310 * structure and dequeue an item.
312 blocking_pipe_header *
313 receive_blocking_req_internal(
317 blocking_pipe_header * req;
322 /* wait for tickle from the producer side */
323 wait_for_sem(c->workitems_pending, NULL);
325 /* >>>> ACCESS LOCKING STARTS >>>> */
326 wait_for_sem(c->accesslock, NULL);
327 qhead = c->head_workitem;
329 qtail = c->tail_workitem;
332 c->tail_workitem = qtail + 1;
333 qtail %= c->workitems_alloc;
334 req = c->workitems[qtail];
335 c->workitems[qtail] = NULL;
336 } while (NULL == req);
337 tickle_sem(c->accesslock);
338 /* <<<< ACCESS LOCKING ENDS <<<< */
340 } while (NULL == req);
343 if (CHILD_EXIT_REQ == req) { /* idled out */
344 send_blocking_resp_internal(c, CHILD_GONE_RESP);
351 /* --------------------------------------------------------------------
352 * Push a response into the return queue and eventually tickle the
356 send_blocking_resp_internal(
358 blocking_pipe_header * resp
364 /* >>>> ACCESS LOCKING STARTS >>>> */
365 wait_for_sem(c->accesslock, NULL);
366 empty = ensure_workresp_empty_slot(c);
367 qhead = c->head_response;
368 c->responses[qhead % c->responses_alloc] = resp;
369 c->head_response = 1 + qhead;
370 tickle_sem(c->accesslock);
371 /* <<<< ACCESS LOCKING ENDS <<<< */
373 /* queue consumer wake-up notification */
377 write(c->resp_write_pipe, "", 1);
379 tickle_sem(c->responses_pending);
388 /* --------------------------------------------------------------------
389 * Check if a (Windows-)hanndle to a semaphore is actually the same we
390 * are using inside the sema wrapper.
398 return obj && osh && (obj->shnd == (HANDLE)osh);
401 /* --------------------------------------------------------------------
402 * Find the shared context that associates to an OS handle and make sure
403 * the data is dequeued and processed.
406 handle_blocking_resp_sem(
414 for (idx = 0; idx < blocking_children_alloc; idx++) {
415 c = blocking_children[idx];
417 c->thread_ref != NULL &&
418 same_os_sema(c->responses_pending, context))
421 if (idx < blocking_children_alloc)
422 process_blocking_resp(c);
424 #endif /* !WORK_PIPE */
426 /* --------------------------------------------------------------------
427 * Fetch the next response from the return queue. In case of signalling
428 * via pipe, make sure the pipe is flushed, too.
430 blocking_pipe_header *
431 receive_blocking_resp_internal(
435 blocking_pipe_header * removed;
436 size_t qhead, qtail, slot;
443 rc = read(c->resp_read_pipe, scratch, sizeof(scratch));
444 while (-1 == rc && EINTR == errno);
447 /* >>>> ACCESS LOCKING STARTS >>>> */
448 wait_for_sem(c->accesslock, NULL);
449 qhead = c->head_response;
450 qtail = c->tail_response;
451 for (removed = NULL; !removed && (qhead != qtail); ++qtail) {
452 slot = qtail % c->responses_alloc;
453 removed = c->responses[slot];
454 c->responses[slot] = NULL;
456 c->tail_response = qtail;
457 tickle_sem(c->accesslock);
458 /* <<<< ACCESS LOCKING ENDS <<<< */
460 if (NULL != removed) {
461 DEBUG_ENSURE(CHILD_GONE_RESP == removed ||
462 BLOCKING_RESP_MAGIC == removed->magic_sig);
464 if (CHILD_GONE_RESP == removed) {
465 cleanup_after_child(c);
472 /* --------------------------------------------------------------------
473 * Light up a new worker.
476 start_blocking_thread(
481 DEBUG_INSIST(!c->reusable);
483 prepare_child_sems(c);
484 start_blocking_thread_internal(c);
487 /* --------------------------------------------------------------------
488 * Create a worker thread. There are several differences between POSIX
489 * and Windows, of course -- most notably the Windows thread is no
490 * detached thread, and we keep the handle around until we want to get
491 * rid of the thread. The notification scheme also differs: Windows
492 * makes use of semaphores in both directions, POSIX uses a pipe for
493 * integration with 'select()' or alike.
496 start_blocking_thread_internal(
503 c->thread_ref = NULL;
504 (*addremove_io_semaphore)(c->responses_pending->shnd, FALSE);
505 c->thr_table[0].thnd =
506 (HANDLE)_beginthreadex(
514 if (NULL == c->thr_table[0].thnd) {
515 msyslog(LOG_ERR, "start blocking thread failed: %m");
518 /* remember the thread priority is only within the process class */
519 if (!SetThreadPriority(c->thr_table[0].thnd,
520 THREAD_PRIORITY_BELOW_NORMAL))
521 msyslog(LOG_ERR, "Error lowering blocking thread priority: %m");
523 resumed = ResumeThread(c->thr_table[0].thnd);
524 DEBUG_INSIST(resumed);
525 c->thread_ref = &c->thr_table[0];
527 #else /* pthreads start_blocking_thread_internal() follows */
529 # ifdef NEED_PTHREAD_INIT
530 static int pthread_init_called;
532 pthread_attr_t thr_attr;
534 int pipe_ends[2]; /* read then write */
539 sigset_t saved_sig_mask;
541 c->thread_ref = NULL;
543 # ifdef NEED_PTHREAD_INIT
545 * from lib/isc/unix/app.c:
546 * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
548 if (!pthread_init_called) {
550 pthread_init_called = TRUE;
554 rc = pipe_socketpair(&pipe_ends[0], &is_pipe);
556 msyslog(LOG_ERR, "start_blocking_thread: pipe_socketpair() %m");
559 c->resp_read_pipe = move_fd(pipe_ends[0]);
560 c->resp_write_pipe = move_fd(pipe_ends[1]);
562 flags = fcntl(c->resp_read_pipe, F_GETFL, 0);
564 msyslog(LOG_ERR, "start_blocking_thread: fcntl(F_GETFL) %m");
567 rc = fcntl(c->resp_read_pipe, F_SETFL, O_NONBLOCK | flags);
570 "start_blocking_thread: fcntl(F_SETFL, O_NONBLOCK) %m");
573 (*addremove_io_fd)(c->resp_read_pipe, c->ispipe, FALSE);
574 pthread_attr_init(&thr_attr);
575 pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
576 #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
577 defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
578 rc = pthread_attr_getstacksize(&thr_attr, &ostacksize);
581 "start_blocking_thread: pthread_attr_getstacksize() -> %s",
584 if (ostacksize < THREAD_MINSTACKSIZE)
585 nstacksize = THREAD_MINSTACKSIZE;
586 else if (ostacksize > THREAD_MAXSTACKSIZE)
587 nstacksize = THREAD_MAXSTACKSIZE;
589 nstacksize = ostacksize;
590 if (nstacksize != ostacksize)
591 rc = pthread_attr_setstacksize(&thr_attr, nstacksize);
594 "start_blocking_thread: pthread_attr_setstacksize(0x%lx -> 0x%lx) -> %s",
595 (u_long)ostacksize, (u_long)nstacksize,
599 UNUSED_ARG(nstacksize);
600 UNUSED_ARG(ostacksize);
602 #if defined(PTHREAD_SCOPE_SYSTEM) && defined(NEED_PTHREAD_SCOPE_SYSTEM)
603 pthread_attr_setscope(&thr_attr, PTHREAD_SCOPE_SYSTEM);
605 c->thread_ref = emalloc_zero(sizeof(*c->thread_ref));
606 block_thread_signals(&saved_sig_mask);
607 rc = pthread_create(&c->thr_table[0], &thr_attr,
608 &blocking_thread, c);
609 pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);
610 pthread_attr_destroy(&thr_attr);
612 msyslog(LOG_ERR, "start_blocking_thread: pthread_create() -> %s",
616 c->thread_ref = &c->thr_table[0];
620 /* --------------------------------------------------------------------
621 * block_thread_signals()
623 * Temporarily block signals used by ntpd main thread, so that signal
624 * mask inherited by child threads leaves them blocked. Returns prior
625 * active signal mask via pmask, to be restored by the main thread
626 * after pthread_create().
630 block_thread_signals(
637 # ifdef HAVE_SIGNALED_IO
639 sigaddset(&block, SIGIO);
642 sigaddset(&block, SIGPOLL);
644 # endif /* HAVE_SIGNALED_IO */
645 sigaddset(&block, SIGALRM);
646 sigaddset(&block, MOREDEBUGSIG);
647 sigaddset(&block, LESSDEBUGSIG);
649 sigaddset(&block, SIGDIE1);
652 sigaddset(&block, SIGDIE2);
655 sigaddset(&block, SIGDIE3);
658 sigaddset(&block, SIGDIE4);
661 sigaddset(&block, SIGBUS);
664 pthread_sigmask(SIG_BLOCK, &block, pmask);
666 #endif /* !SYS_WINNT */
669 /* --------------------------------------------------------------------
670 * Create & destroy semaphores. This is sufficiently different between
671 * POSIX and Windows to warrant wrapper functions and close enough to
672 * use the concept of synchronization via semaphore for all platforms.
683 if (NULL != semptr) {
684 svini = (inival < LONG_MAX)
685 ? (long)inival : LONG_MAX;
686 svmax = (maxval < LONG_MAX && maxval > 0)
687 ? (long)maxval : LONG_MAX;
688 semptr->shnd = CreateSemaphore(NULL, svini, svmax, NULL);
689 if (NULL == semptr->shnd)
696 if (semptr && sem_init(semptr, FALSE, inival))
704 /* ------------------------------------------------------------------ */
714 CloseHandle(obj->shnd);
728 /* --------------------------------------------------------------------
729 * prepare_child_sems()
731 * create sync & access semaphores
733 * All semaphores are cleared, only the access semaphore has 1 unit.
734 * Childs wait on 'workitems_pending', then grabs 'sema_access'
735 * and dequeues jobs. When done, 'sema_access' is given one unit back.
737 * The producer grabs 'sema_access', manages the queue, restores
738 * 'sema_access' and puts one unit into 'workitems_pending'.
740 * The story goes the same for the response queue.
747 if (NULL == worker_memlock)
748 worker_memlock = create_sema(&worker_mmutex, 1, 1);
750 c->accesslock = create_sema(&c->sem_table[0], 1, 1);
751 c->workitems_pending = create_sema(&c->sem_table[1], 0, 0);
752 c->wake_scheduled_sleep = create_sema(&c->sem_table[2], 0, 1);
754 c->responses_pending = create_sema(&c->sem_table[3], 0, 0);
758 /* --------------------------------------------------------------------
759 * wait for semaphore. Where the wait can be interrupted, it will
760 * internally resume -- When this function returns, there is either no
761 * semaphore at all, a timeout occurred, or the caller could
762 * successfully take a token from the semaphore.
764 * For untimed wait, not checking the result of this function at all is
765 * definitely an option.
770 struct timespec * timeout /* wall-clock */
775 struct timespec delta;
779 if (!(sem && sem->shnd)) {
784 if (NULL == timeout) {
787 getclock(TIMEOFDAY, &now);
788 delta = sub_tspec(*timeout, now);
789 if (delta.tv_sec < 0) {
791 } else if ((delta.tv_sec + 1) >= (MAXDWORD / 1000)) {
794 msec = 1000 * (DWORD)delta.tv_sec;
795 msec += delta.tv_nsec / (1000 * 1000);
798 rc = WaitForSingleObject(sem->shnd, msec);
799 if (WAIT_OBJECT_0 == rc)
801 if (WAIT_TIMEOUT == rc) {
805 msyslog(LOG_ERR, "WaitForSingleObject unexpected 0x%x", rc);
809 #else /* pthreads wait_for_sem() follows */
817 rc = sem_timedwait(sem, timeout);
818 } while (rc == -1 && errno == EINTR);
826 /* --------------------------------------------------------------------
827 * blocking_thread - thread functions have WINAPI (aka 'stdcall')
828 * calling conventions under Windows and POSIX-defined signature
843 exit_worker(blocking_child_common(c));
849 /* --------------------------------------------------------------------
850 * req_child_exit() runs in the parent.
852 * This function is called from from the idle timer, too, and possibly
853 * without a thread being there any longer. Since we have folded up our
854 * tent in that case and all the semaphores are already gone, we simply
855 * ignore this request in this case.
857 * Since the existence of the semaphores is controlled exclusively by
858 * the parent, there's no risk of data race here.
865 return (c->accesslock)
866 ? queue_req_pointer(c, CHILD_EXIT_REQ)
870 /* --------------------------------------------------------------------
871 * cleanup_after_child() runs in parent.
878 DEBUG_INSIST(!c->reusable);
881 /* The thread was not created in detached state, so we better
884 if (c->thread_ref && c->thread_ref->thnd) {
885 WaitForSingleObject(c->thread_ref->thnd, INFINITE);
886 INSIST(CloseHandle(c->thread_ref->thnd));
887 c->thread_ref->thnd = NULL;
890 c->thread_ref = NULL;
892 /* remove semaphores and (if signalling vi IO) pipes */
894 c->accesslock = delete_sema(c->accesslock);
895 c->workitems_pending = delete_sema(c->workitems_pending);
896 c->wake_scheduled_sleep = delete_sema(c->wake_scheduled_sleep);
899 DEBUG_INSIST(-1 != c->resp_read_pipe);
900 DEBUG_INSIST(-1 != c->resp_write_pipe);
901 (*addremove_io_fd)(c->resp_read_pipe, c->ispipe, TRUE);
902 close(c->resp_write_pipe);
903 close(c->resp_read_pipe);
904 c->resp_write_pipe = -1;
905 c->resp_read_pipe = -1;
907 DEBUG_INSIST(NULL != c->responses_pending);
908 (*addremove_io_semaphore)(c->responses_pending->shnd, TRUE);
909 c->responses_pending = delete_sema(c->responses_pending);
912 /* Is it necessary to check if there are pending requests and
913 * responses? If so, and if there are, what to do with them?
916 /* re-init buffer index sequencers */
917 c->head_workitem = 0;
918 c->tail_workitem = 0;
919 c->head_response = 0;
920 c->tail_response = 0;
926 #else /* !WORK_THREAD follows */
927 char work_thread_nonempty_compilation_unit;