1 | /* |
2 | * librdkafka - Apache Kafka C library |
3 | * |
4 | * Copyright (c) 2018 Magnus Edenhill |
5 | * All rights reserved. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions are met: |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
13 | * this list of conditions and the following disclaimer in the documentation |
14 | * and/or other materials provided with the distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | |
30 | /** |
31 | * @brief Extra methods added to tinycthread/c11threads |
32 | */ |
33 | |
34 | #include "rd.h" |
35 | #include "rdtime.h" |
36 | #include "tinycthread.h" |
37 | |
38 | |
39 | int thrd_setname (const char *name) { |
40 | #if HAVE_PTHREAD_SETNAME_GNU |
41 | if (!pthread_setname_np(pthread_self(), name)) |
42 | return thrd_success; |
43 | #endif |
44 | return thrd_error; |
45 | } |
46 | |
47 | int thrd_is_current(thrd_t thr) { |
48 | #if defined(_TTHREAD_WIN32_) |
49 | return GetThreadId(thr) == GetCurrentThreadId(); |
50 | #else |
51 | return (pthread_self() == thr); |
52 | #endif |
53 | } |
54 | |
55 | |
56 | |
57 | |
58 | int cnd_timedwait_ms(cnd_t *cnd, mtx_t *mtx, int timeout_ms) { |
59 | if (timeout_ms == -1 /* INFINITE*/) |
60 | return cnd_wait(cnd, mtx); |
61 | #if defined(_TTHREAD_WIN32_) |
62 | return _cnd_timedwait_win32(cnd, mtx, (DWORD)timeout_ms); |
63 | #else |
64 | struct timeval tv; |
65 | struct timespec ts; |
66 | |
67 | gettimeofday(&tv, NULL); |
68 | ts.tv_sec = tv.tv_sec; |
69 | ts.tv_nsec = tv.tv_usec * 1000; |
70 | |
71 | ts.tv_sec += timeout_ms / 1000; |
72 | ts.tv_nsec += (timeout_ms % 1000) * 1000000; |
73 | |
74 | if (ts.tv_nsec >= 1000000000) { |
75 | ts.tv_sec++; |
76 | ts.tv_nsec -= 1000000000; |
77 | } |
78 | |
79 | return cnd_timedwait(cnd, mtx, &ts); |
80 | #endif |
81 | } |
82 | |
83 | int cnd_timedwait_msp (cnd_t *cnd, mtx_t *mtx, int *timeout_msp) { |
84 | rd_ts_t pre = rd_clock(); |
85 | int r; |
86 | r = cnd_timedwait_ms(cnd, mtx, *timeout_msp); |
87 | if (r != thrd_timedout) { |
88 | /* Subtract spent time */ |
89 | (*timeout_msp) -= (int)(rd_clock()-pre) / 1000; |
90 | } |
91 | return r; |
92 | } |
93 | |
94 | int cnd_timedwait_abs (cnd_t *cnd, mtx_t *mtx, const struct timespec *tspec) { |
95 | if (tspec->tv_sec == RD_POLL_INFINITE) |
96 | return cnd_wait(cnd, mtx); |
97 | else if (tspec->tv_sec == RD_POLL_NOWAIT) |
98 | return thrd_timedout; |
99 | |
100 | return cnd_timedwait(cnd, mtx, tspec); |
101 | } |
102 | |
103 | |
104 | /** |
105 | * @name Read-write locks |
106 | * @{ |
107 | */ |
108 | #ifndef _MSC_VER |
109 | int rwlock_init (rwlock_t *rwl) { |
110 | int r = pthread_rwlock_init(rwl, NULL); |
111 | if (r) { |
112 | errno = r; |
113 | return thrd_error; |
114 | } |
115 | return thrd_success; |
116 | } |
117 | |
118 | int rwlock_destroy (rwlock_t *rwl) { |
119 | int r = pthread_rwlock_destroy(rwl); |
120 | if (r) { |
121 | errno = r; |
122 | return thrd_error; |
123 | } |
124 | return thrd_success; |
125 | } |
126 | |
127 | int rwlock_rdlock (rwlock_t *rwl) { |
128 | int r = pthread_rwlock_rdlock(rwl); |
129 | assert(r == 0); |
130 | return thrd_success; |
131 | } |
132 | |
133 | int rwlock_wrlock (rwlock_t *rwl) { |
134 | int r = pthread_rwlock_wrlock(rwl); |
135 | assert(r == 0); |
136 | return thrd_success; |
137 | } |
138 | |
139 | int rwlock_rdunlock (rwlock_t *rwl) { |
140 | int r = pthread_rwlock_unlock(rwl); |
141 | assert(r == 0); |
142 | return thrd_success; |
143 | } |
144 | |
145 | int rwlock_wrunlock (rwlock_t *rwl) { |
146 | int r = pthread_rwlock_unlock(rwl); |
147 | assert(r == 0); |
148 | return thrd_success; |
149 | } |
150 | /**@}*/ |
151 | |
152 | |
153 | #endif /* !_MSC_VER */ |
154 | |