| 1 | /* Copyright (C) 2006-2020 Free Software Foundation, Inc. | 
|---|
| 2 | This file is part of the GNU C Library. | 
|---|
| 3 |  | 
|---|
| 4 | The GNU C Library is free software; you can redistribute it and/or | 
|---|
| 5 | modify it under the terms of the GNU Lesser General Public | 
|---|
| 6 | License as published by the Free Software Foundation; either | 
|---|
| 7 | version 2.1 of the License, or (at your option) any later version. | 
|---|
| 8 |  | 
|---|
| 9 | The GNU C Library is distributed in the hope that it will be useful, | 
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 12 | Lesser General Public License for more details. | 
|---|
| 13 |  | 
|---|
| 14 | You should have received a copy of the GNU Lesser General Public | 
|---|
| 15 | License along with the GNU C Library; if not, see | 
|---|
| 16 | <https://www.gnu.org/licenses/>.  */ | 
|---|
| 17 |  | 
|---|
| 18 | /* We define a special synchronization primitive for AIO.  POSIX | 
|---|
| 19 | conditional variables would be ideal but the pthread_cond_*wait | 
|---|
| 20 | operations do not return on EINTR.  This is a requirement for | 
|---|
| 21 | correct aio_suspend and lio_listio implementations.  */ | 
|---|
| 22 |  | 
|---|
| 23 | #include <assert.h> | 
|---|
| 24 | #include <nptl/pthreadP.h> | 
|---|
| 25 | #include <futex-internal.h> | 
|---|
| 26 |  | 
|---|
| 27 | #define DONT_NEED_AIO_MISC_COND	1 | 
|---|
| 28 |  | 
|---|
| 29 | #define AIO_MISC_NOTIFY(waitlist) \ | 
|---|
| 30 | do {									      \ | 
|---|
| 31 | if (*waitlist->counterp > 0 && --*waitlist->counterp == 0)		      \ | 
|---|
| 32 | futex_wake ((unsigned int *) waitlist->counterp, 1, FUTEX_PRIVATE);     \ | 
|---|
| 33 | } while (0) | 
|---|
| 34 |  | 
|---|
| 35 | #define AIO_MISC_WAIT(result, futex, timeout, cancel)			      \ | 
|---|
| 36 | do {									      \ | 
|---|
| 37 | volatile unsigned int *futexaddr = &futex;				      \ | 
|---|
| 38 | unsigned int oldval = futex;					      \ | 
|---|
| 39 | \ | 
|---|
| 40 | if (oldval != 0)							      \ | 
|---|
| 41 | {									      \ | 
|---|
| 42 | pthread_mutex_unlock (&__aio_requests_mutex);			      \ | 
|---|
| 43 | \ | 
|---|
| 44 | int status;							      \ | 
|---|
| 45 | do								      \ | 
|---|
| 46 | {								      \ | 
|---|
| 47 | if (cancel)							      \ | 
|---|
| 48 | status = futex_reltimed_wait_cancelable (			      \ | 
|---|
| 49 | (unsigned int *) futexaddr, oldval, timeout, FUTEX_PRIVATE);  \ | 
|---|
| 50 | else							      \ | 
|---|
| 51 | status = futex_reltimed_wait ((unsigned int *) futexaddr,	      \ | 
|---|
| 52 | oldval, timeout, FUTEX_PRIVATE);	      		      \ | 
|---|
| 53 | if (status != EAGAIN)					      \ | 
|---|
| 54 | break;							      \ | 
|---|
| 55 | \ | 
|---|
| 56 | oldval = *futexaddr;					      \ | 
|---|
| 57 | }								      \ | 
|---|
| 58 | while (oldval != 0);						      \ | 
|---|
| 59 | \ | 
|---|
| 60 | if (status == EINTR)						      \ | 
|---|
| 61 | result = EINTR;						      \ | 
|---|
| 62 | else if (status == ETIMEDOUT)					      \ | 
|---|
| 63 | result = EAGAIN;						      \ | 
|---|
| 64 | else								      \ | 
|---|
| 65 | assert (status == 0 || status == EAGAIN);			      \ | 
|---|
| 66 | \ | 
|---|
| 67 | pthread_mutex_lock (&__aio_requests_mutex);			      \ | 
|---|
| 68 | }									      \ | 
|---|
| 69 | } while (0) | 
|---|
| 70 |  | 
|---|
| 71 | #include_next <aio_misc.h> | 
|---|
| 72 |  | 
|---|