2019-12-02 11:19:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2019-12-02 11:19:55 +01:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2019-12-02 11:19:55 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2022-04-27 17:41:47 +02:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
2019-12-02 11:19:55 +01:00
|
|
|
#include <uv.h>
|
|
|
|
|
2022-04-27 17:41:47 +02:00
|
|
|
#include <isc/result.h>
|
New event loop handling API
This commit introduces new APIs for applications and signal handling,
intended to replace isc_app for applications built on top of libisc.
* isc_app will be replaced with isc_loopmgr, which handles the
starting and stopping of applications. In isc_loopmgr, the main
thread is not blocked, but is part of the working thread set.
The loop manager will start a number of threads, each with a
uv_loop event loop running. Setup and teardown functions can be
assigned which will run when the loop starts and stops, and
jobs can be scheduled to run in the meantime. When
isc_loopmgr_shutdown() is run from any the loops, all loops
will shut down and the application can terminate.
* signal handling will now be handled with a separate isc_signal unit.
isc_loopmgr only handles SIGTERM and SIGINT for application
termination, but the application may install additional signal
handlers, such as SIGHUP as a signal to reload configuration.
* new job running primitives, isc_job and isc_async, have been added.
Both units schedule callbacks (specifying a callback function and
argument) on an event loop. The difference is that isc_job unit is
unlocked and not thread-safe, so it can be used to efficiently
run jobs in the same thread, while isc_async is thread-safe and
uses locking, so it can be used to pass jobs from one thread to
another.
* isc_tid will be used to track the thread ID in isc_loop worker
threads.
* unit tests have been added for the new APIs.
2022-07-26 13:03:22 +02:00
|
|
|
#include <isc/tid.h>
|
2022-04-27 17:41:47 +02:00
|
|
|
|
2021-05-27 13:38:21 +02:00
|
|
|
#define UV_VERSION(major, minor, patch) ((major << 16) | (minor << 8) | (patch))
|
|
|
|
|
2022-04-25 14:43:14 +02:00
|
|
|
/*
|
|
|
|
* Copied verbatim from libuv/src/version.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define UV_STRINGIFY(v) UV_STRINGIFY_HELPER(v)
|
|
|
|
#define UV_STRINGIFY_HELPER(v) #v
|
|
|
|
|
|
|
|
#define UV_VERSION_STRING_BASE \
|
|
|
|
UV_STRINGIFY(UV_VERSION_MAJOR) \
|
|
|
|
"." UV_STRINGIFY(UV_VERSION_MINOR) "." UV_STRINGIFY(UV_VERSION_PATCH)
|
|
|
|
|
|
|
|
#if UV_VERSION_IS_RELEASE
|
|
|
|
#define UV_VERSION_STRING UV_VERSION_STRING_BASE
|
|
|
|
#else
|
|
|
|
#define UV_VERSION_STRING UV_VERSION_STRING_BASE "-" UV_VERSION_SUFFIX
|
|
|
|
#endif
|
|
|
|
|
2022-02-09 12:45:37 +01:00
|
|
|
#if !defined(UV__ERR)
|
|
|
|
#define UV__ERR(x) (-(x))
|
|
|
|
#endif
|
|
|
|
|
2022-04-27 17:41:47 +02:00
|
|
|
/*
|
|
|
|
* These are used with all versions of libuv:
|
|
|
|
*/
|
2021-05-27 12:34:06 +02:00
|
|
|
|
2022-04-27 17:41:47 +02:00
|
|
|
#define UV_RUNTIME_CHECK(func, ret) \
|
|
|
|
if (ret != 0) { \
|
|
|
|
isc_error_fatal(__FILE__, __LINE__, "%s failed: %s\n", #func, \
|
|
|
|
uv_strerror(ret)); \
|
2021-05-27 12:34:06 +02:00
|
|
|
}
|
|
|
|
|
2022-04-27 17:41:47 +02:00
|
|
|
#define isc_uverr2result(x) \
|
|
|
|
isc__uverr2result(x, true, __FILE__, __LINE__, __func__)
|
|
|
|
isc_result_t
|
|
|
|
isc__uverr2result(int uverr, bool dolog, const char *file, unsigned int line,
|
|
|
|
const char *func);
|
|
|
|
/*%<
|
|
|
|
* Convert a libuv error value into an isc_result_t. The
|
|
|
|
* list of supported error values is not complete; new users
|
|
|
|
* of this function should add any expected errors that are
|
|
|
|
* not already there.
|
|
|
|
*/
|
2021-05-27 12:34:06 +02:00
|
|
|
|
2022-04-27 17:41:47 +02:00
|
|
|
/**
|
|
|
|
* Type-casting helpers
|
|
|
|
*/
|
2021-05-27 12:34:06 +02:00
|
|
|
|
2022-04-27 17:41:47 +02:00
|
|
|
#define uv_handle_set_data(handle, data) \
|
|
|
|
uv_handle_set_data((uv_handle_t *)(handle), (data))
|
|
|
|
#define uv_handle_get_data(handle) uv_handle_get_data((uv_handle_t *)(handle))
|
|
|
|
#define uv_close(handle, close_cb) uv_close((uv_handle_t *)handle, close_cb)
|
New event loop handling API
This commit introduces new APIs for applications and signal handling,
intended to replace isc_app for applications built on top of libisc.
* isc_app will be replaced with isc_loopmgr, which handles the
starting and stopping of applications. In isc_loopmgr, the main
thread is not blocked, but is part of the working thread set.
The loop manager will start a number of threads, each with a
uv_loop event loop running. Setup and teardown functions can be
assigned which will run when the loop starts and stops, and
jobs can be scheduled to run in the meantime. When
isc_loopmgr_shutdown() is run from any the loops, all loops
will shut down and the application can terminate.
* signal handling will now be handled with a separate isc_signal unit.
isc_loopmgr only handles SIGTERM and SIGINT for application
termination, but the application may install additional signal
handlers, such as SIGHUP as a signal to reload configuration.
* new job running primitives, isc_job and isc_async, have been added.
Both units schedule callbacks (specifying a callback function and
argument) on an event loop. The difference is that isc_job unit is
unlocked and not thread-safe, so it can be used to efficiently
run jobs in the same thread, while isc_async is thread-safe and
uses locking, so it can be used to pass jobs from one thread to
another.
* isc_tid will be used to track the thread ID in isc_loop worker
threads.
* unit tests have been added for the new APIs.
2022-07-26 13:03:22 +02:00
|
|
|
|
|
|
|
#if UV_TRACE_INIT
|
|
|
|
|
|
|
|
#define uv_idle_init(loop, idle) \
|
|
|
|
({ \
|
|
|
|
int __r = uv_idle_init(loop, idle); \
|
|
|
|
fprintf(stderr, "%" PRIu32 ":%s_:uv_idle_init(%p, %p)\n", \
|
|
|
|
isc_tid(), __func__, loop, idle); \
|
|
|
|
__r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define uv_timer_init(loop, timer) \
|
|
|
|
({ \
|
|
|
|
int __r = uv_timer_init(loop, timer); \
|
|
|
|
fprintf(stderr, "%" PRIu32 ":%s_:uv_timer_init(%p, %p)\n", \
|
|
|
|
isc_tid(), __func__, loop, timer); \
|
|
|
|
__r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define uv_async_init(loop, async, async_cb) \
|
|
|
|
({ \
|
|
|
|
int __r = uv_async_init(loop, async, async_cb); \
|
|
|
|
fprintf(stderr, "%" PRIu32 ":%s_:uv_timer_init(%p, %p, %p)\n", \
|
|
|
|
isc_tid(), __func__, loop, async, async_cb); \
|
|
|
|
__r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define uv_close(handle, close_cb) \
|
|
|
|
({ \
|
|
|
|
uv_close(handle, close_cb); \
|
|
|
|
fprintf(stderr, "%" PRIu32 ":%s_:uv_close(%p, %p)\n", \
|
|
|
|
isc_tid(), __func__, handle, close_cb); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#endif
|