2012-04-04 12:52:01 +00:00
|
|
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "uv.h"
|
|
|
|
|
#include "tree.h"
|
|
|
|
|
#include "internal.h"
|
2013-09-12 08:26:24 +00:00
|
|
|
#include "heap-inl.h"
|
2012-04-04 12:52:01 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2012-04-05 23:24:20 +00:00
|
|
|
#include <unistd.h>
|
2012-04-04 12:52:01 +00:00
|
|
|
|
2014-02-17 21:37:20 +00:00
|
|
|
int uv_loop_init(uv_loop_t* loop) {
|
2016-07-20 12:18:50 +00:00
|
|
|
void* saved_data;
|
2013-06-06 21:10:50 +00:00
|
|
|
int err;
|
2012-08-08 12:42:34 +00:00
|
|
|
|
2012-10-16 15:42:30 +00:00
|
|
|
uv__signal_global_once_init();
|
|
|
|
|
|
2016-07-20 12:18:50 +00:00
|
|
|
saved_data = loop->data;
|
2012-05-04 13:01:22 +00:00
|
|
|
memset(loop, 0, sizeof(*loop));
|
2016-07-20 12:18:50 +00:00
|
|
|
loop->data = saved_data;
|
|
|
|
|
|
2013-09-12 08:26:24 +00:00
|
|
|
heap_init((struct heap*) &loop->timer_heap);
|
2013-03-26 20:11:29 +00:00
|
|
|
QUEUE_INIT(&loop->wq);
|
|
|
|
|
QUEUE_INIT(&loop->active_reqs);
|
|
|
|
|
QUEUE_INIT(&loop->idle_handles);
|
|
|
|
|
QUEUE_INIT(&loop->async_handles);
|
|
|
|
|
QUEUE_INIT(&loop->check_handles);
|
|
|
|
|
QUEUE_INIT(&loop->prepare_handles);
|
|
|
|
|
QUEUE_INIT(&loop->handle_queue);
|
2012-08-22 23:14:41 +00:00
|
|
|
|
|
|
|
|
loop->nfds = 0;
|
|
|
|
|
loop->watchers = NULL;
|
|
|
|
|
loop->nwatchers = 0;
|
2013-03-26 20:11:29 +00:00
|
|
|
QUEUE_INIT(&loop->pending_queue);
|
|
|
|
|
QUEUE_INIT(&loop->watcher_queue);
|
2012-08-22 23:14:41 +00:00
|
|
|
|
2012-05-29 22:08:22 +00:00
|
|
|
loop->closing_handles = NULL;
|
2013-10-30 22:59:05 +00:00
|
|
|
uv__update_time(loop);
|
2017-03-14 09:01:35 +00:00
|
|
|
loop->async_io_watcher.fd = -1;
|
|
|
|
|
loop->async_wfd = -1;
|
2012-10-16 15:42:30 +00:00
|
|
|
loop->signal_pipefd[0] = -1;
|
|
|
|
|
loop->signal_pipefd[1] = -1;
|
2012-08-22 23:14:41 +00:00
|
|
|
loop->backend_fd = -1;
|
2012-09-10 14:20:56 +00:00
|
|
|
loop->emfile_fd = -1;
|
2012-08-22 23:14:41 +00:00
|
|
|
|
2013-02-07 04:59:17 +00:00
|
|
|
loop->timer_counter = 0;
|
2013-01-16 08:36:20 +00:00
|
|
|
loop->stop_flag = 0;
|
2013-02-07 04:59:17 +00:00
|
|
|
|
2015-01-20 10:00:00 +00:00
|
|
|
err = uv__platform_loop_init(loop);
|
2013-06-06 21:10:50 +00:00
|
|
|
if (err)
|
|
|
|
|
return err;
|
2012-05-17 04:28:46 +00:00
|
|
|
|
2015-06-20 00:51:42 +00:00
|
|
|
err = uv_signal_init(loop, &loop->child_watcher);
|
|
|
|
|
if (err)
|
|
|
|
|
goto fail_signal_init;
|
|
|
|
|
|
2012-08-08 12:42:34 +00:00
|
|
|
uv__handle_unref(&loop->child_watcher);
|
|
|
|
|
loop->child_watcher.flags |= UV__HANDLE_INTERNAL;
|
2014-05-05 20:15:15 +00:00
|
|
|
QUEUE_INIT(&loop->process_handles);
|
2012-08-08 12:42:34 +00:00
|
|
|
|
2015-06-20 00:51:42 +00:00
|
|
|
err = uv_rwlock_init(&loop->cloexec_lock);
|
|
|
|
|
if (err)
|
|
|
|
|
goto fail_rwlock_init;
|
2014-01-29 21:30:23 +00:00
|
|
|
|
2015-06-20 00:51:42 +00:00
|
|
|
err = uv_mutex_init(&loop->wq_mutex);
|
|
|
|
|
if (err)
|
|
|
|
|
goto fail_mutex_init;
|
2012-09-30 22:38:39 +00:00
|
|
|
|
2015-06-20 00:51:42 +00:00
|
|
|
err = uv_async_init(loop, &loop->wq_async, uv__work_done);
|
|
|
|
|
if (err)
|
|
|
|
|
goto fail_async_init;
|
2012-09-30 22:38:39 +00:00
|
|
|
|
|
|
|
|
uv__handle_unref(&loop->wq_async);
|
|
|
|
|
loop->wq_async.flags |= UV__HANDLE_INTERNAL;
|
|
|
|
|
|
2012-04-04 12:52:01 +00:00
|
|
|
return 0;
|
2015-06-20 00:51:42 +00:00
|
|
|
|
|
|
|
|
fail_async_init:
|
|
|
|
|
uv_mutex_destroy(&loop->wq_mutex);
|
|
|
|
|
|
|
|
|
|
fail_mutex_init:
|
|
|
|
|
uv_rwlock_destroy(&loop->cloexec_lock);
|
|
|
|
|
|
|
|
|
|
fail_rwlock_init:
|
|
|
|
|
uv__signal_loop_cleanup(loop);
|
|
|
|
|
|
|
|
|
|
fail_signal_init:
|
|
|
|
|
uv__platform_loop_delete(loop);
|
|
|
|
|
|
|
|
|
|
return err;
|
2012-04-04 12:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-01-20 10:00:00 +00:00
|
|
|
void uv__loop_close(uv_loop_t* loop) {
|
2012-10-16 15:42:30 +00:00
|
|
|
uv__signal_loop_cleanup(loop);
|
2012-08-17 12:48:51 +00:00
|
|
|
uv__platform_loop_delete(loop);
|
2017-03-14 09:01:35 +00:00
|
|
|
uv__async_stop(loop);
|
2012-09-28 05:01:59 +00:00
|
|
|
|
2012-09-10 14:20:56 +00:00
|
|
|
if (loop->emfile_fd != -1) {
|
2013-10-01 00:37:45 +00:00
|
|
|
uv__close(loop->emfile_fd);
|
2012-09-10 14:20:56 +00:00
|
|
|
loop->emfile_fd = -1;
|
|
|
|
|
}
|
2012-09-30 22:38:39 +00:00
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
if (loop->backend_fd != -1) {
|
2013-10-01 00:37:45 +00:00
|
|
|
uv__close(loop->backend_fd);
|
2012-08-22 23:14:41 +00:00
|
|
|
loop->backend_fd = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-30 22:38:39 +00:00
|
|
|
uv_mutex_lock(&loop->wq_mutex);
|
2013-03-26 20:11:29 +00:00
|
|
|
assert(QUEUE_EMPTY(&loop->wq) && "thread pool work queue not empty!");
|
2013-08-27 12:26:41 +00:00
|
|
|
assert(!uv__has_active_reqs(loop));
|
2012-09-30 22:38:39 +00:00
|
|
|
uv_mutex_unlock(&loop->wq_mutex);
|
|
|
|
|
uv_mutex_destroy(&loop->wq_mutex);
|
2012-08-22 23:14:41 +00:00
|
|
|
|
2014-01-29 21:30:23 +00:00
|
|
|
/*
|
|
|
|
|
* Note that all thread pool stuff is finished at this point and
|
|
|
|
|
* it is safe to just destroy rw lock
|
|
|
|
|
*/
|
|
|
|
|
uv_rwlock_destroy(&loop->cloexec_lock);
|
|
|
|
|
|
2012-08-22 23:14:41 +00:00
|
|
|
#if 0
|
2013-03-26 20:11:29 +00:00
|
|
|
assert(QUEUE_EMPTY(&loop->pending_queue));
|
|
|
|
|
assert(QUEUE_EMPTY(&loop->watcher_queue));
|
2012-08-22 23:14:41 +00:00
|
|
|
assert(loop->nfds == 0);
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-05-26 08:51:10 +00:00
|
|
|
uv__free(loop->watchers);
|
2012-08-22 23:14:41 +00:00
|
|
|
loop->watchers = NULL;
|
|
|
|
|
loop->nwatchers = 0;
|
2012-04-04 12:52:01 +00:00
|
|
|
}
|
2014-11-27 07:43:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
int uv__loop_configure(uv_loop_t* loop, uv_loop_option option, va_list ap) {
|
|
|
|
|
if (option != UV_LOOP_BLOCK_SIGNAL)
|
|
|
|
|
return UV_ENOSYS;
|
|
|
|
|
|
|
|
|
|
if (va_arg(ap, int) != SIGPROF)
|
|
|
|
|
return UV_EINVAL;
|
|
|
|
|
|
|
|
|
|
loop->flags |= UV_LOOP_BLOCK_SIGPROF;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|