1 /* Handle general operations.
2 Copyright (C) 1997,1998,1999,2000,2001,2002,2003
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
22 #include <kaio_misc.h>
38 static void add_request_to_runlist (struct requestlist
*newrequest
)
40 static int add_request_to_list (struct requestlist
*newrequest
, int fildes
,
43 static void * handle_kernel_aio (void *arg
);
44 static void kernel_callback (kctx_t ctx
, struct kiocb
*kiocb
, long res
,
47 /* Pool of request list entries. */
48 static struct requestlist
**pool
;
50 /* Number of total and allocated pool entries. */
51 static size_t pool_max_size
;
52 static size_t pool_size
;
54 /* Kernel AIO context. */
55 kctx_t __aio_kioctx
= KCTX_NONE
;
56 int __have_no_kernel_aio
;
57 int __kernel_thread_started
;
59 /* We implement a two dimensional array but allocate each row separately.
60 The macro below determines how many entries should be used per row.
61 It should better be a power of two. */
62 #define ENTRIES_PER_ROW 32
64 /* How many rows we allocate at once. */
67 /* List of available entries. */
68 static struct requestlist
*freelist
;
70 /* List of request waiting to be processed. */
71 static struct requestlist
*runlist
;
73 /* Structure list of all currently processed requests. */
74 static struct requestlist
*requests
, *krequests
;
76 /* Number of threads currently running. */
79 /* Number of threads waiting for work to arrive. */
80 static int idle_thread_count
;
83 /* These are the values used to optimize the use of AIO. The user can
84 overwrite them by using the `aio_init' function. */
85 static struct aioinit optim
=
87 20, /* int aio_threads; Maximal number of threads. */
88 64, /* int aio_num; Number of expected simultanious requests. */
98 /* Since the list is global we need a mutex protecting it. */
99 pthread_mutex_t __aio_requests_mutex
= PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
;
101 /* When you add a request to the list and there are idle threads present,
102 you signal this condition variable. When a thread finishes work, it waits
103 on this condition variable for a time before it actually exits. */
104 pthread_cond_t __aio_new_request_notification
= PTHREAD_COND_INITIALIZER
;
107 /* Functions to handle request list pool. */
108 static struct requestlist
*
111 struct requestlist
*result
;
113 if (freelist
== NULL
)
115 struct requestlist
*new_row
;
118 assert (sizeof (struct aiocb
) == sizeof (struct aiocb64
));
120 if (pool_size
+ 1 >= pool_max_size
)
122 size_t new_max_size
= pool_max_size
+ ROWS_STEP
;
123 struct requestlist
**new_tab
;
125 new_tab
= (struct requestlist
**)
126 realloc (pool
, new_max_size
* sizeof (struct requestlist
*));
131 pool_max_size
= new_max_size
;
135 /* Allocate the new row. */
136 cnt
= pool_size
== 0 ? optim
.aio_num
: ENTRIES_PER_ROW
;
137 new_row
= (struct requestlist
*) calloc (cnt
,
138 sizeof (struct requestlist
));
142 pool
[pool_size
++] = new_row
;
144 /* Put all the new entries in the freelist. */
147 new_row
->next_prio
= freelist
;
148 freelist
= new_row
++;
154 freelist
= freelist
->next_prio
;
162 __aio_free_request (struct requestlist
*elem
)
165 elem
->next_prio
= freelist
;
172 __aio_find_req (aiocb_union
*elem
)
174 struct requestlist
*runp
;
175 int fildes
= elem
->aiocb
.aio_fildes
;
178 for (i
= 0; i
< 2; i
++)
180 runp
= i
? requests
: krequests
;
182 while (runp
!= NULL
&& runp
->aiocbp
->aiocb
.aio_fildes
< fildes
)
183 runp
= runp
->next_fd
;
187 if (runp
->aiocbp
->aiocb
.aio_fildes
!= fildes
)
190 while (runp
!= NULL
&& runp
->aiocbp
!= elem
)
191 runp
= runp
->next_prio
;
203 __aio_find_req_fd (int fildes
)
205 struct requestlist
*runp
= requests
;
207 while (runp
!= NULL
&& runp
->aiocbp
->aiocb
.aio_fildes
< fildes
)
208 runp
= runp
->next_fd
;
210 return (runp
!= NULL
&& runp
->aiocbp
->aiocb
.aio_fildes
== fildes
217 __aio_find_kreq_fd (int fildes
)
219 struct requestlist
*runp
= krequests
;
221 while (runp
!= NULL
&& runp
->aiocbp
->aiocb
.aio_fildes
< fildes
)
222 runp
= runp
->next_fd
;
224 return (runp
!= NULL
&& runp
->aiocbp
->aiocb
.aio_fildes
== fildes
231 __aio_remove_request (struct requestlist
*last
, struct requestlist
*req
,
234 assert (req
->running
== yes
|| req
->running
== queued
235 || req
->running
== done
);
236 assert (req
->kioctx
== KCTX_NONE
);
239 last
->next_prio
= all
? NULL
: req
->next_prio
;
242 if (all
|| req
->next_prio
== NULL
)
244 if (req
->last_fd
!= NULL
)
245 req
->last_fd
->next_fd
= req
->next_fd
;
247 requests
= req
->next_fd
;
248 if (req
->next_fd
!= NULL
)
249 req
->next_fd
->last_fd
= req
->last_fd
;
253 if (req
->last_fd
!= NULL
)
254 req
->last_fd
->next_fd
= req
->next_prio
;
256 requests
= req
->next_prio
;
258 if (req
->next_fd
!= NULL
)
259 req
->next_fd
->last_fd
= req
->next_prio
;
261 req
->next_prio
->last_fd
= req
->last_fd
;
262 req
->next_prio
->next_fd
= req
->next_fd
;
264 /* Mark this entry as runnable. */
265 req
->next_prio
->running
= yes
;
268 if (req
->running
== yes
)
270 struct requestlist
*runp
= runlist
;
278 runlist
= runp
->next_run
;
280 last
->next_run
= runp
->next_run
;
284 runp
= runp
->next_run
;
292 __aio_remove_krequest (struct requestlist
*req
)
294 assert (req
->running
== yes
|| req
->running
== queued
295 || req
->running
== done
);
296 assert (req
->kioctx
!= KCTX_NONE
);
298 if (req
->prev_prio
!= NULL
)
300 req
->prev_prio
->next_prio
= req
->next_prio
;
301 if (req
->next_prio
!= NULL
)
302 req
->next_prio
->prev_prio
= req
->prev_prio
;
304 else if (req
->next_prio
== NULL
)
306 if (req
->last_fd
!= NULL
)
307 req
->last_fd
->next_fd
= req
->next_fd
;
309 krequests
= req
->next_fd
;
310 if (req
->next_fd
!= NULL
)
311 req
->next_fd
->last_fd
= req
->last_fd
;
315 if (req
->last_fd
!= NULL
)
316 req
->last_fd
->next_fd
= req
->next_prio
;
318 krequests
= req
->next_prio
;
319 if (req
->next_fd
!= NULL
)
320 req
->next_fd
->last_fd
= req
->next_prio
;
322 req
->next_prio
->prev_prio
= NULL
;
323 req
->next_prio
->last_fd
= req
->last_fd
;
324 req
->next_prio
->next_fd
= req
->next_fd
;
329 /* The thread handler. */
330 static void *handle_fildes_io (void *arg
);
331 static int wait_for_kernel_requests (int fildes
);
334 /* User optimization. */
336 __aio_init (const struct aioinit
*init
)
339 pthread_mutex_lock (&__aio_requests_mutex
);
341 /* Only allow writing new values if the table is not yet allocated. */
344 optim
.aio_threads
= init
->aio_threads
< 1 ? 1 : init
->aio_threads
;
345 optim
.aio_num
= (init
->aio_num
< ENTRIES_PER_ROW
347 : init
->aio_num
& ~ENTRIES_PER_ROW
);
350 if (init
->aio_idle_time
!= 0)
351 optim
.aio_idle_time
= init
->aio_idle_time
;
353 /* Release the mutex. */
354 pthread_mutex_unlock (&__aio_requests_mutex
);
356 weak_alias (__aio_init
, aio_init
)
359 kernel_callback (kctx_t ctx
, struct kiocb
*kiocb
, long res
, long res2
)
361 struct requestlist
*req
= (struct requestlist
*)kiocb
;
363 req
->aiocbp
->aiocb
.__error_code
= 0;
364 req
->aiocbp
->aiocb
.__return_value
= res
;
365 if (res
< 0 && res
> -1000)
367 req
->aiocbp
->aiocb
.__error_code
= -res
;
368 req
->aiocbp
->aiocb
.__return_value
= -1;
371 assert (req
->running
== allocated
);
373 __aio_remove_krequest (req
);
374 __aio_free_request (req
);
379 __aio_read_one_event (void)
381 struct kio_event ev
[10];
385 if (__aio_kioctx
== KCTX_NONE
)
391 INTERNAL_SYSCALL_DECL (err
);
392 count
= INTERNAL_SYSCALL (io_getevents
, err
, 5, __aio_kioctx
, 0, 10,
394 if (INTERNAL_SYSCALL_ERROR_P (count
, err
) || count
== 0)
396 pthread_mutex_lock (&__aio_requests_mutex
);
397 for (i
= 0; i
< count
; i
++)
399 void (*cb
)(kctx_t
, struct kiocb
*, long, long);
401 cb
= (void *) (uintptr_t) ev
[i
].kioe_data
;
402 cb (__aio_kioctx
, (struct kiocb
*) (uintptr_t) ev
[i
].kioe_obj
,
403 ev
[i
].kioe_res
, ev
[i
].kioe_res2
);
405 pthread_mutex_unlock (&__aio_requests_mutex
);
412 __aio_wait_for_events (kctx_t kctx
, const struct timespec
*timespec
)
415 struct kio_event ev
[10];
417 INTERNAL_SYSCALL_DECL (err
);
419 pthread_mutex_unlock (&__aio_requests_mutex
);
424 ret
= INTERNAL_SYSCALL (io_getevents
, err
, 5, kctx
, 0, 10, ev
,
426 if (INTERNAL_SYSCALL_ERROR_P (ret
, err
) || ret
== 0)
429 pthread_mutex_lock (&__aio_requests_mutex
);
430 for (i
= 0; i
< ret
; i
++)
432 void (*cb
)(kctx_t
, struct kiocb
*, long, long);
434 cb
= (void *) (uintptr_t) ev
[i
].kioe_data
;
435 cb (kctx
, (struct kiocb
*) (uintptr_t) ev
[i
].kioe_obj
,
436 ev
[i
].kioe_res
, ev
[i
].kioe_res2
);
440 pthread_mutex_unlock (&__aio_requests_mutex
);
445 pthread_mutex_lock (&__aio_requests_mutex
);
446 return (timespec
!= &ts
447 && INTERNAL_SYSCALL_ERROR_P (ret
, err
)
448 && INTERNAL_SYSCALL_ERRNO (ret
, err
) == ETIMEDOUT
) ? ETIMEDOUT
: 0;
453 __aio_create_kernel_thread (void)
458 if (__kernel_thread_started
)
461 /* Make sure the thread is created detached. */
462 pthread_attr_init (&attr
);
463 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
465 if (pthread_create (&thid
, &attr
, handle_kernel_aio
, NULL
) != 0)
467 __kernel_thread_started
= 1;
472 handle_kernel_aio (void *arg
__attribute__((unused
)))
475 INTERNAL_SYSCALL_DECL (err
);
476 struct kio_event ev
[10];
480 ret
= INTERNAL_SYSCALL (io_getevents
, err
, 5, __aio_kioctx
, 0, 10, ev
,
482 if (INTERNAL_SYSCALL_ERROR_P (ret
, err
) || ret
== 0)
484 pthread_mutex_lock (&__aio_requests_mutex
);
485 for (i
= 0; i
< ret
; i
++)
487 void (*cb
)(kctx_t
, struct kiocb
*, long, long);
489 cb
= (void *) (uintptr_t) ev
[i
].kioe_data
;
490 cb (__aio_kioctx
, (struct kiocb
*) (uintptr_t) ev
[i
].kioe_obj
,
491 ev
[i
].kioe_res
, ev
[i
].kioe_res2
);
493 pthread_mutex_unlock (&__aio_requests_mutex
);
500 add_request_to_list (struct requestlist
*newp
, int fildes
, int prio
)
502 struct requestlist
*last
, *runp
, *reqs
;
505 reqs
= newp
->kioctx
!= KCTX_NONE
? krequests
: requests
;
508 /* First look whether the current file descriptor is currently
511 && runp
->aiocbp
->aiocb
.aio_fildes
< fildes
)
514 runp
= runp
->next_fd
;
518 && runp
->aiocbp
->aiocb
.aio_fildes
== fildes
)
520 /* The current file descriptor is worked on. It makes no sense
521 to start another thread since this new thread would fight
522 with the running thread for the resources. But we also cannot
523 say that the thread processing this desriptor shall immediately
524 after finishing the current job process this request if there
525 are other threads in the running queue which have a higher
528 /* Simply enqueue it after the running one according to the
530 while (runp
->next_prio
!= NULL
531 && runp
->next_prio
->aiocbp
->aiocb
.__abs_prio
>= prio
)
532 runp
= runp
->next_prio
;
534 newp
->next_prio
= runp
->next_prio
;
535 runp
->next_prio
= newp
;
536 if (newp
->kioctx
!= KCTX_NONE
)
538 newp
->prev_prio
= runp
;
539 if (newp
->next_prio
!= NULL
)
540 newp
->next_prio
->prev_prio
= newp
;
546 /* Enqueue this request for a new descriptor. */
549 newp
->last_fd
= NULL
;
550 newp
->next_fd
= reqs
;
552 reqs
->last_fd
= newp
;
553 if (newp
->kioctx
!= KCTX_NONE
)
560 newp
->next_fd
= last
->next_fd
;
561 newp
->last_fd
= last
;
562 last
->next_fd
= newp
;
563 if (newp
->next_fd
!= NULL
)
564 newp
->next_fd
->last_fd
= newp
;
567 newp
->next_prio
= NULL
;
568 if (newp
->kioctx
!= KCTX_NONE
)
569 newp
->prev_prio
= NULL
;
576 __aio_enqueue_user_request (struct requestlist
*newp
)
579 int running
= add_request_to_list (newp
, newp
->aiocbp
->aiocb
.aio_fildes
,
580 newp
->aiocbp
->aiocb
.__abs_prio
);
584 /* We try to create a new thread for this file descriptor. The
585 function which gets called will handle all available requests
586 for this descriptor and when all are processed it will
589 If no new thread can be created or if the specified limit of
590 threads for AIO is reached we queue the request. */
592 /* See if we need to and are able to create a thread. */
593 if (nthreads
< optim
.aio_threads
&& idle_thread_count
== 0)
598 /* Make sure the thread is created detached. */
599 pthread_attr_init (&attr
);
600 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
602 running
= newp
->running
= allocated
;
604 /* Now try to start a thread. */
605 if (pthread_create (&thid
, &attr
, handle_fildes_io
, newp
) == 0)
606 /* We managed to enqueue the request. All errors which can
607 happen now can be recognized by calls to `aio_return' and
612 /* Reset the running flag. The new request is not running. */
613 running
= newp
->running
= yes
;
616 /* We cannot create a thread in the moment and there is
617 also no thread running. This is a problem. `errno' is
618 set to EAGAIN if this is only a temporary problem. */
624 /* Enqueue the request in the run queue if it is not yet running. */
625 if (running
== yes
&& result
== 0)
627 add_request_to_runlist (newp
);
629 /* If there is a thread waiting for work, then let it know that we
630 have just given it something to do. */
631 if (idle_thread_count
> 0)
632 pthread_cond_signal (&__aio_new_request_notification
);
636 newp
->running
= running
;
640 /* The main function of the async I/O handling. It enqueues requests
641 and if necessary starts and handles threads. */
644 __aio_enqueue_request_ctx (aiocb_union
*aiocbp
, int operation
, kctx_t kctx
)
647 struct sched_param param
;
648 struct requestlist
*newp
;
649 int op
= (operation
& 0xffff);
651 if (op
== LIO_SYNC
|| op
== LIO_DSYNC
)
653 aiocbp
->aiocb
.aio_reqprio
= 0;
654 /* FIXME: Kernel doesn't support sync yet. */
655 operation
&= ~LIO_KTHREAD
;
657 else if (aiocbp
->aiocb
.aio_reqprio
< 0
658 || aiocbp
->aiocb
.aio_reqprio
> AIO_PRIO_DELTA_MAX
)
660 /* Invalid priority value. */
661 __set_errno (EINVAL
);
662 aiocbp
->aiocb
.__error_code
= EINVAL
;
663 aiocbp
->aiocb
.__return_value
= -1;
667 /* Compute priority for this request. */
668 pthread_getschedparam (pthread_self (), &policy
, ¶m
);
669 prio
= param
.sched_priority
- aiocbp
->aiocb
.aio_reqprio
;
672 pthread_mutex_lock (&__aio_requests_mutex
);
674 if (operation
& LIO_KTHREAD
)
676 if (__aio_kioctx
== KCTX_NONE
&& !__have_no_kernel_aio
)
679 INTERNAL_SYSCALL_DECL (err
);
683 res
= INTERNAL_SYSCALL (io_setup
, err
, 2, 1024, &__aio_kioctx
);
684 while (INTERNAL_SYSCALL_ERROR_P (res
, err
)
685 && INTERNAL_SYSCALL_ERRNO (res
, err
) == EINTR
);
686 if (INTERNAL_SYSCALL_ERROR_P (res
, err
))
688 __have_no_kernel_aio
= 1;
689 __aio_kioctx
= KCTX_NONE
;
695 if (kctx
!= KCTX_NONE
&& !__kernel_thread_started
696 && ((operation
& LIO_KTHREAD_REQUIRED
)
697 || aiocbp
->aiocb
.aio_sigevent
.sigev_notify
!= SIGEV_NONE
))
699 if (__aio_create_kernel_thread () < 0)
704 /* Get a new element for the waiting list. */
708 pthread_mutex_unlock (&__aio_requests_mutex
);
709 __set_errno (EAGAIN
);
712 newp
->aiocbp
= aiocbp
;
713 #ifdef BROKEN_THREAD_SIGNALS
714 newp
->caller_pid
= (aiocbp
->aiocb
.aio_sigevent
.sigev_notify
== SIGEV_SIGNAL
717 newp
->waiting
= NULL
;
720 aiocbp
->aiocb
.__abs_prio
= prio
;
721 aiocbp
->aiocb
.__policy
= policy
;
722 aiocbp
->aiocb
.aio_lio_opcode
= op
;
723 aiocbp
->aiocb
.__error_code
= EINPROGRESS
;
724 aiocbp
->aiocb
.__return_value
= 0;
726 if (newp
->kioctx
!= KCTX_NONE
)
729 INTERNAL_SYSCALL_DECL (err
);
731 aiocb_union
*aiocbp
= newp
->aiocbp
;
732 struct kiocb
*kiocbs
[] __attribute__((unused
)) = { &newp
->kiocb
};
734 newp
->kiocb
.kiocb_data
= (uintptr_t) kernel_callback
;
737 case LIO_READ
: newp
->kiocb
.kiocb_lio_opcode
= IO_CMD_PREAD
; break;
738 case LIO_WRITE
: newp
->kiocb
.kiocb_lio_opcode
= IO_CMD_PWRITE
; break;
740 case LIO_DSYNC
: newp
->kiocb
.kiocb_lio_opcode
= IO_CMD_FSYNC
; break;
743 newp
->kiocb
.kiocb_offset
= aiocbp
->aiocb64
.aio_offset
;
745 newp
->kiocb
.kiocb_offset
= aiocbp
->aiocb
.aio_offset
;
746 newp
->kiocb
.kiocb_fildes
= aiocbp
->aiocb
.aio_fildes
;
747 newp
->kiocb
.kiocb_buf
= (uintptr_t) aiocbp
->aiocb
.aio_buf
;
748 newp
->kiocb
.kiocb_nbytes
= aiocbp
->aiocb
.aio_nbytes
;
750 newp
->kiocb
.kiocb_req_prio
= 0;
751 res
= INTERNAL_SYSCALL (io_submit
, err
, 3, newp
->kioctx
, 1, kiocbs
);
752 if (! INTERNAL_SYSCALL_ERROR_P (res
, err
))
754 newp
->running
= allocated
;
755 add_request_to_list (newp
, aiocbp
->aiocb
.aio_fildes
, prio
);
756 /* Release the mutex. */
757 pthread_mutex_unlock (&__aio_requests_mutex
);
760 newp
->kioctx
= KCTX_NONE
;
763 if (__aio_enqueue_user_request (newp
))
765 /* Something went wrong. */
766 __aio_free_request (newp
);
770 /* Release the mutex. */
771 pthread_mutex_unlock (&__aio_requests_mutex
);
778 wait_for_kernel_requests (int fildes
)
780 pthread_mutex_lock (&__aio_requests_mutex
);
782 struct requestlist
*kreq
= __aio_find_kreq_fd (fildes
), *req
;
789 if (req
->running
== allocated
)
791 req
= req
->next_prio
;
796 if (__aio_create_kernel_thread () < 0)
798 pthread_mutex_unlock (&__aio_requests_mutex
);
802 pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
803 struct waitlist waitlist
[nent
];
808 if (kreq
->running
== allocated
)
810 waitlist
[cnt
].cond
= &cond
;
811 waitlist
[cnt
].next
= kreq
->waiting
;
812 waitlist
[cnt
].counterp
= &nent
;
813 waitlist
[cnt
].sigevp
= NULL
;
814 #ifdef BROKEN_THREAD_SIGNALS
815 waitlist
[cnt
].caller_pid
= 0; /* Not needed. */
817 kreq
->waiting
= &waitlist
[cnt
++];
819 kreq
= kreq
->next_prio
;
823 pthread_cond_wait (&cond
, &__aio_requests_mutex
);
826 pthread_cond_destroy (&cond
);
829 pthread_mutex_unlock (&__aio_requests_mutex
);
835 __attribute__ ((noreturn
))
836 handle_fildes_io (void *arg
)
838 pthread_t self
= pthread_self ();
839 struct sched_param param
;
840 struct requestlist
*runp
= (struct requestlist
*) arg
;
845 pthread_getschedparam (self
, &policy
, ¶m
);
849 /* If runp is NULL, then we were created to service the work queue
850 in general, not to handle any particular request. In that case we
851 skip the "do work" stuff on the first pass, and go directly to the
852 "get work off the work queue" part of this loop, which is near the
855 pthread_mutex_lock (&__aio_requests_mutex
);
858 /* Hopefully this request is marked as running. */
859 assert (runp
->running
== allocated
);
861 /* Update our variables. */
862 aiocbp
= runp
->aiocbp
;
863 fildes
= aiocbp
->aiocb
.aio_fildes
;
865 /* Change the priority to the requested value (if necessary). */
866 if (aiocbp
->aiocb
.__abs_prio
!= param
.sched_priority
867 || aiocbp
->aiocb
.__policy
!= policy
)
869 param
.sched_priority
= aiocbp
->aiocb
.__abs_prio
;
870 policy
= aiocbp
->aiocb
.__policy
;
871 pthread_setschedparam (self
, policy
, ¶m
);
874 /* Process request pointed to by RUNP. We must not be disturbed
876 if ((aiocbp
->aiocb
.aio_lio_opcode
& 127) == LIO_READ
)
878 if (aiocbp
->aiocb
.aio_lio_opcode
& 128)
879 aiocbp
->aiocb
.__return_value
=
880 TEMP_FAILURE_RETRY (__pread64 (fildes
, (void *)
881 aiocbp
->aiocb64
.aio_buf
,
882 aiocbp
->aiocb64
.aio_nbytes
,
883 aiocbp
->aiocb64
.aio_offset
));
885 aiocbp
->aiocb
.__return_value
=
886 TEMP_FAILURE_RETRY (pread (fildes
,
887 (void *) aiocbp
->aiocb
.aio_buf
,
888 aiocbp
->aiocb
.aio_nbytes
,
889 aiocbp
->aiocb
.aio_offset
));
891 if (aiocbp
->aiocb
.__return_value
== -1 && errno
== ESPIPE
)
892 /* The Linux kernel is different from others. It returns
893 ESPIPE if using pread on a socket. Other platforms
894 simply ignore the offset parameter and behave like
896 aiocbp
->aiocb
.__return_value
=
897 TEMP_FAILURE_RETRY (read (fildes
,
898 (void *) aiocbp
->aiocb64
.aio_buf
,
899 aiocbp
->aiocb64
.aio_nbytes
));
901 else if ((aiocbp
->aiocb
.aio_lio_opcode
& 127) == LIO_WRITE
)
903 if (aiocbp
->aiocb
.aio_lio_opcode
& 128)
904 aiocbp
->aiocb
.__return_value
=
905 TEMP_FAILURE_RETRY (__pwrite64 (fildes
, (const void *)
906 aiocbp
->aiocb64
.aio_buf
,
907 aiocbp
->aiocb64
.aio_nbytes
,
908 aiocbp
->aiocb64
.aio_offset
));
910 aiocbp
->aiocb
.__return_value
=
911 TEMP_FAILURE_RETRY (__libc_pwrite (fildes
, (const void *)
912 aiocbp
->aiocb
.aio_buf
,
913 aiocbp
->aiocb
.aio_nbytes
,
914 aiocbp
->aiocb
.aio_offset
));
916 if (aiocbp
->aiocb
.__return_value
== -1 && errno
== ESPIPE
)
917 /* The Linux kernel is different from others. It returns
918 ESPIPE if using pwrite on a socket. Other platforms
919 simply ignore the offset parameter and behave like
921 aiocbp
->aiocb
.__return_value
=
922 TEMP_FAILURE_RETRY (write (fildes
,
923 (void *) aiocbp
->aiocb64
.aio_buf
,
924 aiocbp
->aiocb64
.aio_nbytes
));
926 else if (aiocbp
->aiocb
.aio_lio_opcode
== LIO_DSYNC
927 || aiocbp
->aiocb
.aio_lio_opcode
== LIO_SYNC
)
929 if (wait_for_kernel_requests (fildes
) < 0)
931 aiocbp
->aiocb
.__return_value
= -1;
932 __set_errno (ENOMEM
);
934 else if (aiocbp
->aiocb
.aio_lio_opcode
== LIO_DSYNC
)
935 aiocbp
->aiocb
.__return_value
=
936 TEMP_FAILURE_RETRY (fdatasync (fildes
));
938 aiocbp
->aiocb
.__return_value
=
939 TEMP_FAILURE_RETRY (fsync (fildes
));
943 /* This is an invalid opcode. */
944 aiocbp
->aiocb
.__return_value
= -1;
945 __set_errno (EINVAL
);
949 pthread_mutex_lock (&__aio_requests_mutex
);
951 /* In theory we would need here a write memory barrier since the
952 callers test using aio_error() whether the request finished
953 and once this value != EINPROGRESS the field __return_value
954 must be committed to memory.
956 But since the pthread_mutex_lock call involves write memory
957 barriers as well it is not necessary. */
959 if (aiocbp
->aiocb
.__return_value
== -1)
960 aiocbp
->aiocb
.__error_code
= errno
;
962 aiocbp
->aiocb
.__error_code
= 0;
964 /* Send the signal to notify about finished processing of the
968 /* For debugging purposes we reset the running flag of the
970 assert (runp
->running
== allocated
);
971 runp
->running
= done
;
973 /* Now dequeue the current request. */
974 __aio_remove_request (NULL
, runp
, 0);
975 if (runp
->next_prio
!= NULL
)
976 add_request_to_runlist (runp
->next_prio
);
978 /* Free the old element. */
979 __aio_free_request (runp
);
984 /* If the runlist is empty, then we sleep for a while, waiting for
985 something to arrive in it. */
986 if (runp
== NULL
&& optim
.aio_idle_time
>= 0)
989 struct timespec wakeup_time
;
992 gettimeofday (&now
, NULL
);
993 wakeup_time
.tv_sec
= now
.tv_sec
+ optim
.aio_idle_time
;
994 wakeup_time
.tv_nsec
= now
.tv_usec
* 1000;
995 if (wakeup_time
.tv_nsec
> 1000000000)
997 wakeup_time
.tv_nsec
-= 1000000000;
998 ++wakeup_time
.tv_sec
;
1000 pthread_cond_timedwait (&__aio_new_request_notification
,
1001 &__aio_requests_mutex
,
1003 --idle_thread_count
;
1011 assert (runp
->running
== yes
);
1012 runp
->running
= allocated
;
1013 runlist
= runp
->next_run
;
1015 /* If we have a request to process, and there's still another in
1016 the run list, then we need to either wake up or create a new
1017 thread to service the request that is still in the run list. */
1018 if (runlist
!= NULL
)
1020 /* There are at least two items in the work queue to work on.
1021 If there are other idle threads, then we should wake them
1022 up for these other work elements; otherwise, we should try
1023 to create a new thread. */
1024 if (idle_thread_count
> 0)
1025 pthread_cond_signal (&__aio_new_request_notification
);
1026 else if (nthreads
< optim
.aio_threads
)
1029 pthread_attr_t attr
;
1031 /* Make sure the thread is created detached. */
1032 pthread_attr_init (&attr
);
1033 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
1035 /* Now try to start a thread. If we fail, no big deal,
1036 because we know that there is at least one thread (us)
1037 that is working on AIO operations. */
1038 if (pthread_create (&thid
, &attr
, handle_fildes_io
, NULL
)
1045 /* Release the mutex. */
1046 pthread_mutex_unlock (&__aio_requests_mutex
);
1048 while (runp
!= NULL
);
1050 pthread_exit (NULL
);
1054 /* Free allocated resources. */
1055 libc_freeres_fn (free_res
)
1059 for (row
= 0; row
< pool_max_size
; ++row
)
1066 /* Add newrequest to the runlist. The __abs_prio flag of newrequest must
1067 be correctly set to do this. Also, you had better set newrequest's
1068 "running" flag to "yes" before you release your lock or you'll throw an
1072 add_request_to_runlist (struct requestlist
*newrequest
)
1074 int prio
= newrequest
->aiocbp
->aiocb
.__abs_prio
;
1075 struct requestlist
*runp
;
1077 if (runlist
== NULL
|| runlist
->aiocbp
->aiocb
.__abs_prio
< prio
)
1079 newrequest
->next_run
= runlist
;
1080 runlist
= newrequest
;
1086 while (runp
->next_run
!= NULL
1087 && runp
->next_run
->aiocbp
->aiocb
.__abs_prio
>= prio
)
1088 runp
= runp
->next_run
;
1090 newrequest
->next_run
= runp
->next_run
;
1091 runp
->next_run
= newrequest
;