This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit b1ff969aa626d238b22161153133d5aa3e66146d
Author: yangjianqing <[email protected]>
AuthorDate: Thu Mar 5 16:17:30 2026 +0800

    libc/netdb: Add getaddrinfo_a(), gai_suspend(), gai_cancel() and 
gai_error().
    
    Add the glibc compatible asynchronous name resolution interface.  With
    GAI_WAIT, getaddrinfo_a() resolves every request of the list before it
    returns.  With GAI_NOWAIT it starts a worker thread per batch, reports
    the result of each request through gai_error() and notifies the caller
    through the optional struct sigevent.  gai_suspend() follows the glibc
    implementation: it waits until any request of the list that is in
    progress completes and returns 0, and returns EAI_ALLDONE when no
    request of the list is in progress.  gai_cancel() cancels a pending
    request.
    
    The worker thread is cancelled through pthread cancellation and frees its
    resources with pthread cleanup handlers, so the interface depends on
    CANCELLATION_POINTS and on a cleanup stack of at least four entries
    (TLS_NCLEANUP): the worker, getaddrinfo(), the DNS query and the
    resolv.conf scan each push one.  NETDB_GETADDRINFO_A is enabled when
    these are met, and it is the single condition for the header, the build
    and libc.csv, so a program cannot see the functions without them being
    built.
    
    EAI_INPROGRESS, EAI_CANCELED, EAI_ALLDONE and EAI_INTR are added for the
    new functions and the stack size of the worker thread is configurable
    through NETDB_GETADDRINFO_A_STACKSIZE.
    
    Signed-off-by: yangjianqing <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 include/netdb.h                     |  78 +++++-
 libs/libc/libc.csv                  |   4 +
 libs/libc/netdb/CMakeLists.txt      |   6 +
 libs/libc/netdb/Kconfig             |  18 ++
 libs/libc/netdb/Make.defs           |   6 +
 libs/libc/netdb/lib_gaistrerror.c   |   4 +
 libs/libc/netdb/lib_getaddrinfo_a.c | 516 ++++++++++++++++++++++++++++++++++++
 7 files changed, 631 insertions(+), 1 deletion(-)

diff --git a/include/netdb.h b/include/netdb.h
index 1c30080c2ae..a130be8a9ae 100644
--- a/include/netdb.h
+++ b/include/netdb.h
@@ -35,9 +35,11 @@
  */
 
 #include <inttypes.h>
-
 #include <netinet/in.h>
 #include <sys/socket.h>
+#include <signal.h>
+#include <time.h>
+#include <pthread.h>
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -132,6 +134,15 @@
 #define EAI_SOCKTYPE    8
 #define EAI_SYSTEM      9
 #define EAI_OVERFLOW    10
+#define EAI_INPROGRESS  11  /* Request in progress (getaddrinfo_a) */
+#define EAI_CANCELED    12  /* Request canceled (getaddrinfo_a) */
+#define EAI_ALLDONE     13  /* All requests done (getaddrinfo_a) */
+#define EAI_INTR        14  /* Interrupted by a signal (gai_suspend) */
+
+/* Modes for getaddrinfo_a() */
+
+#define GAI_WAIT        0   /* Synchronous: block until all lookups complete */
+#define GAI_NOWAIT      1   /* Asynchronous: return immediately */
 
 /* h_errno values that may be returned by gethosbyname(), gethostbyname_r(),
  * gethostbyaddr(), or gethostbyaddr_r()
@@ -253,6 +264,30 @@ struct addrinfo
   FAR struct addrinfo *ai_next;      /* Pointer to next in list. */
 };
 
