xref: /unit/auto/threads (revision 0:a63ceefd6ab0)
1
2# Copyright (C) Igor Sysoev
3# Copyright (C) NGINX, Inc.
4
5
6case "$NXT_SYSTEM" in
7
8    Linux)
9        NXT_PTHREAD="-lpthread"
10    ;;
11
12    FreeBSD)
13        # FreeBSD libc supports only pthread stubs.
14        NXT_PTHREAD="-lpthread"
15    ;;
16
17    SunOS)
18        case "$NXT_SYSTEM_VERSION" in
19        5.8 | 5.9)
20            NXT_PTHREAD="-lpthread"
21        ;;
22        *)
23            # Solaris 10 libpthread.so.1 is a filter to libc.so.1.
24            NXT_PTHREAD=
25        ;;
26        esac
27    ;;
28
29    Darwin)
30        # MacOSX libpthread.dylib is a symlink to libSystem.dylib.
31        NXT_PTHREAD=
32    ;;
33
34    *)
35        NXT_PTHREAD="-lpthread"
36    ;;
37esac
38
39
40# Linux, FreeBSD.
41
42nxt_feature="pthread_yield()"
43nxt_feature_name=NXT_HAVE_PTHREAD_YIELD
44nxt_feature_run=
45nxt_feature_incs=
46nxt_feature_libs=$NXT_PTHREAD
47nxt_feature_test="#define _GNU_SOURCE
48              #include <pthread.h>
49
50              int main() {
51                  pthread_yield();
52                  return 0;
53              }"
54. auto/feature
55
56
57if [ $nxt_found = no ]; then
58
59    # MacOSX.
60
61    nxt_feature="pthread_yield_np()"
62    nxt_feature_name=NXT_HAVE_PTHREAD_YIELD_NP
63    nxt_feature_run=
64    nxt_feature_incs=
65    nxt_feature_libs=$NXT_PTHREAD
66    nxt_feature_test="#include <pthread.h>
67
68                  int main() {
69                      pthread_yield_np();
70                      return 0;
71                  }"
72    . auto/feature
73fi
74
75
76# FreeBSD, Solaris, AIX.
77
78nxt_feature="pthread spinlock"
79nxt_feature_name=NXT_HAVE_PTHREAD_SPINLOCK
80nxt_feature_run=yes
81nxt_feature_incs=
82nxt_feature_libs=$NXT_PTHREAD
83nxt_feature_test="#include <pthread.h>
84
85              int main() {
86                  pthread_spinlock_t  lock;
87
88                  if (pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE) != 0)
89                      return 1;
90                  if (pthread_spin_lock(&lock) != 0)
91                      return 1;
92                  if (pthread_spin_unlock(&lock) != 0)
93                      return 1;
94                  if (pthread_spin_destroy(&lock) != 0)
95                      return 1;
96                  return 0;
97              }"
98. auto/feature
99
100
101if [ $nxt_found = yes ]; then
102
103    # Linux glibc uses 0 as pthread_spinlock_t initial value on the most
104    # platforms.  However, on i386 and x86_64 the initial value is 1.
105
106    nxt_feature="pthread spinlock zero initial value"
107    nxt_feature_name=NXT_HAVE_PTHREAD_SPINLOCK_ZERO
108    nxt_feature_run=yes
109    nxt_feature_incs=
110    nxt_feature_libs=$NXT_PTHREAD
111    nxt_feature_test="#include <pthread.h>
112
113                      pthread_spinlock_t  lock = 0;
114
115                      int main() {
116                          if (pthread_spin_trylock(&lock) != 0)
117                              return 1;
118                          if (pthread_spin_unlock(&lock) != 0)
119                              return 1;
120                          return 0;
121                      }"
122    . auto/feature
123fi
124
125
126if [ $nxt_found = no ]; then
127
128    # MacOSX spinlock(3).
129
130    nxt_feature="MacOSX spinlock"
131    nxt_feature_name=NXT_HAVE_MACOSX_SPINLOCK
132    nxt_feature_run=yes
133    nxt_feature_incs=
134    nxt_feature_libs=$NXT_PTHREAD
135    nxt_feature_test="#include <libkern/OSAtomic.h>
136
137                  int main() {
138                      OSSpinLock  lock = 0;
139
140                      if (OSSpinLockTry(&lock) == 0)
141                          return 1;
142                      OSSpinLockUnlock(&lock);
143                      return 0;
144                  }"
145    . auto/feature
146fi
147
148
149nxt_feature="sem_timedwait()"
150nxt_feature_name=NXT_HAVE_SEM_TIMEDWAIT
151nxt_feature_run=yes
152nxt_feature_incs=
153nxt_feature_libs=
154nxt_feature_test="#include <semaphore.h>
155
156                  int main() {
157                      sem_t            sem;
158                      struct timespec  ts;
159
160                      if (sem_init(&sem, 0, 0) != 0)
161                          return 1;
162                      if (sem_post(&sem) != 0)
163                          return 1;
164
165                      ts.tv_sec = 0;
166                      ts.tv_nsec = 0;
167                      if (sem_timedwait(&sem, &ts) != 0)
168                          return 1;
169
170                      if (sem_destroy(&sem) != 0)
171                          return 1;
172                      return 0;
173                  }"
174. auto/feature
175
176
177if [ $nxt_found = no ]; then
178
179    if [ -n "$NXT_PTHREAD" ]; then
180
181        # Linux requires libpthread.
182
183        nxt_feature="sem_timedwait() in libpthread"
184        nxt_feature_libs=$NXT_PTHREAD
185        . auto/feature
186    fi
187
188    if [ $nxt_found = no ]; then
189
190        # Solaris 10 requires librt.
191
192        nxt_feature="sem_timedwait() in librt"
193        nxt_feature_libs="-lrt"
194        . auto/feature
195
196        if [ $nxt_found = yes ]; then
197            NXT_LIBRT="-lrt"
198        fi
199    fi
200fi
201
202
203# Thread Local Storage / Thread Specific Data.
204#
205# Linux, FreeBSD 5.3, Solaris.
206# MacOSX 10.7 (Lion) Clang.  However, if the -mmacosx-version-min
207# option is specified it must be at least 10.7.
208
209nxt_feature="__thread"
210nxt_feature_name=NXT_HAVE_THREAD_STORAGE_CLASS
211nxt_feature_run=yes
212nxt_feature_incs=
213nxt_feature_libs=$NXT_PTHREAD
214nxt_feature_test="#include <pthread.h>
215                  #include <stdlib.h>
216
217                  __thread int  key;
218
219                  void *func(void *p);
220
221                  void *func(void *p) {
222                      key = 0x9abcdef0;
223                      return NULL;
224                  }
225
226                  int main() {
227                      void       *n;
228                      pthread_t  pt;
229
230                      key = 0x12345678;
231                      if (pthread_create(&pt, NULL, func, NULL))
232                          return 1;
233                      if (pthread_join(pt, &n))
234                          return 1;
235                      if (key != 0x12345678)
236                          return 1;
237                      return 0;
238                  }"
239. auto/feature
240
241
242if [ $nxt_found = no ]; then
243
244    # MacOSX GCC lacks __thread support.
245    # On NetBSD __thread causes segmentation fault.
246
247    nxt_feature="phtread_key_t"
248    nxt_feature_name=NXT_HAVE_PTHREAD_SPECIFIC_DATA
249    nxt_feature_run=yes
250    nxt_feature_incs=
251    nxt_feature_libs=$NXT_PTHREAD
252    nxt_feature_test="#include <pthread.h>
253
254                      int main() {
255                          pthread_key_t  key = -1;
256
257                          if (pthread_key_create(&key, NULL))
258                              return 1;
259                          if (pthread_setspecific(key, (void *) 0x12345678))
260                              return 1;
261                          if (pthread_getspecific(key) != (void *) 0x12345678)
262                              return 1;
263                          return 0;
264                      }"
265    . auto/feature
266
267
268    nxt_feature="PTHREAD_KEYS_MAX"
269    nxt_feature_name=
270    nxt_feature_run=value
271    nxt_feature_incs=
272    nxt_feature_libs=
273    nxt_feature_test="#include <limits.h>
274                      #include <pthread.h>
275                      #include <stdio.h>
276
277                      int main() {
278                          printf(\"%d\", PTHREAD_KEYS_MAX);
279                          return 0;
280                      }"
281    . auto/feature
282fi
283