11cb0ef41Sopenharmony_ci/* MIT License 21cb0ef41Sopenharmony_ci * 31cb0ef41Sopenharmony_ci * Copyright (c) 2024 Brad House 41cb0ef41Sopenharmony_ci * 51cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 61cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal 71cb0ef41Sopenharmony_ci * in the Software without restriction, including without limitation the rights 81cb0ef41Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 91cb0ef41Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is 101cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions: 111cb0ef41Sopenharmony_ci * 121cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice (including the next 131cb0ef41Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 141cb0ef41Sopenharmony_ci * Software. 151cb0ef41Sopenharmony_ci * 161cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 171cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 181cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 191cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 201cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 211cb0ef41Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 221cb0ef41Sopenharmony_ci * SOFTWARE. 231cb0ef41Sopenharmony_ci * 241cb0ef41Sopenharmony_ci * SPDX-License-Identifier: MIT 251cb0ef41Sopenharmony_ci */ 261cb0ef41Sopenharmony_ci#include "ares_setup.h" 271cb0ef41Sopenharmony_ci#include "ares.h" 281cb0ef41Sopenharmony_ci#include "ares_private.h" 291cb0ef41Sopenharmony_ci#include "ares_event.h" 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cistatic void ares_event_destroy_cb(void *arg) 321cb0ef41Sopenharmony_ci{ 331cb0ef41Sopenharmony_ci ares_event_t *event = arg; 341cb0ef41Sopenharmony_ci if (event == NULL) { 351cb0ef41Sopenharmony_ci return; 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci /* Unregister from the event thread if it was registered with one */ 391cb0ef41Sopenharmony_ci if (event->e) { 401cb0ef41Sopenharmony_ci const ares_event_thread_t *e = event->e; 411cb0ef41Sopenharmony_ci e->ev_sys->event_del(event); 421cb0ef41Sopenharmony_ci event->e = NULL; 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci if (event->free_data_cb && event->data) { 461cb0ef41Sopenharmony_ci event->free_data_cb(event->data); 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci ares_free(event); 501cb0ef41Sopenharmony_ci} 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci/* See if a pending update already exists. We don't want to enqueue multiple 531cb0ef41Sopenharmony_ci * updates for the same event handle. Right now this is O(n) based on number 541cb0ef41Sopenharmony_ci * of updates already enqueued. In the future, it might make sense to make 551cb0ef41Sopenharmony_ci * this O(1) with a hashtable. 561cb0ef41Sopenharmony_ci * NOTE: in some cases a delete then re-add of the same fd, but really pointing 571cb0ef41Sopenharmony_ci * to a different destination can happen due to a quick close of a 581cb0ef41Sopenharmony_ci * connection then creation of a new one. So we need to look at the 591cb0ef41Sopenharmony_ci * flags and ignore any delete events when finding a match since we 601cb0ef41Sopenharmony_ci * need to process the delete always, it can't be combined with other 611cb0ef41Sopenharmony_ci * updates. */ 621cb0ef41Sopenharmony_cistatic ares_event_t *ares_event_update_find(ares_event_thread_t *e, 631cb0ef41Sopenharmony_ci ares_socket_t fd, const void *data) 641cb0ef41Sopenharmony_ci{ 651cb0ef41Sopenharmony_ci ares__llist_node_t *node; 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci for (node = ares__llist_node_first(e->ev_updates); node != NULL; 681cb0ef41Sopenharmony_ci node = ares__llist_node_next(node)) { 691cb0ef41Sopenharmony_ci ares_event_t *ev = ares__llist_node_val(node); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci if (fd != ARES_SOCKET_BAD && fd == ev->fd && ev->flags != 0) { 721cb0ef41Sopenharmony_ci return ev; 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci if (fd == ARES_SOCKET_BAD && ev->fd == ARES_SOCKET_BAD && 761cb0ef41Sopenharmony_ci data == ev->data && ev->flags != 0) { 771cb0ef41Sopenharmony_ci return ev; 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci return NULL; 821cb0ef41Sopenharmony_ci} 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ciares_status_t ares_event_update(ares_event_t **event, ares_event_thread_t *e, 851cb0ef41Sopenharmony_ci ares_event_flags_t flags, ares_event_cb_t cb, 861cb0ef41Sopenharmony_ci ares_socket_t fd, void *data, 871cb0ef41Sopenharmony_ci ares_event_free_data_t free_data_cb, 881cb0ef41Sopenharmony_ci ares_event_signal_cb_t signal_cb) 891cb0ef41Sopenharmony_ci{ 901cb0ef41Sopenharmony_ci ares_event_t *ev = NULL; 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci if (e == NULL || cb == NULL) { 931cb0ef41Sopenharmony_ci return ARES_EFORMERR; 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci if (event != NULL) { 971cb0ef41Sopenharmony_ci *event = NULL; 981cb0ef41Sopenharmony_ci } 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci /* Validate flags */ 1011cb0ef41Sopenharmony_ci if (fd == ARES_SOCKET_BAD) { 1021cb0ef41Sopenharmony_ci if (flags & (ARES_EVENT_FLAG_READ | ARES_EVENT_FLAG_WRITE)) { 1031cb0ef41Sopenharmony_ci return ARES_EFORMERR; 1041cb0ef41Sopenharmony_ci } 1051cb0ef41Sopenharmony_ci if (!(flags & ARES_EVENT_FLAG_OTHER)) { 1061cb0ef41Sopenharmony_ci return ARES_EFORMERR; 1071cb0ef41Sopenharmony_ci } 1081cb0ef41Sopenharmony_ci } else { 1091cb0ef41Sopenharmony_ci if (flags & ARES_EVENT_FLAG_OTHER) { 1101cb0ef41Sopenharmony_ci return ARES_EFORMERR; 1111cb0ef41Sopenharmony_ci } 1121cb0ef41Sopenharmony_ci } 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci /* That's all the validation we can really do */ 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci /* See if we have a queued update already */ 1171cb0ef41Sopenharmony_ci ev = ares_event_update_find(e, fd, data); 1181cb0ef41Sopenharmony_ci if (ev == NULL) { 1191cb0ef41Sopenharmony_ci /* Allocate a new one */ 1201cb0ef41Sopenharmony_ci ev = ares_malloc_zero(sizeof(*ev)); 1211cb0ef41Sopenharmony_ci if (ev == NULL) { 1221cb0ef41Sopenharmony_ci return ARES_ENOMEM; 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci if (ares__llist_insert_last(e->ev_updates, ev) == NULL) { 1261cb0ef41Sopenharmony_ci ares_free(ev); 1271cb0ef41Sopenharmony_ci return ARES_ENOMEM; 1281cb0ef41Sopenharmony_ci } 1291cb0ef41Sopenharmony_ci } 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci ev->flags = flags; 1321cb0ef41Sopenharmony_ci ev->fd = fd; 1331cb0ef41Sopenharmony_ci if (ev->cb == NULL) { 1341cb0ef41Sopenharmony_ci ev->cb = cb; 1351cb0ef41Sopenharmony_ci } 1361cb0ef41Sopenharmony_ci if (ev->data == NULL) { 1371cb0ef41Sopenharmony_ci ev->data = data; 1381cb0ef41Sopenharmony_ci } 1391cb0ef41Sopenharmony_ci if (ev->free_data_cb == NULL) { 1401cb0ef41Sopenharmony_ci ev->free_data_cb = free_data_cb; 1411cb0ef41Sopenharmony_ci } 1421cb0ef41Sopenharmony_ci if (ev->signal_cb == NULL) { 1431cb0ef41Sopenharmony_ci ev->signal_cb = signal_cb; 1441cb0ef41Sopenharmony_ci } 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci if (event != NULL) { 1471cb0ef41Sopenharmony_ci *event = ev; 1481cb0ef41Sopenharmony_ci } 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci return ARES_SUCCESS; 1511cb0ef41Sopenharmony_ci} 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_cistatic void ares_event_signal(const ares_event_t *event) 1541cb0ef41Sopenharmony_ci{ 1551cb0ef41Sopenharmony_ci if (event == NULL || event->signal_cb == NULL) { 1561cb0ef41Sopenharmony_ci return; 1571cb0ef41Sopenharmony_ci } 1581cb0ef41Sopenharmony_ci event->signal_cb(event); 1591cb0ef41Sopenharmony_ci} 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_cistatic void ares_event_thread_wake(const ares_event_thread_t *e) 1621cb0ef41Sopenharmony_ci{ 1631cb0ef41Sopenharmony_ci if (e == NULL) { 1641cb0ef41Sopenharmony_ci return; 1651cb0ef41Sopenharmony_ci } 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci ares_event_signal(e->ev_signal); 1681cb0ef41Sopenharmony_ci} 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_cistatic void ares_event_thread_process_fd(ares_event_thread_t *e, 1711cb0ef41Sopenharmony_ci ares_socket_t fd, void *data, 1721cb0ef41Sopenharmony_ci ares_event_flags_t flags) 1731cb0ef41Sopenharmony_ci{ 1741cb0ef41Sopenharmony_ci (void)data; 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci ares_process_fd(e->channel, 1771cb0ef41Sopenharmony_ci (flags & ARES_EVENT_FLAG_READ) ? fd : ARES_SOCKET_BAD, 1781cb0ef41Sopenharmony_ci (flags & ARES_EVENT_FLAG_WRITE) ? fd : ARES_SOCKET_BAD); 1791cb0ef41Sopenharmony_ci} 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_cistatic void ares_event_thread_sockstate_cb(void *data, ares_socket_t socket_fd, 1821cb0ef41Sopenharmony_ci int readable, int writable) 1831cb0ef41Sopenharmony_ci{ 1841cb0ef41Sopenharmony_ci ares_event_thread_t *e = data; 1851cb0ef41Sopenharmony_ci ares_event_flags_t flags = ARES_EVENT_FLAG_NONE; 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci if (readable) { 1881cb0ef41Sopenharmony_ci flags |= ARES_EVENT_FLAG_READ; 1891cb0ef41Sopenharmony_ci } 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci if (writable) { 1921cb0ef41Sopenharmony_ci flags |= ARES_EVENT_FLAG_WRITE; 1931cb0ef41Sopenharmony_ci } 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci /* Update channel fd */ 1961cb0ef41Sopenharmony_ci ares__thread_mutex_lock(e->mutex); 1971cb0ef41Sopenharmony_ci ares_event_update(NULL, e, flags, ares_event_thread_process_fd, socket_fd, 1981cb0ef41Sopenharmony_ci NULL, NULL, NULL); 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ci /* Wake the event thread so it properly enqueues any updates */ 2011cb0ef41Sopenharmony_ci ares_event_thread_wake(e); 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci ares__thread_mutex_unlock(e->mutex); 2041cb0ef41Sopenharmony_ci} 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_cistatic void ares_event_process_updates(ares_event_thread_t *e) 2071cb0ef41Sopenharmony_ci{ 2081cb0ef41Sopenharmony_ci ares__llist_node_t *node; 2091cb0ef41Sopenharmony_ci 2101cb0ef41Sopenharmony_ci /* Iterate across all updates and apply to internal list, removing from update 2111cb0ef41Sopenharmony_ci * list */ 2121cb0ef41Sopenharmony_ci while ((node = ares__llist_node_first(e->ev_updates)) != NULL) { 2131cb0ef41Sopenharmony_ci ares_event_t *newev = ares__llist_node_claim(node); 2141cb0ef41Sopenharmony_ci ares_event_t *oldev = 2151cb0ef41Sopenharmony_ci ares__htable_asvp_get_direct(e->ev_handles, newev->fd); 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci /* Adding new */ 2181cb0ef41Sopenharmony_ci if (oldev == NULL) { 2191cb0ef41Sopenharmony_ci newev->e = e; 2201cb0ef41Sopenharmony_ci /* Don't try to add a new event if all flags are cleared, that's basically 2211cb0ef41Sopenharmony_ci * someone trying to delete something already deleted. Also if it fails 2221cb0ef41Sopenharmony_ci * to add, cleanup. */ 2231cb0ef41Sopenharmony_ci if (newev->flags == ARES_EVENT_FLAG_NONE || 2241cb0ef41Sopenharmony_ci !e->ev_sys->event_add(newev)) { 2251cb0ef41Sopenharmony_ci newev->e = NULL; 2261cb0ef41Sopenharmony_ci ares_event_destroy_cb(newev); 2271cb0ef41Sopenharmony_ci } else { 2281cb0ef41Sopenharmony_ci ares__htable_asvp_insert(e->ev_handles, newev->fd, newev); 2291cb0ef41Sopenharmony_ci } 2301cb0ef41Sopenharmony_ci continue; 2311cb0ef41Sopenharmony_ci } 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci /* Removal request */ 2341cb0ef41Sopenharmony_ci if (newev->flags == ARES_EVENT_FLAG_NONE) { 2351cb0ef41Sopenharmony_ci /* the callback for the removal will call e->ev_sys->event_del(e, event) 2361cb0ef41Sopenharmony_ci */ 2371cb0ef41Sopenharmony_ci ares__htable_asvp_remove(e->ev_handles, newev->fd); 2381cb0ef41Sopenharmony_ci ares_free(newev); 2391cb0ef41Sopenharmony_ci continue; 2401cb0ef41Sopenharmony_ci } 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci /* Modify request -- only flags can be changed */ 2431cb0ef41Sopenharmony_ci e->ev_sys->event_mod(oldev, newev->flags); 2441cb0ef41Sopenharmony_ci oldev->flags = newev->flags; 2451cb0ef41Sopenharmony_ci ares_free(newev); 2461cb0ef41Sopenharmony_ci } 2471cb0ef41Sopenharmony_ci} 2481cb0ef41Sopenharmony_ci 2491cb0ef41Sopenharmony_cistatic void *ares_event_thread(void *arg) 2501cb0ef41Sopenharmony_ci{ 2511cb0ef41Sopenharmony_ci ares_event_thread_t *e = arg; 2521cb0ef41Sopenharmony_ci ares__thread_mutex_lock(e->mutex); 2531cb0ef41Sopenharmony_ci 2541cb0ef41Sopenharmony_ci while (e->isup) { 2551cb0ef41Sopenharmony_ci struct timeval tv; 2561cb0ef41Sopenharmony_ci const struct timeval *tvout; 2571cb0ef41Sopenharmony_ci unsigned long timeout_ms = 0; /* 0 = unlimited */ 2581cb0ef41Sopenharmony_ci 2591cb0ef41Sopenharmony_ci tvout = ares_timeout(e->channel, NULL, &tv); 2601cb0ef41Sopenharmony_ci if (tvout != NULL) { 2611cb0ef41Sopenharmony_ci timeout_ms = 2621cb0ef41Sopenharmony_ci (unsigned long)((tvout->tv_sec * 1000) + (tvout->tv_usec / 1000) + 1); 2631cb0ef41Sopenharmony_ci } 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ci ares_event_process_updates(e); 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ci /* Don't hold a mutex while waiting on events */ 2681cb0ef41Sopenharmony_ci ares__thread_mutex_unlock(e->mutex); 2691cb0ef41Sopenharmony_ci e->ev_sys->wait(e, timeout_ms); 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci /* Each iteration should do timeout processing */ 2721cb0ef41Sopenharmony_ci if (e->isup) { 2731cb0ef41Sopenharmony_ci ares_process_fd(e->channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD); 2741cb0ef41Sopenharmony_ci } 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ci /* Relock before we loop again */ 2771cb0ef41Sopenharmony_ci ares__thread_mutex_lock(e->mutex); 2781cb0ef41Sopenharmony_ci } 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci ares__thread_mutex_unlock(e->mutex); 2811cb0ef41Sopenharmony_ci return NULL; 2821cb0ef41Sopenharmony_ci} 2831cb0ef41Sopenharmony_ci 2841cb0ef41Sopenharmony_cistatic void ares_event_thread_destroy_int(ares_event_thread_t *e) 2851cb0ef41Sopenharmony_ci{ 2861cb0ef41Sopenharmony_ci ares__llist_node_t *node; 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ci /* Wake thread and tell it to shutdown if it exists */ 2891cb0ef41Sopenharmony_ci ares__thread_mutex_lock(e->mutex); 2901cb0ef41Sopenharmony_ci if (e->isup) { 2911cb0ef41Sopenharmony_ci e->isup = ARES_FALSE; 2921cb0ef41Sopenharmony_ci ares_event_thread_wake(e); 2931cb0ef41Sopenharmony_ci } 2941cb0ef41Sopenharmony_ci ares__thread_mutex_unlock(e->mutex); 2951cb0ef41Sopenharmony_ci 2961cb0ef41Sopenharmony_ci /* Wait for thread to shutdown */ 2971cb0ef41Sopenharmony_ci if (e->thread) { 2981cb0ef41Sopenharmony_ci ares__thread_join(e->thread, NULL); 2991cb0ef41Sopenharmony_ci e->thread = NULL; 3001cb0ef41Sopenharmony_ci } 3011cb0ef41Sopenharmony_ci 3021cb0ef41Sopenharmony_ci /* Manually free any updates that weren't processed */ 3031cb0ef41Sopenharmony_ci while ((node = ares__llist_node_first(e->ev_updates)) != NULL) { 3041cb0ef41Sopenharmony_ci ares_event_destroy_cb(ares__llist_node_claim(node)); 3051cb0ef41Sopenharmony_ci } 3061cb0ef41Sopenharmony_ci ares__llist_destroy(e->ev_updates); 3071cb0ef41Sopenharmony_ci e->ev_updates = NULL; 3081cb0ef41Sopenharmony_ci 3091cb0ef41Sopenharmony_ci ares__htable_asvp_destroy(e->ev_handles); 3101cb0ef41Sopenharmony_ci e->ev_handles = NULL; 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_ci if (e->ev_sys->destroy) { 3131cb0ef41Sopenharmony_ci e->ev_sys->destroy(e); 3141cb0ef41Sopenharmony_ci } 3151cb0ef41Sopenharmony_ci 3161cb0ef41Sopenharmony_ci ares__thread_mutex_destroy(e->mutex); 3171cb0ef41Sopenharmony_ci e->mutex = NULL; 3181cb0ef41Sopenharmony_ci 3191cb0ef41Sopenharmony_ci ares_free(e); 3201cb0ef41Sopenharmony_ci} 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_civoid ares_event_thread_destroy(ares_channel_t *channel) 3231cb0ef41Sopenharmony_ci{ 3241cb0ef41Sopenharmony_ci ares_event_thread_t *e = channel->sock_state_cb_data; 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ci if (e == NULL) { 3271cb0ef41Sopenharmony_ci return; 3281cb0ef41Sopenharmony_ci } 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci ares_event_thread_destroy_int(e); 3311cb0ef41Sopenharmony_ci} 3321cb0ef41Sopenharmony_ci 3331cb0ef41Sopenharmony_cistatic const ares_event_sys_t *ares_event_fetch_sys(ares_evsys_t evsys) 3341cb0ef41Sopenharmony_ci{ 3351cb0ef41Sopenharmony_ci switch (evsys) { 3361cb0ef41Sopenharmony_ci case ARES_EVSYS_WIN32: 3371cb0ef41Sopenharmony_ci#if defined(_WIN32) 3381cb0ef41Sopenharmony_ci return &ares_evsys_win32; 3391cb0ef41Sopenharmony_ci#else 3401cb0ef41Sopenharmony_ci return NULL; 3411cb0ef41Sopenharmony_ci#endif 3421cb0ef41Sopenharmony_ci 3431cb0ef41Sopenharmony_ci case ARES_EVSYS_EPOLL: 3441cb0ef41Sopenharmony_ci#if defined(HAVE_EPOLL) 3451cb0ef41Sopenharmony_ci return &ares_evsys_epoll; 3461cb0ef41Sopenharmony_ci#else 3471cb0ef41Sopenharmony_ci return NULL; 3481cb0ef41Sopenharmony_ci#endif 3491cb0ef41Sopenharmony_ci 3501cb0ef41Sopenharmony_ci case ARES_EVSYS_KQUEUE: 3511cb0ef41Sopenharmony_ci#if defined(HAVE_KQUEUE) 3521cb0ef41Sopenharmony_ci return &ares_evsys_kqueue; 3531cb0ef41Sopenharmony_ci#else 3541cb0ef41Sopenharmony_ci return NULL; 3551cb0ef41Sopenharmony_ci#endif 3561cb0ef41Sopenharmony_ci 3571cb0ef41Sopenharmony_ci case ARES_EVSYS_POLL: 3581cb0ef41Sopenharmony_ci#if defined(HAVE_POLL) 3591cb0ef41Sopenharmony_ci return &ares_evsys_poll; 3601cb0ef41Sopenharmony_ci#else 3611cb0ef41Sopenharmony_ci return NULL; 3621cb0ef41Sopenharmony_ci#endif 3631cb0ef41Sopenharmony_ci 3641cb0ef41Sopenharmony_ci case ARES_EVSYS_SELECT: 3651cb0ef41Sopenharmony_ci#if defined(HAVE_PIPE) 3661cb0ef41Sopenharmony_ci return &ares_evsys_select; 3671cb0ef41Sopenharmony_ci#else 3681cb0ef41Sopenharmony_ci return NULL; 3691cb0ef41Sopenharmony_ci#endif 3701cb0ef41Sopenharmony_ci 3711cb0ef41Sopenharmony_ci /* case ARES_EVSYS_DEFAULT: */ 3721cb0ef41Sopenharmony_ci default: 3731cb0ef41Sopenharmony_ci#if defined(_WIN32) 3741cb0ef41Sopenharmony_ci return &ares_evsys_win32; 3751cb0ef41Sopenharmony_ci#elif defined(HAVE_KQUEUE) 3761cb0ef41Sopenharmony_ci return &ares_evsys_kqueue; 3771cb0ef41Sopenharmony_ci#elif defined(HAVE_EPOLL) 3781cb0ef41Sopenharmony_ci return &ares_evsys_epoll; 3791cb0ef41Sopenharmony_ci#elif defined(HAVE_POLL) 3801cb0ef41Sopenharmony_ci return &ares_evsys_poll; 3811cb0ef41Sopenharmony_ci#elif defined(HAVE_PIPE) 3821cb0ef41Sopenharmony_ci return &ares_evsys_select; 3831cb0ef41Sopenharmony_ci#else 3841cb0ef41Sopenharmony_ci break; 3851cb0ef41Sopenharmony_ci#endif 3861cb0ef41Sopenharmony_ci } 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ci return NULL; 3891cb0ef41Sopenharmony_ci} 3901cb0ef41Sopenharmony_ci 3911cb0ef41Sopenharmony_ciares_status_t ares_event_thread_init(ares_channel_t *channel) 3921cb0ef41Sopenharmony_ci{ 3931cb0ef41Sopenharmony_ci ares_event_thread_t *e; 3941cb0ef41Sopenharmony_ci 3951cb0ef41Sopenharmony_ci e = ares_malloc_zero(sizeof(*e)); 3961cb0ef41Sopenharmony_ci if (e == NULL) { 3971cb0ef41Sopenharmony_ci return ARES_ENOMEM; 3981cb0ef41Sopenharmony_ci } 3991cb0ef41Sopenharmony_ci 4001cb0ef41Sopenharmony_ci e->mutex = ares__thread_mutex_create(); 4011cb0ef41Sopenharmony_ci if (e->mutex == NULL) { 4021cb0ef41Sopenharmony_ci ares_event_thread_destroy_int(e); 4031cb0ef41Sopenharmony_ci return ARES_ENOMEM; 4041cb0ef41Sopenharmony_ci } 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_ci e->ev_updates = ares__llist_create(NULL); 4071cb0ef41Sopenharmony_ci if (e->ev_updates == NULL) { 4081cb0ef41Sopenharmony_ci ares_event_thread_destroy_int(e); 4091cb0ef41Sopenharmony_ci return ARES_ENOMEM; 4101cb0ef41Sopenharmony_ci } 4111cb0ef41Sopenharmony_ci 4121cb0ef41Sopenharmony_ci e->ev_handles = ares__htable_asvp_create(ares_event_destroy_cb); 4131cb0ef41Sopenharmony_ci if (e->ev_handles == NULL) { 4141cb0ef41Sopenharmony_ci ares_event_thread_destroy_int(e); 4151cb0ef41Sopenharmony_ci return ARES_ENOMEM; 4161cb0ef41Sopenharmony_ci } 4171cb0ef41Sopenharmony_ci 4181cb0ef41Sopenharmony_ci e->channel = channel; 4191cb0ef41Sopenharmony_ci e->isup = ARES_TRUE; 4201cb0ef41Sopenharmony_ci e->ev_sys = ares_event_fetch_sys(channel->evsys); 4211cb0ef41Sopenharmony_ci if (e->ev_sys == NULL) { 4221cb0ef41Sopenharmony_ci ares_event_thread_destroy_int(e); 4231cb0ef41Sopenharmony_ci return ARES_ENOTIMP; 4241cb0ef41Sopenharmony_ci } 4251cb0ef41Sopenharmony_ci 4261cb0ef41Sopenharmony_ci channel->sock_state_cb = ares_event_thread_sockstate_cb; 4271cb0ef41Sopenharmony_ci channel->sock_state_cb_data = e; 4281cb0ef41Sopenharmony_ci 4291cb0ef41Sopenharmony_ci if (!e->ev_sys->init(e)) { 4301cb0ef41Sopenharmony_ci ares_event_thread_destroy_int(e); 4311cb0ef41Sopenharmony_ci channel->sock_state_cb = NULL; 4321cb0ef41Sopenharmony_ci channel->sock_state_cb_data = NULL; 4331cb0ef41Sopenharmony_ci return ARES_ESERVFAIL; 4341cb0ef41Sopenharmony_ci } 4351cb0ef41Sopenharmony_ci 4361cb0ef41Sopenharmony_ci /* Before starting the thread, process any possible events the initialization 4371cb0ef41Sopenharmony_ci * might have enqueued as we may actually depend on these being valid 4381cb0ef41Sopenharmony_ci * immediately upon return, which may mean before the thread is fully spawned 4391cb0ef41Sopenharmony_ci * and processed the list itself. We don't want any sort of race conditions 4401cb0ef41Sopenharmony_ci * (like the event system wake handle itself). */ 4411cb0ef41Sopenharmony_ci ares_event_process_updates(e); 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ci /* Start thread */ 4441cb0ef41Sopenharmony_ci if (ares__thread_create(&e->thread, ares_event_thread, e) != ARES_SUCCESS) { 4451cb0ef41Sopenharmony_ci ares_event_thread_destroy_int(e); 4461cb0ef41Sopenharmony_ci channel->sock_state_cb = NULL; 4471cb0ef41Sopenharmony_ci channel->sock_state_cb_data = NULL; 4481cb0ef41Sopenharmony_ci return ARES_ESERVFAIL; 4491cb0ef41Sopenharmony_ci } 4501cb0ef41Sopenharmony_ci 4511cb0ef41Sopenharmony_ci return ARES_SUCCESS; 4521cb0ef41Sopenharmony_ci} 453