xref: /haiku/src/tools/cppunit/SemaphoreSyncObject.cpp (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
1 #include <SemaphoreSyncObject.h>
2 #include <cppunit/Exception.h>
3 
4 _EXPORT
5 SemaphoreSyncObject::SemaphoreSyncObject()
6 	: fSemId(create_sem(1, "CppUnitSync"))
7 {
8 	if (fSemId < B_OK)
9 		throw CppUnit::Exception("SemaphoreSyncObject::SemaphoreSyncObject() -- Error creating semaphore");
10 }
11 
12 _EXPORT
13 SemaphoreSyncObject::~SemaphoreSyncObject() {
14 	delete_sem(fSemId);
15 }
16 
17 _EXPORT
18 void
19 SemaphoreSyncObject::lock() {
20 	if (acquire_sem(fSemId) < B_OK)
21 		throw CppUnit::Exception("SemaphoreSyncObject::lock() -- Error acquiring semaphore");
22 }
23 
24 _EXPORT
25 void
26 SemaphoreSyncObject::unlock() {
27 	if (release_sem(fSemId) < B_OK)
28 		throw CppUnit::Exception("SemaphoreSyncObject::unlock() -- Error releasing semaphore");
29 }
30