summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_semaphore.c
blob: ad05d8b9a156df845c7bb9512460dc8f0768016d (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

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


#if (NXT_HAVE_SEM_TIMEDWAIT)

/*
 * Linux POSIX semaphores use atomic/futex operations in since glibc 2.3.
 *
 * FreeBSD has two POSIX semaphore implementations.  The first implementation
 * has been introduced in FreeBSD 5.0 but it has some drawbacks:
 *   1) it had a bug (http://bugs.freebsd.org/127545) fixed in FreeBSD 7.2;
 *   2) it does not use atomic operations and always calls ksem syscalls;
 *   3) a number of semaphores is just 30 by default and until FreeBSD 8.1
 *      the number cannot be changed after boot time.
 *
 * The second implementation has been introduced in FreeBSD 6.1 in libthr
 * and uses atomic operations and umtx syscall.  However, until FreeBSD 9.0
 * a choice of implementation depended on linking order of libthr and libc.
 * In FreeBSD 9.0 the umtx implementation has been moved to libc.
 *
 * Solaris have POSIX semaphores.
 *
 * MacOSX has limited POSIX semaphore implementation:
 *  1) sem_init() exists but returns ENOSYS;
 *  2) no sem_timedwait().
 */

nxt_int_t
nxt_sem_init(nxt_sem_t *sem, nxt_uint_t count)
{
    if (sem_init(sem, 0, count) == 0) {
        nxt_thread_log_debug("sem_init(%p)", sem);
        return NXT_OK;
    }

    nxt_thread_log_alert("sem_init(%p) failed %E", sem, nxt_errno);
    return NXT_ERROR;
}


void
nxt_sem_destroy(nxt_sem_t *sem)
{
    if (sem_destroy(sem) == 0) {
        nxt_thread_log_debug("sem_destroy(%p)", sem);
        return;
    }

    nxt_thread_log_alert("sem_destroy(%p) failed %E", sem, nxt_errno);
}


nxt_int_t
nxt_sem_post(nxt_sem_t *sem)
{
    nxt_thread_log_debug("sem_post(%p)", sem);

    if (nxt_fast_path(sem_post(sem) == 0)) {
        return NXT_OK;
    }

    nxt_thread_log_alert("sem_post(%p) failed %E", sem, nxt_errno);

    return NXT_ERROR;
}


nxt_err_t
nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout)
{
    int              n;
    nxt_err_t        err;
    nxt_nsec_t       ns;
    nxt_thread_t     *thr;
    nxt_realtime_t   *now;
    struct timespec  ts;

    thr = nxt_thread();

    if (timeout == NXT_INFINITE_NSEC) {
        nxt_log_debug(thr->log, "sem_wait(%p) enter", sem);

        for ( ;; ) {
            n = sem_wait(sem);

            err = nxt_errno;

            nxt_thread_time_update(thr);

            if (nxt_fast_path(n == 0)) {
                nxt_thread_log_debug("sem_wait(%p) exit", sem);
                return 0;
            }

            switch (err) {

            case NXT_EINTR:
                nxt_log_error(NXT_LOG_INFO, thr->log, "sem_wait(%p) failed %E",
                              sem, err);
                continue;

            default:
                nxt_log_alert(thr->log, "sem_wait(%p) failed %E", sem, err);
                return err;
            }
        }
    }

#if (NXT_HAVE_SEM_TRYWAIT_FAST)

    nxt_log_debug(thr->log, "sem_trywait(%p) enter", sem);

    /*
     * Fast sem_trywait() using atomic operations may eliminate
     * timeout processing.
     */

    if (nxt_fast_path(sem_trywait(sem) == 0)) {
        return 0;
    }

#endif

    nxt_log_debug(thr->log, "sem_timedwait(%p, %N) enter", sem, timeout);

    now = nxt_thread_realtime(thr);
    ns = now->nsec + timeout;
    ts.tv_sec = now->sec + ns / 1000000000;
    ts.tv_nsec = ns % 1000000000;

    for ( ;; ) {
        n = sem_timedwait(sem, &ts);

        err = nxt_errno;

        nxt_thread_time_update(thr);

        if (nxt_fast_path(n == 0)) {
            nxt_thread_log_debug("sem_timedwait(%p) exit", sem);
            return 0;
        }

        switch (err) {

        case NXT_ETIMEDOUT:
            nxt_log_debug(thr->log, "sem_timedwait(%p) exit: %d", sem, err);
            return err;

        case NXT_EINTR:
            nxt_log_error(NXT_LOG_INFO, thr->log, "sem_timedwait(%p) failed %E",
                          sem, err);
            continue;

        default:
            nxt_log_alert(thr->log, "sem_timedwait(%p) failed %E", sem, err);
            return err;
        }
    }
}

#else

/* Semaphore implementation using pthread conditional variable. */

nxt_int_t
nxt_sem_init(nxt_sem_t *sem, nxt_uint_t count)
{
    if (nxt_thread_mutex_create(&sem->mutex) == NXT_OK) {

        if (nxt_thread_cond_create(&sem->cond) == NXT_OK) {
            sem->count = count;
            return NXT_OK;
        }

        nxt_thread_mutex_destroy(&sem->mutex);
    }

    return NXT_ERROR;
}


void
nxt_sem_destroy(nxt_sem_t *sem)
{
    nxt_thread_cond_destroy(&sem->cond);
    nxt_thread_mutex_destroy(&sem->mutex);
}


nxt_int_t
nxt_sem_post(nxt_sem_t *sem)
{
    nxt_int_t  ret;

    if (nxt_slow_path(nxt_thread_mutex_lock(&sem->mutex) != NXT_OK)) {
        return NXT_ERROR;
    }

    ret = nxt_thread_cond_signal(&sem->cond);

    sem->count++;

    /* NXT_ERROR overrides NXT_OK. */

    return (nxt_thread_mutex_unlock(&sem->mutex) | ret);
}


nxt_err_t
nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout)
{
    nxt_err_t  err;

    err = 0;

    if (nxt_slow_path(nxt_thread_mutex_lock(&sem->mutex) != NXT_OK)) {
        return NXT_ERROR;
    }

    while (sem->count == 0) {

        err = nxt_thread_cond_wait(&sem->cond, &sem->mutex, timeout);

        if (err != 0) {
            goto error;
        }
    }

    sem->count--;

error:

    /* NXT_ERROR overrides NXT_OK and NXT_ETIMEDOUT. */

    return (nxt_thread_mutex_unlock(&sem->mutex) | err);
}

#endif