Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Haiku #131

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Haiku #131

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 60 additions & 0 deletions atomicops.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ extern "C" {
#include <mach/mach.h>
#elif defined(__unix__)
#include <semaphore.h>
#elif defined(__HAIKU__)
#include <kernel/OS.h>
#elif defined(FREERTOS)
#include <FreeRTOS.h>
#include <semphr.h>
Expand Down Expand Up @@ -581,6 +583,64 @@ namespace moodycamel
}
}
};
#elif defined(__HAIKU__)
//---------------------------------------------------------
// Semaphore (Haiku)
//---------------------------------------------------------
class Semaphore
{
private:
sem_id m_sema;

Semaphore(const Semaphore& other);
Semaphore& operator=(const Semaphore& other);

public:
AE_NO_TSAN Semaphore(int initialCount = 0) : m_sema()
{
assert(initialCount >= 0);
m_sema = create_sem(initialCount, "");
assert(m_sema >= B_OK);
}

AE_NO_TSAN ~Semaphore()
{
delete_sem(m_sema);
}

bool wait() AE_NO_TSAN
{
// accquire the semaphore
status_t rc;
rc = acquire_sem(m_sema);
return rc == B_NO_ERROR;
}

bool try_wait() AE_NO_TSAN
{
// accquire the semaphore
status_t rc;
rc = acquire_sem_etc(m_sema, 1, B_RELATIVE_TIMEOUT, 0);
return rc == B_NO_ERROR;
}

bool timed_wait(std::uint64_t usecs) AE_NO_TSAN
{
status_t rc;
rc = acquire_sem_etc(m_sema, 1, B_RELATIVE_TIMEOUT, (bigtime_t) usecs);
return rc == B_NO_ERROR;
}

void signal() AE_NO_TSAN
{
while (release_sem(m_sema) != B_NO_ERROR);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like an infinite loop on error? As far as I can tell from the docs, release_sem does not return spuriously.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the while loop as it didn't make sense. The reason I had it in there was because I copied the unix version of it and replaced the methods at first, which was kind of naive from me. If it is ok I'd create a recipe for HaikuPorts once this is merged so it can be installed as an develpment header from a package

}

void signal(int count) AE_NO_TSAN
{
release_sem_etc(m_sema, count, 0);
}
};
#elif defined(FREERTOS)
//---------------------------------------------------------
// Semaphore (FreeRTOS)
Expand Down
12 changes: 8 additions & 4 deletions tests/stabtest/makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
ifeq ($(OS),Windows_NT)
EXT=.exe
PLATFORM_OPTS=-static
PLATFORM_LD_OPTS=-Wl,--no-as-needed
PLATFORM_LD_OPTS=-pthread -Wl,--no-as-needed
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
EXT=
PLATFORM_OPTS=
PLATFORM_LD_OPTS=
PLATFORM_LD_OPTS=-pthread
else ifeq ($(UNAME_S),Haiku)
EXT=
PLATFORM_OPTS=
PLATFORM_LD_OPTS=-Wl,--no-as-needed
else
EXT=
PLATFORM_OPTS=
PLATFORM_LD_OPTS=-lrt -Wl,--no-as-needed
PLATFORM_LD_OPTS=-pthread -lrt -Wl,--no-as-needed
endif
endif

default: stabtest$(EXT)

stabtest$(EXT): stabtest.cpp ../../readerwriterqueue.h ../../atomicops.h ../common/simplethread.h ../common/simplethread.cpp makefile
g++ $(PLATFORM_OPTS) -std=c++11 -Wsign-conversion -Wpedantic -Wall -DNDEBUG -O3 stabtest.cpp ../common/simplethread.cpp -o stabtest$(EXT) -pthread $(PLATFORM_LD_OPTS)
g++ $(PLATFORM_OPTS) -std=c++11 -Wsign-conversion -Wpedantic -Wall -DNDEBUG -O3 stabtest.cpp ../common/simplethread.cpp -o stabtest$(EXT) $(PLATFORM_LD_OPTS)

run: stabtest$(EXT)
./stabtest$(EXT)
12 changes: 8 additions & 4 deletions tests/unittests/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
ifeq ($(OS),Windows_NT)
EXT=.exe
PLATFORM_OPTS=-static
PLATFORM_LD_OPTS=-Wl,--no-as-needed
PLATFORM_LD_OPTS=-pthread -Wl,--no-as-needed
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
EXT=
PLATFORM_OPTS=
PLATFORM_LD_OPTS=
PLATFORM_LD_OPTS=-pthread
else ifeq ($(UNAME_S),Haiku)
EXT=
PLATFORM_OPTS=
PLATFORM_LD_OPTS=-Wl,--no-as-needed
else
EXT=
PLATFORM_OPTS=
PLATFORM_LD_OPTS=-lrt -Wl,--no-as-needed
PLATFORM_LD_OPTS=-pthread -lrt -Wl,--no-as-needed
endif
endif


default: unittests$(EXT)

unittests$(EXT): unittests.cpp ../../readerwriterqueue.h ../../readerwritercircularbuffer.h ../../atomicops.h ../common/simplethread.h ../common/simplethread.cpp minitest.h makefile
g++ $(PLATFORM_OPTS) -std=c++11 -Wsign-conversion -Wpedantic -Wall -DNDEBUG -O3 -g unittests.cpp ../common/simplethread.cpp -o unittests$(EXT) -pthread $(PLATFORM_LD_OPTS)
g++ $(PLATFORM_OPTS) -std=c++11 -Wsign-conversion -Wpedantic -Wall -DNDEBUG -O3 -g unittests.cpp ../common/simplethread.cpp -o unittests$(EXT) $(PLATFORM_LD_OPTS)

run: unittests$(EXT)
./unittests$(EXT)