]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/pthread/aio_misc.c
(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[thirdparty/glibc.git] / sysdeps / pthread / aio_misc.c
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.
6
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.
11
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.
16
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
20 02111-1307 USA. */
21
22 #include <aio.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <pthread.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <aio_misc.h>
32
33 #ifndef aio_create_helper_thread
34 # define aio_create_helper_thread __aio_create_helper_thread
35
36 extern inline int
37 __aio_create_helper_thread (pthread_t *threadp, void *(*tf) (void *), void *arg)
38 {
39 pthread_attr_t attr;
40
41 /* Make sure the thread is created detached. */
42 pthread_attr_init (&attr);
43 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
44
45 int ret = pthread_create (threadp, &attr, tf, arg);
46
47 (void) pthread_attr_destroy (&attr);
48 return ret;
49 }
50
51 #endif
52
53 static void add_request_to_runlist (struct requestlist *newrequest);
54
55 /* Pool of request list entries. */
56 static struct requestlist **pool;
57
58 /* Number of total and allocated pool entries. */
59 static size_t pool_max_size;
60 static size_t pool_size;
61
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
66
67 /* How many rows we allocate at once. */
68 #define ROWS_STEP 8
69
70 /* List of available entries. */
71 static struct requestlist *freelist;
72
73 /* List of request waiting to be processed. */
74 static struct requestlist *runlist;
75
76 /* Structure list of all currently processed requests. */
77 static struct requestlist *requests;
78
79 /* Number of threads currently running. */
80 static int nthreads;
81
82 /* Number of threads waiting for work to arrive. */
83 static int idle_thread_count;
84
85
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 =
89 {
90 20, /* int aio_threads; Maximal number of threads. */
91 64, /* int aio_num; Number of expected simultanious requests. */
92 0,
93 0,
94 0,
95 0,
96 1,
97 0
98 };
99
100
101 /* Since the list is global we need a mutex protecting it. */
102 pthread_mutex_t __aio_requests_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
103
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;
108
109
110 /* Functions to handle request list pool. */
111 static struct requestlist *
112 get_elem (void)
113 {
114 struct requestlist *result;
115
116 if (freelist == NULL)
117 {
118 struct requestlist *new_row;
119 int cnt;
120
121 assert (sizeof (struct aiocb) == sizeof (struct aiocb64));
122
123 if (pool_size + 1 >= pool_max_size)
124 {
125 size_t new_max_size = pool_max_size + ROWS_STEP;
126 struct requestlist **new_tab;
127
128 new_tab = (struct requestlist **)
129 realloc (pool, new_max_size * sizeof (struct requestlist *));
130
131 if (new_tab == NULL)
132 return NULL;
133
134 pool_max_size = new_max_size;
135 pool = new_tab;
136 }
137
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));
142 if (new_row == NULL)
143 return NULL;
144
145 pool[pool_size++] = new_row;
146
147 /* Put all the new entries in the freelist. */
148 do
149 {
150 new_row->next_prio = freelist;
151 freelist = new_row++;
152 }
153 while (--cnt > 0);
154 }
155
156 result = freelist;
157 freelist = freelist->next_prio;
158
159 return result;
160 }
161
162
163 void
164 internal_function
165 __aio_free_request (struct requestlist *elem)
166 {
167 elem->running = no;
168 elem->next_prio = freelist;
169 freelist = elem;
170 }
171
172
173 struct requestlist *
174 internal_function
175 __aio_find_req (aiocb_union *elem)
176 {
177 struct requestlist *runp = requests;
178 int fildes = elem->aiocb.aio_fildes;
179
180 while (runp != NULL && runp->aiocbp->aiocb.aio_fildes < fildes)
181 runp = runp->next_fd;
182
183 if (runp != NULL)
184 {
185 if (runp->aiocbp->aiocb.aio_fildes != fildes)
186 runp = NULL;
187 else
188 while (runp != NULL && runp->aiocbp != elem)
189 runp = runp->next_prio;
190 }
191
192 return runp;
193 }
194
195
196 struct requestlist *
197 internal_function
198 __aio_find_req_fd (int fildes)
199 {
200 struct requestlist *runp = requests;
201
202 while (runp != NULL && runp->aiocbp->aiocb.aio_fildes < fildes)
203 runp = runp->next_fd;
204
205 return (runp != NULL && runp->aiocbp->aiocb.aio_fildes == fildes
206 ? runp : NULL);
207 }
208
209
210 void
211 internal_function
212 __aio_remove_request (struct requestlist *last, struct requestlist *req,
213 int all)
214 {
215 assert (req->running == yes || req->running == queued
216 || req->running == done);
217
218 if (last != NULL)
219 last->next_prio = all ? NULL : req->next_prio;
220 else
221 {
222 if (all || req->next_prio == NULL)
223 {
224 if (req->last_fd != NULL)
225 req->last_fd->next_fd = req->next_fd;
226 else
227 requests = req->next_fd;
228 if (req->next_fd != NULL)
229 req->next_fd->last_fd = req->last_fd;
230 }
231 else
232 {
233 if (req->last_fd != NULL)
234 req->last_fd->next_fd = req->next_prio;
235 else
236 requests = req->next_prio;
237
238 if (req->next_fd != NULL)
239 req->next_fd->last_fd = req->next_prio;
240
241 req->next_prio->last_fd = req->last_fd;
242 req->next_prio->next_fd = req->next_fd;
243
244 /* Mark this entry as runnable. */
245 req->next_prio->running = yes;
246 }
247
248 if (req->running == yes)
249 {
250 struct requestlist *runp = runlist;
251
252 last = NULL;
253 while (runp != NULL)
254 {
255 if (runp == req)
256 {
257 if (last == NULL)
258 runlist = runp->next_run;
259 else
260 last->next_run = runp->next_run;
261 break;
262 }
263 last = runp;
264 runp = runp->next_run;
265 }
266 }
267 }
268 }
269
270
271 /* The thread handler. */
272 static void *handle_fildes_io (void *arg);
273
274
275 /* User optimization. */
276 void
277 __aio_init (const struct aioinit *init)
278 {
279 /* Get the mutex. */
280 pthread_mutex_lock (&__aio_requests_mutex);
281
282 /* Only allow writing new values if the table is not yet allocated. */
283 if (pool == NULL)
284 {
285 optim.aio_threads = init->aio_threads < 1 ? 1 : init->aio_threads;
286 optim.aio_num = (init->aio_num < ENTRIES_PER_ROW
287 ? ENTRIES_PER_ROW
288 : init->aio_num & ~ENTRIES_PER_ROW);
289 }
290
291 if (init->aio_idle_time != 0)
292 optim.aio_idle_time = init->aio_idle_time;
293
294 /* Release the mutex. */
295 pthread_mutex_unlock (&__aio_requests_mutex);
296 }
297 weak_alias (__aio_init, aio_init)
298
299
300 /* The main function of the async I/O handling. It enqueues requests
301 and if necessary starts and handles threads. */
302 struct requestlist *
303 internal_function
304 __aio_enqueue_request (aiocb_union *aiocbp, int operation)
305 {
306 int result = 0;
307 int policy, prio;
308 struct sched_param param;
309 struct requestlist *last, *runp, *newp;
310 int running = no;
311
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)
316 {
317 /* Invalid priority value. */
318 __set_errno (EINVAL);
319 aiocbp->aiocb.__error_code = EINVAL;
320 aiocbp->aiocb.__return_value = -1;
321 return NULL;
322 }
323
324 /* Compute priority for this request. */
325 pthread_getschedparam (pthread_self (), &policy, &param);
326 prio = param.sched_priority - aiocbp->aiocb.aio_reqprio;
327
328 /* Get the mutex. */
329 pthread_mutex_lock (&__aio_requests_mutex);
330
331 last = NULL;
332 runp = requests;
333 /* First look whether the current file descriptor is currently
334 worked with. */
335 while (runp != NULL
336 && runp->aiocbp->aiocb.aio_fildes < aiocbp->aiocb.aio_fildes)
337 {
338 last = runp;
339 runp = runp->next_fd;
340 }
341
342 /* Get a new element for the waiting list. */
343 newp = get_elem ();
344 if (newp == NULL)
345 {
346 pthread_mutex_unlock (&__aio_requests_mutex);
347 __set_errno (EAGAIN);
348 return NULL;
349 }
350 newp->aiocbp = aiocbp;
351 #ifdef BROKEN_THREAD_SIGNALS
352 newp->caller_pid = (aiocbp->aiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL
353 ? getpid () : 0);
354 #endif
355 newp->waiting = NULL;
356
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;
362
363 if (runp != NULL
364 && runp->aiocbp->aiocb.aio_fildes == aiocbp->aiocb.aio_fildes)
365 {
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
372 priority. */
373
374 /* Simply enqueue it after the running one according to the
375 priority. */
376 while (runp->next_prio != NULL
377 && runp->next_prio->aiocbp->aiocb.__abs_prio >= prio)
378 runp = runp->next_prio;
379
380 newp->next_prio = runp->next_prio;
381 runp->next_prio = newp;
382
383 running = queued;
384 }
385 else
386 {
387 running = yes;
388 /* Enqueue this request for a new descriptor. */
389 if (last == NULL)
390 {
391 newp->last_fd = NULL;
392 newp->next_fd = requests;
393 if (requests != NULL)
394 requests->last_fd = newp;
395 requests = newp;
396 }
397 else
398 {
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;
404 }
405
406 newp->next_prio = NULL;
407 }
408
409 if (running == yes)
410 {
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
414 terminate.
415
416 If no new thread can be created or if the specified limit of
417 threads for AIO is reached we queue the request. */
418
419 /* See if we need to and are able to create a thread. */
420 if (nthreads < optim.aio_threads && idle_thread_count == 0)
421 {
422 pthread_t thid;
423
424 running = newp->running = allocated;
425
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
430 `aio_error'. */
431 ++nthreads;
432 else
433 {
434 /* Reset the running flag. The new request is not running. */
435 running = newp->running = yes;
436
437 if (nthreads == 0)
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. */
441 result = -1;
442 }
443 }
444 }
445
446 /* Enqueue the request in the run queue if it is not yet running. */
447 if (running == yes && result == 0)
448 {
449 add_request_to_runlist (newp);
450
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);
455 }
456
457 if (result == 0)
458 newp->running = running;
459 else
460 {
461 /* Something went wrong. */
462 __aio_free_request (newp);
463 newp = NULL;
464 }
465
466 /* Release the mutex. */
467 pthread_mutex_unlock (&__aio_requests_mutex);
468
469 return newp;
470 }
471
472
473 static void *
474 handle_fildes_io (void *arg)
475 {
476 pthread_t self = pthread_self ();
477 struct sched_param param;
478 struct requestlist *runp = (struct requestlist *) arg;
479 aiocb_union *aiocbp;
480 int policy;
481 int fildes;
482
483 pthread_getschedparam (self, &policy, &param);
484
485 do
486 {
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
491 end. */
492 if (runp == NULL)
493 pthread_mutex_lock (&__aio_requests_mutex);
494 else
495 {
496 /* Hopefully this request is marked as running. */
497 assert (runp->running == allocated);
498
499 /* Update our variables. */
500 aiocbp = runp->aiocbp;
501 fildes = aiocbp->aiocb.aio_fildes;
502
503 /* Change the priority to the requested value (if necessary). */
504 if (aiocbp->aiocb.__abs_prio != param.sched_priority
505 || aiocbp->aiocb.__policy != policy)
506 {
507 param.sched_priority = aiocbp->aiocb.__abs_prio;
508 policy = aiocbp->aiocb.__policy;
509 pthread_setschedparam (self, policy, &param);
510 }
511
512 /* Process request pointed to by RUNP. We must not be disturbed
513 by signals. */
514 if ((aiocbp->aiocb.aio_lio_opcode & 127) == LIO_READ)
515 {
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));
522 else
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));
528
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
533 read. */
534 aiocbp->aiocb.__return_value =
535 TEMP_FAILURE_RETRY (read (fildes,
536 (void *) aiocbp->aiocb64.aio_buf,
537 aiocbp->aiocb64.aio_nbytes));
538 }
539 else if ((aiocbp->aiocb.aio_lio_opcode & 127) == LIO_WRITE)
540 {
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));
547 else
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));
553
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
558 write. */
559 aiocbp->aiocb.__return_value =
560 TEMP_FAILURE_RETRY (write (fildes,
561 (void *) aiocbp->aiocb64.aio_buf,
562 aiocbp->aiocb64.aio_nbytes));
563 }
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));
570 else
571 {
572 /* This is an invalid opcode. */
573 aiocbp->aiocb.__return_value = -1;
574 __set_errno (EINVAL);
575 }
576
577 /* Get the mutex. */
578 pthread_mutex_lock (&__aio_requests_mutex);
579
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.
584
585 But since the pthread_mutex_lock call involves write memory
586 barriers as well it is not necessary. */
587
588 if (aiocbp->aiocb.__return_value == -1)
589 aiocbp->aiocb.__error_code = errno;
590 else
591 aiocbp->aiocb.__error_code = 0;
592
593 /* Send the signal to notify about finished processing of the
594 request. */
595 __aio_notify (runp);
596
597 /* For debugging purposes we reset the running flag of the
598 finished request. */
599 assert (runp->running == allocated);
600 runp->running = done;
601
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);
606
607 /* Free the old element. */
608 __aio_free_request (runp);
609 }
610
611 runp = runlist;
612
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)
616 {
617 struct timeval now;
618 struct timespec wakeup_time;
619
620 ++idle_thread_count;
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)
625 {
626 wakeup_time.tv_nsec -= 1000000000;
627 ++wakeup_time.tv_sec;
628 }
629 pthread_cond_timedwait (&__aio_new_request_notification,
630 &__aio_requests_mutex,
631 &wakeup_time);
632 --idle_thread_count;
633 runp = runlist;
634 }
635
636 if (runp == NULL)
637 --nthreads;
638 else
639 {
640 assert (runp->running == yes);
641 runp->running = allocated;
642 runlist = runp->next_run;
643
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. */
647 if (runlist != NULL)
648 {
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)
656 {
657 pthread_t thid;
658 pthread_attr_t attr;
659
660 /* Make sure the thread is created detached. */
661 pthread_attr_init (&attr);
662 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
663
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)
668 == 0)
669 ++nthreads;
670 }
671 }
672 }
673
674 /* Release the mutex. */
675 pthread_mutex_unlock (&__aio_requests_mutex);
676 }
677 while (runp != NULL);
678
679 return NULL;
680 }
681
682
683 /* Free allocated resources. */
684 libc_freeres_fn (free_res)
685 {
686 size_t row;
687
688 for (row = 0; row < pool_max_size; ++row)
689 free (pool[row]);
690
691 free (pool);
692 }
693
694
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
698 assertion. */
699 static void
700 add_request_to_runlist (struct requestlist *newrequest)
701 {
702 int prio = newrequest->aiocbp->aiocb.__abs_prio;
703 struct requestlist *runp;
704
705 if (runlist == NULL || runlist->aiocbp->aiocb.__abs_prio < prio)
706 {
707 newrequest->next_run = runlist;
708 runlist = newrequest;
709 }
710 else
711 {
712 runp = runlist;
713
714 while (runp->next_run != NULL
715 && runp->next_run->aiocbp->aiocb.__abs_prio >= prio)
716 runp = runp->next_run;
717
718 newrequest->next_run = runp->next_run;
719 runp->next_run = newrequest;
720 }
721 }