summaryrefslogtreecommitdiff
path: root/src/stdio/ftrylockfile.c
blob: 1d0a1ff8662baad6ce8593348c4179530b70ca4e (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)
{
	if (!libc.lockfile) libc.lockfile = __lockfile;
	if (f->owner && f->owner == pthread_self()->tid) {
		if (f->lockcount == INT_MAX)
			return -1;
		f->lockcount++;
		return 0;
	}
	if (a_swap(&f->lock, 1))
		return -1;
	f->owner = pthread_self()->tid;
	f->lockcount = 1;
	return 0;
}