summaryrefslogtreecommitdiff
path: root/src/stdio/ftrylockfile.c
blob: 67f4b6a070ca2c1c833c8d92401e6f8ac75c251f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdio_impl.h"
#include "pthread_impl.h"

int ftrylockfile(FILE *f)
{
	int tid = pthread_self()->tid;
	if (!libc.lockfile) libc.lockfile = __lockfile;
	if (f->lock == tid) {
		if (f->lockcount == INT_MAX)
			return -1;
		f->lockcount++;
		return 0;
	}
	if (f->lock || a_cas(&f->lock, 0, tid))
		return -1;
	f->lockcount = 1;
	return 0;
}