Back to index

glibc  2.9
aio_notify.c
Go to the documentation of this file.
00001 /* Notify initiator of AIO request.
00002    Copyright (C) 1997-2001, 2003, 2004, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <pthread.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <aio_misc.h>
00026 
00027 #ifndef aio_start_notify_thread
00028 # define aio_start_notify_thread() do { } while (0)
00029 #endif
00030 
00031 struct notify_func
00032   {
00033     void (*func) (sigval_t);
00034     sigval_t value;
00035   };
00036 
00037 static void *
00038 notify_func_wrapper (void *arg)
00039 {
00040   aio_start_notify_thread ();
00041   struct notify_func *const n = arg;
00042   void (*func) (sigval_t) = n->func;
00043   sigval_t value = n->value;
00044   free (n);
00045   (*func) (value);
00046   return NULL;
00047 }
00048 
00049 
00050 int
00051 internal_function
00052 #ifdef BROKEN_THREAD_SIGNALS
00053 __aio_notify_only (struct sigevent *sigev, pid_t caller_pid)
00054 #else
00055 __aio_notify_only (struct sigevent *sigev)
00056 #endif
00057 {
00058   int result = 0;
00059 
00060   /* Send the signal to notify about finished processing of the request.  */
00061   if (__builtin_expect (sigev->sigev_notify == SIGEV_THREAD, 0))
00062     {
00063       /* We have to start a thread.  */
00064       pthread_t tid;
00065       pthread_attr_t attr, *pattr;
00066 
00067       pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
00068       if (pattr == NULL)
00069        {
00070          pthread_attr_init (&attr);
00071          pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
00072          pattr = &attr;
00073        }
00074 
00075       /* SIGEV may be freed as soon as we return, so we cannot let the
00076         notification thread use that pointer.  Even though a sigval_t is
00077         only one word and the same size as a void *, we cannot just pass
00078         the value through pthread_create as the argument and have the new
00079         thread run the user's function directly, because on some machines
00080         the calling convention for a union like sigval_t is different from
00081         that for a pointer type like void *.  */
00082       struct notify_func *nf = malloc (sizeof *nf);
00083       if (nf == NULL)
00084        result = -1;
00085       else
00086        {
00087          nf->func = sigev->sigev_notify_function;
00088          nf->value = sigev->sigev_value;
00089          if (pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0)
00090            {
00091              free (nf);
00092              result = -1;
00093            }
00094        }
00095     }
00096   else if (sigev->sigev_notify == SIGEV_SIGNAL)
00097     {
00098       /* We have to send a signal.  */
00099 #if _POSIX_REALTIME_SIGNALS
00100       /* Note that the standard gives us the option of using a plain
00101         non-queuing signal here when SA_SIGINFO is not set for the signal.  */
00102 # ifdef BROKEN_THREAD_SIGNALS
00103       if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid)
00104          < 0)
00105        result = -1;
00106 # else
00107       if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, getpid ())
00108          < 0)
00109        result = -1;
00110 # endif
00111 #else
00112       /* There are no queued signals on this system at all.  */
00113       result = raise (sigev->sigev_signo);
00114 #endif
00115     }
00116 
00117   return result;
00118 }
00119 
00120 
00121 void
00122 internal_function
00123 __aio_notify (struct requestlist *req)
00124 {
00125   struct waitlist *waitlist;
00126   struct aiocb *aiocbp = &req->aiocbp->aiocb;
00127 
00128 #ifdef BROKEN_THREAD_SIGNALS
00129   if (__aio_notify_only (&aiocbp->aio_sigevent, req->caller_pid) != 0)
00130 #else
00131   if (__aio_notify_only (&aiocbp->aio_sigevent) != 0)
00132 #endif
00133     {
00134       /* XXX What shall we do if already an error is set by
00135         read/write/fsync?  */
00136       aiocbp->__error_code = errno;
00137       aiocbp->__return_value = -1;
00138     }
00139 
00140   /* Now also notify possibly waiting threads.  */
00141   waitlist = req->waiting;
00142   while (waitlist != NULL)
00143     {
00144       struct waitlist *next = waitlist->next;
00145 
00146       if (waitlist->sigevp == NULL)
00147        {
00148          if (waitlist->result != NULL && aiocbp->__return_value == -1)
00149            *waitlist->result = -1;
00150 
00151 #ifdef DONT_NEED_AIO_MISC_COND
00152          AIO_MISC_NOTIFY (waitlist);
00153 #else
00154          /* Decrement the counter.  */
00155          --*waitlist->counterp;
00156 
00157          pthread_cond_signal (waitlist->cond);
00158 #endif
00159        }
00160       else
00161        /* This is part of a asynchronous `lio_listio' operation.  If
00162           this request is the last one, send the signal.  */
00163        if (--*waitlist->counterp == 0)
00164          {
00165 #ifdef BROKEN_THREAD_SIGNALS
00166            __aio_notify_only (waitlist->sigevp, waitlist->caller_pid);
00167 #else
00168            __aio_notify_only (waitlist->sigevp);
00169 #endif
00170            /* This is tricky.  See lio_listio.c for the reason why
00171               this works.  */
00172            free ((void *) waitlist->counterp);
00173          }
00174 
00175       waitlist = next;
00176     }
00177 }