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
|
|
|
|
|
|
|
int ftrylockfile(FILE *f)
|
|
|
|
{
|
2014-06-10 04:02:40 -04:00
|
|
|
int tid = __pthread_self()->tid;
|
2011-03-24 23:16:52 -04:00
|
|
|
if (f->lock == 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;
|
|
|
|
}
|
2011-07-30 08:02:14 -04:00
|
|
|
if (f->lock < 0) f->lock = 0;
|
2011-03-24 23:16:52 -04:00
|
|
|
if (f->lock || a_cas(&f->lock, 0, tid))
|
2011-03-12 21:55:45 -05:00
|
|
|
return -1;
|
|
|
|
f->lockcount = 1;
|
|
|
|
return 0;
|
|
|
|
}
|