1 /* Handle general operations.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
33 #ifndef aio_create_helper_thread
34 # define aio_create_helper_thread __aio_create_helper_thread
37 __aio_create_helper_thread (pthread_t
*threadp
, void *(*tf
) (void *), void *arg
)
41 /* Make sure the thread is created detached. */
42 pthread_attr_init (&attr
);
43 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
45 int ret
= pthread_create (threadp
, &attr
, tf
, arg
);
47 (void) pthread_attr_destroy (&attr
);
53 static void add_request_to_runlist (struct requestlist
*newrequest
);
55 /* Pool of request list entries. */
56 static struct requestlist
**pool
;
58 /* Number of total and allocated pool entries. */
59 static size_t pool_max_size
;
60 static size_t pool_size
;
62 /* We implement a two dimensional array but allocate each row separately.
63 The macro below determines how many entries should be used per row.
64 It should better be a power of two. */
65 #define ENTRIES_PER_ROW 32
67 /* How many rows we allocate at once. */
70 /* List of available entries. */
71 static struct requestlist
*freelist
;
73 /* List of request waiting to be processed. */
74 static struct requestlist
*runlist
;
76 /* Structure list of all currently processed requests. */
77 static struct requestlist
*requests
;
79 /* Number of threads currently running. */
82 /* Number of threads waiting for work to arrive. */
83 static int idle_thread_count
;
86 /* These are the values used to optimize the use of AIO. The user can
87 overwrite them by using the `aio_init' function. */
88 static struct aioinit optim
=
90 20, /* int aio_threads; Maximal number of threads. */
91 64, /* int aio_num; Number of expected simultanious requests. */
101 /* Since the list is global we need a mutex protecting it. */
102 pthread_mutex_t __aio_requests_mutex
= PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
;
104 /* When you add a request to the list and there are idle threads present,
105 you signal this condition variable. When a thread finishes work, it waits
106 on this condition variable for a time before it actually exits. */
107 pthread_cond_t __aio_new_request_notification
= PTHREAD_COND_INITIALIZER
;
110 /* Functions to handle request list pool. */
111 static struct requestlist
*
114 struct requestlist
*result
;
116 if (freelist
== NULL
)
118 struct requestlist
*new_row
;
121 assert (sizeof (struct aiocb
) == sizeof (struct aiocb64
));
123 if (pool_size
+ 1 >= pool_max_size
)
125 size_t new_max_size
= pool_max_size
+ ROWS_STEP
;
126 struct requestlist
**new_tab
;
128 new_tab
= (struct requestlist
**)
129 realloc (pool
, new_max_size
* sizeof (struct requestlist
*));
134 pool_max_size
= new_max_size
;
138 /* Allocate the new row. */
139 cnt
= pool_size
== 0 ? optim
.aio_num
: ENTRIES_PER_ROW
;
140 new_row
= (struct requestlist
*) calloc (cnt
,
141 sizeof (struct requestlist
));
145 pool
[pool_size
++] = new_row
;
147 /* Put all the new entries in the freelist. */
150 new_row
->next_prio
= freelist
;
151 freelist
= new_row
++;
157 freelist
= freelist
->next_prio
;
165 __aio_free_request (struct requestlist
*elem
)
168 elem
->next_prio
= freelist
;
175 __aio_find_req (aiocb_union
*elem
)
177 struct requestlist
*runp
= requests
;
178 int fildes
= elem
->aiocb
.aio_fildes
;
180 while (runp
!= NULL
&& runp
->aiocbp
->aiocb
.aio_fildes
< fildes
)
181 runp
= runp
->next_fd
;
185 if (runp
->aiocbp
->aiocb
.aio_fildes
!= fildes
)
188 while (runp
!= NULL
&& runp
->aiocbp
!= elem
)
189 runp
= runp
->next_prio
;
198 __aio_find_req_fd (int fildes
)
200 struct requestlist
*runp
= requests
;
202 while (runp
!= NULL
&& runp
->aiocbp
->aiocb
.aio_fildes
< fildes
)
203 runp
= runp
->next_fd
;
205 return (runp
!= NULL
&& runp
->aiocbp
->aiocb
.aio_fildes
== fildes
212 __aio_remove_request (struct requestlist
*last
, struct requestlist
*req
,
215 assert (req
->running
== yes
|| req
->running
== queued
216 || req
->running
== done
);
219 last
->next_prio
= all
? NULL
: req
->next_prio
;
222 if (all
|| req
->next_prio
== NULL
)
224 if (req
->last_fd
!= NULL
)
225 req
->last_fd
->next_fd
= req
->next_fd
;
227 requests
= req
->next_fd
;
228 if (req
->next_fd
!= NULL
)
229 req
->next_fd
->last_fd
= req
->last_fd
;
233 if (req
->last_fd
!= NULL
)
234 req
->last_fd
->next_fd
= req
->next_prio
;
236 requests
= req
->next_prio
;
238 if (req
->next_fd
!= NULL
)
239 req
->next_fd
->last_fd
= req
->next_prio
;
241 req
->next_prio
->last_fd
= req
->last_fd
;
242 req
->next_prio
->next_fd
= req
->next_fd
;
244 /* Mark this entry as runnable. */
245 req
->next_prio
->running
= yes
;
248 if (req
->running
== yes
)
250 struct requestlist
*runp
= runlist
;
258 runlist
= runp
->next_run
;
260 last
->next_run
= runp
->next_run
;
264 runp
= runp
->next_run
;
271 /* The thread handler. */
272 static void *handle_fildes_io (void *arg
);
275 /* User optimization. */
277 __aio_init (const struct aioinit
*init
)
280 pthread_mutex_lock (&__aio_requests_mutex
);
282 /* Only allow writing new values if the table is not yet allocated. */
285 optim
.aio_threads
= init
->aio_threads
< 1 ? 1 : init
->aio_threads
;
286 optim
.aio_num
= (init
->aio_num
< ENTRIES_PER_ROW
288 : init
->aio_num
& ~ENTRIES_PER_ROW
);
291 if (init
->aio_idle_time
!= 0)
292 optim
.aio_idle_time
= init
->aio_idle_time
;
294 /* Release the mutex. */
295 pthread_mutex_unlock (&__aio_requests_mutex
);
297 weak_alias (__aio_init
, aio_init
)
300 /* The main function of the async I/O handling. It enqueues requests
301 and if necessary starts and handles threads. */
304 __aio_enqueue_request (aiocb_union
*aiocbp
, int operation
)
308 struct sched_param param
;
309 struct requestlist
*last
, *runp
, *newp
;
312 if (operation
== LIO_SYNC
|| operation
== LIO_DSYNC
)
313 aiocbp
->aiocb
.aio_reqprio
= 0;
314 else if (aiocbp
->aiocb
.aio_reqprio
< 0
315 || aiocbp
->aiocb
.aio_reqprio
> AIO_PRIO_DELTA_MAX
)
317 /* Invalid priority value. */
318 __set_errno (EINVAL
);
319 aiocbp
->aiocb
.__error_code
= EINVAL
;
320 aiocbp
->aiocb
.__return_value
= -1;
324 /* Compute priority for this request. */
325 pthread_getschedparam (pthread_self (), &policy
, ¶m
);
326 prio
= param
.sched_priority
- aiocbp
->aiocb
.aio_reqprio
;
329 pthread_mutex_lock (&__aio_requests_mutex
);
333 /* First look whether the current file descriptor is currently
336 && runp
->aiocbp
->aiocb
.aio_fildes
< aiocbp
->aiocb
.aio_fildes
)
339 runp
= runp
->next_fd
;
342 /* Get a new element for the waiting list. */
346 pthread_mutex_unlock (&__aio_requests_mutex
);
347 __set_errno (EAGAIN
);
350 newp
->aiocbp
= aiocbp
;
351 #ifdef BROKEN_THREAD_SIGNALS
352 newp
->caller_pid
= (aiocbp
->aiocb
.aio_sigevent
.sigev_notify
== SIGEV_SIGNAL
355 newp
->waiting
= NULL
;
357 aiocbp
->aiocb
.__abs_prio
= prio
;
358 aiocbp
->aiocb
.__policy
= policy
;
359 aiocbp
->aiocb
.aio_lio_opcode
= operation
;
360 aiocbp
->aiocb
.__error_code
= EINPROGRESS
;
361 aiocbp
->aiocb
.__return_value
= 0;
364 && runp
->aiocbp
->aiocb
.aio_fildes
== aiocbp
->aiocb
.aio_fildes
)
366 /* The current file descriptor is worked on. It makes no sense
367 to start another thread since this new thread would fight
368 with the running thread for the resources. But we also cannot
369 say that the thread processing this desriptor shall immediately
370 after finishing the current job process this request if there
371 are other threads in the running queue which have a higher
374 /* Simply enqueue it after the running one according to the
376 while (runp
->next_prio
!= NULL
377 && runp
->next_prio
->aiocbp
->aiocb
.__abs_prio
>= prio
)
378 runp
= runp
->next_prio
;
380 newp
->next_prio
= runp
->next_prio
;
381 runp
->next_prio
= newp
;
388 /* Enqueue this request for a new descriptor. */
391 newp
->last_fd
= NULL
;
392 newp
->next_fd
= requests
;
393 if (requests
!= NULL
)
394 requests
->last_fd
= newp
;
399 newp
->next_fd
= last
->next_fd
;
400 newp
->last_fd
= last
;
401 last
->next_fd
= newp
;
402 if (newp
->next_fd
!= NULL
)
403 newp
->next_fd
->last_fd
= newp
;
406 newp
->next_prio
= NULL
;
411 /* We try to create a new thread for this file descriptor. The
412 function which gets called will handle all available requests
413 for this descriptor and when all are processed it will
416 If no new thread can be created or if the specified limit of
417 threads for AIO is reached we queue the request. */
419 /* See if we need to and are able to create a thread. */
420 if (nthreads
< optim
.aio_threads
&& idle_thread_count
== 0)
424 running
= newp
->running
= allocated
;
426 /* Now try to start a thread. */
427 if (aio_create_helper_thread (&thid
, handle_fildes_io
, newp
) == 0)
428 /* We managed to enqueue the request. All errors which can
429 happen now can be recognized by calls to `aio_return' and
434 /* Reset the running flag. The new request is not running. */
435 running
= newp
->running
= yes
;
438 /* We cannot create a thread in the moment and there is
439 also no thread running. This is a problem. `errno' is
440 set to EAGAIN if this is only a temporary problem. */
446 /* Enqueue the request in the run queue if it is not yet running. */
447 if (running
== yes
&& result
== 0)
449 add_request_to_runlist (newp
);
451 /* If there is a thread waiting for work, then let it know that we
452 have just given it something to do. */
453 if (idle_thread_count
> 0)
454 pthread_cond_signal (&__aio_new_request_notification
);
458 newp
->running
= running
;
461 /* Something went wrong. */
462 __aio_free_request (newp
);
466 /* Release the mutex. */
467 pthread_mutex_unlock (&__aio_requests_mutex
);
474 handle_fildes_io (void *arg
)
476 pthread_t self
= pthread_self ();
477 struct sched_param param
;
478 struct requestlist
*runp
= (struct requestlist
*) arg
;
483 pthread_getschedparam (self
, &policy
, ¶m
);
487 /* If runp is NULL, then we were created to service the work queue
488 in general, not to handle any particular request. In that case we
489 skip the "do work" stuff on the first pass, and go directly to the
490 "get work off the work queue" part of this loop, which is near the
493 pthread_mutex_lock (&__aio_requests_mutex
);
496 /* Hopefully this request is marked as running. */
497 assert (runp
->running
== allocated
);
499 /* Update our variables. */
500 aiocbp
= runp
->aiocbp
;
501 fildes
= aiocbp
->aiocb
.aio_fildes
;
503 /* Change the priority to the requested value (if necessary). */
504 if (aiocbp
->aiocb
.__abs_prio
!= param
.sched_priority
505 || aiocbp
->aiocb
.__policy
!= policy
)
507 param
.sched_priority
= aiocbp
->aiocb
.__abs_prio
;
508 policy
= aiocbp
->aiocb
.__policy
;
509 pthread_setschedparam (self
, policy
, ¶m
);
512 /* Process request pointed to by RUNP. We must not be disturbed
514 if ((aiocbp
->aiocb
.aio_lio_opcode
& 127) == LIO_READ
)
516 if (aiocbp
->aiocb
.aio_lio_opcode
& 128)
517 aiocbp
->aiocb
.__return_value
=
518 TEMP_FAILURE_RETRY (__pread64 (fildes
, (void *)
519 aiocbp
->aiocb64
.aio_buf
,
520 aiocbp
->aiocb64
.aio_nbytes
,
521 aiocbp
->aiocb64
.aio_offset
));
523 aiocbp
->aiocb
.__return_value
=
524 TEMP_FAILURE_RETRY (pread (fildes
,
525 (void *) aiocbp
->aiocb
.aio_buf
,
526 aiocbp
->aiocb
.aio_nbytes
,
527 aiocbp
->aiocb
.aio_offset
));
529 if (aiocbp
->aiocb
.__return_value
== -1 && errno
== ESPIPE
)
530 /* The Linux kernel is different from others. It returns
531 ESPIPE if using pread on a socket. Other platforms
532 simply ignore the offset parameter and behave like
534 aiocbp
->aiocb
.__return_value
=
535 TEMP_FAILURE_RETRY (read (fildes
,
536 (void *) aiocbp
->aiocb64
.aio_buf
,
537 aiocbp
->aiocb64
.aio_nbytes
));
539 else if ((aiocbp
->aiocb
.aio_lio_opcode
& 127) == LIO_WRITE
)
541 if (aiocbp
->aiocb
.aio_lio_opcode
& 128)
542 aiocbp
->aiocb
.__return_value
=
543 TEMP_FAILURE_RETRY (__pwrite64 (fildes
, (const void *)
544 aiocbp
->aiocb64
.aio_buf
,
545 aiocbp
->aiocb64
.aio_nbytes
,
546 aiocbp
->aiocb64
.aio_offset
));
548 aiocbp
->aiocb
.__return_value
=
549 TEMP_FAILURE_RETRY (__libc_pwrite (fildes
, (const void *)
550 aiocbp
->aiocb
.aio_buf
,
551 aiocbp
->aiocb
.aio_nbytes
,
552 aiocbp
->aiocb
.aio_offset
));
554 if (aiocbp
->aiocb
.__return_value
== -1 && errno
== ESPIPE
)
555 /* The Linux kernel is different from others. It returns
556 ESPIPE if using pwrite on a socket. Other platforms
557 simply ignore the offset parameter and behave like
559 aiocbp
->aiocb
.__return_value
=
560 TEMP_FAILURE_RETRY (write (fildes
,
561 (void *) aiocbp
->aiocb64
.aio_buf
,
562 aiocbp
->aiocb64
.aio_nbytes
));
564 else if (aiocbp
->aiocb
.aio_lio_opcode
== LIO_DSYNC
)
565 aiocbp
->aiocb
.__return_value
=
566 TEMP_FAILURE_RETRY (fdatasync (fildes
));
567 else if (aiocbp
->aiocb
.aio_lio_opcode
== LIO_SYNC
)
568 aiocbp
->aiocb
.__return_value
=
569 TEMP_FAILURE_RETRY (fsync (fildes
));
572 /* This is an invalid opcode. */
573 aiocbp
->aiocb
.__return_value
= -1;
574 __set_errno (EINVAL
);
578 pthread_mutex_lock (&__aio_requests_mutex
);
580 /* In theory we would need here a write memory barrier since the
581 callers test using aio_error() whether the request finished
582 and once this value != EINPROGRESS the field __return_value
583 must be committed to memory.
585 But since the pthread_mutex_lock call involves write memory
586 barriers as well it is not necessary. */
588 if (aiocbp
->aiocb
.__return_value
== -1)
589 aiocbp
->aiocb
.__error_code
= errno
;
591 aiocbp
->aiocb
.__error_code
= 0;
593 /* Send the signal to notify about finished processing of the
597 /* For debugging purposes we reset the running flag of the
599 assert (runp
->running
== allocated
);
600 runp
->running
= done
;
602 /* Now dequeue the current request. */
603 __aio_remove_request (NULL
, runp
, 0);
604 if (runp
->next_prio
!= NULL
)
605 add_request_to_runlist (runp
->next_prio
);
607 /* Free the old element. */
608 __aio_free_request (runp
);
613 /* If the runlist is empty, then we sleep for a while, waiting for
614 something to arrive in it. */
615 if (runp
== NULL
&& optim
.aio_idle_time
>= 0)
618 struct timespec wakeup_time
;
621 gettimeofday (&now
, NULL
);
622 wakeup_time
.tv_sec
= now
.tv_sec
+ optim
.aio_idle_time
;
623 wakeup_time
.tv_nsec
= now
.tv_usec
* 1000;
624 if (wakeup_time
.tv_nsec
> 1000000000)
626 wakeup_time
.tv_nsec
-= 1000000000;
627 ++wakeup_time
.tv_sec
;
629 pthread_cond_timedwait (&__aio_new_request_notification
,
630 &__aio_requests_mutex
,
640 assert (runp
->running
== yes
);
641 runp
->running
= allocated
;
642 runlist
= runp
->next_run
;
644 /* If we have a request to process, and there's still another in
645 the run list, then we need to either wake up or create a new
646 thread to service the request that is still in the run list. */
649 /* There are at least two items in the work queue to work on.
650 If there are other idle threads, then we should wake them
651 up for these other work elements; otherwise, we should try
652 to create a new thread. */
653 if (idle_thread_count
> 0)
654 pthread_cond_signal (&__aio_new_request_notification
);
655 else if (nthreads
< optim
.aio_threads
)
660 /* Make sure the thread is created detached. */
661 pthread_attr_init (&attr
);
662 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
664 /* Now try to start a thread. If we fail, no big deal,
665 because we know that there is at least one thread (us)
666 that is working on AIO operations. */
667 if (pthread_create (&thid
, &attr
, handle_fildes_io
, NULL
)
674 /* Release the mutex. */
675 pthread_mutex_unlock (&__aio_requests_mutex
);
677 while (runp
!= NULL
);
683 /* Free allocated resources. */
684 libc_freeres_fn (free_res
)
688 for (row
= 0; row
< pool_max_size
; ++row
)
695 /* Add newrequest to the runlist. The __abs_prio flag of newrequest must
696 be correctly set to do this. Also, you had better set newrequest's
697 "running" flag to "yes" before you release your lock or you'll throw an
700 add_request_to_runlist (struct requestlist
*newrequest
)
702 int prio
= newrequest
->aiocbp
->aiocb
.__abs_prio
;
703 struct requestlist
*runp
;
705 if (runlist
== NULL
|| runlist
->aiocbp
->aiocb
.__abs_prio
< prio
)
707 newrequest
->next_run
= runlist
;
708 runlist
= newrequest
;
714 while (runp
->next_run
!= NULL
715 && runp
->next_run
->aiocbp
->aiocb
.__abs_prio
>= prio
)
716 runp
= runp
->next_run
;
718 newrequest
->next_run
= runp
->next_run
;
719 runp
->next_run
= newrequest
;