passenger 5.0.8 → 5.0.9

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of passenger might be problematic. Click here for more details.

Files changed (168) hide show
  1. checksums.yaml +8 -8
  2. checksums.yaml.gz.asc +7 -7
  3. data.tar.gz.asc +7 -7
  4. data/.editorconfig +20 -0
  5. data/CHANGELOG +21 -0
  6. data/bin/passenger-install-apache2-module +3 -1
  7. data/build/agents.rb +7 -5
  8. data/build/basics.rb +3 -3
  9. data/build/common_library.rb +52 -30
  10. data/build/cxx_tests.rb +20 -13
  11. data/build/misc.rb +5 -5
  12. data/doc/Design and Architecture.html +1 -1
  13. data/doc/Design and Architecture.txt +1 -1
  14. data/doc/Packaging.html +4 -4
  15. data/doc/Packaging.txt.md +4 -4
  16. data/doc/Users guide Apache.html +22 -9
  17. data/doc/Users guide Apache.idmap.txt +4 -2
  18. data/doc/Users guide Apache.txt +2 -0
  19. data/doc/Users guide Nginx.html +22 -9
  20. data/doc/Users guide Nginx.idmap.txt +4 -2
  21. data/doc/Users guide Nginx.txt +2 -0
  22. data/doc/Users guide Standalone.html +14 -9
  23. data/doc/Users guide Standalone.idmap.txt +4 -2
  24. data/doc/users_guide_snippets/installation.txt +10 -6
  25. data/ext/apache2/Hooks.cpp +13 -2
  26. data/ext/common/ApplicationPool2/Pool/Inspection.h +8 -3
  27. data/ext/common/BackgroundEventLoop.cpp +249 -67
  28. data/ext/common/BackgroundEventLoop.h +5 -5
  29. data/ext/common/Constants.h +1 -1
  30. data/ext/common/InstanceDirectory.h +8 -6
  31. data/ext/common/ServerKit/Context.h +8 -2
  32. data/ext/common/ServerKit/FileBufferedChannel.h +262 -226
  33. data/ext/common/ServerKit/HeaderTable.h +28 -3
  34. data/ext/common/ServerKit/HttpHeaderParser.h +37 -13
  35. data/ext/common/ServerKit/HttpServer.h +17 -1
  36. data/ext/common/ServerKit/Implementation.cpp +2 -0
  37. data/ext/common/ServerKit/Server.h +25 -28
  38. data/ext/common/Utils/IOUtils.cpp +11 -0
  39. data/ext/common/Utils/ProcessMetricsCollector.h +4 -0
  40. data/ext/common/Utils/StrIntUtils.cpp +11 -7
  41. data/ext/common/Utils/StrIntUtils.h +1 -1
  42. data/ext/common/Utils/StrIntUtilsNoStrictAliasing.cpp +21 -16
  43. data/ext/common/agents/Base.cpp +6 -0
  44. data/ext/common/agents/Base.h +2 -0
  45. data/ext/common/agents/HelperAgent/AdminServer.h +25 -25
  46. data/ext/common/agents/HelperAgent/Main.cpp +37 -12
  47. data/ext/common/agents/HelperAgent/RequestHandler.h +18 -20
  48. data/ext/common/agents/HelperAgent/RequestHandler/AppResponse.h +4 -0
  49. data/ext/common/agents/HelperAgent/RequestHandler/ForwardResponse.cpp +10 -6
  50. data/ext/common/agents/HelperAgent/RequestHandler/Hooks.cpp +2 -0
  51. data/ext/common/agents/HelperAgent/RequestHandler/InitRequest.cpp +1 -1
  52. data/ext/common/agents/HelperAgent/RequestHandler/SendRequest.cpp +1 -1
  53. data/ext/common/agents/HelperAgent/RequestHandler/Utils.cpp +9 -2
  54. data/ext/common/agents/HelperAgent/ResponseCache.h +11 -11
  55. data/ext/common/agents/LoggingAgent/AdminServer.h +8 -8
  56. data/ext/common/agents/LoggingAgent/Main.cpp +6 -5
  57. data/ext/common/agents/Watchdog/AdminServer.h +13 -13
  58. data/ext/common/agents/Watchdog/Main.cpp +8 -3
  59. data/ext/libuv/.gitignore +72 -0
  60. data/ext/libuv/AUTHORS +199 -0
  61. data/ext/libuv/ChangeLog +2023 -0
  62. data/ext/libuv/LICENSE +46 -0
  63. data/ext/libuv/Makefile.am +336 -0
  64. data/ext/libuv/README.md +197 -0
  65. data/ext/libuv/checksparse.sh +233 -0
  66. data/ext/libuv/common.gypi +210 -0
  67. data/ext/libuv/configure.ac +67 -0
  68. data/ext/libuv/gyp_uv.py +96 -0
  69. data/ext/libuv/include/android-ifaddrs.h +54 -0
  70. data/ext/libuv/include/pthread-fixes.h +72 -0
  71. data/ext/libuv/include/tree.h +768 -0
  72. data/ext/libuv/include/uv-aix.h +32 -0
  73. data/ext/libuv/include/uv-bsd.h +34 -0
  74. data/ext/libuv/include/uv-darwin.h +61 -0
  75. data/ext/libuv/include/uv-errno.h +418 -0
  76. data/ext/libuv/include/uv-linux.h +34 -0
  77. data/ext/libuv/include/uv-sunos.h +44 -0
  78. data/ext/libuv/include/uv-threadpool.h +37 -0
  79. data/ext/libuv/include/uv-unix.h +383 -0
  80. data/ext/libuv/include/uv-version.h +39 -0
  81. data/ext/libuv/include/uv.h +1455 -0
  82. data/ext/libuv/libuv.pc.in +11 -0
  83. data/ext/libuv/m4/.gitignore +4 -0
  84. data/ext/libuv/m4/as_case.m4 +21 -0
  85. data/ext/libuv/m4/libuv-check-flags.m4 +319 -0
  86. data/ext/libuv/src/fs-poll.c +255 -0
  87. data/ext/libuv/src/heap-inl.h +245 -0
  88. data/ext/libuv/src/inet.c +313 -0
  89. data/ext/libuv/src/queue.h +92 -0
  90. data/ext/libuv/src/threadpool.c +303 -0
  91. data/ext/libuv/src/unix/aix.c +1240 -0
  92. data/ext/libuv/src/unix/android-ifaddrs.c +703 -0
  93. data/ext/libuv/src/unix/async.c +284 -0
  94. data/ext/libuv/src/unix/atomic-ops.h +60 -0
  95. data/ext/libuv/src/unix/core.c +985 -0
  96. data/ext/libuv/src/unix/darwin-proctitle.c +206 -0
  97. data/ext/libuv/src/unix/darwin.c +331 -0
  98. data/ext/libuv/src/unix/dl.c +83 -0
  99. data/ext/libuv/src/unix/freebsd.c +435 -0
  100. data/ext/libuv/src/unix/fs.c +1189 -0
  101. data/ext/libuv/src/unix/fsevents.c +899 -0
  102. data/ext/libuv/src/unix/getaddrinfo.c +202 -0
  103. data/ext/libuv/src/unix/getnameinfo.c +120 -0
  104. data/ext/libuv/src/unix/internal.h +314 -0
  105. data/ext/libuv/src/unix/kqueue.c +418 -0
  106. data/ext/libuv/src/unix/linux-core.c +876 -0
  107. data/ext/libuv/src/unix/linux-inotify.c +257 -0
  108. data/ext/libuv/src/unix/linux-syscalls.c +471 -0
  109. data/ext/libuv/src/unix/linux-syscalls.h +158 -0
  110. data/ext/libuv/src/unix/loop-watcher.c +63 -0
  111. data/ext/libuv/src/unix/loop.c +135 -0
  112. data/ext/libuv/src/unix/netbsd.c +368 -0
  113. data/ext/libuv/src/unix/openbsd.c +384 -0
  114. data/ext/libuv/src/unix/pipe.c +288 -0
  115. data/ext/libuv/src/unix/poll.c +113 -0
  116. data/ext/libuv/src/unix/process.c +551 -0
  117. data/ext/libuv/src/unix/proctitle.c +102 -0
  118. data/ext/libuv/src/unix/pthread-fixes.c +103 -0
  119. data/ext/libuv/src/unix/signal.c +465 -0
  120. data/ext/libuv/src/unix/spinlock.h +53 -0
  121. data/ext/libuv/src/unix/stream.c +1598 -0
  122. data/ext/libuv/src/unix/sunos.c +763 -0
  123. data/ext/libuv/src/unix/tcp.c +327 -0
  124. data/ext/libuv/src/unix/thread.c +519 -0
  125. data/ext/libuv/src/unix/timer.c +172 -0
  126. data/ext/libuv/src/unix/tty.c +265 -0
  127. data/ext/libuv/src/unix/udp.c +833 -0
  128. data/ext/libuv/src/uv-common.c +544 -0
  129. data/ext/libuv/src/uv-common.h +214 -0
  130. data/ext/libuv/src/version.c +49 -0
  131. data/ext/libuv/uv.gyp +487 -0
  132. data/ext/nginx/ContentHandler.c +21 -10
  133. data/ext/nginx/ngx_http_passenger_module.c +7 -0
  134. data/ext/oxt/implementation.cpp +9 -2
  135. data/ext/oxt/initialize.hpp +5 -1
  136. data/lib/phusion_passenger.rb +3 -3
  137. data/lib/phusion_passenger/admin_tools/instance.rb +10 -6
  138. data/lib/phusion_passenger/admin_tools/instance_registry.rb +6 -2
  139. data/lib/phusion_passenger/packaging.rb +3 -4
  140. data/lib/phusion_passenger/platform_info.rb +13 -1
  141. data/lib/phusion_passenger/platform_info/apache.rb +15 -4
  142. data/lib/phusion_passenger/platform_info/apache_detector.rb +5 -1
  143. data/lib/phusion_passenger/rack/thread_handler_extension.rb +184 -99
  144. data/lib/phusion_passenger/request_handler/thread_handler.rb +13 -6
  145. data/lib/phusion_passenger/standalone/start_command.rb +2 -2
  146. data/resources/templates/apache2/apache_install_broken.txt.erb +2 -1
  147. metadata +99 -22
  148. metadata.gz.asc +7 -7
  149. data/ext/libeio/Changes +0 -76
  150. data/ext/libeio/LICENSE +0 -36
  151. data/ext/libeio/Makefile.am +0 -15
  152. data/ext/libeio/Makefile.in +0 -694
  153. data/ext/libeio/aclocal.m4 +0 -9418
  154. data/ext/libeio/autogen.sh +0 -3
  155. data/ext/libeio/config.guess +0 -1540
  156. data/ext/libeio/config.h.in +0 -136
  157. data/ext/libeio/config.sub +0 -1779
  158. data/ext/libeio/configure +0 -14822
  159. data/ext/libeio/configure.ac +0 -22
  160. data/ext/libeio/demo.c +0 -194
  161. data/ext/libeio/ecb.h +0 -714
  162. data/ext/libeio/eio.c +0 -2818
  163. data/ext/libeio/eio.h +0 -414
  164. data/ext/libeio/install-sh +0 -520
  165. data/ext/libeio/libeio.m4 +0 -195
  166. data/ext/libeio/ltmain.sh +0 -9636
  167. data/ext/libeio/missing +0 -376
  168. data/ext/libeio/xthread.h +0 -166