+/* Asynchronous getaddrinfo (getaddrinfo_a) support.
+ * gaicb holds a single async lookup request.
+ */
+
+#ifdef CONFIG_NETDB_GETADDRINFO_A
+struct gaicb
+{
+  FAR const char            *ar_name;    /* Hostname to resolve              */
+  FAR const char            *ar_service; /* Service name or port string      */
+  FAR const struct addrinfo *ar_request; /* Hints (may be NULL)              */
+  FAR struct addrinfo       *ar_result;  /* Result filled in on completion   */
+
+  /* Completion status — read via gai_error(), do not access directly */
+
+  int                        ar_return;
+
+  /* Internal fields — must not be accessed by app. */
+
+  pthread_t                  ar_worker;       /* Worker thread id          */
+  pthread_mutex_t            ar_lock;         /* Protects mutable fields   */
+  pthread_cond_t             ar_cond;         /* Signalled on completion   */
+};
+#endif
+
 /****************************************************************************
  * Public Data
  ****************************************************************************/
@@ -298,6 +333,47 @@ int                  getaddrinfo(FAR const char *nodename,
                                  FAR const char *servname,
                                  FAR const struct addrinfo *hints,
                                  FAR struct addrinfo **res);
+
+/* Asynchronous getaddrinfo — getaddrinfo_a() and helpers.
+ *
+ * Two modes of operation:
+ *
+ *   1. Synchronous (GAI_WAIT)
+ *      getaddrinfo_a(GAI_WAIT, list, n, NULL) resolves every entry in
+ *      list[] in the caller's thread and returns 0.  Each gaicb.ar_return
+ *      holds the per-request result (0 or an EAI_* error).  No worker
+ *      thread is created; gai_suspend / gai_cancel are not needed.
+ *
+ *   2. Asynchronous (GAI_NOWAIT)
+ *      getaddrinfo_a(GAI_NOWAIT, list, n, sevp) spawns a background
+ *      worker thread and returns immediately.  The optional sigevent
+ *      *sevp fires when the entire batch finishes.
+ *
+ *      Poll or wait for individual results with:
+ *        gai_error(req)   — returns EAI_INPROGRESS while pending,
+ *                           or the final EAI_* / 0 result.
+ *        gai_suspend()    — blocks until at least one request completes.
+ *        gai_cancel(req)  — attempts to cancel (cancels the whole batch).
+ *
+ *      IMPORTANT: The gaicb lifetime is owned by the caller.  Before
+ *      freeing or reusing a gaicb, the caller MUST confirm the request
+ *      has finished by calling gai_error() and observing a return value
+ *      other than EAI_INPROGRESS.  Only then is it safe to free the
+ *      gaicb, because the worker thread no longer references it.
+ *      Freeing a gaicb while gai_error() still returns EAI_INPROGRESS
+ *      is undefined behavior — the worker thread may still be writing
+ *      to it.
+ */
+
+#ifdef CONFIG_NETDB_GETADDRINFO_A
+int                  getaddrinfo_a(int mode, FAR struct gaicb *list[],
+                                   int n, FAR struct sigevent *sevp);
+int                  gai_suspend(FAR const struct gaicb * const list[],
+                                 int n, FAR const struct timespec *timeout);
+int                  gai_error(FAR struct gaicb *req);
+int                  gai_cancel(FAR struct gaicb *req);
+#endif
+
 int                  getnameinfo(FAR const struct sockaddr *sa,
                                  socklen_t salen, FAR char *node,
                                  socklen_t nodelen, FAR char *service,
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index 59bb3135572..cd04138f4ce 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -112,9 +112,13 @@
 "ftrylockfile","stdio.h","defined(CONFIG_FILE_STREAM)","int","FAR FILE *"
 "funlockfile","stdio.h","defined(CONFIG_FILE_STREAM)","void","FAR FILE *"
 "fwrite","stdio.h","defined(CONFIG_FILE_STREAM)","size_t","FAR const void 
*","size_t","size_t","FAR FILE *"
+"gai_cancel","netdb.h","defined(CONFIG_NETDB_GETADDRINFO_A)","int","FAR struct 
gaicb *"
+"gai_error","netdb.h","defined(CONFIG_NETDB_GETADDRINFO_A)","int","FAR struct 
gaicb *"
 "gai_strerror","netdb.h","defined(CONFIG_LIBC_NETDB)","FAR const char *","int"
+"gai_suspend","netdb.h","defined(CONFIG_NETDB_GETADDRINFO_A)","int","FAR const 
struct gaicb * const *","int","FAR const struct timespec *"
 "get_environ_ptr","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","FAR char ***"
 "getaddrinfo","netdb.h","defined(CONFIG_LIBC_NETDB)","int","FAR const char 
*","FAR const char *","FAR const struct addrinfo *","FAR struct addrinfo **"
+"getaddrinfo_a","netdb.h","defined(CONFIG_NETDB_GETADDRINFO_A)","int","int","FAR
 struct gaicb **","int","FAR struct sigevent *"
 "getc","stdio.h","defined(CONFIG_FILE_STREAM)","int","FAR FILE *"
 "getcwd","unistd.h","!defined(CONFIG_DISABLE_ENVIRON)","FAR char *","FAR char 
*","size_t"
 "get_current_dir_name","unistd.h","!defined(CONFIG_DISABLE_ENVIRON)","FAR char 
*","void"
diff --git a/libs/libc/netdb/CMakeLists.txt b/libs/libc/netdb/CMakeLists.txt
index 4c4666a7236..658ef2252d1 100644
--- a/libs/libc/netdb/CMakeLists.txt
+++ b/libs/libc/netdb/CMakeLists.txt
@@ -51,6 +51,12 @@ if(CONFIG_LIBC_NETDB)
     lib_proto.c
     lib_protor.c)
 
