summaryrefslogtreecommitdiff
path: root/src/mutex.cpp
blob: 2f772202146b3ce3c27b82ab296394ecc7096e39 (plain) (blame)
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
#include <mutex.h>
#include <exception.h>

#include <cerrno>

using namespace newsbeuter;

mutex::mutex() {
	pthread_mutex_init(&mtx, NULL);
}

mutex::~mutex() {
	pthread_mutex_destroy(&mtx);
}

void mutex::lock() {
	int rc = pthread_mutex_lock(&mtx);
	if (rc != 0) {
		throw exception(rc);
	}
}

void mutex::unlock() {
	int rc = pthread_mutex_unlock(&mtx);
	if (rc != 0) {
		throw exception(rc);
	}
}

bool mutex::trylock() {
	int rc = pthread_mutex_trylock(&mtx);
	if (rc != 0) {
		if (EBUSY == rc) {
			return false;
		} else {
			throw exception(rc);
		}
	} else {
		return true;
	}
}