1 /* Suspend until termination of a requests.
2 Copyright (C) 1997,1998,1999,2000,2002,2003,2005
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
23 /* We use an UGLY hack to prevent gcc from finding us cheating. The
24 implementations of aio_suspend and aio_suspend64 are identical and so
25 we want to avoid code duplication by using aliases. But gcc sees
26 the different parameter lists and prints a warning. We define here
27 a function so that aio_suspend64 has no prototype. */
28 #define aio_suspend64 XXX
30 /* And undo the hack. */
39 #include <bits/libc-lock.h>
45 const struct aiocb
*const *list
;
46 struct waitlist
*waitlist
;
47 struct requestlist
**requestlist
;
56 const struct clparam
*param
= (const struct clparam
*) arg
;
58 /* Now remove the entry in the waiting list for all requests
59 which didn't terminate. */
60 int cnt
= param
->nent
;
62 if (param
->list
[cnt
] != NULL
63 && param
->list
[cnt
]->__error_code
== EINPROGRESS
)
65 struct waitlist
**listp
;
67 assert (param
->requestlist
[cnt
] != NULL
);
69 /* There is the chance that we cannot find our entry anymore. This
70 could happen if the request terminated and restarted again. */
71 listp
= ¶m
->requestlist
[cnt
]->waiting
;
72 while (*listp
!= NULL
&& *listp
!= ¶m
->waitlist
[cnt
])
73 listp
= &(*listp
)->next
;
76 *listp
= (*listp
)->next
;
79 /* Release the conditional variable. */
80 (void) pthread_cond_destroy (param
->cond
);
82 /* Release the mutex. */
83 pthread_mutex_unlock (&__aio_requests_mutex
);
88 aio_suspend (list
, nent
, timeout
)
89 const struct aiocb
*const list
[];
91 const struct timespec
*timeout
;
93 struct waitlist waitlist
[nent
];
94 struct requestlist
*requestlist
[nent
];
95 pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
101 /* Request the mutex. */
102 pthread_mutex_lock (&__aio_requests_mutex
);
104 /* There is not yet a finished request. Signal the request that
105 we are working for it. */
106 for (cnt
= 0; cnt
< nent
; ++cnt
)
107 if (list
[cnt
] != NULL
)
109 if (list
[cnt
]->__error_code
== EINPROGRESS
)
111 requestlist
[cnt
] = __aio_find_req ((aiocb_union
*) list
[cnt
]);
113 if (requestlist
[cnt
] != NULL
)
115 waitlist
[cnt
].cond
= &cond
;
116 waitlist
[cnt
].next
= requestlist
[cnt
]->waiting
;
117 waitlist
[cnt
].counterp
= &dummy
;
118 waitlist
[cnt
].sigevp
= NULL
;
119 #ifdef BROKEN_THREAD_SIGNALS
120 waitlist
[cnt
].caller_pid
= 0; /* Not needed. */
122 requestlist
[cnt
]->waiting
= &waitlist
[cnt
];
126 /* We will never suspend. */
130 /* We will never suspend. */
135 /* Only if none of the entries is NULL or finished to be wait. */
136 if (cnt
== nent
&& any
)
138 struct clparam clparam
=
141 .waitlist
= waitlist
,
142 .requestlist
= requestlist
,
147 pthread_cleanup_push (cleanup
, &clparam
);
150 result
= pthread_cond_wait (&cond
, &__aio_requests_mutex
);
153 /* We have to convert the relative timeout value into an
154 absolute time value with pthread_cond_timedwait expects. */
156 struct timespec abstime
;
158 __gettimeofday (&now
, NULL
);
159 abstime
.tv_nsec
= timeout
->tv_nsec
+ now
.tv_usec
* 1000;
160 abstime
.tv_sec
= timeout
->tv_sec
+ now
.tv_sec
;
161 if (abstime
.tv_nsec
>= 1000000000)
163 abstime
.tv_nsec
-= 1000000000;
167 result
= pthread_cond_timedwait (&cond
, &__aio_requests_mutex
,
171 pthread_cleanup_pop (0);
174 /* Now remove the entry in the waiting list for all requests
175 which didn't terminate. */
177 if (list
[cnt
] != NULL
&& list
[cnt
]->__error_code
== EINPROGRESS
)
179 struct waitlist
**listp
;
181 assert (requestlist
[cnt
] != NULL
);
183 /* There is the chance that we cannot find our entry anymore. This
184 could happen if the request terminated and restarted again. */
185 listp
= &requestlist
[cnt
]->waiting
;
186 while (*listp
!= NULL
&& *listp
!= &waitlist
[cnt
])
187 listp
= &(*listp
)->next
;
190 *listp
= (*listp
)->next
;
193 /* Release the conditional variable. */
194 if (__builtin_expect (pthread_cond_destroy (&cond
) != 0, 0))
195 /* This must never happen. */
200 /* An error occurred. Possibly it's EINTR. We have to translate
201 the timeout error report of `pthread_cond_timedwait' to the
202 form expected from `aio_suspend'. */
203 if (result
== ETIMEDOUT
)
204 __set_errno (EAGAIN
);
206 __set_errno (result
);
211 /* Release the mutex. */
212 pthread_mutex_unlock (&__aio_requests_mutex
);
217 weak_alias (aio_suspend
, aio_suspend64
)