3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
11 * The threads implementation uses the rfork(RFPROC|RFTHREAD|RFMEM) syscall
12 * to create threads. All threads use the stacks of the same size mmap()ed
13 * below the main stack. Thus the current thread id is determinated via
14 * the stack pointer value.
16 * The mutex implementation uses the ngx_atomic_cmp_set() operation
17 * to acquire a mutex and the SysV semaphore to wait on a mutex and to wake up
18 * the waiting threads. The light mutex does not use semaphore, so after
19 * spinning in the lock the thread calls sched_yield(). However the light
20 * mutecies are intended to be used with the "trylock" operation only.
21 * The SysV semop() is a cheap syscall, particularly if it has little sembuf's
22 * and does not use SEM_UNDO.
24 * The condition variable implementation uses the signal #64.
25 * The signal handler is SIG_IGN so the kill() is a cheap syscall.
26 * The thread waits a signal in kevent(). The use of the EVFILT_SIGNAL
27 * is safe since FreeBSD 4.10-STABLE.
29 * This threads implementation currently works on i386 (486+) and amd64
34 char *ngx_freebsd_kern_usrstack;
35 size_t ngx_thread_stack_size;
38 static size_t rz_size;
39 static size_t usable_stack_size;
40 static char *last_stack;
42 static ngx_uint_t nthreads;
43 static ngx_uint_t max_threads;
45 static ngx_uint_t nkeys;
46 static ngx_tid_t *tids; /* the threads tids array */
47 void **ngx_tls; /* the threads tls's array */
49 /* the thread-safe libc errno */
51 static int errno0; /* the main thread's errno */
52 static int *errnos; /* the threads errno's array */
61 return tid ? &errnos[tid - 1] : &errno0;
66 * __isthreaded enables the spinlocks in some libc functions, i.e. in malloc()
67 * and some other places. Nevertheless we protect our malloc()/free() calls
68 * by own mutex that is more efficient than the spinlock.
70 * _spinlock() is a weak referenced stub in src/lib/libc/gen/_spinlock_stub.c
74 extern int __isthreaded;
77 _spinlock(ngx_atomic_t *lock)
86 if (ngx_ncpu > 1 && tries++ < 1000) {
94 if (ngx_atomic_cmp_set(lock, 0, 1)) {
103 * Before FreeBSD 5.1 _spinunlock() is a simple #define in
104 * src/lib/libc/include/spinlock.h that zeroes lock.
106 * Since FreeBSD 5.1 _spinunlock() is a weak referenced stub in
107 * src/lib/libc/gen/_spinlock_stub.c that does nothing.
113 _spinunlock(ngx_atomic_t *lock)
122 ngx_create_thread(ngx_tid_t *tid, ngx_thread_value_t (*func)(void *arg),
123 void *arg, ngx_log_t *log)
127 char *stack, *stack_top;
129 if (nthreads >= max_threads) {
130 ngx_log_error(NGX_LOG_CRIT, log, 0,
131 "no more than %ui threads can be created", max_threads);
135 last_stack -= ngx_thread_stack_size;
137 stack = mmap(last_stack, usable_stack_size, PROT_READ|PROT_WRITE,
140 if (stack == MAP_FAILED) {
141 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
142 "mmap(%p:%uz, MAP_STACK) thread stack failed",
143 last_stack, usable_stack_size);
147 if (stack != last_stack) {
148 ngx_log_error(NGX_LOG_ALERT, log, 0,
149 "stack %p address was changed to %p", last_stack, stack);
153 stack_top = stack + usable_stack_size;
155 ngx_log_debug2(NGX_LOG_DEBUG_CORE, log, 0,
156 "thread stack: %p-%p", stack, stack_top);
160 id = rfork_thread(RFPROC|RFTHREAD|RFMEM, stack_top,
161 (ngx_rfork_thread_func_pt) func, arg);
166 ngx_log_error(NGX_LOG_ALERT, log, err, "rfork() failed");
170 nthreads = (ngx_freebsd_kern_usrstack - stack_top)
171 / ngx_thread_stack_size;
174 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "rfork()ed thread: %P", id);
182 ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle)
184 char *red_zone, *zone;
191 for (i = 0; i < n; i++) {
192 ngx_memzero(&sa, sizeof(struct sigaction));
193 sa.sa_handler = SIG_IGN;
194 sigemptyset(&sa.sa_mask);
195 if (sigaction(NGX_CV_SIGNAL, &sa, NULL) == -1) {
196 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
197 "sigaction(%d, SIG_IGN) failed", NGX_CV_SIGNAL);
202 len = sizeof(ngx_freebsd_kern_usrstack);
203 if (sysctlbyname("kern.usrstack", &ngx_freebsd_kern_usrstack, &len,
206 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
207 "sysctlbyname(kern.usrstack) failed");
211 /* the main thread stack red zone */
212 rz_size = ngx_pagesize;
213 red_zone = ngx_freebsd_kern_usrstack - (size + rz_size);
215 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
216 "usrstack: %p red zone: %p",
217 ngx_freebsd_kern_usrstack, red_zone);
219 zone = mmap(red_zone, rz_size, PROT_NONE, MAP_ANON, -1, 0);
220 if (zone == MAP_FAILED) {
221 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
222 "mmap(%p:%uz, PROT_NONE, MAP_ANON) red zone failed",
227 if (zone != red_zone) {
228 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
229 "red zone %p address was changed to %p", red_zone, zone);
233 /* create the thread errno' array */
235 errnos = ngx_calloc(n * sizeof(int), cycle->log);
236 if (errnos == NULL) {
240 /* create the thread tids array */
242 tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log);
249 /* create the thread tls' array */
251 ngx_tls = ngx_calloc(NGX_THREAD_KEYS_MAX * (n + 1) * sizeof(void *),
253 if (ngx_tls == NULL) {
259 last_stack = zone + rz_size;
260 usable_stack_size = size;
261 ngx_thread_stack_size = size + rz_size;
263 /* allow the spinlock in libc malloc() */
288 ngx_thread_key_create(ngx_tls_key_t *key)
290 if (nkeys >= NGX_THREAD_KEYS_MAX) {
301 ngx_thread_set_tls(ngx_tls_key_t key, void *value)
303 if (key >= NGX_THREAD_KEYS_MAX) {
307 ngx_tls[key * NGX_THREAD_KEYS_MAX + ngx_gettid()] = value;
313 ngx_mutex_init(ngx_log_t *log, ngx_uint_t flags)
318 m = ngx_alloc(sizeof(ngx_mutex_t), log);
326 if (flags & NGX_MUTEX_LIGHT) {
331 m->semid = semget(IPC_PRIVATE, 1, SEM_R|SEM_A);
332 if (m->semid == -1) {
333 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "semget() failed");
339 if (semctl(m->semid, 0, SETVAL, op) == -1) {
340 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "semctl(SETVAL) failed");
342 if (semctl(m->semid, 0, IPC_RMID) == -1) {
343 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
344 "semctl(IPC_RMID) failed");
355 ngx_mutex_destroy(ngx_mutex_t *m)
357 if (semctl(m->semid, 0, IPC_RMID) == -1) {
358 ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
359 "semctl(IPC_RMID) failed");
362 ngx_free((void *) m);
367 ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try)
379 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
380 "try lock mutex %p lock:%XD", m, m->lock);
382 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
383 "lock mutex %p lock:%XD", m, m->lock);
391 if (old & NGX_MUTEX_LOCK_BUSY) {
397 if (ngx_ncpu > 1 && tries++ < 1000) {
399 /* the spinlock is used only on the SMP system */
405 if (m->semid == -1) {
413 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
414 "mutex %p lock:%XD", m, m->lock);
417 * The mutex is locked so we increase a number
418 * of the threads that are waiting on the mutex
423 if ((lock & ~NGX_MUTEX_LOCK_BUSY) > nthreads) {
424 ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
425 "%D threads wait for mutex %p, "
426 "while only %ui threads are available",
427 lock & ~NGX_MUTEX_LOCK_BUSY, m, nthreads);
431 if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
433 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
434 "wait mutex %p lock:%XD", m, m->lock);
437 * The number of the waiting threads has been increased
438 * and we would wait on the SysV semaphore.
439 * A semaphore should wake up us more efficiently than
440 * a simple sched_yield() or usleep().
447 if (semop(m->semid, &op, 1) == -1) {
448 ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
449 "semop() failed while waiting on mutex %p", m);
453 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
454 "mutex waked up %p lock:%XD", m, m->lock);
464 lock = old | NGX_MUTEX_LOCK_BUSY;
466 if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
468 /* we locked the mutex */
476 if (tries++ > 1000) {
478 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
479 "mutex %p is contested", m);
481 /* the mutex is probably contested so we are giving up now */
490 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
491 "mutex %p is locked, lock:%XD", m, m->lock);
498 ngx_mutex_unlock(ngx_mutex_t *m)
509 if (!(old & NGX_MUTEX_LOCK_BUSY)) {
510 ngx_log_error(NGX_LOG_ALERT, m->log, 0,
511 "trying to unlock the free mutex %p", m);
518 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
519 "unlock mutex %p lock:%XD", m, old);
523 lock = old & ~NGX_MUTEX_LOCK_BUSY;
525 if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
532 if (m->semid == -1) {
533 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
534 "mutex %p is unlocked", m);
539 /* check whether we need to wake up a waiting thread */
544 if (old & NGX_MUTEX_LOCK_BUSY) {
546 /* the mutex is just locked by another thread */
555 /* there are the waiting threads */
559 if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
561 /* wake up the thread that waits on semaphore */
563 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
564 "wake up mutex %p", m);
570 if (semop(m->semid, &op, 1) == -1) {
571 ngx_log_error(NGX_LOG_ALERT, m->log, ngx_errno,
572 "semop() failed while waking up on mutex %p", m);
582 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
583 "mutex %p is unlocked", m);
590 ngx_cond_init(ngx_log_t *log)
594 cv = ngx_alloc(sizeof(ngx_cond_t), log);
599 cv->signo = NGX_CV_SIGNAL;
609 ngx_cond_destroy(ngx_cond_t *cv)
611 if (close(cv->kq) == -1) {
612 ngx_log_error(NGX_LOG_ALERT, cv->log, ngx_errno,
613 "kqueue close() failed");
621 ngx_cond_wait(ngx_cond_t *cv, ngx_mutex_t *m)
631 * We have to add the EVFILT_SIGNAL filter in the rfork()ed thread.
632 * Otherwise the thread would not get a signal event.
634 * However, we have not to open the kqueue in the thread,
635 * it is simply handy do it together.
640 ngx_log_error(NGX_LOG_ALERT, cv->log, ngx_errno, "kqueue() failed");
644 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cv->log, 0,
645 "cv kq:%d signo:%d", cv->kq, cv->signo);
647 kev.ident = cv->signo;
648 kev.filter = EVFILT_SIGNAL;
657 if (kevent(cv->kq, &kev, 1, NULL, 0, &ts) == -1) {
658 ngx_log_error(NGX_LOG_ALERT, cv->log, ngx_errno, "kevent() failed");
662 cv->tid = ngx_thread_self();
667 ngx_log_debug3(NGX_LOG_DEBUG_CORE, cv->log, 0,
668 "cv %p wait, kq:%d, signo:%d", cv, cv->kq, cv->signo);
671 n = kevent(cv->kq, NULL, 0, &kev, 1, NULL);
673 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cv->log, 0,
674 "cv %p kevent: %d", cv, n);
678 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
680 "kevent() failed while waiting condition variable %p",
683 if (err == NGX_EINTR) {
691 ngx_log_error(NGX_LOG_ALERT, cv->log, 0,
692 "kevent() returned no events "
693 "while waiting condition variable %p",
698 if (kev.filter != EVFILT_SIGNAL) {
699 ngx_log_error(NGX_LOG_ALERT, cv->log, 0,
700 "kevent() returned unexpected events: %d "
701 "while waiting condition variable %p",
706 if (kev.ident != (uintptr_t) cv->signo) {
707 ngx_log_error(NGX_LOG_ALERT, cv->log, 0,
708 "kevent() returned unexpected signal: %d ",
709 "while waiting condition variable %p",
717 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, "cv %p is waked up", cv);
726 ngx_cond_signal(ngx_cond_t *cv)
730 ngx_log_debug3(NGX_LOG_DEBUG_CORE, cv->log, 0,
731 "cv %p to signal %P %d",
732 cv, cv->tid, cv->signo);
738 if (kill(cv->tid, cv->signo) == -1) {
742 ngx_log_error(NGX_LOG_ALERT, cv->log, err,
743 "kill() failed while signaling condition variable %p", cv);
745 if (err == NGX_ESRCH) {
752 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, "cv %p is signaled", cv);