From 5ec73bb91f231a0d7a3c54183a9282cafd5511cb Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Sat, 16 Jun 2001 04:47:37 +0000 Subject: 2001-06-15 Roland McGrath * rt/Makefile (aio-routines, clock-routines, timer-routines): New variables, broken out of librt-routines. (librt-routines): Set using those. (extra-libs, extra-libc-others, tests): Set these unconditionally. (distribute): No need for $(librt-map) or $(librt-routines:=.c) here. * sysdeps/generic/aio_fsync.c: New file. * sysdeps/generic/aio_misc.c: New file. * sysdeps/generic/aio_write.c: New file. * sysdeps/generic/aio_write64.c: New file. * sysdeps/generic/aio_read64.c: New file. * sysdeps/generic/aio_read.c: New file. * sysdeps/generic/aio_notify.c: New file. * sysdeps/generic/lio_listio64.c: New file. * sysdeps/generic/lio_listio.c: New file. * sysdeps/generic/aio_cancel.c: New file. * sysdeps/generic/aio_misc.h: New file. * sysdeps/generic/aio_suspend.c: New file. * rt/aio_cancel.c: Moved to ... * sysdeps/pthread/aio_cancel.c: ... here. * rt/aio_fsync.c: Moved to ... * sysdeps/pthread/aio_fsync.c: ... here. * rt/aio_misc.c: Moved to ... * sysdeps/pthread/aio_misc.c: ... here. * rt/aio_misc.h: Moved to ... * sysdeps/pthread/aio_misc.h: ... here. * rt/aio_notify.c: Moved to ... * sysdeps/pthread/aio_notify.c: ... here. * rt/aio_read.c: Moved to ... * sysdeps/pthread/aio_read.c: ... here. * rt/aio_read64.c: Moved to ... * sysdeps/pthread/aio_read64.c: ... here. * rt/aio_suspend.c: Moved to ... * sysdeps/pthread/aio_suspend.c: ... here. * rt/aio_write.c: Moved to ... * sysdeps/pthread/aio_write.c: ... here. * rt/aio_write64.c: Moved to ... * sysdeps/pthread/aio_write64.c: ... here. * rt/lio_listio.c: Moved to ... * sysdeps/pthread/lio_listio.c: ... here. * rt/lio_listio64.c: Moved to ... * sysdeps/pthread/lio_listio64.c: ... here. --- sysdeps/pthread/aio_cancel.c | 142 +++++++++ sysdeps/pthread/aio_fsync.c | 50 +++ sysdeps/pthread/aio_misc.c | 708 +++++++++++++++++++++++++++++++++++++++++ sysdeps/pthread/aio_misc.h | 127 ++++++++ sysdeps/pthread/aio_notify.c | 119 +++++++ sysdeps/pthread/aio_read.c | 32 ++ sysdeps/pthread/aio_read64.c | 32 ++ sysdeps/pthread/aio_suspend.c | 151 +++++++++ sysdeps/pthread/aio_write.c | 32 ++ sysdeps/pthread/aio_write64.c | 32 ++ sysdeps/pthread/lio_listio.c | 187 +++++++++++ sysdeps/pthread/lio_listio64.c | 169 ++++++++++ 12 files changed, 1781 insertions(+) create mode 100644 sysdeps/pthread/aio_cancel.c create mode 100644 sysdeps/pthread/aio_fsync.c create mode 100644 sysdeps/pthread/aio_misc.c create mode 100644 sysdeps/pthread/aio_misc.h create mode 100644 sysdeps/pthread/aio_notify.c create mode 100644 sysdeps/pthread/aio_read.c create mode 100644 sysdeps/pthread/aio_read64.c create mode 100644 sysdeps/pthread/aio_suspend.c create mode 100644 sysdeps/pthread/aio_write.c create mode 100644 sysdeps/pthread/aio_write64.c create mode 100644 sysdeps/pthread/lio_listio.c create mode 100644 sysdeps/pthread/lio_listio64.c (limited to 'sysdeps/pthread') diff --git a/sysdeps/pthread/aio_cancel.c b/sysdeps/pthread/aio_cancel.c new file mode 100644 index 0000000000..c1cfea50c8 --- /dev/null +++ b/sysdeps/pthread/aio_cancel.c @@ -0,0 +1,142 @@ +/* Cancel requests associated with given file descriptor. + Copyright (C) 1997, 1998, 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + + +/* We use an UGLY hack to prevent gcc from finding us cheating. The + implementation of aio_cancel and aio_cancel64 are identical and so + we want to avoid code duplication by using aliases. But gcc sees + the different parameter lists and prints a warning. We define here + a function so that aio_cancel64 has no prototype. */ +#define aio_cancel64 XXX +#include +/* And undo the hack. */ +#undef aio_cancel64 + +#include +#include + +#include "aio_misc.h" + + +int +aio_cancel (fildes, aiocbp) + int fildes; + struct aiocb *aiocbp; +{ + struct requestlist *req = NULL; + int result = AIO_ALLDONE; + + /* Request the mutex. */ + pthread_mutex_lock (&__aio_requests_mutex); + + /* We are asked to cancel a specific AIO request. */ + if (aiocbp != NULL) + { + /* If the AIO request is not for this descriptor it has no value + to look for the request block. */ + if (aiocbp->aio_fildes == fildes) + { + struct requestlist *last = NULL; + + req = __aio_find_req_fd (fildes); + + if (req == NULL) + { + not_found: + pthread_mutex_unlock (&__aio_requests_mutex); + __set_errno (EINVAL); + return -1; + } + + while (req->aiocbp != (aiocb_union *) aiocbp) + { + last = req; + req = req->next_prio; + if (req == NULL) + goto not_found; + } + + /* Don't remove the entry if a thread is already working on it. */ + if (req->running == allocated) + { + result = AIO_NOTCANCELED; + req = NULL; + } + else + { + /* We can remove the entry. */ + __aio_remove_request (last, req, 0); + + result = AIO_CANCELED; + + req->next_prio = NULL; + } + } + } + else + { + /* Find the beginning of the list of all requests for this + desriptor. */ + req = __aio_find_req_fd (fildes); + + /* If any request is worked on by a thread it must be the first. + So either we can delete all requests or all but the first. */ + if (req != NULL) + { + if (req->running == allocated) + { + struct requestlist *old = req; + req = req->next_prio; + old->next_prio = NULL; + + result = AIO_NOTCANCELED; + + if (req != NULL) + __aio_remove_request (old, req, 1); + } + else + { + result = AIO_CANCELED; + + /* We can remove the entry. */ + __aio_remove_request (NULL, req, 1); + } + } + } + + /* Mark requests as canceled and send signal. */ + while (req != NULL) + { + struct requestlist *old = req; + assert (req->running == yes || req->running == queued); + req->aiocbp->aiocb.__error_code = ECANCELED; + req->aiocbp->aiocb.__return_value = -1; + __aio_notify (req); + req = req->next_prio; + __aio_free_request (old); + } + + /* Release the mutex. */ + pthread_mutex_unlock (&__aio_requests_mutex); + + return result; +} + +weak_alias (aio_cancel, aio_cancel64) diff --git a/sysdeps/pthread/aio_fsync.c b/sysdeps/pthread/aio_fsync.c new file mode 100644 index 0000000000..298b4ec87d --- /dev/null +++ b/sysdeps/pthread/aio_fsync.c @@ -0,0 +1,50 @@ +/* Synchronize I/O in given file descriptor. + Copyright (C) 1997, 1999 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + + +/* We use an UGLY hack to prevent gcc from finding us cheating. The + implementation of aio_fsync and aio_fsync64 are identical and so + we want to avoid code duplication by using aliases. But gcc sees + the different parameter lists and prints a warning. We define here + a function so that aio_fsync64 has no prototype. */ +#define aio_fsync64 XXX +#include +/* And undo the hack. */ +#undef aio_fsync64 +#include + +#include "aio_misc.h" + + +int +aio_fsync (int op, struct aiocb *aiocbp) +{ + if (op != O_DSYNC && op != O_SYNC) + { + __set_errno (EINVAL); + return -1; + } + + return (__aio_enqueue_request ((aiocb_union *) aiocbp, + op == O_SYNC ? LIO_SYNC : LIO_DSYNC) == NULL + ? -1 : 0); +} + +weak_alias (aio_fsync, aio_fsync64) diff --git a/sysdeps/pthread/aio_misc.c b/sysdeps/pthread/aio_misc.c new file mode 100644 index 0000000000..1026e061c8 --- /dev/null +++ b/sysdeps/pthread/aio_misc.c @@ -0,0 +1,708 @@ +/* Handle general operations. + Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "aio_misc.h" + +static void add_request_to_runlist (struct requestlist *newrequest); + +/* Pool of request list entries. */ +static struct requestlist **pool; + +/* Number of total and allocated pool entries. */ +static size_t pool_max_size; +static size_t pool_size; + +/* We implement a two dimensional array but allocate each row separately. + The macro below determines how many entries should be used per row. + It should better be a power of two. */ +#define ENTRIES_PER_ROW 32 + +/* How many rows we allocate at once. */ +#define ROWS_STEP 8 + +/* List of available entries. */ +static struct requestlist *freelist; + +/* List of request waiting to be processed. */ +static struct requestlist *runlist; + +/* Structure list of all currently processed requests. */ +static struct requestlist *requests; + +/* Number of threads currently running. */ +static int nthreads; + +/* Number of threads waiting for work to arrive. */ +static int idle_thread_count; + + +/* These are the values used to optimize the use of AIO. The user can + overwrite them by using the `aio_init' function. */ +static struct aioinit optim = +{ + 20, /* int aio_threads; Maximal number of threads. */ + 64, /* int aio_num; Number of expected simultanious requests. */ + 0, + 0, + 0, + 0, + 1, + 0 +}; + + +/* Since the list is global we need a mutex protecting it. */ +pthread_mutex_t __aio_requests_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; + +/* When you add a request to the list and there are idle threads present, + you signal this condition variable. When a thread finishes work, it waits + on this condition variable for a time before it actually exits. */ +pthread_cond_t __aio_new_request_notification = PTHREAD_COND_INITIALIZER; + + +/* Functions to handle request list pool. */ +static struct requestlist * +get_elem (void) +{ + struct requestlist *result; + + if (freelist == NULL) + { + struct requestlist *new_row; + int cnt; + + assert (sizeof (struct aiocb) == sizeof (struct aiocb64)); + + if (pool_size + 1 >= pool_max_size) + { + size_t new_max_size = pool_max_size + ROWS_STEP; + struct requestlist **new_tab; + + new_tab = (struct requestlist **) + realloc (pool, new_max_size * sizeof (struct requestlist *)); + + if (new_tab == NULL) + return NULL; + + pool_max_size = new_max_size; + pool = new_tab; + } + + /* Allocate the new row. */ + cnt = pool_size == 0 ? optim.aio_num : ENTRIES_PER_ROW; + new_row = (struct requestlist *) calloc (cnt, + sizeof (struct requestlist)); + if (new_row == NULL) + return NULL; + + pool[pool_size++] = new_row; + + /* Put all the new entries in the freelist. */ + do + { + new_row->next_prio = freelist; + freelist = new_row++; + } + while (--cnt > 0); + } + + result = freelist; + freelist = freelist->next_prio; + + return result; +} + + +void +internal_function +__aio_free_request (struct requestlist *elem) +{ + elem->running = no; + elem->next_prio = freelist; + freelist = elem; +} + + +struct requestlist * +internal_function +__aio_find_req (aiocb_union *elem) +{ + struct requestlist *runp = requests; + int fildes = elem->aiocb.aio_fildes; + + while (runp != NULL && runp->aiocbp->aiocb.aio_fildes < fildes) + runp = runp->next_fd; + + if (runp != NULL) + { + if (runp->aiocbp->aiocb.aio_fildes != fildes) + runp = NULL; + else + while (runp != NULL && runp->aiocbp != elem) + runp = runp->next_prio; + } + + return runp; +} + + +struct requestlist * +internal_function +__aio_find_req_fd (int fildes) +{ + struct requestlist *runp = requests; + + while (runp != NULL && runp->aiocbp->aiocb.aio_fildes < fildes) + runp = runp->next_fd; + + return (runp != NULL && runp->aiocbp->aiocb.aio_fildes == fildes + ? runp : NULL); +} + + +void +internal_function +__aio_remove_request (struct requestlist *last, struct requestlist *req, + int all) +{ + assert (req->running == yes || req->running == queued + || req->running == done); + + if (last != NULL) + last->next_prio = all ? NULL : req->next_prio; + else + { + if (all || req->next_prio == NULL) + { + if (req->last_fd != NULL) + req->last_fd->next_fd = req->next_fd; + else + requests = req->next_fd; + if (req->next_fd != NULL) + req->next_fd->last_fd = req->last_fd; + } + else + { + if (req->last_fd != NULL) + req->last_fd->next_fd = req->next_prio; + else + requests = req->next_prio; + + if (req->next_fd != NULL) + req->next_fd->last_fd = req->next_prio; + + req->next_prio->last_fd = req->last_fd; + req->next_prio->next_fd = req->next_fd; + + /* Mark this entry as runnable. */ + req->next_prio->running = yes; + } + + if (req->running == yes) + { + struct requestlist *runp = runlist; + + last = NULL; + while (runp != NULL) + { + if (runp == req) + { + if (last == NULL) + runlist = runp->next_run; + else + last->next_run = runp->next_run; + break; + } + last = runp; + runp = runp->next_run; + } + } + } +} + + +/* The thread handler. */ +static void *handle_fildes_io (void *arg); + + +/* User optimization. */ +void +__aio_init (const struct aioinit *init) +{ + /* Get the mutex. */ + pthread_mutex_lock (&__aio_requests_mutex); + + /* Only allow writing new values if the table is not yet allocated. */ + if (pool == NULL) + { + optim.aio_threads = init->aio_threads < 1 ? 1 : init->aio_threads; + optim.aio_num = (init->aio_num < ENTRIES_PER_ROW + ? ENTRIES_PER_ROW + : init->aio_num & ~ENTRIES_PER_ROW); + } + + if (init->aio_idle_time != 0) + optim.aio_idle_time = init->aio_idle_time; + + /* Release the mutex. */ + pthread_mutex_unlock (&__aio_requests_mutex); +} +weak_alias (__aio_init, aio_init) + + +/* The main function of the async I/O handling. It enqueues requests + and if necessary starts and handles threads. */ +struct requestlist * +internal_function +__aio_enqueue_request (aiocb_union *aiocbp, int operation) +{ + int result = 0; + int policy, prio; + struct sched_param param; + struct requestlist *last, *runp, *newp; + int running = no; + + if (operation == LIO_SYNC || operation == LIO_DSYNC) + aiocbp->aiocb.aio_reqprio = 0; + else if (aiocbp->aiocb.aio_reqprio < 0 + || aiocbp->aiocb.aio_reqprio > AIO_PRIO_DELTA_MAX) + { + /* Invalid priority value. */ + __set_errno (EINVAL); + aiocbp->aiocb.__error_code = EINVAL; + aiocbp->aiocb.__return_value = -1; + return NULL; + } + + /* Compute priority for this request. */ + pthread_getschedparam (pthread_self (), &policy, ¶m); + prio = param.sched_priority - aiocbp->aiocb.aio_reqprio; + + /* Get the mutex. */ + pthread_mutex_lock (&__aio_requests_mutex); + + last = NULL; + runp = requests; + /* First look whether the current file descriptor is currently + worked with. */ + while (runp != NULL + && runp->aiocbp->aiocb.aio_fildes < aiocbp->aiocb.aio_fildes) + { + last = runp; + runp = runp->next_fd; + } + + /* Get a new element for the waiting list. */ + newp = get_elem (); + if (newp == NULL) + { + pthread_mutex_unlock (&__aio_requests_mutex); + __set_errno (EAGAIN); + return NULL; + } + newp->aiocbp = aiocbp; + newp->caller_pid = (aiocbp->aiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL + ? getpid () : 0); + newp->waiting = NULL; + + aiocbp->aiocb.__abs_prio = prio; + aiocbp->aiocb.__policy = policy; + aiocbp->aiocb.aio_lio_opcode = operation; + aiocbp->aiocb.__error_code = EINPROGRESS; + aiocbp->aiocb.__return_value = 0; + + if (runp != NULL + && runp->aiocbp->aiocb.aio_fildes == aiocbp->aiocb.aio_fildes) + { + /* The current file descriptor is worked on. It makes no sense + to start another thread since this new thread would fight + with the running thread for the resources. But we also cannot + say that the thread processing this desriptor shall immediately + after finishing the current job process this request if there + are other threads in the running queue which have a higher + priority. */ + + /* Simply enqueue it after the running one according to the + priority. */ + while (runp->next_prio != NULL + && runp->next_prio->aiocbp->aiocb.__abs_prio >= prio) + runp = runp->next_prio; + + newp->next_prio = runp->next_prio; + runp->next_prio = newp; + + running = queued; + } + else + { + running = yes; + /* Enqueue this request for a new descriptor. */ + if (last == NULL) + { + newp->last_fd = NULL; + newp->next_fd = requests; + if (requests != NULL) + requests->last_fd = newp; + requests = newp; + } + else + { + newp->next_fd = last->next_fd; + newp->last_fd = last; + last->next_fd = newp; + if (newp->next_fd != NULL) + newp->next_fd->last_fd = newp; + } + + newp->next_prio = NULL; + } + + if (running == yes) + { + /* We try to create a new thread for this file descriptor. The + function which gets called will handle all available requests + for this descriptor and when all are processed it will + terminate. + + If no new thread can be created or if the specified limit of + threads for AIO is reached we queue the request. */ + + /* See if we need to and are able to create a thread. */ + if (nthreads < optim.aio_threads && idle_thread_count == 0) + { + pthread_t thid; + pthread_attr_t attr; + + /* Make sure the thread is created detached. */ + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + + running = newp->running = allocated; + + /* Now try to start a thread. */ + if (pthread_create (&thid, &attr, handle_fildes_io, newp) == 0) + /* We managed to enqueue the request. All errors which can + happen now can be recognized by calls to `aio_return' and + `aio_error'. */ + ++nthreads; + else + { + /* Reset the running flag. The new request is not running. */ + running = newp->running = yes; + + if (nthreads == 0) + /* We cannot create a thread in the moment and there is + also no thread running. This is a problem. `errno' is + set to EAGAIN if this is only a temporary problem. */ + result = -1; + } + } + } + + /* Enqueue the request in the run queue if it is not yet running. */ + if (running == yes && result == 0) + { + add_request_to_runlist (newp); + + /* If there is a thread waiting for work, then let it know that we + have just given it something to do. */ + if (idle_thread_count > 0) + pthread_cond_signal (&__aio_new_request_notification); + } + + if (result == 0) + newp->running = running; + else + { + /* Something went wrong. */ + __aio_free_request (newp); + newp = NULL; + } + + /* Release the mutex. */ + pthread_mutex_unlock (&__aio_requests_mutex); + + return newp; +} + + +static void * +__attribute__ ((noreturn)) +handle_fildes_io (void *arg) +{ + pthread_t self = pthread_self (); + struct sched_param param; + struct requestlist *runp = (struct requestlist *) arg; + aiocb_union *aiocbp; + int policy; + int fildes; + + pthread_getschedparam (self, &policy, ¶m); + + do + { + /* If runp is NULL, then we were created to service the work queue + in general, not to handle any particular request. In that case we + skip the "do work" stuff on the first pass, and go directly to the + "get work off the work queue" part of this loop, which is near the + end. */ + if (runp == NULL) + pthread_mutex_lock (&__aio_requests_mutex); + else + { + /* Hopefully this request is marked as running. */ + assert (runp->running == allocated); + + /* Update our variables. */ + aiocbp = runp->aiocbp; + fildes = aiocbp->aiocb.aio_fildes; + + /* Change the priority to the requested value (if necessary). */ + if (aiocbp->aiocb.__abs_prio != param.sched_priority + || aiocbp->aiocb.__policy != policy) + { + param.sched_priority = aiocbp->aiocb.__abs_prio; + policy = aiocbp->aiocb.__policy; + pthread_setschedparam (self, policy, ¶m); + } + + /* Process request pointed to by RUNP. We must not be disturbed + by signals. */ + if ((aiocbp->aiocb.aio_lio_opcode & 127) == LIO_READ) + { + if (aiocbp->aiocb.aio_lio_opcode & 128) + aiocbp->aiocb.__return_value = + TEMP_FAILURE_RETRY (__pread64 (fildes, (void *) + aiocbp->aiocb64.aio_buf, + aiocbp->aiocb64.aio_nbytes, + aiocbp->aiocb64.aio_offset)); + else + aiocbp->aiocb.__return_value = + TEMP_FAILURE_RETRY (pread (fildes, + (void *) aiocbp->aiocb.aio_buf, + aiocbp->aiocb.aio_nbytes, + aiocbp->aiocb.aio_offset)); + + if (aiocbp->aiocb.__return_value == -1 && errno == ESPIPE) + /* The Linux kernel is different from others. It returns + ESPIPE if using pread on a socket. Other platforms + simply ignore the offset parameter and behave like + read. */ + aiocbp->aiocb.__return_value = + TEMP_FAILURE_RETRY (read (fildes, + (void *) aiocbp->aiocb64.aio_buf, + aiocbp->aiocb64.aio_nbytes)); + } + else if ((aiocbp->aiocb.aio_lio_opcode & 127) == LIO_WRITE) + { + if (aiocbp->aiocb.aio_lio_opcode & 128) + aiocbp->aiocb.__return_value = + TEMP_FAILURE_RETRY (__pwrite64 (fildes, (const void *) + aiocbp->aiocb64.aio_buf, + aiocbp->aiocb64.aio_nbytes, + aiocbp->aiocb64.aio_offset)); + else + aiocbp->aiocb.__return_value = + TEMP_FAILURE_RETRY (pwrite (fildes, (const void *) + aiocbp->aiocb.aio_buf, + aiocbp->aiocb.aio_nbytes, + aiocbp->aiocb.aio_offset)); + + if (aiocbp->aiocb.__return_value == -1 && errno == ESPIPE) + /* The Linux kernel is different from others. It returns + ESPIPE if using pwrite on a socket. Other platforms + simply ignore the offset parameter and behave like + write. */ + aiocbp->aiocb.__return_value = + TEMP_FAILURE_RETRY (write (fildes, + (void *) aiocbp->aiocb64.aio_buf, + aiocbp->aiocb64.aio_nbytes)); + } + else if (aiocbp->aiocb.aio_lio_opcode == LIO_DSYNC) + aiocbp->aiocb.__return_value = + TEMP_FAILURE_RETRY (fdatasync (fildes)); + else if (aiocbp->aiocb.aio_lio_opcode == LIO_SYNC) + aiocbp->aiocb.__return_value = + TEMP_FAILURE_RETRY (fsync (fildes)); + else + { + /* This is an invalid opcode. */ + aiocbp->aiocb.__return_value = -1; + __set_errno (EINVAL); + } + + /* Get the mutex. */ + pthread_mutex_lock (&__aio_requests_mutex); + + /* In theory we would need here a write memory barrier since the + callers test using aio_error() whether the request finished + and once this value != EINPROGRESS the field __return_value + must be committed to memory. + + But since the pthread_mutex_lock call involves write memory + barriers as well it is not necessary. */ + + if (aiocbp->aiocb.__return_value == -1) + aiocbp->aiocb.__error_code = errno; + else + aiocbp->aiocb.__error_code = 0; + + /* Send the signal to notify about finished processing of the + request. */ + __aio_notify (runp); + + /* For debugging purposes we reset the running flag of the + finished request. */ + assert (runp->running == allocated); + runp->running = done; + + /* Now dequeue the current request. */ + __aio_remove_request (NULL, runp, 0); + if (runp->next_prio != NULL) + add_request_to_runlist (runp->next_prio); + + /* Free the old element. */ + __aio_free_request (runp); + } + + runp = runlist; + + /* If the runlist is empty, then we sleep for a while, waiting for + something to arrive in it. */ + if (runp == NULL && optim.aio_idle_time >= 0) + { + struct timeval now; + struct timespec wakeup_time; + + ++idle_thread_count; + gettimeofday (&now, NULL); + wakeup_time.tv_sec = now.tv_sec + optim.aio_idle_time; + wakeup_time.tv_nsec = now.tv_usec * 1000; + if (wakeup_time.tv_nsec > 1000000000) + { + wakeup_time.tv_nsec -= 1000000000; + ++wakeup_time.tv_sec; + } + pthread_cond_timedwait (&__aio_new_request_notification, + &__aio_requests_mutex, + &wakeup_time); + --idle_thread_count; + runp = runlist; + } + + if (runp == NULL) + --nthreads; + else + { + assert (runp->running == yes); + runp->running = allocated; + runlist = runp->next_run; + + /* If we have a request to process, and there's still another in + the run list, then we need to either wake up or create a new + thread to service the request that is still in the run list. */ + if (runlist != NULL) + { + /* There are at least two items in the work queue to work on. + If there are other idle threads, then we should wake them + up for these other work elements; otherwise, we should try + to create a new thread. */ + if (idle_thread_count > 0) + pthread_cond_signal (&__aio_new_request_notification); + else if (nthreads < optim.aio_threads) + { + pthread_t thid; + pthread_attr_t attr; + + /* Make sure the thread is created detached. */ + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + + /* Now try to start a thread. If we fail, no big deal, + because we know that there is at least one thread (us) + that is working on AIO operations. */ + if (pthread_create (&thid, &attr, handle_fildes_io, NULL) + == 0) + ++nthreads; + } + } + } + + /* Release the mutex. */ + pthread_mutex_unlock (&__aio_requests_mutex); + } + while (runp != NULL); + + pthread_exit (NULL); +} + + +/* Free allocated resources. */ +static void +__attribute__ ((unused)) +free_res (void) +{ + size_t row; + + for (row = 0; row < pool_max_size; ++row) + free (pool[row]); + + free (pool); +} +text_set_element (__libc_subfreeres, free_res); + + +/* Add newrequest to the runlist. The __abs_prio flag of newrequest must + be correctly set to do this. Also, you had better set newrequest's + "running" flag to "yes" before you release your lock or you'll throw an + assertion. */ +static void +add_request_to_runlist (struct requestlist *newrequest) +{ + int prio = newrequest->aiocbp->aiocb.__abs_prio; + struct requestlist *runp; + + if (runlist == NULL || runlist->aiocbp->aiocb.__abs_prio < prio) + { + newrequest->next_run = runlist; + runlist = newrequest; + } + else + { + runp = runlist; + + while (runp->next_run != NULL + && runp->next_run->aiocbp->aiocb.__abs_prio >= prio) + runp = runp->next_run; + + newrequest->next_run = runp->next_run; + runp->next_run = newrequest; + } +} diff --git a/sysdeps/pthread/aio_misc.h b/sysdeps/pthread/aio_misc.h new file mode 100644 index 0000000000..372b5556ca --- /dev/null +++ b/sysdeps/pthread/aio_misc.h @@ -0,0 +1,127 @@ +/* Copyright (C) 1997, 1999, 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#ifndef _AIO_MISC_H +#define _AIO_MISC_H 1 + +#include +#include + + +/* Extend the operation enum. */ +enum +{ + LIO_DSYNC = LIO_READ + 1, + LIO_SYNC, + LIO_READ64 = LIO_READ | 128, + LIO_WRITE64 = LIO_WRITE | 128 +}; + + +/* Union of the two request types. */ +typedef union + { + struct aiocb aiocb; + struct aiocb64 aiocb64; + } aiocb_union; + + +/* Used to synchronize. */ +struct waitlist + { + struct waitlist *next; + + pthread_cond_t *cond; + volatile int *counterp; + /* The next field is used in asynchronous `lio_listio' operations. */ + struct sigevent *sigevp; + /* XXX See requestlist, it's used to work around the broken signal + handling in Linux. */ + pid_t caller_pid; + }; + + +/* Status of a request. */ +enum +{ + no, + queued, + yes, + allocated, + done +}; + + +/* Used to queue requests.. */ +struct requestlist + { + int running; + + struct requestlist *last_fd; + struct requestlist *next_fd; + struct requestlist *next_prio; + struct requestlist *next_run; + + /* Pointer to the actual data. */ + aiocb_union *aiocbp; + + /* PID of the initiator thread. + XXX This is only necessary for the broken signal handling on Linux. */ + pid_t caller_pid; + + /* List of waiting processes. */ + struct waitlist *waiting; + }; + + +/* Lock for global I/O list of requests. */ +extern pthread_mutex_t __aio_requests_mutex; + + +/* Enqueue request. */ +extern struct requestlist *__aio_enqueue_request (aiocb_union *aiocbp, + int operation) + internal_function; + +/* Find request entry for given AIO control block. */ +extern struct requestlist *__aio_find_req (aiocb_union *elem) + internal_function; + +/* Find request entry for given file descriptor. */ +extern struct requestlist *__aio_find_req_fd (int fildes) internal_function; + +/* Remove request from the list. */ +extern void __aio_remove_request (struct requestlist *last, + struct requestlist *req, int all) + internal_function; + +/* Release the entry for the request. */ +extern void __aio_free_request (struct requestlist *req) internal_function; + +/* Notify initiator of request and tell this everybody listening. */ +extern void __aio_notify (struct requestlist *req) internal_function; + +/* Notify initiator of request. */ +extern int __aio_notify_only (struct sigevent *sigev, pid_t caller_pid) + internal_function; + +/* Send the signal. */ +extern int __aio_sigqueue (int sig, const union sigval val, pid_t caller_pid) + internal_function; + +#endif /* aio_misc.h */ diff --git a/sysdeps/pthread/aio_notify.c b/sysdeps/pthread/aio_notify.c new file mode 100644 index 0000000000..9b17750d03 --- /dev/null +++ b/sysdeps/pthread/aio_notify.c @@ -0,0 +1,119 @@ +/* Notify initiator of AIO request. + Copyright (C) 1997,98,99,2000,01 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include +#include +#include +#include +#include "aio_misc.h" + + +static void * +notify_func_wrapper (void *arg) +{ + struct sigevent *sigev = arg; + sigev->sigev_notify_function (sigev->sigev_value); + return NULL; +} + + +int +internal_function +__aio_notify_only (struct sigevent *sigev, pid_t caller_pid) +{ + int result = 0; + + /* Send the signal to notify about finished processing of the request. */ + if (sigev->sigev_notify == SIGEV_THREAD) + { + /* We have to start a thread. */ + pthread_t tid; + pthread_attr_t attr, *pattr; + + pattr = (pthread_attr_t *) sigev->sigev_notify_attributes; + if (pattr == NULL) + { + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + pattr = &attr; + } + + if (pthread_create (&tid, pattr, notify_func_wrapper, sigev) < 0) + result = -1; + } + else if (sigev->sigev_notify == SIGEV_SIGNAL) + { + /* We have to send a signal. */ +#if _POSIX_REALTIME_SIGNALS + /* Note that the standard gives us the option of using a plain + non-queuing signal here when SA_SIGINFO is not set for the signal. */ + if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid) + < 0) + result = -1; +#else + /* There are no queued signals on this system at all. */ + result = raise (sigev->sigev_signo); +#endif + } + + return result; +} + + +void +internal_function +__aio_notify (struct requestlist *req) +{ + struct waitlist *waitlist; + struct aiocb *aiocbp = &req->aiocbp->aiocb; + + if (__aio_notify_only (&aiocbp->aio_sigevent, req->caller_pid) != 0) + { + /* XXX What shall we do if already an error is set by + read/write/fsync? */ + aiocbp->__error_code = errno; + aiocbp->__return_value = -1; + } + + /* Now also notify possibly waiting threads. */ + waitlist = req->waiting; + while (waitlist != NULL) + { + struct waitlist *next = waitlist->next; + + /* Decrement the counter. This is used in both cases. */ + --*waitlist->counterp; + + if (waitlist->sigevp == NULL) + pthread_cond_signal (waitlist->cond); + else + /* This is part of a asynchronous `lio_listio' operation. If + this request is the last one, send the signal. */ + if (*waitlist->counterp == 0) + { + __aio_notify_only (waitlist->sigevp, waitlist->caller_pid); + /* This is tricky. See lio_listio.c for the reason why + this works. */ + free ((void *) waitlist->counterp); + } + + waitlist = next; + } +} diff --git a/sysdeps/pthread/aio_read.c b/sysdeps/pthread/aio_read.c new file mode 100644 index 0000000000..4b5d404190 --- /dev/null +++ b/sysdeps/pthread/aio_read.c @@ -0,0 +1,32 @@ +/* Asynchronous read. + Copyright (C) 1997, 1999 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include + +#include "aio_misc.h" + + +int +aio_read (aiocbp) + struct aiocb *aiocbp; +{ + return (__aio_enqueue_request ((aiocb_union *) aiocbp, LIO_READ) == NULL + ? -1 : 0); +} diff --git a/sysdeps/pthread/aio_read64.c b/sysdeps/pthread/aio_read64.c new file mode 100644 index 0000000000..0006ad6303 --- /dev/null +++ b/sysdeps/pthread/aio_read64.c @@ -0,0 +1,32 @@ +/* Asynchronous read, 64bit offset version. + Copyright (C) 1997, 1999 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include + +#include "aio_misc.h" + + +int +aio_read64 (aiocbp) + struct aiocb64 *aiocbp; +{ + return (__aio_enqueue_request ((aiocb_union *) aiocbp, LIO_READ64) == NULL + ? -1 : 0); +} diff --git a/sysdeps/pthread/aio_suspend.c b/sysdeps/pthread/aio_suspend.c new file mode 100644 index 0000000000..5ac2d23ffb --- /dev/null +++ b/sysdeps/pthread/aio_suspend.c @@ -0,0 +1,151 @@ +/* Suspend until termination of a requests. + Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + + +/* We use an UGLY hack to prevent gcc from finding us cheating. The + implementations of aio_suspend and aio_suspend64 are identical and so + we want to avoid code duplication by using aliases. But gcc sees + the different parameter lists and prints a warning. We define here + a function so that aio_suspend64 has no prototype. */ +#define aio_suspend64 XXX +#include +/* And undo the hack. */ +#undef aio_suspend64 + +#include +#include +#include + +#include "aio_misc.h" + + +int +aio_suspend (list, nent, timeout) + const struct aiocb *const list[]; + int nent; + const struct timespec *timeout; +{ + struct waitlist waitlist[nent]; + struct requestlist *requestlist[nent]; + pthread_cond_t cond = PTHREAD_COND_INITIALIZER; + int cnt; + int result = 0; + int dummy; + int none = 1; + + /* Request the mutex. */ + pthread_mutex_lock (&__aio_requests_mutex); + + /* There is not yet a finished request. Signal the request that + we are working for it. */ + for (cnt = 0; cnt < nent; ++cnt) + if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS) + { + requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]); + + if (requestlist[cnt] != NULL) + { + waitlist[cnt].cond = &cond; + waitlist[cnt].next = requestlist[cnt]->waiting; + waitlist[cnt].counterp = &dummy; + waitlist[cnt].sigevp = NULL; + waitlist[cnt].caller_pid = 0; /* Not needed. */ + requestlist[cnt]->waiting = &waitlist[cnt]; + none = 0; + } + } + + /* If there is a not finished request wait for it. */ + if (!none) + { + int oldstate; + + /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation + points we must be careful. We added entries to the waiting lists + which we must remove. So defer cancelation for now. */ + pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate); + + if (timeout == NULL) + result = pthread_cond_wait (&cond, &__aio_requests_mutex); + else + { + /* We have to convert the relative timeout value into an + absolute time value with pthread_cond_timedwait expects. */ + struct timeval now; + struct timespec abstime; + + __gettimeofday (&now, NULL); + abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000; + abstime.tv_sec = timeout->tv_sec + now.tv_sec; + if (abstime.tv_nsec >= 1000000000) + { + abstime.tv_nsec -= 1000000000; + abstime.tv_sec += 1; + } + + result = pthread_cond_timedwait (&cond, &__aio_requests_mutex, + &abstime); + } + + /* Now remove the entry in the waiting list for all requests + which didn't terminate. */ + for (cnt = 0; cnt < nent; ++cnt) + if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS + && requestlist[cnt] != NULL) + { + struct waitlist **listp = &requestlist[cnt]->waiting; + + /* There is the chance that we cannot find our entry anymore. + This could happen if the request terminated and restarted + again. */ + while (*listp != NULL && *listp != &waitlist[cnt]) + listp = &(*listp)->next; + + if (*listp != NULL) + *listp = (*listp)->next; + } + + /* Now it's time to restore the cancelation state. */ + pthread_setcancelstate (oldstate, NULL); + + /* Release the conditional variable. */ + if (pthread_cond_destroy (&cond) != 0) + /* This must never happen. */ + abort (); + + if (result != 0) + { + /* An error occurred. Possibly it's EINTR. We have to translate + the timeout error report of `pthread_cond_timedwait' to the + form expected from `aio_suspend'. */ + if (result == ETIMEDOUT) + __set_errno (EAGAIN); + + result = -1; + } + } + + /* Release the mutex. */ + pthread_mutex_unlock (&__aio_requests_mutex); + + return result; +} + +weak_alias (aio_suspend, aio_suspend64) diff --git a/sysdeps/pthread/aio_write.c b/sysdeps/pthread/aio_write.c new file mode 100644 index 0000000000..77ad0a685b --- /dev/null +++ b/sysdeps/pthread/aio_write.c @@ -0,0 +1,32 @@ +/* Asynchronous write. + Copyright (C) 1997, 1999 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include + +#include "aio_misc.h" + + +int +aio_write (aiocbp) + struct aiocb *aiocbp; +{ + return (__aio_enqueue_request ((aiocb_union *) aiocbp, LIO_WRITE) == NULL + ? -1 : 0); +} diff --git a/sysdeps/pthread/aio_write64.c b/sysdeps/pthread/aio_write64.c new file mode 100644 index 0000000000..bc95e50b49 --- /dev/null +++ b/sysdeps/pthread/aio_write64.c @@ -0,0 +1,32 @@ +/* Asynchronous write, 64bit offset version. + Copyright (C) 1997, 1999 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include + +#include "aio_misc.h" + + +int +aio_write64 (aiocbp) + struct aiocb64 *aiocbp; +{ + return (__aio_enqueue_request ((aiocb_union *) aiocbp, LIO_WRITE64) == NULL + ? -1 : 0); +} diff --git a/sysdeps/pthread/lio_listio.c b/sysdeps/pthread/lio_listio.c new file mode 100644 index 0000000000..e1df603fd8 --- /dev/null +++ b/sysdeps/pthread/lio_listio.c @@ -0,0 +1,187 @@ +/* Enqueue and list of read or write requests. + Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include +#include +#include +#include +#include + +#include "aio_misc.h" + + +/* We need this special structure to handle asynchronous I/O. */ +struct async_waitlist + { + int counter; + struct sigevent sigev; + struct waitlist list[0]; + }; + + +int +lio_listio (mode, list, nent, sig) + int mode; + struct aiocb *const list[]; + int nent; + struct sigevent *sig; +{ + struct sigevent defsigev; + struct requestlist *requests[nent]; + int cnt; + volatile int total = 0; + int result = 0; + + /* Check arguments. */ + if (mode != LIO_WAIT && mode != LIO_NOWAIT) + { + __set_errno (EINVAL); + return -1; + } + + if (sig == NULL) + { + defsigev.sigev_notify = SIGEV_NONE; + sig = &defsigev; + } + + /* Request the mutex. */ + pthread_mutex_lock (&__aio_requests_mutex); + + /* Now we can enqueue all requests. Since we already acquired the + mutex the enqueue function need not do this. */ + for (cnt = 0; cnt < nent; ++cnt) + if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP) + { + list[cnt]->aio_sigevent.sigev_notify = SIGEV_NONE; + requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt], + list[cnt]->aio_lio_opcode); + + if (requests[cnt] != NULL) + /* Successfully enqueued. */ + ++total; + else + /* Signal that we've seen an error. `errno' and the error code + of the aiocb will tell more. */ + result = -1; + } + else + requests[cnt] = NULL; + + if (total == 0) + { + /* We don't have anything to do except signalling if we work + asynchronously. */ + + /* Release the mutex. We do this before raising a signal since the + signal handler might do a `siglongjmp' and then the mutex is + locked forever. */ + pthread_mutex_unlock (&__aio_requests_mutex); + + if (mode == LIO_NOWAIT) + __aio_notify_only (sig, + sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0); + + return result; + } + else if (mode == LIO_WAIT) + { + pthread_cond_t cond = PTHREAD_COND_INITIALIZER; + struct waitlist waitlist[nent]; + int oldstate; + + total = 0; + for (cnt = 0; cnt < nent; ++cnt) + { + assert (requests[cnt] == NULL || list[cnt] != NULL); + + if (requests[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP) + { + waitlist[cnt].cond = &cond; + waitlist[cnt].next = requests[cnt]->waiting; + waitlist[cnt].counterp = &total; + waitlist[cnt].sigevp = NULL; + waitlist[cnt].caller_pid = 0; /* Not needed. */ + requests[cnt]->waiting = &waitlist[cnt]; + ++total; + } + } + + /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation + points we must be careful. We added entries to the waiting lists + which we must remove. So defer cancelation for now. */ + pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate); + + while (total > 0) + pthread_cond_wait (&cond, &__aio_requests_mutex); + + /* Now it's time to restore the cancelation state. */ + pthread_setcancelstate (oldstate, NULL); + + /* Release the conditional variable. */ + if (pthread_cond_destroy (&cond) != 0) + /* This must never happen. */ + abort (); + } + else + { + struct async_waitlist *waitlist; + + waitlist = (struct async_waitlist *) + malloc (sizeof (struct async_waitlist) + + (nent * sizeof (struct waitlist))); + + if (waitlist == NULL) + { + __set_errno (EAGAIN); + result = -1; + } + else + { + pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0; + total = 0; + + for (cnt = 0; cnt < nent; ++cnt) + { + assert (requests[cnt] == NULL || list[cnt] != NULL); + + if (requests[cnt] != NULL + && list[cnt]->aio_lio_opcode != LIO_NOP) + { + waitlist->list[cnt].cond = NULL; + waitlist->list[cnt].next = requests[cnt]->waiting; + waitlist->list[cnt].counterp = &waitlist->counter; + waitlist->list[cnt].sigevp = &waitlist->sigev; + waitlist->list[cnt].caller_pid = caller_pid; + requests[cnt]->waiting = &waitlist->list[cnt]; + ++total; + } + } + + waitlist->counter = total; + waitlist->sigev = *sig; + } + } + + /* Release the mutex. */ + pthread_mutex_unlock (&__aio_requests_mutex); + + return result; +} diff --git a/sysdeps/pthread/lio_listio64.c b/sysdeps/pthread/lio_listio64.c new file mode 100644 index 0000000000..5df2596c0f --- /dev/null +++ b/sysdeps/pthread/lio_listio64.c @@ -0,0 +1,169 @@ +/* Enqueue and list of read or write requests, 64bit offset version. + Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include +#include +#include +#include + +#include "aio_misc.h" + + +/* We need this special structure to handle asynchronous I/O. */ +struct async_waitlist + { + int counter; + struct sigevent sigev; + struct waitlist list[0]; + }; + + +int +lio_listio64 (mode, list, nent, sig) + int mode; + struct aiocb64 *const list[]; + int nent; + struct sigevent *sig; +{ + struct requestlist *requests[nent]; + int cnt; + volatile int total = 0; + int result = 0; + + /* Check arguments. */ + if (mode != LIO_WAIT && mode != LIO_NOWAIT) + { + __set_errno (EINVAL); + return -1; + } + + /* Request the mutex. */ + pthread_mutex_lock (&__aio_requests_mutex); + + /* Now we can enqueue all requests. Since we already acquired the + mutex the enqueue function need not do this. */ + for (cnt = 0; cnt < nent; ++cnt) + if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP) + { + requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt], + (list[cnt]->aio_lio_opcode + | 128)); + if (requests[cnt] != NULL) + /* Successfully enqueued. */ + ++total; + else + /* Signal that we've seen an error. `errno' and the error code + of the aiocb will tell more. */ + result = -1; + } + + if (total == 0) + { + /* We don't have anything to do except signalling if we work + asynchronously. */ + + /* Release the mutex. We do this before raising a signal since the + signal handler might do a `siglongjmp' and then the mutex is + locked forever. */ + pthread_mutex_unlock (&__aio_requests_mutex); + + if (mode == LIO_NOWAIT) + __aio_notify_only (sig, + sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0); + + return result; + } + else if (mode == LIO_WAIT) + { + pthread_cond_t cond = PTHREAD_COND_INITIALIZER; + struct waitlist waitlist[nent]; + int oldstate; + + total = 0; + for (cnt = 0; cnt < nent; ++cnt) + if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP + && requests[cnt] != NULL) + { + waitlist[cnt].cond = &cond; + waitlist[cnt].next = requests[cnt]->waiting; + waitlist[cnt].counterp = &total; + waitlist[cnt].sigevp = NULL; + waitlist[cnt].caller_pid = 0; /* Not needed. */ + requests[cnt]->waiting = &waitlist[cnt]; + ++total; + } + + /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation + points we must be careful. We added entries to the waiting lists + which we must remove. So defer cancelation for now. */ + pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate); + + while (total > 0) + pthread_cond_wait (&cond, &__aio_requests_mutex); + + /* Now it's time to restore the cancelation state. */ + pthread_setcancelstate (oldstate, NULL); + + /* Release the conditional variable. */ + if (pthread_cond_destroy (&cond) != 0) + /* This must never happen. */ + abort (); + } + else + { + struct async_waitlist *waitlist; + + waitlist = (struct async_waitlist *) + malloc (sizeof (struct async_waitlist) + + (nent * sizeof (struct waitlist))); + + if (waitlist == NULL) + { + __set_errno (EAGAIN); + result = -1; + } + else + { + pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0; + total = 0; + + for (cnt = 0; cnt < nent; ++cnt) + if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP + && requests[cnt] != NULL) + { + waitlist->list[cnt].cond = NULL; + waitlist->list[cnt].next = requests[cnt]->waiting; + waitlist->list[cnt].counterp = &waitlist->counter; + waitlist->list[cnt].sigevp = &waitlist->sigev; + waitlist->list[cnt].caller_pid = caller_pid; + requests[cnt]->waiting = &waitlist->list[cnt]; + ++total; + } + + waitlist->counter = total; + waitlist->sigev = *sig; + } + } + + /* Release the mutex. */ + pthread_mutex_unlock (&__aio_requests_mutex); + + return result; +} -- cgit 1.4.1