Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmlibuv/src/unix/loop.c
3156 views
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 "uv/tree.h"
24
#include "internal.h"
25
#include "heap-inl.h"
26
#include <stdlib.h>
27
#include <string.h>
28
#include <unistd.h>
29
30
int uv_loop_init(uv_loop_t* loop) {
31
uv__loop_internal_fields_t* lfields;
32
void* saved_data;
33
int err;
34
35
36
saved_data = loop->data;
37
memset(loop, 0, sizeof(*loop));
38
loop->data = saved_data;
39
40
lfields = (uv__loop_internal_fields_t*) uv__calloc(1, sizeof(*lfields));
41
if (lfields == NULL)
42
return UV_ENOMEM;
43
loop->internal_fields = lfields;
44
45
err = uv_mutex_init(&lfields->loop_metrics.lock);
46
if (err)
47
goto fail_metrics_mutex_init;
48
49
heap_init((struct heap*) &loop->timer_heap);
50
QUEUE_INIT(&loop->wq);
51
QUEUE_INIT(&loop->idle_handles);
52
QUEUE_INIT(&loop->async_handles);
53
QUEUE_INIT(&loop->check_handles);
54
QUEUE_INIT(&loop->prepare_handles);
55
QUEUE_INIT(&loop->handle_queue);
56
57
loop->active_handles = 0;
58
loop->active_reqs.count = 0;
59
loop->nfds = 0;
60
loop->watchers = NULL;
61
loop->nwatchers = 0;
62
QUEUE_INIT(&loop->pending_queue);
63
QUEUE_INIT(&loop->watcher_queue);
64
65
loop->closing_handles = NULL;
66
uv__update_time(loop);
67
loop->async_io_watcher.fd = -1;
68
loop->async_wfd = -1;
69
loop->signal_pipefd[0] = -1;
70
loop->signal_pipefd[1] = -1;
71
loop->backend_fd = -1;
72
loop->emfile_fd = -1;
73
74
loop->timer_counter = 0;
75
loop->stop_flag = 0;
76
77
err = uv__platform_loop_init(loop);
78
if (err)
79
goto fail_platform_init;
80
81
uv__signal_global_once_init();
82
err = uv__process_init(loop);
83
if (err)
84
goto fail_signal_init;
85
QUEUE_INIT(&loop->process_handles);
86
87
err = uv_rwlock_init(&loop->cloexec_lock);
88
if (err)
89
goto fail_rwlock_init;
90
91
err = uv_mutex_init(&loop->wq_mutex);
92
if (err)
93
goto fail_mutex_init;
94
95
err = uv_async_init(loop, &loop->wq_async, uv__work_done);
96
if (err)
97
goto fail_async_init;
98
99
uv__handle_unref(&loop->wq_async);
100
loop->wq_async.flags |= UV_HANDLE_INTERNAL;
101
102
return 0;
103
104
fail_async_init:
105
uv_mutex_destroy(&loop->wq_mutex);
106
107
fail_mutex_init:
108
uv_rwlock_destroy(&loop->cloexec_lock);
109
110
fail_rwlock_init:
111
uv__signal_loop_cleanup(loop);
112
113
fail_signal_init:
114
uv__platform_loop_delete(loop);
115
116
fail_platform_init:
117
uv_mutex_destroy(&lfields->loop_metrics.lock);
118
119
fail_metrics_mutex_init:
120
uv__free(lfields);
121
loop->internal_fields = NULL;
122
123
uv__free(loop->watchers);
124
loop->nwatchers = 0;
125
return err;
126
}
127
128
129
int uv_loop_fork(uv_loop_t* loop) {
130
int err;
131
unsigned int i;
132
uv__io_t* w;
133
134
err = uv__io_fork(loop);
135
if (err)
136
return err;
137
138
err = uv__async_fork(loop);
139
if (err)
140
return err;
141
142
err = uv__signal_loop_fork(loop);
143
if (err)
144
return err;
145
146
/* Rearm all the watchers that aren't re-queued by the above. */
147
for (i = 0; i < loop->nwatchers; i++) {
148
w = loop->watchers[i];
149
if (w == NULL)
150
continue;
151
152
if (w->pevents != 0 && QUEUE_EMPTY(&w->watcher_queue)) {
153
w->events = 0; /* Force re-registration in uv__io_poll. */
154
QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue);
155
}
156
}
157
158
return 0;
159
}
160
161
162
void uv__loop_close(uv_loop_t* loop) {
163
uv__loop_internal_fields_t* lfields;
164
165
uv__signal_loop_cleanup(loop);
166
uv__platform_loop_delete(loop);
167
uv__async_stop(loop);
168
169
if (loop->emfile_fd != -1) {
170
uv__close(loop->emfile_fd);
171
loop->emfile_fd = -1;
172
}
173
174
if (loop->backend_fd != -1) {
175
uv__close(loop->backend_fd);
176
loop->backend_fd = -1;
177
}
178
179
uv_mutex_lock(&loop->wq_mutex);
180
assert(QUEUE_EMPTY(&loop->wq) && "thread pool work queue not empty!");
181
assert(!uv__has_active_reqs(loop));
182
uv_mutex_unlock(&loop->wq_mutex);
183
uv_mutex_destroy(&loop->wq_mutex);
184
185
/*
186
* Note that all thread pool stuff is finished at this point and
187
* it is safe to just destroy rw lock
188
*/
189
uv_rwlock_destroy(&loop->cloexec_lock);
190
191
#if 0
192
assert(QUEUE_EMPTY(&loop->pending_queue));
193
assert(QUEUE_EMPTY(&loop->watcher_queue));
194
assert(loop->nfds == 0);
195
#endif
196
197
uv__free(loop->watchers);
198
loop->watchers = NULL;
199
loop->nwatchers = 0;
200
201
lfields = uv__get_internal_fields(loop);
202
uv_mutex_destroy(&lfields->loop_metrics.lock);
203
uv__free(lfields);
204
loop->internal_fields = NULL;
205
}
206
207
208
int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap) {
209
uv__loop_internal_fields_t* lfields;
210
211
lfields = uv__get_internal_fields(loop);
212
if (option == UV_METRICS_IDLE_TIME) {
213
lfields->flags |= UV_METRICS_IDLE_TIME;
214
return 0;
215
}
216
217
if (option != UV_LOOP_BLOCK_SIGNAL)
218
return UV_ENOSYS;
219
220
if (va_arg(ap, int) != SIGPROF)
221
return UV_EINVAL;
222
223
loop->flags |= UV_LOOP_BLOCK_SIGPROF;
224
return 0;
225
}
226
227