1 #include "stdio_impl.h" 2 #include "pthread_impl.h" 3 #include <limits.h> 4 5 #ifdef ENABLE_HWASAN 6 __attribute__((no_sanitize("hwaddress"))) 7 #endif __do_orphaned_stdio_locks()8void __do_orphaned_stdio_locks() 9 { 10 FILE *f; 11 for (f=__pthread_self()->stdio_locks; f; f=f->next_locked) 12 a_store(&f->lock, 0x40000000); 13 } 14 __unlist_locked_file(FILE * f)15void __unlist_locked_file(FILE *f) 16 { 17 if (f->lockcount) { 18 if (f->next_locked) f->next_locked->prev_locked = f->prev_locked; 19 if (f->prev_locked) f->prev_locked->next_locked = f->next_locked; 20 else __pthread_self()->stdio_locks = f->next_locked; 21 } 22 } 23 __register_locked_file(FILE * f,pthread_t self)24void __register_locked_file(FILE *f, pthread_t self) 25 { 26 f->lockcount = 1; 27 f->prev_locked = 0; 28 f->next_locked = self->stdio_locks; 29 if (f->next_locked) f->next_locked->prev_locked = f; 30 self->stdio_locks = f; 31 } 32 ftrylockfile(FILE * f)33int ftrylockfile(FILE *f) 34 { 35 pthread_t self = __pthread_self(); 36 int tid = self->tid; 37 int owner = f->lock; 38 if ((owner & ~MAYBE_WAITERS) == tid) { 39 if (f->lockcount == LONG_MAX) 40 return -1; 41 f->lockcount++; 42 return 0; 43 } 44 if (owner < 0) f->lock = owner = 0; 45 if (owner || a_cas(&f->lock, 0, tid)) 46 return -1; 47 __register_locked_file(f, self); 48 return 0; 49 } 50