+  # Add async getaddrinfo (getaddrinfo_a) support
+
+  if(CONFIG_NETDB_GETADDRINFO_A)
+    list(APPEND SRCS lib_getaddrinfo_a.c)
+  endif()
+
   # Add host file support
 
   if(CONFIG_NETDB_HOSTFILE)
diff --git a/libs/libc/netdb/Kconfig b/libs/libc/netdb/Kconfig
index febc2e3eaf0..c647b52652a 100644
--- a/libs/libc/netdb/Kconfig
+++ b/libs/libc/netdb/Kconfig
@@ -49,6 +49,24 @@ config LIBC_GAISTRERROR_ERRNUM
                for unknown errors like "Unknown error 11". Default enabled when
                LIBC_GAISTRERROR is not selected.
 
+config NETDB_GETADDRINFO_A
+       bool "getaddrinfo_a() support"
+       default y
+       depends on LIBC_NETDB && !DISABLE_PTHREAD && CANCELLATION_POINTS && 
TLS_NCLEANUP > 3
+       ---help---
+               Build getaddrinfo_a(), gai_suspend(), gai_error() and 
gai_cancel().
+               gai_cancel() cancels the worker thread, and the worker thread
+               cancelled in a DNS lookup uses up to four cleanup stack entries,
+               so CONFIG_TLS_NCLEANUP must be at least 4.
+
+config NETDB_GETADDRINFO_A_STACKSIZE
+       int "Worker thread stack size for getaddrinfo_a"
+       default DEFAULT_TASK_STACKSIZE
+       depends on NETDB_GETADDRINFO_A
+       ---help---
+               Stack size (bytes) for the per-batch worker thread created by
+               getaddrinfo_a() in GAI_NOWAIT mode.
+
 config NETDB_BUFSIZE
        int "gethostbyname/gethostbyaddr buffer size"
        depends on LIBC_NETDB
diff --git a/libs/libc/netdb/Make.defs b/libs/libc/netdb/Make.defs
index c47d80d6693..cf32308188d 100644
--- a/libs/libc/netdb/Make.defs
+++ b/libs/libc/netdb/Make.defs
@@ -34,6 +34,12 @@ CSRCS += lib_gaistrerror.c lib_herror.c lib_freeaddrinfo.c 
lib_getaddrinfo.c
 CSRCS += lib_getnameinfo.c lib_rexec.c lib_dn.c lib_resmkquery.c
 CSRCS += lib_proto.c lib_protor.c
 
