Back to index

glibc  2.9
gai_notify.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <netdb.h>
00021 #include <pthread.h>
00022 #include <stdlib.h>
00023 #include <gai_misc.h>
00024 
00025 
00026 struct notify_func
00027   {
00028     void (*func) (sigval_t);
00029     sigval_t value;
00030   };
00031 
00032 static void *
00033 notify_func_wrapper (void *arg)
00034 {
00035   gai_start_notify_thread ();
00036   struct notify_func *const n = arg;
00037   void (*func) (sigval_t) = n->func;
00038   sigval_t value = n->value;
00039   free (n);
00040   (*func) (value);
00041   return NULL;
00042 }
00043 
00044 
00045 int
00046 internal_function
00047 __gai_notify_only (struct sigevent *sigev, pid_t caller_pid)
00048 {
00049   int result = 0;
00050 
00051   /* Send the signal to notify about finished processing of the request.  */
00052   if (sigev->sigev_notify == SIGEV_THREAD)
00053     {
00054       /* We have to start a thread.  */
00055       pthread_t tid;
00056       pthread_attr_t attr, *pattr;
00057 
00058       pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
00059       if (pattr == NULL)
00060        {
00061          pthread_attr_init (&attr);
00062          pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
00063          pattr = &attr;
00064        }
00065 
00066       /* SIGEV may be freed as soon as we return, so we cannot let the
00067         notification thread use that pointer.  Even though a sigval_t is
00068         only one word and the same size as a void *, we cannot just pass
00069         the value through pthread_create as the argument and have the new
00070         thread run the user's function directly, because on some machines
00071         the calling convention for a union like sigval_t is different from
00072         that for a pointer type like void *.  */
00073       struct notify_func *nf = malloc (sizeof *nf);
00074       if (nf == NULL)
00075        result = -1;
00076       else
00077        {
00078          nf->func = sigev->sigev_notify_function;
00079          nf->value = sigev->sigev_value;
00080          if (pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0)
00081            {
00082              free (nf);
00083              result = -1;
00084            }
00085        }
00086     }
00087   else if (sigev->sigev_notify == SIGEV_SIGNAL)
00088     /* We have to send a signal.  */
00089     if (__gai_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid)
00090        < 0)
00091       result = -1;
00092 
00093   return result;
00094 }
00095 
00096 
00097 void
00098 internal_function
00099 __gai_notify (struct requestlist *req)
00100 {
00101   struct waitlist *waitlist;
00102 
00103   /* Now also notify possibly waiting threads.  */
00104   waitlist = req->waiting;
00105   while (waitlist != NULL)
00106     {
00107       struct waitlist *next = waitlist->next;
00108 
00109       if (waitlist->sigevp == NULL)
00110        {
00111 #ifdef DONT_NEED_GAI_MISC_COND
00112          GAI_MISC_NOTIFY (waitlist);
00113 #else
00114          /* Decrement the counter.  */
00115          --*waitlist->counterp;
00116 
00117          pthread_cond_signal (waitlist->cond);
00118 #endif
00119        }
00120       else
00121        /* This is part of a asynchronous `getaddrinfo_a' operation.  If
00122           this request is the last one, send the signal.  */
00123        if (--*waitlist->counterp == 0)
00124          {
00125            __gai_notify_only (waitlist->sigevp, waitlist->caller_pid);
00126            /* This is tricky.  See getaddrinfo_a.c for the reason why
00127               this works.  */
00128            free ((void *) waitlist->counterp);
00129          }
00130 
00131       waitlist = next;
00132     }
00133 }