summaryrefslogtreecommitdiff
path: root/drivers/unix/rw_lock_posix.cpp
blob: 772a7b812a16498e82da4be111a640b6a3fea41f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

#if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)

#include "os/memory.h"
#include "rw_lock_posix.h"
#include "error_macros.h"
#include <stdio.h>

void RWLockPosix::read_lock() {

	int err =pthread_rwlock_rdlock(&rwlock);
	if (err!=0) {
		perror("wtf: ");
	}
	ERR_FAIL_COND(err!=0);
}

void RWLockPosix::read_unlock() {

	pthread_rwlock_unlock(&rwlock);
}

Error RWLockPosix::read_try_lock() {

	if (pthread_rwlock_tryrdlock(&rwlock)!=0) {
		return ERR_BUSY;
	} else {
		return OK;
	}

}

void RWLockPosix::write_lock() {

	int err = pthread_rwlock_wrlock(&rwlock);
	ERR_FAIL_COND(err!=0);
}

void RWLockPosix::write_unlock() {

	pthread_rwlock_unlock(&rwlock);
}

Error RWLockPosix::write_try_lock() {
	if (pthread_rwlock_trywrlock(&rwlock)!=0) {
		return ERR_BUSY;
	} else {
		return OK;
	}
}


RWLock *RWLockPosix::create_func_posix() {

	return memnew( RWLockPosix );
}

void RWLockPosix::make_default() {

	create_func=create_func_posix;
}


RWLockPosix::RWLockPosix() {

	rwlock=PTHREAD_RWLOCK_INITIALIZER;
}


RWLockPosix::~RWLockPosix() {

	pthread_rwlock_destroy(&rwlock);

}

#endif