1 /* Handle general operations.
2 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
33 static void add_request_to_runlist (struct requestlist *newrequest);
35 /* Pool of request list entries. */
36 static struct requestlist **pool;
38 /* Number of total and allocated pool entries. */
39 static size_t pool_tab_size;
40 static size_t pool_size;
42 /* We implement a two dimensional array but allocate each row separately.
43 The macro below determines how many entries should be used per row.
44 It should better be a power of two. */
45 #define ENTRIES_PER_ROW 16
47 /* The row table is incremented in units of this. */
50 /* List of available entries. */
51 static struct requestlist *freelist;
53 /* List of request waiting to be processed. */
54 static struct requestlist *runlist;
56 /* Structure list of all currently processed requests. */
57 static struct requestlist *requests;
59 /* Number of threads currently running. */
62 /* Number of threads waiting for work to arrive. */
63 static int idle_thread_count;
66 /* These are the values used to optimize the use of AIO. The user can
67 overwrite them by using the `aio_init' function. */
68 static struct aioinit optim =
70 20, /* int aio_threads; Maximal number of threads. */
71 256, /* int aio_num; Number of expected simultanious requests. */
81 /* Since the list is global we need a mutex protecting it. */
82 pthread_mutex_t __aio_requests_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
84 /* When you add a request to the list and there are idle threads present,
85 you signal this condition variable. When a thread finishes work, it waits
86 on this condition variable for a time before it actually exits. */
87 pthread_cond_t __aio_new_request_notification = PTHREAD_COND_INITIALIZER;
90 /* Functions to handle request list pool. */
91 static struct requestlist *
94 struct requestlist *result;
98 struct requestlist *new_row;
101 assert (sizeof (struct aiocb) == sizeof (struct aiocb64));
103 /* Compute new size. */
104 new_size = pool_size ? pool_size + ENTRIES_PER_ROW : optim.aio_num;
106 if ((new_size / ENTRIES_PER_ROW) >= pool_tab_size)
108 size_t new_tab_size = new_size / ENTRIES_PER_ROW;
109 struct requestlist **new_tab;
111 new_tab = (struct requestlist **)
112 realloc (pool, (new_tab_size * sizeof (struct requestlist *)));
117 pool_tab_size = new_tab_size;
125 new_row = (struct requestlist *)
126 calloc (new_size, sizeof (struct requestlist));
131 for (cnt = 0; cnt < new_size / ENTRIES_PER_ROW; ++cnt)
132 pool[cnt] = &new_row[cnt * ENTRIES_PER_ROW];
136 /* Allocat one new row. */
137 new_row = (struct requestlist *)
138 calloc (ENTRIES_PER_ROW, sizeof (struct requestlist));
142 pool[new_size / ENTRIES_PER_ROW - 1] = new_row;
145 /* Put all the new entries in the freelist. */
148 new_row->next_prio = freelist;
149 freelist = new_row++;
151 while (++pool_size < new_size);
155 freelist = freelist->next_prio;
163 __aio_free_request (struct requestlist *elem)
166 elem->next_prio = freelist;
173 __aio_find_req (aiocb_union *elem)
175 struct requestlist *runp = requests;
176 int fildes = elem->aiocb.aio_fildes;
178 while (runp != NULL && runp->aiocbp->aiocb.aio_fildes < fildes)
179 runp = runp->next_fd;
183 if (runp->aiocbp->aiocb.aio_fildes != fildes)
186 while (runp != NULL && runp->aiocbp != elem)
187 runp = runp->next_prio;
196 __aio_find_req_fd (int fildes)
198 struct requestlist *runp = requests;
200 while (runp != NULL && runp->aiocbp->aiocb.aio_fildes < fildes)
201 runp = runp->next_fd;
203 return (runp != NULL && runp->aiocbp->aiocb.aio_fildes == fildes
210 __aio_remove_request (struct requestlist *last, struct requestlist *req,
214 last->next_prio = req->next_prio;
217 if (all || req->next_prio == NULL)
219 if (req->last_fd != NULL)
220 req->last_fd->next_fd = req->next_fd;
222 requests = req->next_fd;
223 if (req->next_fd != NULL)
224 req->next_fd->last_fd = req->last_fd;
228 if (req->last_fd != NULL)
229 req->last_fd->next_fd = req->next_prio;
231 requests = req->next_prio;
233 if (req->next_fd != NULL)
234 req->next_fd->last_fd = req->next_prio;
236 req->next_prio->last_fd = req->last_fd;
237 req->next_prio->next_fd = req->next_fd;
239 /* Mark this entry as runnable. */
240 req->next_prio->running = yes;
243 if (req->running == yes)
245 struct requestlist *runp = runlist;
253 runlist = runp->next_run;
255 last->next_run = runp->next_run;
259 runp = runp->next_run;
266 /* The thread handler. */
267 static void *handle_fildes_io (void *arg);
270 /* User optimization. */
272 __aio_init (const struct aioinit *init)
275 pthread_mutex_lock (&__aio_requests_mutex);
277 /* Only allow writing new values if the table is not yet allocated. */
280 optim.aio_threads = init->aio_threads < 1 ? 1 : init->aio_threads;
281 optim.aio_num = (init->aio_num < ENTRIES_PER_ROW
283 : init->aio_num & ~ENTRIES_PER_ROW);
286 if (init->aio_idle_time != 0)
287 optim.aio_idle_time = init->aio_idle_time;
289 /* Release the mutex. */
290 pthread_mutex_unlock (&__aio_requests_mutex);
292 weak_alias (__aio_init, aio_init)
295 /* The main function of the async I/O handling. It enqueues requests
296 and if necessary starts and handles threads. */
299 __aio_enqueue_request (aiocb_union *aiocbp, int operation)
303 struct sched_param param;
304 struct requestlist *last, *runp, *newp;
307 if (operation == LIO_SYNC || operation == LIO_DSYNC)
308 aiocbp->aiocb.aio_reqprio = 0;
309 else if (aiocbp->aiocb.aio_reqprio < 0
310 || aiocbp->aiocb.aio_reqprio > AIO_PRIO_DELTA_MAX)
312 /* Invalid priority value. */
313 __set_errno (EINVAL);
314 aiocbp->aiocb.__error_code = EINVAL;
315 aiocbp->aiocb.__return_value = -1;
319 /* Compute priority for this request. */
320 pthread_getschedparam (pthread_self (), &policy, ¶m);
321 prio = param.sched_priority - aiocbp->aiocb.aio_reqprio;
324 pthread_mutex_lock (&__aio_requests_mutex);
328 /* First look whether the current file descriptor is currently
331 && runp->aiocbp->aiocb.aio_fildes < aiocbp->aiocb.aio_fildes)
334 runp = runp->next_fd;
337 /* Get a new element for the waiting list. */
341 pthread_mutex_unlock (&__aio_requests_mutex);
342 __set_errno (EAGAIN);
345 newp->aiocbp = aiocbp;
346 newp->caller_pid = (aiocbp->aiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL
348 newp->waiting = NULL;
350 aiocbp->aiocb.__abs_prio = prio;
351 aiocbp->aiocb.__policy = policy;
352 aiocbp->aiocb.aio_lio_opcode = operation;
353 aiocbp->aiocb.__error_code = EINPROGRESS;
354 aiocbp->aiocb.__return_value = 0;
357 && runp->aiocbp->aiocb.aio_fildes == aiocbp->aiocb.aio_fildes)
359 /* The current file descriptor is worked on. It makes no sense
360 to start another thread since this new thread would fight
361 with the running thread for the resources. But we also cannot
362 say that the thread processing this desriptor shall immediately
363 after finishing the current job process this request if there
364 are other threads in the running queue which have a higher
367 /* Simply enqueue it after the running one according to the
369 while (runp->next_prio != NULL
370 && runp->next_prio->aiocbp->aiocb.__abs_prio >= prio)
371 runp = runp->next_prio;
373 newp->next_prio = runp->next_prio;
374 runp->next_prio = newp;
381 /* Enqueue this request for a new descriptor. */
384 newp->last_fd = NULL;
385 newp->next_fd = requests;
386 if (requests != NULL)
387 requests->last_fd = newp;
392 newp->next_fd = last->next_fd;
393 newp->last_fd = last;
394 last->next_fd = newp;
395 if (newp->next_fd != NULL)
396 newp->next_fd->last_fd = newp;
399 newp->next_prio = NULL;
404 /* We try to create a new thread for this file descriptor. The
405 function which gets called will handle all available requests
406 for this descriptor and when all are processed it will
409 If no new thread can be created or if the specified limit of
410 threads for AIO is reached we queue the request. */
412 /* See if we need to and are able to create a thread. */
413 if (nthreads < optim.aio_threads && idle_thread_count == 0)
418 /* Make sure the thread is created detached. */
419 pthread_attr_init (&attr);
420 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
422 /* Now try to start a thread. */
423 if (pthread_create (&thid, &attr, handle_fildes_io, newp) == 0)
425 /* We managed to enqueue the request. All errors which can
426 happen now can be recognized by calls to `aio_return' and
431 else if (nthreads == 0)
432 /* We cannot create a thread in the moment and there is
433 also no thread running. This is a problem. `errno' is
434 set to EAGAIN if this is only a temporary problem. */
439 /* Enqueue the request in the run queue if it is not yet running. */
440 if (running == yes && result == 0)
442 add_request_to_runlist (newp);
444 /* If there is a thread waiting for work, then let it know that we
445 have just given it something to do. */
446 if (idle_thread_count > 0)
447 pthread_cond_signal (&__aio_new_request_notification);
451 newp->running = running;
454 /* Something went wrong. */
455 __aio_free_request (newp);
459 /* Release the mutex. */
460 pthread_mutex_unlock (&__aio_requests_mutex);
467 handle_fildes_io (void *arg)
469 pthread_t self = pthread_self ();
470 struct sched_param param;
471 struct requestlist *runp = (struct requestlist *) arg;
476 pthread_getschedparam (self, &policy, ¶m);
480 /* If runp is NULL, then we were created to service the work queue
481 in general, not to handle any particular request. In that case we
482 skip the "do work" stuff on the first pass, and go directly to the
483 "get work off the work queue" part of this loop, which is near the
486 pthread_mutex_lock (&__aio_requests_mutex);
489 /* Update our variables. */
490 aiocbp = runp->aiocbp;
491 fildes = aiocbp->aiocb.aio_fildes;
493 /* Change the priority to the requested value (if necessary). */
494 if (aiocbp->aiocb.__abs_prio != param.sched_priority
495 || aiocbp->aiocb.__policy != policy)
497 param.sched_priority = aiocbp->aiocb.__abs_prio;
498 policy = aiocbp->aiocb.__policy;
499 pthread_setschedparam (self, policy, ¶m);
502 /* Process request pointed to by RUNP. We must not be disturbed
504 if ((aiocbp->aiocb.aio_lio_opcode & 127) == LIO_READ)
506 if (aiocbp->aiocb.aio_lio_opcode & 128)
507 aiocbp->aiocb.__return_value =
508 TEMP_FAILURE_RETRY (__pread64 (fildes, (void *)
509 aiocbp->aiocb64.aio_buf,
510 aiocbp->aiocb64.aio_nbytes,
511 aiocbp->aiocb64.aio_offset));
513 aiocbp->aiocb.__return_value =
514 TEMP_FAILURE_RETRY (pread (fildes,
515 (void *) aiocbp->aiocb.aio_buf,
516 aiocbp->aiocb.aio_nbytes,
517 aiocbp->aiocb.aio_offset));
519 if (aiocbp->aiocb.__return_value == -1 && errno == ESPIPE)
520 /* The Linux kernel is different from others. It returns
521 ESPIPE if using pread on a socket. Other platforms
522 simply ignore the offset parameter and behave like
524 aiocbp->aiocb.__return_value =
525 TEMP_FAILURE_RETRY (read (fildes,
526 (void *) aiocbp->aiocb64.aio_buf,
527 aiocbp->aiocb64.aio_nbytes));
529 else if ((aiocbp->aiocb.aio_lio_opcode & 127) == LIO_WRITE)
531 if (aiocbp->aiocb.aio_lio_opcode & 128)
532 aiocbp->aiocb.__return_value =
533 TEMP_FAILURE_RETRY (__pwrite64 (fildes, (const void *)
534 aiocbp->aiocb64.aio_buf,
535 aiocbp->aiocb64.aio_nbytes,
536 aiocbp->aiocb64.aio_offset));
538 aiocbp->aiocb.__return_value =
539 TEMP_FAILURE_RETRY (pwrite (fildes, (const void *)
540 aiocbp->aiocb.aio_buf,
541 aiocbp->aiocb.aio_nbytes,
542 aiocbp->aiocb.aio_offset));
544 if (aiocbp->aiocb.__return_value == -1 && errno == ESPIPE)
545 /* The Linux kernel is different from others. It returns
546 ESPIPE if using pwrite on a socket. Other platforms
547 simply ignore the offset parameter and behave like
549 aiocbp->aiocb.__return_value =
550 TEMP_FAILURE_RETRY (write (fildes,
551 (void *) aiocbp->aiocb64.aio_buf,
552 aiocbp->aiocb64.aio_nbytes));
554 else if (aiocbp->aiocb.aio_lio_opcode == LIO_DSYNC)
555 aiocbp->aiocb.__return_value =
556 TEMP_FAILURE_RETRY (fdatasync (fildes));
557 else if (aiocbp->aiocb.aio_lio_opcode == LIO_SYNC)
558 aiocbp->aiocb.__return_value =
559 TEMP_FAILURE_RETRY (fsync (fildes));
562 /* This is an invalid opcode. */
563 aiocbp->aiocb.__return_value = -1;
564 __set_errno (EINVAL);
568 pthread_mutex_lock (&__aio_requests_mutex);
570 /* In theory we would need here a write memory barrier since the
571 callers test using aio_error() whether the request finished
572 and once this value != EINPROGRESS the field __return_value
573 must be committed to memory.
575 But since the pthread_mutex_lock call involves write memory
576 barriers as well it is not necessary. */
578 if (aiocbp->aiocb.__return_value == -1)
579 aiocbp->aiocb.__error_code = errno;
581 aiocbp->aiocb.__error_code = 0;
583 /* Send the signal to notify about finished processing of the
587 /* Now dequeue the current request. */
588 __aio_remove_request (NULL, runp, 0);
589 if (runp->next_prio != NULL)
590 add_request_to_runlist (runp->next_prio);
592 /* Free the old element. */
593 __aio_free_request (runp);
598 /* If the runlist is empty, then we sleep for a while, waiting for
599 something to arrive in it. */
600 if (runp == NULL && optim.aio_idle_time >= 0)
603 struct timespec wakeup_time;
606 gettimeofday (&now, NULL);
607 wakeup_time.tv_sec = now.tv_sec + optim.aio_idle_time;
608 wakeup_time.tv_nsec = now.tv_usec * 1000;
609 if (wakeup_time.tv_nsec > 1000000000)
611 wakeup_time.tv_nsec -= 1000000000;
612 ++wakeup_time.tv_sec;
614 pthread_cond_timedwait (&__aio_new_request_notification,
615 &__aio_requests_mutex,
625 assert (runp->running == yes);
626 runp->running = allocated;
627 runlist = runp->next_run;
629 /* If we have a request to process, and there's still another in
630 the run list, then we need to either wake up or create a new
631 thread to service the request that is still in the run list. */
634 /* There are at least two items in the work queue to work on.
635 If there are other idle threads, then we should wake them
636 up for these other work elements; otherwise, we should try
637 to create a new thread. */
638 if (idle_thread_count > 0)
639 pthread_cond_signal (&__aio_new_request_notification);
640 else if (nthreads < optim.aio_threads)
645 /* Make sure the thread is created detached. */
646 pthread_attr_init (&attr);
647 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
649 /* Now try to start a thread. If we fail, no big deal,
650 because we know that there is at least one thread (us)
651 that is working on AIO operations. */
652 if (pthread_create (&thid, &attr, handle_fildes_io, NULL)
659 /* Release the mutex. */
660 pthread_mutex_unlock (&__aio_requests_mutex);
662 while (runp != NULL);
668 /* Free allocated resources. */
670 __attribute__ ((unused))
675 /* The first block of rows as specified in OPTIM is allocated in
679 for (row = optim.aio_num / ENTRIES_PER_ROW; row < pool_tab_size; ++row)
684 text_set_element (__libc_subfreeres, free_res);
687 /* Add newrequest to the runlist. The __abs_prio flag of newrequest must
688 be correctly set to do this. Also, you had better set newrequest's
689 "running" flag to "yes" before you release your lock or you'll throw an
692 add_request_to_runlist (struct requestlist *newrequest)
694 int prio = newrequest->aiocbp->aiocb.__abs_prio;
695 struct requestlist *runp;
697 if (runlist == NULL || runlist->aiocbp->aiocb.__abs_prio < prio)
699 newrequest->next_run = runlist;
700 runlist = newrequest;
706 while (runp->next_run != NULL
707 && runp->next_run->aiocbp->aiocb.__abs_prio >= prio)
708 runp = runp->next_run;
710 newrequest->next_run = runp->next_run;
711 runp->next_run = newrequest;