1570af302Sopenharmony_ci#include "stdio_impl.h" 2570af302Sopenharmony_ci#include "pthread_impl.h" 3570af302Sopenharmony_ci 4570af302Sopenharmony_ciint __lockfile(FILE *f) 5570af302Sopenharmony_ci{ 6570af302Sopenharmony_ci int owner = f->lock, tid = __pthread_self()->tid; 7570af302Sopenharmony_ci if ((owner & ~MAYBE_WAITERS) == tid) 8570af302Sopenharmony_ci return 0; 9570af302Sopenharmony_ci owner = a_cas(&f->lock, 0, tid); 10570af302Sopenharmony_ci if (!owner) return 1; 11570af302Sopenharmony_ci while ((owner = a_cas(&f->lock, 0, tid|MAYBE_WAITERS))) { 12570af302Sopenharmony_ci if ((owner & MAYBE_WAITERS) || 13570af302Sopenharmony_ci a_cas(&f->lock, owner, owner|MAYBE_WAITERS)==owner) 14570af302Sopenharmony_ci __futexwait(&f->lock, owner|MAYBE_WAITERS, 1); 15570af302Sopenharmony_ci } 16570af302Sopenharmony_ci return 1; 17570af302Sopenharmony_ci} 18570af302Sopenharmony_ci 19570af302Sopenharmony_civoid __unlockfile(FILE *f) 20570af302Sopenharmony_ci{ 21570af302Sopenharmony_ci if (a_swap(&f->lock, 0) & MAYBE_WAITERS) 22570af302Sopenharmony_ci __wake(&f->lock, 1, 1); 23570af302Sopenharmony_ci} 24