2011-03-12 21:55:45 -05:00
|
|
|
#include "stdio_impl.h"
|
|
|
|
#include "pthread_impl.h"
|
|
|
|
|
|
|
|
void __lockfile(FILE *f)
|
|
|
|
{
|
2011-05-06 21:45:48 -04:00
|
|
|
int spins=10000;
|
2011-03-24 23:16:52 -04:00
|
|
|
int tid;
|
|
|
|
|
|
|
|
if (f->lock < 0) return;
|
|
|
|
tid = __pthread_self()->tid;
|
|
|
|
if (f->lock == tid) {
|
2011-03-12 21:55:45 -05:00
|
|
|
while (f->lockcount == INT_MAX);
|
|
|
|
f->lockcount++;
|
|
|
|
return;
|
|
|
|
}
|
2011-03-28 22:36:55 -04:00
|
|
|
while (a_cas(&f->lock, 0, tid))
|
2011-03-12 21:55:45 -05:00
|
|
|
if (spins) spins--, a_spin();
|
2011-04-17 16:32:15 -04:00
|
|
|
else __syscall(SYS_sched_yield);
|
2011-03-12 21:55:45 -05:00
|
|
|
f->lockcount = 1;
|
|
|
|
}
|