Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ngx_freebsd_rfork_thread.c
Go to the documentation of this file.
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) Nginx, Inc.
5  */
6 
7 
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 
11 /*
12  * The threads implementation uses the rfork(RFPROC|RFTHREAD|RFMEM) syscall
13  * to create threads. All threads use the stacks of the same size mmap()ed
14  * below the main stack. Thus the current thread id is determined via
15  * the stack pointer value.
16  *
17  * The mutex implementation uses the ngx_atomic_cmp_set() operation
18  * to acquire a mutex and the SysV semaphore to wait on a mutex and to wake up
19  * the waiting threads. The light mutex does not use semaphore, so after
20  * spinning in the lock the thread calls sched_yield(). However the light
21  * mutexes are intended to be used with the "trylock" operation only.
22  * The SysV semop() is a cheap syscall, particularly if it has little sembuf's
23  * and does not use SEM_UNDO.
24  *
25  * The condition variable implementation uses the signal #64.
26  * The signal handler is SIG_IGN so the kill() is a cheap syscall.
27  * The thread waits a signal in kevent(). The use of the EVFILT_SIGNAL
28  * is safe since FreeBSD 4.10-STABLE.
29  *
30  * This threads implementation currently works on i386 (486+) and amd64
31  * platforms only.
32  */
33 
34 
37 
38 
39 static size_t rz_size;
40 static size_t usable_stack_size;
41 static char *last_stack;
42 
43 static ngx_uint_t nthreads;
44 static ngx_uint_t max_threads;
45 
46 static ngx_uint_t nkeys;
47 static ngx_tid_t *tids; /* the threads tids array */
48 void **ngx_tls; /* the threads tls's array */
49 
50 /* the thread-safe libc errno */
51 
52 static int errno0; /* the main thread's errno */
53 static int *errnos; /* the threads errno's array */
54 
55 int *
57 {
58  int tid;
59 
60  tid = ngx_gettid();
61 
62  return tid ? &errnos[tid - 1] : &errno0;
63 }
64 
65 
66 /*
67  * __isthreaded enables the spinlocks in some libc functions, i.e. in malloc()
68  * and some other places. Nevertheless we protect our malloc()/free() calls
69  * by own mutex that is more efficient than the spinlock.
70  *
71  * _spinlock() is a weak referenced stub in src/lib/libc/gen/_spinlock_stub.c
72  * that does nothing.
73  */
74 
75 extern int __isthreaded;
76 
77 void
79 {
80  ngx_int_t tries;
81 
82  tries = 0;
83 
84  for ( ;; ) {
85 
86  if (*lock) {
87  if (ngx_ncpu > 1 && tries++ < 1000) {
88  continue;
89  }
90 
91  sched_yield();
92  tries = 0;
93 
94  } else {
95  if (ngx_atomic_cmp_set(lock, 0, 1)) {
96  return;
97  }
98  }
99  }
100 }
101 
102 
103 /*
104  * Before FreeBSD 5.1 _spinunlock() is a simple #define in
105  * src/lib/libc/include/spinlock.h that zeroes lock.
106  *
107  * Since FreeBSD 5.1 _spinunlock() is a weak referenced stub in
108  * src/lib/libc/gen/_spinlock_stub.c that does nothing.
109  */
110 
111 #ifndef _spinunlock
112 
113 void
115 {
116  *lock = 0;
117 }
118 
119 #endif
120 
121 
122 ngx_err_t
123 ngx_create_thread(ngx_tid_t *tid, ngx_thread_value_t (*func)(void *arg),
124  void *arg, ngx_log_t *log)
125 {
126  ngx_pid_t id;
127  ngx_err_t err;
128  char *stack, *stack_top;
129 
130  if (nthreads >= max_threads) {
131  ngx_log_error(NGX_LOG_CRIT, log, 0,
132  "no more than %ui threads can be created", max_threads);
133  return NGX_ERROR;
134  }
135 
136  last_stack -= ngx_thread_stack_size;
137 
138  stack = mmap(last_stack, usable_stack_size, PROT_READ|PROT_WRITE,
139  MAP_STACK, -1, 0);
140 
141  if (stack == MAP_FAILED) {
143  "mmap(%p:%uz, MAP_STACK) thread stack failed",
144  last_stack, usable_stack_size);
145  return NGX_ERROR;
146  }
147 
148  if (stack != last_stack) {
150  "stack %p address was changed to %p", last_stack, stack);
151  return NGX_ERROR;
152  }
153 
154  stack_top = stack + usable_stack_size;
155 
157  "thread stack: %p-%p", stack, stack_top);
158 
159  ngx_set_errno(0);
160 
161  id = rfork_thread(RFPROC|RFTHREAD|RFMEM, stack_top,
162  (ngx_rfork_thread_func_pt) func, arg);
163 
164  err = ngx_errno;
165 
166  if (id == -1) {
167  ngx_log_error(NGX_LOG_ALERT, log, err, "rfork() failed");
168 
169  } else {
170  *tid = id;
171  nthreads = (ngx_freebsd_kern_usrstack - stack_top)
173  tids[nthreads] = id;
174 
175  ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "rfork()ed thread: %P", id);
176  }
177 
178  return err;
179 }
180 
181 
182 ngx_int_t
183 ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle)
184 {
185  char *red_zone, *zone;
186  size_t len;
187  ngx_int_t i;
188  struct sigaction sa;
189 
190  max_threads = n + 1;
191 
192  for (i = 0; i < n; i++) {
193  ngx_memzero(&sa, sizeof(struct sigaction));
194  sa.sa_handler = SIG_IGN;
195  sigemptyset(&sa.sa_mask);
196  if (sigaction(NGX_CV_SIGNAL, &sa, NULL) == -1) {
198  "sigaction(%d, SIG_IGN) failed", NGX_CV_SIGNAL);
199  return NGX_ERROR;
200  }
201  }
202 
203  len = sizeof(ngx_freebsd_kern_usrstack);
204  if (sysctlbyname("kern.usrstack", &ngx_freebsd_kern_usrstack, &len,
205  NULL, 0) == -1)
206  {
208  "sysctlbyname(kern.usrstack) failed");
209  return NGX_ERROR;
210  }
211 
212  /* the main thread stack red zone */
213  rz_size = ngx_pagesize;
214  red_zone = ngx_freebsd_kern_usrstack - (size + rz_size);
215 
217  "usrstack: %p red zone: %p",
218  ngx_freebsd_kern_usrstack, red_zone);
219 
220  zone = mmap(red_zone, rz_size, PROT_NONE, MAP_ANON, -1, 0);
221  if (zone == MAP_FAILED) {
223  "mmap(%p:%uz, PROT_NONE, MAP_ANON) red zone failed",
224  red_zone, rz_size);
225  return NGX_ERROR;
226  }
227 
228  if (zone != red_zone) {
229  ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
230  "red zone %p address was changed to %p", red_zone, zone);
231  return NGX_ERROR;
232  }
233 
234  /* create the thread errno' array */
235 
236  errnos = ngx_calloc(n * sizeof(int), cycle->log);
237  if (errnos == NULL) {
238  return NGX_ERROR;
239  }
240 
241  /* create the thread tids array */
242 
243  tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log);
244  if (tids == NULL) {
245  return NGX_ERROR;
246  }
247 
248  tids[0] = ngx_pid;
249 
250  /* create the thread tls' array */
251 
252  ngx_tls = ngx_calloc(NGX_THREAD_KEYS_MAX * (n + 1) * sizeof(void *),
253  cycle->log);
254  if (ngx_tls == NULL) {
255  return NGX_ERROR;
256  }
257 
258  nthreads = 1;
259 
260  last_stack = zone + rz_size;
261  usable_stack_size = size;
262  ngx_thread_stack_size = size + rz_size;
263 
264  /* allow the spinlock in libc malloc() */
265  __isthreaded = 1;
266 
267  ngx_threaded = 1;
268 
269  return NGX_OK;
270 }
271 
272 
273 ngx_tid_t
275 {
276  ngx_int_t tid;
277 
278  tid = ngx_gettid();
279 
280  if (tids == NULL) {
281  return ngx_pid;
282  }
283 
284  return tids[tid];
285 }
286 
287 
288 ngx_err_t
290 {
291  if (nkeys >= NGX_THREAD_KEYS_MAX) {
292  return NGX_ENOMEM;
293  }
294 
295  *key = nkeys++;
296 
297  return 0;
298 }
299 
300 
301 ngx_err_t
303 {
304  if (key >= NGX_THREAD_KEYS_MAX) {
305  return NGX_EINVAL;
306  }
307 
308  ngx_tls[key * NGX_THREAD_KEYS_MAX + ngx_gettid()] = value;
309  return 0;
310 }
311 
312 
313 ngx_mutex_t *
315 {
316  ngx_mutex_t *m;
317  union semun op;
318 
319  m = ngx_alloc(sizeof(ngx_mutex_t), log);
320  if (m == NULL) {
321  return NULL;
322  }
323 
324  m->lock = 0;
325  m->log = log;
326 
327  if (flags & NGX_MUTEX_LIGHT) {
328  m->semid = -1;
329  return m;
330  }
331 
332  m->semid = semget(IPC_PRIVATE, 1, SEM_R|SEM_A);
333  if (m->semid == -1) {
334  ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "semget() failed");
335  return NULL;
336  }
337 
338  op.val = 0;
339 
340  if (semctl(m->semid, 0, SETVAL, op) == -1) {
341  ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "semctl(SETVAL) failed");
342 
343  if (semctl(m->semid, 0, IPC_RMID) == -1) {
345  "semctl(IPC_RMID) failed");
346  }
347 
348  return NULL;
349  }
350 
351  return m;
352 }
353 
354 
355 void
357 {
358  if (semctl(m->semid, 0, IPC_RMID) == -1) {
360  "semctl(IPC_RMID) failed");
361  }
362 
363  ngx_free((void *) m);
364 }
365 
366 
367 ngx_int_t
369 {
370  uint32_t lock, old;
371  ngx_uint_t tries;
372  struct sembuf op;
373 
374  if (!ngx_threaded) {
375  return NGX_OK;
376  }
377 
378 #if (NGX_DEBUG)
379  if (try) {
381  "try lock mutex %p lock:%XD", m, m->lock);
382  } else {
384  "lock mutex %p lock:%XD", m, m->lock);
385  }
386 #endif
387 
388  old = m->lock;
389  tries = 0;
390 
391  for ( ;; ) {
392  if (old & NGX_MUTEX_LOCK_BUSY) {
393 
394  if (try) {
395  return NGX_AGAIN;
396  }
397 
398  if (ngx_ncpu > 1 && tries++ < 1000) {
399 
400  /* the spinlock is used only on the SMP system */
401 
402  old = m->lock;
403  continue;
404  }
405 
406  if (m->semid == -1) {
407  sched_yield();
408 
409  tries = 0;
410  old = m->lock;
411  continue;
412  }
413 
415  "mutex %p lock:%XD", m, m->lock);
416 
417  /*
418  * The mutex is locked so we increase a number
419  * of the threads that are waiting on the mutex
420  */
421 
422  lock = old + 1;
423 
424  if ((lock & ~NGX_MUTEX_LOCK_BUSY) > nthreads) {
426  "%D threads wait for mutex %p, "
427  "while only %ui threads are available",
428  lock & ~NGX_MUTEX_LOCK_BUSY, m, nthreads);
429  ngx_abort();
430  }
431 
432  if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
433 
435  "wait mutex %p lock:%XD", m, m->lock);
436 
437  /*
438  * The number of the waiting threads has been increased
439  * and we would wait on the SysV semaphore.
440  * A semaphore should wake up us more efficiently than
441  * a simple sched_yield() or usleep().
442  */
443 
444  op.sem_num = 0;
445  op.sem_op = -1;
446  op.sem_flg = 0;
447 
448  if (semop(m->semid, &op, 1) == -1) {
450  "semop() failed while waiting on mutex %p", m);
451  ngx_abort();
452  }
453 
455  "mutex waked up %p lock:%XD", m, m->lock);
456 
457  tries = 0;
458  old = m->lock;
459  continue;
460  }
461 
462  old = m->lock;
463 
464  } else {
465  lock = old | NGX_MUTEX_LOCK_BUSY;
466 
467  if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
468 
469  /* we locked the mutex */
470 
471  break;
472  }
473 
474  old = m->lock;
475  }
476 
477  if (tries++ > 1000) {
478 
480  "mutex %p is contested", m);
481 
482  /* the mutex is probably contested so we are giving up now */
483 
484  sched_yield();
485 
486  tries = 0;
487  old = m->lock;
488  }
489  }
490 
492  "mutex %p is locked, lock:%XD", m, m->lock);
493 
494  return NGX_OK;
495 }
496 
497 
498 void
500 {
501  uint32_t lock, old;
502  struct sembuf op;
503 
504  if (!ngx_threaded) {
505  return;
506  }
507 
508  old = m->lock;
509 
510  if (!(old & NGX_MUTEX_LOCK_BUSY)) {
512  "trying to unlock the free mutex %p", m);
513  ngx_abort();
514  }
515 
516  /* free the mutex */
517 
518 #if 0
520  "unlock mutex %p lock:%XD", m, old);
521 #endif
522 
523  for ( ;; ) {
524  lock = old & ~NGX_MUTEX_LOCK_BUSY;
525 
526  if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
527  break;
528  }
529 
530  old = m->lock;
531  }
532 
533  if (m->semid == -1) {
535  "mutex %p is unlocked", m);
536 
537  return;
538  }
539 
540  /* check whether we need to wake up a waiting thread */
541 
542  old = m->lock;
543 
544  for ( ;; ) {
545  if (old & NGX_MUTEX_LOCK_BUSY) {
546 
547  /* the mutex is just locked by another thread */
548 
549  break;
550  }
551 
552  if (old == 0) {
553  break;
554  }
555 
556  /* there are the waiting threads */
557 
558  lock = old - 1;
559 
560  if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
561 
562  /* wake up the thread that waits on semaphore */
563 
565  "wake up mutex %p", m);
566 
567  op.sem_num = 0;
568  op.sem_op = 1;
569  op.sem_flg = 0;
570 
571  if (semop(m->semid, &op, 1) == -1) {
573  "semop() failed while waking up on mutex %p", m);
574  ngx_abort();
575  }
576 
577  break;
578  }
579 
580  old = m->lock;
581  }
582 
584  "mutex %p is unlocked", m);
585 
586  return;
587 }
588 
589 
590 ngx_cond_t *
592 {
593  ngx_cond_t *cv;
594 
595  cv = ngx_alloc(sizeof(ngx_cond_t), log);
596  if (cv == NULL) {
597  return NULL;
598  }
599 
600  cv->signo = NGX_CV_SIGNAL;
601  cv->tid = -1;
602  cv->log = log;
603  cv->kq = -1;
604 
605  return cv;
606 }
607 
608 
609 void
611 {
612  if (close(cv->kq) == -1) {
614  "kqueue close() failed");
615  }
616 
617  ngx_free(cv);
618 }
619 
620 
621 ngx_int_t
623 {
624  int n;
625  ngx_err_t err;
626  struct kevent kev;
627  struct timespec ts;
628 
629  if (cv->kq == -1) {
630 
631  /*
632  * We have to add the EVFILT_SIGNAL filter in the rfork()ed thread.
633  * Otherwise the thread would not get a signal event.
634  *
635  * However, we have not to open the kqueue in the thread,
636  * it is simply handy do it together.
637  */
638 
639  cv->kq = kqueue();
640  if (cv->kq == -1) {
641  ngx_log_error(NGX_LOG_ALERT, cv->log, ngx_errno, "kqueue() failed");
642  return NGX_ERROR;
643  }
644 
646  "cv kq:%d signo:%d", cv->kq, cv->signo);
647 
648  kev.ident = cv->signo;
649  kev.filter = EVFILT_SIGNAL;
650  kev.flags = EV_ADD;
651  kev.fflags = 0;
652  kev.data = 0;
653  kev.udata = NULL;
654 
655  ts.tv_sec = 0;
656  ts.tv_nsec = 0;
657 
658  if (kevent(cv->kq, &kev, 1, NULL, 0, &ts) == -1) {
659  ngx_log_error(NGX_LOG_ALERT, cv->log, ngx_errno, "kevent() failed");
660  return NGX_ERROR;
661  }
662 
663  cv->tid = ngx_thread_self();
664  }
665 
666  ngx_mutex_unlock(m);
667 
669  "cv %p wait, kq:%d, signo:%d", cv, cv->kq, cv->signo);
670 
671  for ( ;; ) {
672  n = kevent(cv->kq, NULL, 0, &kev, 1, NULL);
673 
675  "cv %p kevent: %d", cv, n);
676 
677  if (n == -1) {
678  err = ngx_errno;
680  cv->log, ngx_errno,
681  "kevent() failed while waiting condition variable %p",
682  cv);
683 
684  if (err == NGX_EINTR) {
685  break;
686  }
687 
688  return NGX_ERROR;
689  }
690 
691  if (n == 0) {
693  "kevent() returned no events "
694  "while waiting condition variable %p",
695  cv);
696  continue;
697  }
698 
699  if (kev.filter != EVFILT_SIGNAL) {
701  "kevent() returned unexpected events: %d "
702  "while waiting condition variable %p",
703  kev.filter, cv);
704  continue;
705  }
706 
707  if (kev.ident != (uintptr_t) cv->signo) {
709  "kevent() returned unexpected signal: %d ",
710  "while waiting condition variable %p",
711  kev.ident, cv);
712  continue;
713  }
714 
715  break;
716  }
717 
718  ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, "cv %p is waked up", cv);
719 
720  ngx_mutex_lock(m);
721 
722  return NGX_OK;
723 }
724 
725 
726 ngx_int_t
728 {
729  ngx_err_t err;
730 
732  "cv %p to signal %P %d",
733  cv, cv->tid, cv->signo);
734 
735  if (cv->tid == -1) {
736  return NGX_OK;
737  }
738 
739  if (kill(cv->tid, cv->signo) == -1) {
740 
741  err = ngx_errno;
742 
743  ngx_log_error(NGX_LOG_ALERT, cv->log, err,
744  "kill() failed while signaling condition variable %p", cv);
745 
746  if (err == NGX_ESRCH) {
747  cv->tid = -1;
748  }
749 
750  return NGX_ERROR;
751  }
752 
753  ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, "cv %p is signaled", cv);
754 
755  return NGX_OK;
756 }