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 "uv_log.h"
25 #include "internal.h"
26 #include "heap-inl.h"
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
uv_loop_init(uv_loop_t * loop)31 int uv_loop_init(uv_loop_t* loop) {
32 uv__loop_internal_fields_t* lfields;
33 void* saved_data;
34 int err;
35
36 UV_LOGI("init:%{public}zu", (size_t)loop % UV_ADDR_MOD);
37 saved_data = loop->data;
38 memset(loop, 0, sizeof(*loop));
39 loop->data = saved_data;
40
41 lfields = (uv__loop_internal_fields_t*) uv__calloc(1, sizeof(*lfields));
42 if (lfields == NULL)
43 return UV_ENOMEM;
44 loop->internal_fields = lfields;
45
46 err = uv_mutex_init(&lfields->loop_metrics.lock);
47 if (err)
48 goto fail_metrics_mutex_init;
49 memset(&lfields->loop_metrics.metrics,
50 0,
51 sizeof(lfields->loop_metrics.metrics));
52
53 heap_init((struct heap*) &loop->timer_heap);
54 uv__queue_init(&loop->wq);
55 #ifdef USE_FFRT
56 uv__loop_internal_fields_t* lfields_qos = uv__get_internal_fields(loop);
57 uv__queue_init(&(lfields_qos->wq_sub[uv_qos_background]));
58 uv__queue_init(&(lfields_qos->wq_sub[uv_qos_utility]));
59 uv__queue_init(&(lfields_qos->wq_sub[uv_qos_default]));
60 uv__queue_init(&(lfields_qos->wq_sub[uv_qos_user_initiated]));
61 uv__queue_init(&(lfields_qos->wq_sub[uv_qos_reserved]));
62 uv__queue_init(&(lfields_qos->wq_sub[uv_qos_user_interactive]));
63 #endif
64
65 uv__queue_init(&loop->idle_handles);
66 uv__queue_init(&loop->async_handles);
67 uv__queue_init(&loop->check_handles);
68 uv__queue_init(&loop->prepare_handles);
69 uv__queue_init(&loop->handle_queue);
70
71 loop->active_handles = 0;
72 #if defined(USE_OHOS_DFX) && defined(__aarch64__)
73 uv__init_thread_id(loop);
74 #endif
75 #ifdef __aarch64__
76 uv__loop_internal_fields_t* lfields_flag = uv__get_internal_fields(loop);
77 lfields_flag->register_flag = 0;
78 #endif
79 loop->active_reqs.count = 0;
80 loop->nfds = 0;
81 loop->watchers = NULL;
82 loop->nwatchers = 0;
83 uv__queue_init(&loop->pending_queue);
84 uv__queue_init(&loop->watcher_queue);
85
86 loop->closing_handles = NULL;
87 uv__update_time(loop);
88 loop->async_io_watcher.fd = -1;
89 loop->async_wfd = -1;
90 loop->signal_pipefd[0] = -1;
91 loop->signal_pipefd[1] = -1;
92 loop->backend_fd = -1;
93 loop->emfile_fd = -1;
94
95 loop->timer_counter = 0;
96 loop->stop_flag = 0;
97
98 err = uv__platform_loop_init(loop);
99 if (err)
100 goto fail_platform_init;
101
102 uv__signal_global_once_init();
103 err = uv__process_init(loop);
104 if (err)
105 goto fail_signal_init;
106 uv__queue_init(&loop->process_handles);
107
108 err = uv_rwlock_init(&loop->cloexec_lock);
109 if (err)
110 goto fail_rwlock_init;
111
112 err = uv_mutex_init(&loop->wq_mutex);
113 if (err)
114 goto fail_mutex_init;
115
116 err = uv_async_init(loop, &loop->wq_async, uv__work_done);
117 if (err)
118 goto fail_async_init;
119
120 uv__handle_unref(&loop->wq_async);
121 loop->wq_async.flags |= UV_HANDLE_INTERNAL;
122
123 #ifdef USE_FFRT
124 loop->magic = UV_LOOP_MAGIC;
125 #endif
126 return 0;
127
128 fail_async_init:
129 uv_mutex_destroy(&loop->wq_mutex);
130
131 fail_mutex_init:
132 uv_rwlock_destroy(&loop->cloexec_lock);
133
134 fail_rwlock_init:
135 uv__signal_loop_cleanup(loop);
136
137 fail_signal_init:
138 uv__platform_loop_delete(loop);
139
140 fail_platform_init:
141 uv_mutex_destroy(&lfields->loop_metrics.lock);
142
143 fail_metrics_mutex_init:
144 uv__free(lfields);
145 loop->internal_fields = NULL;
146
147 uv__free(loop->watchers);
148 loop->nwatchers = 0;
149 return err;
150 }
151
152
uv_loop_fork(uv_loop_t * loop)153 int uv_loop_fork(uv_loop_t* loop) {
154 int err;
155 unsigned int i;
156 uv__io_t* w;
157
158 err = uv__io_fork(loop);
159 if (err)
160 return err;
161
162 err = uv__async_fork(loop);
163 if (err)
164 return err;
165
166 err = uv__signal_loop_fork(loop);
167 if (err)
168 return err;
169
170 /* Rearm all the watchers that aren't re-queued by the above. */
171 for (i = 0; i < loop->nwatchers; i++) {
172 w = loop->watchers[i];
173 if (w == NULL)
174 continue;
175
176 if (w->pevents != 0 && uv__queue_empty(&w->watcher_queue)) {
177 w->events = 0; /* Force re-registration in uv__io_poll. */
178 uv__queue_insert_tail(&loop->watcher_queue, &w->watcher_queue);
179 }
180 }
181
182 return 0;
183 }
184
185
uv__loop_close(uv_loop_t * loop)186 void uv__loop_close(uv_loop_t* loop) {
187 uv__loop_internal_fields_t* lfields;
188
189 uv__signal_loop_cleanup(loop);
190 uv__platform_loop_delete(loop);
191 uv__async_stop(loop);
192
193 if (loop->emfile_fd != -1) {
194 uv__close(loop->emfile_fd);
195 loop->emfile_fd = -1;
196 }
197
198 if (loop->backend_fd != -1) {
199 #ifdef USE_OHOS_DFX
200 fdsan_close_with_tag(loop->backend_fd, uv__get_addr_tag((void *)&loop->backend_fd));
201 #else
202 uv__close(loop->backend_fd);
203 #endif
204 UV_LOGI("close:%{public}zu, backend_fd:%{public}d", (size_t)loop % UV_ADDR_MOD, loop->backend_fd);
205 loop->backend_fd = -1;
206 }
207
208 uv_mutex_lock(&loop->wq_mutex);
209 #ifndef USE_FFRT
210 assert(uv__queue_empty(&loop->wq) && "thread pool work queue not empty!");
211 #endif
212 assert(!uv__has_active_reqs(loop));
213 uv_mutex_unlock(&loop->wq_mutex);
214 uv_mutex_destroy(&loop->wq_mutex);
215
216 /*
217 * Note that all thread pool stuff is finished at this point and
218 * it is safe to just destroy rw lock
219 */
220 uv_rwlock_destroy(&loop->cloexec_lock);
221
222 #if 0
223 assert(uv__queue_empty(&loop->pending_queue));
224 assert(uv__queue_empty(&loop->watcher_queue));
225 assert(loop->nfds == 0);
226 #endif
227
228 uv__free(loop->watchers);
229 loop->watchers = NULL;
230 loop->nwatchers = 0;
231
232 lfields = uv__get_internal_fields(loop);
233 uv_mutex_destroy(&lfields->loop_metrics.lock);
234 uv__free(lfields);
235 loop->internal_fields = NULL;
236 #ifdef USE_FFRT
237 loop->magic = ~UV_LOOP_MAGIC;
238 #endif
239 }
240
241
uv__loop_configure(uv_loop_t * loop,uv_loop_option option,va_list ap)242 int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap) {
243 uv__loop_internal_fields_t* lfields;
244
245 lfields = uv__get_internal_fields(loop);
246 if (option == UV_METRICS_IDLE_TIME) {
247 lfields->flags |= UV_METRICS_IDLE_TIME;
248 return 0;
249 }
250
251 if (option != UV_LOOP_BLOCK_SIGNAL)
252 return UV_ENOSYS;
253
254 if (va_arg(ap, int) != SIGPROF)
255 return UV_EINVAL;
256
257 loop->flags |= UV_LOOP_BLOCK_SIGPROF;
258 return 0;
259 }
260