+# Add async getaddrinfo (getaddrinfo_a) support
+
+ifeq ($(CONFIG_NETDB_GETADDRINFO_A),y)
+CSRCS += lib_getaddrinfo_a.c
+endif
+
 # Add host file support
 
 ifeq ($(CONFIG_NETDB_HOSTFILE),y)
diff --git a/libs/libc/netdb/lib_gaistrerror.c 
b/libs/libc/netdb/lib_gaistrerror.c
index 7bdea95e235..d5d43fc4fd7 100644
--- a/libs/libc/netdb/lib_gaistrerror.c
+++ b/libs/libc/netdb/lib_gaistrerror.c
@@ -76,6 +76,10 @@ static const struct errno_strmap_s g_gaierrnomap[] =
   { EAI_SOCKTYPE,        "EAI_SOCKTYPE"      },
   { EAI_SYSTEM,          "EAI_SYSTEM"        },
   { EAI_OVERFLOW,        "EAI_OVERFLOW"      },
+  { EAI_INPROGRESS,      "EAI_INPROGRESS"    },
+  { EAI_CANCELED,        "EAI_CANCELED"      },
+  { EAI_ALLDONE,         "EAI_ALLDONE"       },
+  { EAI_INTR,            "EAI_INTR"          },
 };
 
 #define NERRNO_STRS (sizeof(g_gaierrnomap) / sizeof(struct errno_strmap_s))
