blob: 841636a044ed98b2231bf85bbe1d396760826a9c [file] [log] [blame]
Igor Sysoevb54698b2004-02-23 20:57:12 +00001#ifndef _NGX_THREAD_H_INCLUDED_
2#define _NGX_THREAD_H_INCLUDED_
3
4
5#include <ngx_config.h>
6#include <ngx_core.h>
7
8#if (NGX_THREADS)
9
Igor Sysoevea0b1d92004-03-02 15:40:59 +000010#define ngx_thread_volatile volatile
11
Igor Sysoevf2334412004-02-25 20:16:15 +000012#if (NGX_USE_RFORK)
Igor Sysoevb54698b2004-02-23 20:57:12 +000013
14#include <sys/ipc.h>
15#include <sys/sem.h>
16#include <sched.h>
17
18typedef pid_t ngx_tid_t;
19
Igor Sysoevb54698b2004-02-23 20:57:12 +000020#undef ngx_log_pid
21#define ngx_log_pid ngx_thread_self()
Igor Sysoevd94049b2004-02-29 21:03:02 +000022#define ngx_log_tid 0
23
24#define TID_T_FMT PID_T_FMT
Igor Sysoevb54698b2004-02-23 20:57:12 +000025
26
27#define NGX_MUTEX_LIGHT 1
28#define NGX_MUTEX_CV 2
29
30#define NGX_MUTEX_LOCK_BUSY 0x80000000
31
Igor Sysoev48fef662004-02-24 17:31:46 +000032typedef volatile struct {
Igor Sysoevb54698b2004-02-23 20:57:12 +000033 ngx_atomic_t lock;
34 ngx_log_t *log;
35 int semid;
36} ngx_mutex_t;
37
38
Igor Sysoev6a930452004-03-04 16:34:23 +000039#define ngx_thread_sigmask(how, set, oset) \
40 (sigprocmask(how, set, oset) == -1) ? ngx_errno : 0
41
42#define ngx_thread_sigmask_n "sigprocmask()"
43
44
Igor Sysoevd94049b2004-02-29 21:03:02 +000045extern char *ngx_freebsd_kern_usrstack;
46extern size_t ngx_thread_stack_size;
47
48static inline int ngx_gettid()
49{
50 char *sp;
51
52 if (ngx_thread_stack_size == 0) {
53 return 0;
54 }
55
56#if ( __i386__ )
57
58 __asm__ volatile ("mov %%esp, %0" : "=q" (sp));
59
60#elif ( __amd64__ )
61
62 __asm__ volatile ("mov %%rsp, %0" : "=q" (sp));
63
64#else
65
66#error "rfork()ed threads are not supported on this platform"
67
68#endif
69
70 return (ngx_freebsd_kern_usrstack - sp) / ngx_thread_stack_size;
71}
72
73
74
Igor Sysoevb54698b2004-02-23 20:57:12 +000075#else /* use pthreads */
76
77#include <pthread.h>
78
79typedef pthread_t ngx_tid_t;
80
Igor Sysoevd94049b2004-02-29 21:03:02 +000081#define ngx_gettid() ((ngx_int_t) pthread_getspecific(0))
Igor Sysoevb54698b2004-02-23 20:57:12 +000082#define ngx_log_tid ngx_thread_self()
83
Igor Sysoev6a930452004-03-04 16:34:23 +000084#define ngx_thread_sigmask pthread_sigmask
85#define ngx_thread_sigmask_n "pthread_sigmask()"
86
Igor Sysoevb54698b2004-02-23 20:57:12 +000087#endif
88
89
Igor Sysoevf2334412004-02-25 20:16:15 +000090ngx_int_t ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle);
Igor Sysoevb54698b2004-02-23 20:57:12 +000091int ngx_create_thread(ngx_tid_t *tid, int (*func)(void *arg), void *arg,
92 ngx_log_t *log);
93ngx_tid_t ngx_thread_self();
94
Igor Sysoevd94049b2004-02-29 21:03:02 +000095
Igor Sysoevb54698b2004-02-23 20:57:12 +000096ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, uint flags);
97void ngx_mutex_done(ngx_mutex_t *m);
98
Igor Sysoevd94049b2004-02-29 21:03:02 +000099#define ngx_mutex_trylock(m) ngx_mutex_dolock(m, 1)
100#define ngx_mutex_lock(m) ngx_mutex_dolock(m, 0)
101ngx_int_t ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try);
Igor Sysoevb54698b2004-02-23 20:57:12 +0000102ngx_int_t ngx_mutex_unlock(ngx_mutex_t *m);
103
104
Igor Sysoev898446c2004-02-26 17:10:01 +0000105extern ngx_int_t ngx_threaded;
106
107
Igor Sysoevb54698b2004-02-23 20:57:12 +0000108#else /* !NGX_THREADS */
109
Igor Sysoevea0b1d92004-03-02 15:40:59 +0000110#define ngx_thread_volatile
111
Igor Sysoevb54698b2004-02-23 20:57:12 +0000112#define ngx_log_tid 0
113#define TID_T_FMT "%d"
114
Igor Sysoev5edf3872004-03-03 16:14:15 +0000115#define ngx_mutex_lock(m) NGX_OK
Igor Sysoevb54698b2004-02-23 20:57:12 +0000116#define ngx_mutex_unlock(m)
117
118#endif
119
120
121#endif /* _NGX_THREAD_H_INCLUDED_ */