@@ -0,0 +1,327 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to
5
+ * deal in the Software without restriction, including without limitation the
6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ * sell copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ * IN THE SOFTWARE.
20
+ */
21
+
22
+ #include "uv.h"
23
+ #include "internal.h"
24
+
25
+ #include <stdlib.h>
26
+ #include <unistd.h>
27
+ #include <assert.h>
28
+ #include <errno.h>
29
+
30
+
31
+ int uv_tcp_init(uv_loop_t* loop, uv_tcp_t* tcp) {
32
+ uv__stream_init(loop, (uv_stream_t*)tcp, UV_TCP);
33
+ return 0;
34
+ }
35
+
36
+
37
+ static int maybe_new_socket(uv_tcp_t* handle, int domain, int flags) {
38
+ int sockfd;
39
+ int err;
40
+
41
+ if (uv__stream_fd(handle) != -1)
42
+ return 0;
43
+
44
+ err = uv__socket(domain, SOCK_STREAM, 0);
45
+ if (err < 0)
46
+ return err;
47
+ sockfd = err;
48
+
49
+ err = uv__stream_open((uv_stream_t*) handle, sockfd, flags);
50
+ if (err) {
51
+ uv__close(sockfd);
52
+ return err;
53
+ }
54
+
55
+ return 0;
56
+ }
57
+
58
+
59
+ int uv__tcp_bind(uv_tcp_t* tcp,
60
+ const struct sockaddr* addr,
61
+ unsigned int addrlen,
62
+ unsigned int flags) {
63
+ int err;
64
+ int on;
65
+
66
+ /* Cannot set IPv6-only mode on non-IPv6 socket. */
67
+ if ((flags & UV_TCP_IPV6ONLY) && addr->sa_family != AF_INET6)
68
+ return -EINVAL;
69
+
70
+ err = maybe_new_socket(tcp,
71
+ addr->sa_family,
72
+ UV_STREAM_READABLE | UV_STREAM_WRITABLE);
73
+ if (err)
74
+ return err;
75
+
76
+ on = 1;
77
+ if (setsockopt(tcp->io_watcher.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
78
+ return -errno;
79
+
80
+ #ifdef IPV6_V6ONLY
81
+ if (addr->sa_family == AF_INET6) {
82
+ on = (flags & UV_TCP_IPV6ONLY) != 0;
83
+ if (setsockopt(tcp->io_watcher.fd,
84
+ IPPROTO_IPV6,
85
+ IPV6_V6ONLY,
86
+ &on,
87
+ sizeof on) == -1) {
88
+ return -errno;
89
+ }
90
+ }
91
+ #endif
92
+
93
+ errno = 0;
94
+ if (bind(tcp->io_watcher.fd, addr, addrlen) && errno != EADDRINUSE)
95
+ return -errno;
96
+ tcp->delayed_error = -errno;
97
+
98
+ if (addr->sa_family == AF_INET6)
99
+ tcp->flags |= UV_HANDLE_IPV6;
100
+
101
+ return 0;
102
+ }
103
+
104
+
105
+ int uv__tcp_connect(uv_connect_t* req,
106
+ uv_tcp_t* handle,
107
+ const struct sockaddr* addr,
108
+ unsigned int addrlen,
109
+ uv_connect_cb cb) {
110
+ int err;
111
+ int r;
112
+
113
+ assert(handle->type == UV_TCP);
114
+
115
+ if (handle->connect_req != NULL)
116
+ return -EALREADY; /* FIXME(bnoordhuis) -EINVAL or maybe -EBUSY. */
117
+
118
+ err = maybe_new_socket(handle,
119
+ addr->sa_family,
120
+ UV_STREAM_READABLE | UV_STREAM_WRITABLE);
121
+ if (err)
122
+ return err;
123
+
124
+ handle->delayed_error = 0;
125
+
126
+ do
127
+ r = connect(uv__stream_fd(handle), addr, addrlen);
128
+ while (r == -1 && errno == EINTR);
129
+
130
+ if (r == -1) {
131
+ if (errno == EINPROGRESS)
132
+ ; /* not an error */
133
+ else if (errno == ECONNREFUSED)
134
+ /* If we get a ECONNREFUSED wait until the next tick to report the
135
+ * error. Solaris wants to report immediately--other unixes want to
136
+ * wait.
137
+ */
138
+ handle->delayed_error = -errno;
139
+ else
140
+ return -errno;
141
+ }
142
+
143
+ uv__req_init(handle->loop, req, UV_CONNECT);
144
+ req->cb = cb;
145
+ req->handle = (uv_stream_t*) handle;
146
+ QUEUE_INIT(&req->queue);
147
+ handle->connect_req = req;
148
+
149
+ uv__io_start(handle->loop, &handle->io_watcher, UV__POLLOUT);
150
+
151
+ if (handle->delayed_error)
152
+ uv__io_feed(handle->loop, &handle->io_watcher);
153
+
154
+ return 0;
155
+ }
156
+
157
+
158
+ int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock) {
159
+ int err;
160
+
161
+ err = uv__nonblock(sock, 1);
162
+ if (err)
163
+ return err;
164
+
165
+ return uv__stream_open((uv_stream_t*)handle,
166
+ sock,
167
+ UV_STREAM_READABLE | UV_STREAM_WRITABLE);
168
+ }
169
+
170
+
171
+ int uv_tcp_getsockname(const uv_tcp_t* handle,
172
+ struct sockaddr* name,
173
+ int* namelen) {
174
+ socklen_t socklen;
175
+
176
+ if (handle->delayed_error)
177
+ return handle->delayed_error;
178
+
179
+ if (uv__stream_fd(handle) < 0)
180
+ return -EINVAL; /* FIXME(bnoordhuis) -EBADF */
181
+
182
+ /* sizeof(socklen_t) != sizeof(int) on some systems. */
183
+ socklen = (socklen_t) *namelen;
184
+
185
+ if (getsockname(uv__stream_fd(handle), name, &socklen))
186
+ return -errno;
187
+
188
+ *namelen = (int) socklen;
189
+ return 0;
190
+ }
191
+
192
+
193
+ int uv_tcp_getpeername(const uv_tcp_t* handle,
194
+ struct sockaddr* name,
195
+ int* namelen) {
196
+ socklen_t socklen;
197
+
198
+ if (handle->delayed_error)
199
+ return handle->delayed_error;
200
+
201
+ if (uv__stream_fd(handle) < 0)
202
+ return -EINVAL; /* FIXME(bnoordhuis) -EBADF */
203
+
204
+ /* sizeof(socklen_t) != sizeof(int) on some systems. */
205
+ socklen = (socklen_t) *namelen;
206
+
207
+ if (getpeername(uv__stream_fd(handle), name, &socklen))
208
+ return -errno;
209
+
210
+ *namelen = (int) socklen;
211
+ return 0;
212
+ }
213
+
214
+
215
+ int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
216
+ static int single_accept = -1;
217
+ int err;
218
+
219
+ if (tcp->delayed_error)
220
+ return tcp->delayed_error;
221
+
222
+ if (single_accept == -1) {
223
+ const char* val = getenv("UV_TCP_SINGLE_ACCEPT");
224
+ single_accept = (val != NULL && atoi(val) != 0); /* Off by default. */
225
+ }
226
+
227
+ if (single_accept)
228
+ tcp->flags |= UV_TCP_SINGLE_ACCEPT;
229
+
230
+ err = maybe_new_socket(tcp, AF_INET, UV_STREAM_READABLE);
231
+ if (err)
232
+ return err;
233
+
234
+ if (listen(tcp->io_watcher.fd, backlog))
235
+ return -errno;
236
+
237
+ tcp->connection_cb = cb;
238
+
239
+ /* Start listening for connections. */
240
+ tcp->io_watcher.cb = uv__server_io;
241
+ uv__io_start(tcp->loop, &tcp->io_watcher, UV__POLLIN);
242
+
243
+ return 0;
244
+ }
245
+
246
+
247
+ int uv__tcp_nodelay(int fd, int on) {
248
+ if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)))
249
+ return -errno;
250
+ return 0;
251
+ }
252
+
253
+
254
+ int uv__tcp_keepalive(int fd, int on, unsigned int delay) {
255
+ if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)))
256
+ return -errno;
257
+
258
+ #ifdef TCP_KEEPIDLE
259
+ if (on && setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &delay, sizeof(delay)))
260
+ return -errno;
261
+ #endif
262
+
263
+ /* Solaris/SmartOS, if you don't support keep-alive,
264
+ * then don't advertise it in your system headers...
265
+ */
266
+ /* FIXME(bnoordhuis) That's possibly because sizeof(delay) should be 1. */
267
+ #if defined(TCP_KEEPALIVE) && !defined(__sun)
268
+ if (on && setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay)))
269
+ return -errno;
270
+ #endif
271
+
272
+ return 0;
273
+ }
274
+
275
+
276
+ int uv_tcp_nodelay(uv_tcp_t* handle, int on) {
277
+ int err;
278
+
279
+ if (uv__stream_fd(handle) != -1) {
280
+ err = uv__tcp_nodelay(uv__stream_fd(handle), on);
281
+ if (err)
282
+ return err;
283
+ }
284
+
285
+ if (on)
286
+ handle->flags |= UV_TCP_NODELAY;
287
+ else
288
+ handle->flags &= ~UV_TCP_NODELAY;
289
+
290
+ return 0;
291
+ }
292
+
293
+
294
+ int uv_tcp_keepalive(uv_tcp_t* handle, int on, unsigned int delay) {
295
+ int err;
296
+
297
+ if (uv__stream_fd(handle) != -1) {
298
+ err =uv__tcp_keepalive(uv__stream_fd(handle), on, delay);
299
+ if (err)
300
+ return err;
301
+ }
302
+
303
+ if (on)
304
+ handle->flags |= UV_TCP_KEEPALIVE;
305
+ else
306
+ handle->flags &= ~UV_TCP_KEEPALIVE;
307
+
308
+ /* TODO Store delay if uv__stream_fd(handle) == -1 but don't want to enlarge
309
+ * uv_tcp_t with an int that's almost never used...
310
+ */
311
+
312
+ return 0;
313
+ }
314
+
315
+
316
+ int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable) {
317
+ if (enable)
318
+ handle->flags &= ~UV_TCP_SINGLE_ACCEPT;
319
+ else
320
+ handle->flags |= UV_TCP_SINGLE_ACCEPT;
321
+ return 0;
322
+ }
323
+
324
+
325
+ void uv__tcp_close(uv_tcp_t* handle) {
326
+ uv__stream_close((uv_stream_t*)handle);
327
+ }
@@ -0,0 +1,519 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to
5
+ * deal in the Software without restriction, including without limitation the
6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ * sell copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ * IN THE SOFTWARE.
20
+ */
21
+
22
+ #include "uv.h"
23
+ #include "internal.h"
24
+
25
+ #include <pthread.h>
26
+ #include <assert.h>
27
+ #include <errno.h>
28
+
29
+ #include <sys/time.h>
30
+
31
+ #undef NANOSEC
32
+ #define NANOSEC ((uint64_t) 1e9)
33
+
34
+
35
+ struct thread_ctx {
36
+ void (*entry)(void* arg);
37
+ void* arg;
38
+ };
39
+
40
+
41
+ static void* uv__thread_start(void *arg)
42
+ {
43
+ struct thread_ctx *ctx_p;
44
+ struct thread_ctx ctx;
45
+
46
+ ctx_p = arg;
47
+ ctx = *ctx_p;
48
+ free(ctx_p);
49
+ ctx.entry(ctx.arg);
50
+
51
+ return 0;
52
+ }
53
+
54
+
55
+ int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
56
+ struct thread_ctx* ctx;
57
+ int err;
58
+
59
+ ctx = malloc(sizeof(*ctx));
60
+ if (ctx == NULL)
61
+ return UV_ENOMEM;
62
+
63
+ ctx->entry = entry;
64
+ ctx->arg = arg;
65
+
66
+ err = pthread_create(tid, NULL, uv__thread_start, ctx);
67
+
68
+ if (err)
69
+ free(ctx);
70
+
71
+ return -err;
72
+ }
73
+
74
+
75
+ uv_thread_t uv_thread_self(void) {
76
+ return pthread_self();
77
+ }
78
+
79
+ int uv_thread_join(uv_thread_t *tid) {
80
+ return -pthread_join(*tid, NULL);
81
+ }
82
+
83
+
84
+ int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2) {
85
+ return pthread_equal(*t1, *t2);
86
+ }
87
+
88
+
89
+ int uv_mutex_init(uv_mutex_t* mutex) {
90
+ #if defined(NDEBUG) || !defined(PTHREAD_MUTEX_ERRORCHECK)
91
+ return -pthread_mutex_init(mutex, NULL);
92
+ #else
93
+ pthread_mutexattr_t attr;
94
+ int err;
95
+
96
+ if (pthread_mutexattr_init(&attr))
97
+ abort();
98
+
99
+ if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK))
100
+ abort();
101
+
102
+ err = pthread_mutex_init(mutex, &attr);
103
+
104
+ if (pthread_mutexattr_destroy(&attr))
105
+ abort();
106
+
107
+ return -err;
108
+ #endif
109
+ }
110
+
111
+
112
+ void uv_mutex_destroy(uv_mutex_t* mutex) {
113
+ if (pthread_mutex_destroy(mutex))
114
+ abort();
115
+ }
116
+
117
+
118
+ void uv_mutex_lock(uv_mutex_t* mutex) {
119
+ if (pthread_mutex_lock(mutex))
120
+ abort();
121
+ }
122
+
123
+
124
+ int uv_mutex_trylock(uv_mutex_t* mutex) {
125
+ int err;
126
+
127
+ /* FIXME(bnoordhuis) EAGAIN means recursive lock limit reached. Arguably
128
+ * a bug, should probably abort rather than return -EAGAIN.
129
+ */
130
+ err = pthread_mutex_trylock(mutex);
131
+ if (err && err != EBUSY && err != EAGAIN)
132
+ abort();
133
+
134
+ return -err;
135
+ }
136
+
137
+
138
+ void uv_mutex_unlock(uv_mutex_t* mutex) {
139
+ if (pthread_mutex_unlock(mutex))
140
+ abort();
141
+ }
142
+
143
+
144
+ int uv_rwlock_init(uv_rwlock_t* rwlock) {
145
+ return -pthread_rwlock_init(rwlock, NULL);
146
+ }
147
+
148
+
149
+ void uv_rwlock_destroy(uv_rwlock_t* rwlock) {
150
+ if (pthread_rwlock_destroy(rwlock))
151
+ abort();
152
+ }
153
+
154
+
155
+ void uv_rwlock_rdlock(uv_rwlock_t* rwlock) {
156
+ if (pthread_rwlock_rdlock(rwlock))
157
+ abort();
158
+ }
159
+
160
+
161
+ int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock) {
162
+ int err;
163
+
164
+ err = pthread_rwlock_tryrdlock(rwlock);
165
+ if (err && err != EBUSY && err != EAGAIN)
166
+ abort();
167
+
168
+ return -err;
169
+ }
170
+
171
+
172
+ void uv_rwlock_rdunlock(uv_rwlock_t* rwlock) {
173
+ if (pthread_rwlock_unlock(rwlock))
174
+ abort();
175
+ }
176
+
177
+
178
+ void uv_rwlock_wrlock(uv_rwlock_t* rwlock) {
179
+ if (pthread_rwlock_wrlock(rwlock))
180
+ abort();
181
+ }
182
+
183
+
184
+ int uv_rwlock_trywrlock(uv_rwlock_t* rwlock) {
185
+ int err;
186
+
187
+ err = pthread_rwlock_trywrlock(rwlock);
188
+ if (err && err != EBUSY && err != EAGAIN)
189
+ abort();
190
+
191
+ return -err;
192
+ }
193
+
194
+
195
+ void uv_rwlock_wrunlock(uv_rwlock_t* rwlock) {
196
+ if (pthread_rwlock_unlock(rwlock))
197
+ abort();
198
+ }
199
+
200
+
201
+ void uv_once(uv_once_t* guard, void (*callback)(void)) {
202
+ if (pthread_once(guard, callback))
203
+ abort();
204
+ }
205
+
206
+ #if defined(__APPLE__) && defined(__MACH__)
207
+
208
+ int uv_sem_init(uv_sem_t* sem, unsigned int value) {
209
+ kern_return_t err;
210
+
211
+ err = semaphore_create(mach_task_self(), sem, SYNC_POLICY_FIFO, value);
212
+ if (err == KERN_SUCCESS)
213
+ return 0;
214
+ if (err == KERN_INVALID_ARGUMENT)
215
+ return -EINVAL;
216
+ if (err == KERN_RESOURCE_SHORTAGE)
217
+ return -ENOMEM;
218
+
219
+ abort();
220
+ return -EINVAL; /* Satisfy the compiler. */
221
+ }
222
+
223
+
224
+ void uv_sem_destroy(uv_sem_t* sem) {
225
+ if (semaphore_destroy(mach_task_self(), *sem))
226
+ abort();
227
+ }
228
+
229
+
230
+ void uv_sem_post(uv_sem_t* sem) {
231
+ if (semaphore_signal(*sem))
232
+ abort();
233
+ }
234
+
235
+
236
+ void uv_sem_wait(uv_sem_t* sem) {
237
+ int r;
238
+
239
+ do
240
+ r = semaphore_wait(*sem);
241
+ while (r == KERN_ABORTED);
242
+
243
+ if (r != KERN_SUCCESS)
244
+ abort();
245
+ }
246
+
247
+
248
+ int uv_sem_trywait(uv_sem_t* sem) {
249
+ mach_timespec_t interval;
250
+ kern_return_t err;
251
+
252
+ interval.tv_sec = 0;
253
+ interval.tv_nsec = 0;
254
+
255
+ err = semaphore_timedwait(*sem, interval);
256
+ if (err == KERN_SUCCESS)
257
+ return 0;
258
+ if (err == KERN_OPERATION_TIMED_OUT)
259
+ return -EAGAIN;
260
+
261
+ abort();
262
+ return -EINVAL; /* Satisfy the compiler. */
263
+ }
264
+
265
+ #else /* !(defined(__APPLE__) && defined(__MACH__)) */
266
+
267
+ int uv_sem_init(uv_sem_t* sem, unsigned int value) {
268
+ if (sem_init(sem, 0, value))
269
+ return -errno;
270
+ return 0;
271
+ }
272
+
273
+
274
+ void uv_sem_destroy(uv_sem_t* sem) {
275
+ if (sem_destroy(sem))
276
+ abort();
277
+ }
278
+
279
+
280
+ void uv_sem_post(uv_sem_t* sem) {
281
+ if (sem_post(sem))
282
+ abort();
283
+ }
284
+
285
+
286
+ void uv_sem_wait(uv_sem_t* sem) {
287
+ int r;
288
+
289
+ do
290
+ r = sem_wait(sem);
291
+ while (r == -1 && errno == EINTR);
292
+
293
+ if (r)
294
+ abort();
295
+ }
296
+
297
+
298
+ int uv_sem_trywait(uv_sem_t* sem) {
299
+ int r;
300
+
301
+ do
302
+ r = sem_trywait(sem);
303
+ while (r == -1 && errno == EINTR);
304
+
305
+ if (r) {
306
+ if (errno == EAGAIN)
307
+ return -EAGAIN;
308
+ abort();
309
+ }
310
+
311
+ return 0;
312
+ }
313
+
314
+ #endif /* defined(__APPLE__) && defined(__MACH__) */
315
+
316
+
317
+ #if defined(__APPLE__) && defined(__MACH__)
318
+
319
+ int uv_cond_init(uv_cond_t* cond) {
320
+ return -pthread_cond_init(cond, NULL);
321
+ }
322
+
323
+ #else /* !(defined(__APPLE__) && defined(__MACH__)) */
324
+
325
+ int uv_cond_init(uv_cond_t* cond) {
326
+ pthread_condattr_t attr;
327
+ int err;
328
+
329
+ err = pthread_condattr_init(&attr);
330
+ if (err)
331
+ return -err;
332
+
333
+ #if !(defined(__ANDROID__) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
334
+ err = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
335
+ if (err)
336
+ goto error2;
337
+ #endif
338
+
339
+ err = pthread_cond_init(cond, &attr);
340
+ if (err)
341
+ goto error2;
342
+
343
+ err = pthread_condattr_destroy(&attr);
344
+ if (err)
345
+ goto error;
346
+
347
+ return 0;
348
+
349
+ error:
350
+ pthread_cond_destroy(cond);
351
+ error2:
352
+ pthread_condattr_destroy(&attr);
353
+ return -err;
354
+ }
355
+
356
+ #endif /* defined(__APPLE__) && defined(__MACH__) */
357
+
358
+ void uv_cond_destroy(uv_cond_t* cond) {
359
+ if (pthread_cond_destroy(cond))
360
+ abort();
361
+ }
362
+
363
+ void uv_cond_signal(uv_cond_t* cond) {
364
+ if (pthread_cond_signal(cond))
365
+ abort();
366
+ }
367
+
368
+ void uv_cond_broadcast(uv_cond_t* cond) {
369
+ if (pthread_cond_broadcast(cond))
370
+ abort();
371
+ }
372
+
373
+ void uv_cond_wait(uv_cond_t* cond, uv_mutex_t* mutex) {
374
+ if (pthread_cond_wait(cond, mutex))
375
+ abort();
376
+ }
377
+
378
+
379
+ int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex, uint64_t timeout) {
380
+ int r;
381
+ struct timespec ts;
382
+
383
+ #if defined(__APPLE__) && defined(__MACH__)
384
+ ts.tv_sec = timeout / NANOSEC;
385
+ ts.tv_nsec = timeout % NANOSEC;
386
+ r = pthread_cond_timedwait_relative_np(cond, mutex, &ts);
387
+ #else
388
+ timeout += uv__hrtime(UV_CLOCK_PRECISE);
389
+ ts.tv_sec = timeout / NANOSEC;
390
+ ts.tv_nsec = timeout % NANOSEC;
391
+ #if defined(__ANDROID__) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
392
+ /*
393
+ * The bionic pthread implementation doesn't support CLOCK_MONOTONIC,
394
+ * but has this alternative function instead.
395
+ */
396
+ r = pthread_cond_timedwait_monotonic_np(cond, mutex, &ts);
397
+ #else
398
+ r = pthread_cond_timedwait(cond, mutex, &ts);
399
+ #endif /* __ANDROID__ */
400
+ #endif
401
+
402
+
403
+ if (r == 0)
404
+ return 0;
405
+
406
+ if (r == ETIMEDOUT)
407
+ return -ETIMEDOUT;
408
+
409
+ abort();
410
+ return -EINVAL; /* Satisfy the compiler. */
411
+ }
412
+
413
+
414
+ #if defined(__APPLE__) && defined(__MACH__)
415
+
416
+ int uv_barrier_init(uv_barrier_t* barrier, unsigned int count) {
417
+ int err;
418
+
419
+ barrier->n = count;
420
+ barrier->count = 0;
421
+
422
+ err = uv_mutex_init(&barrier->mutex);
423
+ if (err)
424
+ return -err;
425
+
426
+ err = uv_sem_init(&barrier->turnstile1, 0);
427
+ if (err)
428
+ goto error2;
429
+
430
+ err = uv_sem_init(&barrier->turnstile2, 1);
431
+ if (err)
432
+ goto error;
433
+
434
+ return 0;
435
+
436
+ error:
437
+ uv_sem_destroy(&barrier->turnstile1);
438
+ error2:
439
+ uv_mutex_destroy(&barrier->mutex);
440
+ return -err;
441
+
442
+ }
443
+
444
+
445
+ void uv_barrier_destroy(uv_barrier_t* barrier) {
446
+ uv_sem_destroy(&barrier->turnstile2);
447
+ uv_sem_destroy(&barrier->turnstile1);
448
+ uv_mutex_destroy(&barrier->mutex);
449
+ }
450
+
451
+
452
+ int uv_barrier_wait(uv_barrier_t* barrier) {
453
+ int serial_thread;
454
+
455
+ uv_mutex_lock(&barrier->mutex);
456
+ if (++barrier->count == barrier->n) {
457
+ uv_sem_wait(&barrier->turnstile2);
458
+ uv_sem_post(&barrier->turnstile1);
459
+ }
460
+ uv_mutex_unlock(&barrier->mutex);
461
+
462
+ uv_sem_wait(&barrier->turnstile1);
463
+ uv_sem_post(&barrier->turnstile1);
464
+
465
+ uv_mutex_lock(&barrier->mutex);
466
+ serial_thread = (--barrier->count == 0);
467
+ if (serial_thread) {
468
+ uv_sem_wait(&barrier->turnstile1);
469
+ uv_sem_post(&barrier->turnstile2);
470
+ }
471
+ uv_mutex_unlock(&barrier->mutex);
472
+
473
+ uv_sem_wait(&barrier->turnstile2);
474
+ uv_sem_post(&barrier->turnstile2);
475
+ return serial_thread;
476
+ }
477
+
478
+ #else /* !(defined(__APPLE__) && defined(__MACH__)) */
479
+
480
+ int uv_barrier_init(uv_barrier_t* barrier, unsigned int count) {
481
+ return -pthread_barrier_init(barrier, NULL, count);
482
+ }
483
+
484
+
485
+ void uv_barrier_destroy(uv_barrier_t* barrier) {
486
+ if (pthread_barrier_destroy(barrier))
487
+ abort();
488
+ }
489
+
490
+
491
+ int uv_barrier_wait(uv_barrier_t* barrier) {
492
+ int r = pthread_barrier_wait(barrier);
493
+ if (r && r != PTHREAD_BARRIER_SERIAL_THREAD)
494
+ abort();
495
+ return r == PTHREAD_BARRIER_SERIAL_THREAD;
496
+ }
497
+
498
+ #endif /* defined(__APPLE__) && defined(__MACH__) */
499
+
500
+ int uv_key_create(uv_key_t* key) {
501
+ return -pthread_key_create(key, NULL);
502
+ }
503
+
504
+
505
+ void uv_key_delete(uv_key_t* key) {
506
+ if (pthread_key_delete(*key))
507
+ abort();
508
+ }
509
+
510
+
511
+ void* uv_key_get(uv_key_t* key) {
512
+ return pthread_getspecific(*key);
513
+ }
514
+
515
+
516
+ void uv_key_set(uv_key_t* key, void* value) {
517
+ if (pthread_setspecific(*key, value))
518
+ abort();
519
+ }