diff --git a/libs/libc/netdb/lib_getaddrinfo_a.c 
b/libs/libc/netdb/lib_getaddrinfo_a.c
new file mode 100644
index 00000000000..ea74c47be08
--- /dev/null
+++ b/libs/libc/netdb/lib_getaddrinfo_a.c
@@ -0,0 +1,516 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_getaddrinfo_a.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <unistd.h>
+#include <netdb.h>
+
+#include "libc.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* Heap-allocated batch context, owned and freed by the worker thread.
+ * Contains only the data the worker needs; the status of a request is
+ * protected by its gaicb (ar_lock / ar_cond).
+ */
+
+struct gai_batch_arg
+{
+  struct sigevent   sevp;   /* Batch-completion sigevent copy   */
+  pid_t             pid;    /* Caller PID for SIGEV_SIGNAL      */
+  int               next;   /* Next entry to work on            */
+  int               nitems; /* Number of entries in list        */
+  FAR struct gaicb *list[]; /* Copy of the gaicb pointer array  */
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Broadcast whenever any request completes, so that gai_suspend() can wait
+ * for the first completion among several requests.
+ */
+
+static pthread_mutex_t g_gai_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t  g_gai_cond = PTHREAD_COND_INITIALIZER;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: gai_complete
+ *
+ * Description:
+ *   Store the final status of a request and wake up its waiters.
+ *
+ ****************************************************************************/
+
+static void gai_complete(FAR struct gaicb *req, int ret)
+{
+  pthread_mutex_lock(&req->ar_lock);
+  req->ar_return = ret;
+  pthread_cond_broadcast(&req->ar_cond);
+  pthread_mutex_unlock(&req->ar_lock);
+
+  pthread_mutex_lock(&g_gai_lock);
+  pthread_cond_broadcast(&g_gai_cond);
+  pthread_mutex_unlock(&g_gai_lock);
+}
+
+/****************************************************************************
+ * Name: gai_inprogress
+ *
+ * Description:
+ *   Return the number of requests of the list that are still in progress.
+ *
+ ****************************************************************************/
+
+static int gai_inprogress(FAR const struct gaicb * const list[], int n)
+{
+  int count = 0;
+  int i;
+
+  for (i = 0; i < n; i++)
+    {
+      FAR struct gaicb *req = (FAR struct gaicb *)list[i];
+
+      if (req != NULL)
+        {
+          pthread_mutex_lock(&req->ar_lock);
+          if (req->ar_return == EAI_INPROGRESS)
+            {
+              count++;
+            }
+
+          pthread_mutex_unlock(&req->ar_lock);
+        }
+    }
+
+  return count;
+}
+
+/****************************************************************************
+ * Name: gai_notify
+ *
+ * Description:
+ *   Fire the batch-completion sigevent.
+ *
+ ****************************************************************************/
+
+static void gai_notify(FAR const struct sigevent *sevp, pid_t pid)
+{
+  switch (sevp->sigev_notify)
+    {
+      case SIGEV_SIGNAL:
+        sigqueue(pid, sevp->sigev_signo, sevp->sigev_value);
+        break;
+
+#ifdef CONFIG_SIG_EVTHREAD
+      case SIGEV_THREAD:
+        if (sevp->sigev_notify_function != NULL)
+          {
+            sevp->sigev_notify_function(sevp->sigev_value);
+          }
+#endif
+    }
+}
+
+/****************************************************************************
+ * Name: gai_worker_cleanup
+ *
+ * Description:
+ *   Cleanup handler pushed via pthread_cleanup_push() inside gai_worker().
+ *   Runs on cancellation (pthread_cancel) and the normal completion path.
+ *   Marks remaining in-progress requests as EAI_CANCELED and signals
+ *   their condvars, fires the batch sigevent, then frees the batch arg.
+ *
+ ****************************************************************************/
+
+static void gai_worker_cleanup(FAR void *arg)
+{
+  FAR struct gai_batch_arg *barg = arg;
+
+  for (; barg->next < barg->nitems; barg->next++)
+    {
+      FAR struct gaicb *req = barg->list[barg->next];
+
+      if (req != NULL)
+        {
+          gai_complete(req, EAI_CANCELED);
+        }
+    }
+
+  gai_notify(&barg->sevp, barg->pid);
+  lib_free(barg);
+}
+
+/****************************************************************************
+ * Name: gai_worker
+ *
+ * Description:
+ *   Iterates over all gaicb entries, calls getaddrinfo() synchronously for
+ *   each, writes results into the gaicb under req->ar_lock, then calls
+ *   pthread_cleanup_pop(1) to fire the batch sigevent and free resources.
+ *
+ ****************************************************************************/
+
+static FAR void *gai_worker(FAR void *arg)
+{
+  FAR struct gai_batch_arg *barg = arg;
+  int ret;
+
+  pthread_cleanup_push(gai_worker_cleanup, barg);
+
+  for (; barg->next < barg->nitems; barg->next++)
+    {
+      FAR struct gaicb *req = barg->list[barg->next];
+
+      if (req != NULL)
+        {
+          ret = getaddrinfo(req->ar_name, req->ar_service,
+                            req->ar_request, &req->ar_result);
+          gai_complete(req, ret);
+        }
+    }
+
+  /* Normal completion: pop with executing the cleanup handler */
+
+  pthread_cleanup_pop(1);
+  return NULL;
+}
+
+/****************************************************************************
+ * Name: gai_cb_init
+ *
+ * Description:
+ *   Initialize a gaicb's synchronization primitives (ar_lock, ar_cond)
+ *   and reset its result fields (ar_result, ar_return, ar_worker) to
+ *   their initial state prior to dispatch.
+ *
+ * Note:
+ *   ar_lock/ar_cond don't need to be destroyed manually, since both
+ *   functions are the trivial operation for the the simple mutex.
+ *
+ ****************************************************************************/
+
+static void gai_cb_init(FAR struct gaicb *req)
+{
+  pthread_mutexattr_t attr;
+
+  pthread_mutexattr_init(&attr);
+  pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_NONE);
+  pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_STALLED);
+
+  req->ar_result = NULL;
+  req->ar_return = EAI_INPROGRESS;
+  req->ar_worker = 0;
+
+  pthread_mutex_init(&req->ar_lock, &attr);
+  pthread_cond_init(&req->ar_cond, NULL);
+
+  pthread_mutexattr_destroy(&attr);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: getaddrinfo_a
+ *
+ * Description:
+ *   Asynchronous address resolution.
+ *
+ *   mode == GAI_WAIT:   resolve synchronously in the caller's thread.
+ *   mode == GAI_NOWAIT: spawn a per-batch worker thread and return 0.
+ *                       gai_cancel() waits for it on cancellation.
+ *
+ * Returned Value:
+ *   0 on success; EAI_AGAIN, EAI_MEMORY, or EAI_SYSTEM on error.
+ *
+ ****************************************************************************/
+
+int getaddrinfo_a(int mode, FAR struct gaicb *list[], int n,
+                  FAR struct sigevent *sevp)
+{
+  FAR struct gai_batch_arg *barg;
+  FAR struct gaicb *req;
+  pthread_attr_t attr;
+  pthread_t tid;
+  int ret;
+  int i;
+
+  if (list == NULL || n <= 0)
+    {
+      return EAI_SYSTEM;
+    }
+
+  if (mode != GAI_WAIT && mode != GAI_NOWAIT)
+    {
+      return EAI_SYSTEM;
+    }
+
+  /* Initialize gaicb and call getaddrinfo in the wait mode */
+
+  for (i = 0; i < n; i++)
+    {
+      req = list[i];
+      if (req != NULL)
+        {
+          gai_cb_init(req);
+          if (mode == GAI_WAIT)
+            {
+              req->ar_return = getaddrinfo(req->ar_name,
+                                           req->ar_service,
+                                           req->ar_request,
+                                           &req->ar_result);
+            }
+        }
+    }
+
+  if (mode == GAI_WAIT)
+    {
+      return 0;
+    }
+
+  /* Build batch arg and spawn detached worker thread for no wait mode */
+
+  barg = lib_malloc(sizeof(struct gai_batch_arg) +
+                    n * sizeof(FAR struct gaicb *));
+  if (barg == NULL)
+    {
+      ret = EAI_MEMORY;
+      goto err;
+    }
+
+  barg->next   = 0;
+  barg->nitems = n;
+  barg->pid    = getpid();
+
+  memcpy(barg->list, list, n * sizeof(FAR struct gaicb *));
+
+  if (sevp != NULL)
+    {
+      barg->sevp = *sevp;
+    }
+  else
+    {
+      memset(&barg->sevp, 0, sizeof(barg->sevp));
+      barg->sevp.sigev_notify = SIGEV_NONE;
+    }
+
+  pthread_attr_init(&attr);
+  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+  pthread_attr_setstacksize(&attr, CONFIG_NETDB_GETADDRINFO_A_STACKSIZE);
+
+  ret = pthread_create(&tid, &attr, gai_worker, barg);
+  pthread_attr_destroy(&attr);
+
+  if (ret != 0)
+    {
+      lib_free(barg);
+      ret = EAI_MEMORY;
+      goto err;
+    }
+
+  /* Record worker tid in each req */
+
+  for (i = 0; i < n; i++)
+    {
+      req = list[i];
+      if (req != NULL)
+        {
+          req->ar_worker = tid;
+        }
+    }
+
+  return 0;
+
+err:
+  for (i = 0; i < n; i++)
+    {
+      req = list[i];
+      if (req != NULL)
+        {
+          req->ar_return = ret;
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gai_suspend
+ *
+ * Description:
+ *   Block until at least one of the requests of list[] that are in progress
+ *   completes, or the timeout expires.  Requests that have already
+ *   completed and NULL entries are ignored, as glibc does.
+ *
+ * Returned Value:
+ *   0           - a request of the list completed during the call.
+ *   EAI_ALLDONE - no request of the list is in progress.
+ *   EAI_AGAIN   - timeout expired before any request completed.
+ *   EAI_INTR    - interrupted by a signal.
+ *   EAI_SYSTEM  - system error or list is NULL.
+ *
+ ****************************************************************************/
+
+int gai_suspend(FAR const struct gaicb * const list[], int n,
+                FAR const struct timespec *timeout)
+{
+  struct timespec timeout_abs;
+  int oldstate;
+  int pending;
+  int ret = 0;
+  int rc;
+
+  if (list == NULL || n <= 0)
+    {
+      return EAI_SYSTEM;
+    }
+
+  if (timeout != NULL)
+    {
+      clock_gettime(CLOCK_REALTIME, &timeout_abs);
+      timeout_abs.tv_sec  += timeout->tv_sec;
+      timeout_abs.tv_nsec += timeout->tv_nsec;
+      if (timeout_abs.tv_nsec >= NSEC_PER_SEC)
+        {
+          timeout_abs.tv_sec++;
+          timeout_abs.tv_nsec -= NSEC_PER_SEC;
+        }
+    }
+
+  /* g_gai_lock is held from the first count to the wait, and every
+   * completion takes it before broadcasting g_gai_cond, so a completion
+   * cannot be missed.  Cancellation is disabled during the wait so that
+   * g_gai_lock is never left locked by a cancelled caller.
+   */
+
+  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
+  pthread_mutex_lock(&g_gai_lock);
+
+  pending = gai_inprogress(list, n);
+  if (pending == 0)
+    {
+      ret = EAI_ALLDONE;
+    }
+
+  while (pending > 0 && gai_inprogress(list, n) >= pending)
+    {
+      rc = timeout != NULL
+          ? pthread_cond_timedwait(&g_gai_cond, &g_gai_lock, &timeout_abs)
+          : pthread_cond_wait(&g_gai_cond, &g_gai_lock);
+      if (rc == ETIMEDOUT)
+        {
+          ret = EAI_AGAIN;
+          break;
+        }
+      else if (rc == EINTR)
+        {
+          ret = EAI_INTR;
+          break;
+        }
+      else if (rc != 0)
+        {
+          ret = EAI_SYSTEM;
+          break;
+        }
+    }
+
+  pthread_mutex_unlock(&g_gai_lock);
+  pthread_setcancelstate(oldstate, NULL);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gai_error
+ *
+ * Description:
+ *   Return the status of an async request.
+ *
+ ****************************************************************************/
+
+int gai_error(FAR struct gaicb *req)
+{
+  int ret = EAI_SYSTEM;
+
+  if (req != NULL)
+    {
+      pthread_mutex_lock(&req->ar_lock);
+      ret = req->ar_return;
+      pthread_mutex_unlock(&req->ar_lock);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: gai_cancel
+ *
+ * Description:
+ *   Cancel an async lookup.  If the request is still in progress,
+ *   send pthread_cancel to the worker thread.
+ *
+ *   This function cancels the entire worker thread, which affects
+ *   ALL requests in the same batch, not just the single req passed in.
+ *
+ * Returned Value:
+ *   EAI_CANCELED    - cancel was requested.
+ *   EAI_ALLDONE     - request has already completed or been canceled.
+ *   EAI_SYSTEM      - req is NULL.
+ *
+ ****************************************************************************/
+
+int gai_cancel(FAR struct gaicb *req)
+{
+  int ret = EAI_ALLDONE;
+
+  if (req == NULL)
+    {
+      return EAI_SYSTEM;
+    }
+
+  pthread_mutex_lock(&req->ar_lock);
+  if (req->ar_return == EAI_INPROGRESS)
+    {
+      pthread_cancel(req->ar_worker);
+      while (req->ar_return == EAI_INPROGRESS)
+        {
+          pthread_cond_wait(&req->ar_cond, &req->ar_lock);
+        }
+
+      ret = EAI_CANCELED;
+    }
+
+  pthread_mutex_unlock(&req->ar_lock);
+
+  return ret;
+}

Reply via email to