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
208 /* The thread handler. */
209 static void *handle_fildes_io (void *arg
);
212 /* User optimization. */
214 __aio_init (const struct aioinit
*init
)
217 pthread_mutex_lock (&__aio_requests_mutex
);
219 /* Only allow writing new values if the table is not yet allocated. */
222 optim
.aio_threads
= init
->aio_threads
< 1 ? 1 : init
->aio_threads
;
223 optim
.aio_num
= (init
->aio_num
< ENTRIES_PER_ROW
225 : init
->aio_num
& ~ENTRIES_PER_ROW
);
228 if (init
->aio_idle_time
!= 0)
229 optim
.aio_idle_time
= init
->aio_idle_time
;
231 /* Release the mutex. */
232 pthread_mutex_unlock (&__aio_requests_mutex
);
234 weak_alias (__aio_init
, aio_init
)
237 /* The main function of the async I/O handling. It enqueues requests
238 and if necessary starts and handles threads. */
241 __aio_enqueue_request (aiocb_union
*aiocbp
, int operation
)
245 struct sched_param param
;
246 struct requestlist
*last
, *runp
, *newp
;
249 if (aiocbp
->aiocb
.aio_reqprio
< 0
250 || aiocbp
->aiocb
.aio_reqprio
> AIO_PRIO_DELTA_MAX
)
252 /* Invalid priority value. */
253 __set_errno (EINVAL
);
254 aiocbp
->aiocb
.__error_code
= EINVAL
;
255 aiocbp
->aiocb
.__return_value
= -1;
259 /* Compute priority for this request. */
260 pthread_getschedparam (pthread_self (), &policy
, ¶m
);
261 prio
= param
.sched_priority
- aiocbp
->aiocb
.aio_reqprio
;
264 pthread_mutex_lock (&__aio_requests_mutex
);
268 /* First look whether the current file descriptor is currently
271 && runp
->aiocbp
->aiocb
.aio_fildes
< aiocbp
->aiocb
.aio_fildes
)
274 runp
= runp
->next_fd
;
277 /* Get a new element for the waiting list. */
281 pthread_mutex_unlock (&__aio_requests_mutex
);
282 __set_errno (EAGAIN
);
285 newp
->aiocbp
= aiocbp
;
286 newp
->caller_pid
= (aiocbp
->aiocb
.aio_sigevent
.sigev_notify
== SIGEV_SIGNAL
288 newp
->waiting
= NULL
;
290 aiocbp
->aiocb
.__abs_prio
= prio
;
291 aiocbp
->aiocb
.__policy
= policy
;
292 aiocbp
->aiocb
.aio_lio_opcode
= operation
;
293 aiocbp
->aiocb
.__error_code
= EINPROGRESS
;
294 aiocbp
->aiocb
.__return_value
= 0;
297 && runp
->aiocbp
->aiocb
.aio_fildes
== aiocbp
->aiocb
.aio_fildes
)
299 /* The current file descriptor is worked on. It makes no sense
300 to start another thread since this new thread would fight
301 with the running thread for the resources. But we also cannot
302 say that the thread processing this desriptor shall immediately
303 after finishing the current job process this request if there
304 are other threads in the running queue which have a higher
307 /* Simply enqueue it after the running one according to the
309 while (runp
->next_prio
!= NULL
310 && runp
->next_prio
->aiocbp
->aiocb
.__abs_prio
>= prio
)
311 runp
= runp
->next_prio
;
313 newp
->next_prio
= runp
->next_prio
;
314 runp
->next_prio
= newp
;
321 /* Enqueue this request for a new descriptor. */
324 newp
->last_fd
= NULL
;
325 newp
->next_fd
= requests
;
326 if (requests
!= NULL
)
327 requests
->last_fd
= newp
;
332 newp
->next_fd
= last
->next_fd
;
333 newp
->last_fd
= last
;
334 last
->next_fd
= newp
;
335 if (newp
->next_fd
!= NULL
)
336 newp
->next_fd
->last_fd
= newp
;
339 newp
->next_prio
= NULL
;
344 /* We try to create a new thread for this file descriptor. The
345 function which gets called will handle all available requests
346 for this descriptor and when all are processed it will
349 If no new thread can be created or if the specified limit of
350 threads for AIO is reached we queue the request. */
352 /* See if we need to and are able to create a thread. */
353 if (nthreads
< optim
.aio_threads
&& idle_thread_count
== 0)
358 /* Make sure the thread is created detached. */
359 pthread_attr_init (&attr
);
360 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
362 /* Now try to start a thread. */
363 if (pthread_create (&thid
, &attr
, handle_fildes_io
, newp
) == 0)
365 /* We managed to enqueue the request. All errors which can
366 happen now can be recognized by calls to `aio_return' and
371 else if (nthreads
== 0)
372 /* We cannot create a thread in the moment and there is
373 also no thread running. This is a problem. `errno' is
374 set to EAGAIN if this is only a temporary problem. */
379 /* Enqueue the request in the run queue if it is not yet running. */
380 if (running
== yes
&& result
== 0)
382 add_request_to_runlist (newp
);
384 /* If there is a thread waiting for work, then let it know that we
385 have just given it something to do. */
386 if (idle_thread_count
> 0)
387 pthread_cond_signal (&__aio_new_request_notification
);
391 newp
->running
= running
;
394 /* Something went wrong. */
395 __aio_free_request (newp
);
399 /* Release the mutex. */
400 pthread_mutex_unlock (&__aio_requests_mutex
);
407 handle_fildes_io (void *arg
)
409 pthread_t self
= pthread_self ();
410 struct sched_param param
;
411 struct requestlist
*runp
= (struct requestlist
*) arg
;
416 pthread_getschedparam (self
, &policy
, ¶m
);
420 /* If runp is NULL, then we were created to service the work queue
421 in general, not to handle any particular request. In that case we
422 skip the "do work" stuff on the first pass, and go directly to the
423 "get work off the work queue" part of this loop, which is near the
426 pthread_mutex_lock (&__aio_requests_mutex
);
429 /* Update our variables. */
430 aiocbp
= runp
->aiocbp
;
431 fildes
= aiocbp
->aiocb
.aio_fildes
;
433 /* Change the priority to the requested value (if necessary). */
434 if (aiocbp
->aiocb
.__abs_prio
!= param
.sched_priority
435 || aiocbp
->aiocb
.__policy
!= policy
)
437 param
.sched_priority
= aiocbp
->aiocb
.__abs_prio
;
438 policy
= aiocbp
->aiocb
.__policy
;
439 pthread_setschedparam (self
, policy
, ¶m
);
442 /* Process request pointed to by RUNP. We must not be disturbed
444 if ((aiocbp
->aiocb
.aio_lio_opcode
& 127) == LIO_READ
)
446 if (aiocbp
->aiocb
.aio_lio_opcode
& 128)
447 aiocbp
->aiocb
.__return_value
=
448 TEMP_FAILURE_RETRY (__pread64 (fildes
, (void *)
449 aiocbp
->aiocb64
.aio_buf
,
450 aiocbp
->aiocb64
.aio_nbytes
,
451 aiocbp
->aiocb64
.aio_offset
));
453 aiocbp
->aiocb
.__return_value
=
454 TEMP_FAILURE_RETRY (pread (fildes
,
455 (void *) aiocbp
->aiocb
.aio_buf
,
456 aiocbp
->aiocb
.aio_nbytes
,
457 aiocbp
->aiocb
.aio_offset
));
459 if (aiocbp
->aiocb
.__return_value
== -1 && errno
== ESPIPE
)
460 /* The Linux kernel is different from others. It returns
461 ESPIPE if using pread on a socket. Other platforms
462 simply ignore the offset parameter and behave like
464 aiocbp
->aiocb
.__return_value
=
465 TEMP_FAILURE_RETRY (read (fildes
,
466 (void *) aiocbp
->aiocb64
.aio_buf
,
467 aiocbp
->aiocb64
.aio_nbytes
));
469 else if ((aiocbp
->aiocb
.aio_lio_opcode
& 127) == LIO_WRITE
)
471 if (aiocbp
->aiocb
.aio_lio_opcode
& 128)
472 aiocbp
->aiocb
.__return_value
=
473 TEMP_FAILURE_RETRY (__pwrite64 (fildes
, (const void *)
474 aiocbp
->aiocb64
.aio_buf
,
475 aiocbp
->aiocb64
.aio_nbytes
,
476 aiocbp
->aiocb64
.aio_offset
));
478 aiocbp
->aiocb
.__return_value
=
479 TEMP_FAILURE_RETRY (pwrite (fildes
, (const void *)
480 aiocbp
->aiocb
.aio_buf
,
481 aiocbp
->aiocb
.aio_nbytes
,
482 aiocbp
->aiocb
.aio_offset
));
484 if (aiocbp
->aiocb
.__return_value
== -1 && errno
== ESPIPE
)
485 /* The Linux kernel is different from others. It returns
486 ESPIPE if using pwrite on a socket. Other platforms
487 simply ignore the offset parameter and behave like
489 aiocbp
->aiocb
.__return_value
=
490 TEMP_FAILURE_RETRY (write (fildes
,
491 (void *) aiocbp
->aiocb64
.aio_buf
,
492 aiocbp
->aiocb64
.aio_nbytes
));
494 else if (aiocbp
->aiocb
.aio_lio_opcode
== LIO_DSYNC
)
495 aiocbp
->aiocb
.__return_value
=
496 TEMP_FAILURE_RETRY (fdatasync (fildes
));
497 else if (aiocbp
->aiocb
.aio_lio_opcode
== LIO_SYNC
)
498 aiocbp
->aiocb
.__return_value
=
499 TEMP_FAILURE_RETRY (fsync (fildes
));
502 /* This is an invalid opcode. */
503 aiocbp
->aiocb
.__return_value
= -1;
504 __set_errno (EINVAL
);
508 pthread_mutex_lock (&__aio_requests_mutex
);
510 if (aiocbp
->aiocb
.__return_value
== -1)
511 aiocbp
->aiocb
.__error_code
= errno
;
513 aiocbp
->aiocb
.__error_code
= 0;
515 /* Send the signal to notify about finished processing of the
519 /* Now dequeue the current request. */
520 if (runp
->next_prio
== NULL
)
522 /* No outstanding request for this descriptor. Remove this
523 descriptor from the list. */
524 if (runp
->next_fd
!= NULL
)
525 runp
->next_fd
->last_fd
= runp
->last_fd
;
526 if (runp
->last_fd
!= NULL
)
527 runp
->last_fd
->next_fd
= runp
->next_fd
;
529 requests
= runp
->next_fd
;
533 runp
->next_prio
->last_fd
= runp
->last_fd
;
534 runp
->next_prio
->next_fd
= runp
->next_fd
;
535 runp
->next_prio
->running
= yes
;
536 if (runp
->next_fd
!= NULL
)
537 runp
->next_fd
->last_fd
= runp
->next_prio
;
538 if (runp
->last_fd
!= NULL
)
539 runp
->last_fd
->next_fd
= runp
->next_prio
;
541 requests
= runp
->next_prio
;
542 add_request_to_runlist (runp
->next_prio
);
545 /* Free the old element. */
546 __aio_free_request (runp
);
551 /* If the runlist is empty, then we sleep for a while, waiting for
552 something to arrive in it. */
553 if (runp
== NULL
&& optim
.aio_idle_time
>= 0)
556 struct timespec wakeup_time
;
559 gettimeofday (&now
, NULL
);
560 wakeup_time
.tv_sec
= now
.tv_sec
+ optim
.aio_idle_time
;
561 wakeup_time
.tv_nsec
= now
.tv_usec
* 1000;
562 if (wakeup_time
.tv_nsec
> 1000000000)
564 wakeup_time
.tv_nsec
-= 1000000000;
565 ++wakeup_time
.tv_sec
;
567 pthread_cond_timedwait (&__aio_new_request_notification
,
568 &__aio_requests_mutex
,
578 assert (runp
->running
== yes
);
579 runp
->running
= allocated
;
580 runlist
= runp
->next_run
;
582 /* If we have a request to process, and there's still another in
583 the run list, then we need to either wake up or create a new
584 thread to service the request that is still in the run list. */
587 /* There are at least two items in the work queue to work on.
588 If there are other idle threads, then we should wake them
589 up for these other work elements; otherwise, we should try
590 to create a new thread. */
591 if (idle_thread_count
> 0)
592 pthread_cond_signal (&__aio_new_request_notification
);
593 else if (nthreads
< optim
.aio_threads
)
598 /* Make sure the thread is created detached. */
599 pthread_attr_init (&attr
);
600 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
602 /* Now try to start a thread. If we fail, no big deal,
603 because we know that there is at least one thread (us)
604 that is working on AIO operations. */
605 if (pthread_create (&thid
, &attr
, handle_fildes_io
, NULL
)
612 /* Release the mutex. */
613 pthread_mutex_unlock (&__aio_requests_mutex
);
615 while (runp
!= NULL
);
621 /* Free allocated resources. */
623 __attribute__ ((unused
))
628 /* The first block of rows as specified in OPTIM is allocated in
632 for (row
= optim
.aio_num
/ ENTRIES_PER_ROW
; row
< pool_tab_size
; ++row
)
637 text_set_element (__libc_subfreeres
, free_res
);
640 /* Add newrequest to the runlist. The __abs_prio flag of newrequest must
641 be correctly set to do this. Also, you had better set newrequest's
642 "running" flag to "yes" before you release your lock or you'll throw an
645 add_request_to_runlist (struct requestlist
*newrequest
)
647 int prio
= newrequest
->aiocbp
->aiocb
.__abs_prio
;
648 struct requestlist
*runp
;
650 if (runlist
== NULL
|| runlist
->aiocbp
->aiocb
.__abs_prio
< prio
)
652 newrequest
->next_run
= runlist
;
653 runlist
= newrequest
;
659 while (runp
->next_run
!= NULL
660 && runp
->next_run
->aiocbp
->aiocb
.__abs_prio
>= prio
)
661 runp
= runp
->next_run
;
663 newrequest
->next_run
= runp
->next_run
;
664 runp
->next_run
= newrequest
;