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