summaryrefslogtreecommitdiffhomepage
path: root/auto/threads
blob: ff33eaac7be751cc8ea98190f6085ec0fa37a9e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

# Copyright (C) Igor Sysoev
# Copyright (C) NGINX, Inc.


case "$NXT_SYSTEM" in

    Linux)
        NXT_PTHREAD="-lpthread"
    ;;

    FreeBSD)
        # FreeBSD libc supports only pthread stubs.
        NXT_PTHREAD="-lpthread"
    ;;

    SunOS)
        case "$NXT_SYSTEM_VERSION" in
        5.8 | 5.9)
            NXT_PTHREAD="-lpthread"
        ;;
        *)
            # Solaris 10 libpthread.so.1 is a filter to libc.so.1.
            NXT_PTHREAD=
        ;;
        esac
    ;;

    Darwin)
        # MacOSX libpthread.dylib is a symlink to libSystem.dylib.
        NXT_PTHREAD=
    ;;

    *)
        NXT_PTHREAD="-lpthread"
    ;;
esac


# Linux, FreeBSD.

nxt_feature="pthread_yield()"
nxt_feature_name=NXT_HAVE_PTHREAD_YIELD
nxt_feature_run=
nxt_feature_incs=
nxt_feature_libs=$NXT_PTHREAD
nxt_feature_test="#define _GNU_SOURCE
              #include <pthread.h>

              int main() {
                  pthread_yield();
                  return 0;
              }"
. auto/feature


if [ $nxt_found = no ]; then

    # MacOSX.

    nxt_feature="pthread_yield_np()"
    nxt_feature_name=NXT_HAVE_PTHREAD_YIELD_NP
    nxt_feature_run=
    nxt_feature_incs=
    nxt_feature_libs=$NXT_PTHREAD
    nxt_feature_test="#include <pthread.h>

                  int main() {
                      pthread_yield_np();
                      return 0;
                  }"
    . auto/feature
fi


# FreeBSD, Solaris, AIX.

nxt_feature="pthread spinlock"
nxt_feature_name=NXT_HAVE_PTHREAD_SPINLOCK
nxt_feature_run=yes
nxt_feature_incs=
nxt_feature_libs=$NXT_PTHREAD
nxt_feature_test="#include <pthread.h>

              int main() {
                  pthread_spinlock_t  lock;

                  if (pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE) != 0)
                      return 1;
                  if (pthread_spin_lock(&lock) != 0)
                      return 1;
                  if (pthread_spin_unlock(&lock) != 0)
                      return 1;
                  if (pthread_spin_destroy(&lock) != 0)
                      return 1;
                  return 0;
              }"
. auto/feature


if [ $nxt_found = yes ]; then

    # Linux glibc uses 0 as pthread_spinlock_t initial value on the most
    # platforms.  However, on i386 and x86_64 the initial value is 1.

    nxt_feature="pthread spinlock zero initial value"
    nxt_feature_name=NXT_HAVE_PTHREAD_SPINLOCK_ZERO
    nxt_feature_run=yes
    nxt_feature_incs=
    nxt_feature_libs=$NXT_PTHREAD
    nxt_feature_test="#include <pthread.h>

                      pthread_spinlock_t  lock = 0;

                      int main() {
                          if (pthread_spin_trylock(&lock) != 0)
                              return 1;
                          if (pthread_spin_unlock(&lock) != 0)
                              return 1;
                          return 0;
                      }"
    . auto/feature
fi


nxt_feature="sem_timedwait()"
nxt_feature_name=NXT_HAVE_SEM_TIMEDWAIT
nxt_feature_run=yes
nxt_feature_incs=
nxt_feature_libs=
nxt_feature_test="#include <semaphore.h>

                  int main() {
                      sem_t            sem;
                      struct timespec  ts;

                      if (sem_init(&sem, 0, 0) != 0)
                          return 1;
                      if (sem_post(&sem) != 0)
                          return 1;

                      ts.tv_sec = 0;
                      ts.tv_nsec = 0;
                      if (sem_timedwait(&sem, &ts) != 0)
                          return 1;

                      if (sem_destroy(&sem) != 0)
                          return 1;
                      return 0;
                  }"
. auto/feature


if [ $nxt_found = no ]; then

    if [ -n "$NXT_PTHREAD" ]; then

        # Linux requires libpthread.

        nxt_feature="sem_timedwait() in libpthread"
        nxt_feature_libs=$NXT_PTHREAD
        . auto/feature
    fi

    if [ $nxt_found = no ]; then

        # Solaris 10 requires librt.

        nxt_feature="sem_timedwait() in librt"
        nxt_feature_libs="-lrt"
        . auto/feature

        if [ $nxt_found = yes ]; then
            NXT_LIBRT="-lrt"
        fi
    fi
fi


# Thread Local Storage / Thread Specific Data.
#
# Linux, FreeBSD 5.3, Solaris.
# MacOSX 10.7 (Lion) Clang.

nxt_feature="__thread"
nxt_feature_name=NXT_HAVE_THREAD_STORAGE_CLASS
nxt_feature_run=yes
nxt_feature_incs=
nxt_feature_libs=$NXT_PTHREAD
nxt_feature_test="#include <pthread.h>
                  #include <stdlib.h>

                  __thread int  key;

                  void *func(void *p);

                  void *func(void *p) {
                      key = 0x9abcdef0;
                      return NULL;
                  }

                  int main() {
                      void       *n;
                      pthread_t  pt;

                      key = 0x12345678;
                      if (pthread_create(&pt, NULL, func, NULL))
                          return 1;
                      if (pthread_join(pt, &n))
                          return 1;
                      if (key != 0x12345678)
                          return 1;
                      return 0;
                  }"
. auto/feature


if [ $nxt_found = no ]; then

    # MacOSX GCC lacks __thread support.
    # On NetBSD __thread causes segmentation fault.

    nxt_feature="phtread_key_t"
    nxt_feature_name=NXT_HAVE_PTHREAD_SPECIFIC_DATA
    nxt_feature_run=yes
    nxt_feature_incs=
    nxt_feature_libs=$NXT_PTHREAD
    nxt_feature_test="#include <pthread.h>

                      int main() {
                          pthread_key_t  key = -1;

                          if (pthread_key_create(&key, NULL))
                              return 1;
                          if (pthread_setspecific(key, (void *) 0x12345678))
                              return 1;
                          if (pthread_getspecific(key) != (void *) 0x12345678)
                              return 1;
                          return 0;
                      }"
    . auto/feature


    nxt_feature="PTHREAD_KEYS_MAX"
    nxt_feature_name=
    nxt_feature_run=value
    nxt_feature_incs=
    nxt_feature_libs=
    nxt_feature_test="#include <limits.h>
                      #include <pthread.h>
                      #include <stdio.h>

                      int main() {
                          printf(\"%d\", PTHREAD_KEYS_MAX);
                          return 0;
                      }"
    . auto/feature
fi