2011-03-12 21:55:45 -05:00
|
|
|
#include "stdio_impl.h"
|
|
|
|
#include "pthread_impl.h"
|
2012-11-08 17:04:20 -05:00
|
|
|
#include <limits.h>
|
2011-03-12 21:55:45 -05:00
|
|
|
|
2018-04-17 23:59:41 -04:00
|
|
|
#define MAYBE_WAITERS 0x40000000
|
|
|
|
|
2014-08-23 23:35:10 -04:00
|
|
|
void __do_orphaned_stdio_locks()
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
for (f=__pthread_self()->stdio_locks; f; f=f->next_locked)
|
|
|
|
a_store(&f->lock, 0x40000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __unlist_locked_file(FILE *f)
|
|
|
|
{
|
|
|
|
if (f->lockcount) {
|
|
|
|
if (f->next_locked) f->next_locked->prev_locked = f->prev_locked;
|
|
|
|
if (f->prev_locked) f->prev_locked->next_locked = f->next_locked;
|
|
|
|
else __pthread_self()->stdio_locks = f->next_locked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-12 21:55:45 -05:00
|
|
|
int ftrylockfile(FILE *f)
|
|
|
|
{
|
2014-08-23 23:35:10 -04:00
|
|
|
pthread_t self = __pthread_self();
|
|
|
|
int tid = self->tid;
|
2018-04-17 23:59:41 -04:00
|
|
|
int owner = f->lock;
|
|
|
|
if ((owner & ~MAYBE_WAITERS) == tid) {
|
2011-07-30 08:02:14 -04:00
|
|
|
if (f->lockcount == LONG_MAX)
|
2011-03-12 21:55:45 -05:00
|
|
|
return -1;
|
|
|
|
f->lockcount++;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-17 23:59:41 -04:00
|
|
|
if (owner < 0) f->lock = owner = 0;
|
|
|
|
if (owner || a_cas(&f->lock, 0, tid))
|
2011-03-12 21:55:45 -05:00
|
|
|
return -1;
|
|
|
|
f->lockcount = 1;
|
2014-08-23 23:35:10 -04:00
|
|
|
f->prev_locked = 0;
|
|
|
|
f->next_locked = self->stdio_locks;
|
2014-09-19 12:28:45 -04:00
|
|
|
if (f->next_locked) f->next_locked->prev_locked = f;
|
2014-08-23 23:35:10 -04:00
|
|
|
self->stdio_locks = f;
|
2011-03-12 21:55:45 -05:00
|
|
|
return 0;
|
|
|
|
}
|