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