2391d786524e75d183771d829945b4678108af41
[musl] / src / aio / aio_suspend.c
1 #include <aio.h>
2 #include <errno.h>
3 #include <time.h>
4 #include "atomic.h"
5 #include "libc.h"
6 #include "pthread_impl.h"
7
8 extern volatile int __aio_fut;
9
10 int aio_suspend(const struct aiocb *const cbs[], int cnt, const struct timespec *ts)
11 {
12         int i, tid = 0, ret, expect = 0;
13         struct timespec at;
14         volatile int dummy_fut, *pfut;
15         int nzcnt = 0;
16         const struct aiocb *cb = 0;
17
18         if (cnt<0) {
19                 errno = EINVAL;
20                 return -1;
21         }
22
23         for (i=0; i<cnt; i++) if (cbs[i]) {
24                 if (aio_error(cbs[i]) != EINPROGRESS) return 0;
25                 nzcnt++;
26                 cb = cbs[i];
27         }
28
29         if (ts) {
30                 clock_gettime(CLOCK_MONOTONIC, &at);
31                 at.tv_sec += ts->tv_sec;
32                 if ((at.tv_nsec += ts->tv_nsec) >= 1000000000) {
33                         at.tv_nsec -= 1000000000;
34                         at.tv_sec++;
35                 }
36         }
37
38         for (;;) {
39                 for (i=0; i<cnt; i++)
40                         if (cbs[i] && aio_error(cbs[i]) != EINPROGRESS)
41                                 return 0;
42
43                 switch (nzcnt) {
44                 case 0:
45                         pfut = &dummy_fut;
46                         break;
47                 case 1:
48                         pfut = (void *)&cb->__err;
49                         expect = EINPROGRESS | 0x80000000;
50                         a_cas(pfut, EINPROGRESS, expect);
51                         break;
52                 default:
53                         pfut = &__aio_fut;
54                         if (!tid) tid = __pthread_self()->tid;
55                         expect = a_cas(pfut, 0, tid);
56                         if (!expect) expect = tid;
57                         /* Need to recheck the predicate before waiting. */
58                         for (i=0; i<cnt; i++)
59                                 if (cbs[i] && aio_error(cbs[i]) != EINPROGRESS)
60                                         return 0;
61                         break;
62                 }
63
64                 ret = __timedwait(pfut, expect, CLOCK_MONOTONIC, ts?&at:0, 0, 0, 1);
65
66                 if (ret) {
67                         errno = ret==ETIMEDOUT ? EAGAIN : ret;
68                         return -1;
69                 }
70         }
71 }
72
73 LFS64(aio_suspend);