1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
4 int __lockfile(FILE *f
)
6 int owner
= f
->lock
, tid
= __pthread_self()->tid
;
7 if ((owner
& ~MAYBE_WAITERS
) == tid
)
9 owner
= a_cas(&f
->lock
, 0, tid
);
11 while ((owner
= a_cas(&f
->lock
, 0, tid
|MAYBE_WAITERS
))) {
12 if ((owner
& MAYBE_WAITERS
) ||
13 a_cas(&f
->lock
, owner
, owner
|MAYBE_WAITERS
)==owner
)
14 __futexwait(&f
->lock
, owner
|MAYBE_WAITERS
, 1);
19 void __unlockfile(FILE *f
)
21 if (a_swap(&f
->lock
, 0) & MAYBE_WAITERS
)
22 __wake(&f
->lock
, 1, 1);