Skip to content

Commit

Permalink
Error handling of semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
netheril96 committed Apr 14, 2024
1 parent 6d022fe commit 307206a
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions sources/fuse2_workaround.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,37 @@ namespace
class Semaphore
{
public:
Semaphore() { sem_init(&s_, 0, 0); }
Semaphore()
{
if (sem_init(&s_, 0, 0) < 0)
THROW_POSIX_EXCEPTION(errno, "Failed to initialize semaphore");
}
~Semaphore() { sem_destroy(&s_); }
DISABLE_COPY_MOVE(Semaphore);

void wait() { sem_wait(&s_); }
void post() { sem_post(&s_); }
void wait()
{
while (true)
{
int rc = sem_wait(&s_);
if (rc < 0 && errno == EINTR)
{
continue;
}
if (rc < 0)
{
THROW_POSIX_EXCEPTION(errno, "sem_wait fails");
}
return;
}
}
void post()
{
if (sem_post(&s_) < 0)
{
THROW_POSIX_EXCEPTION(errno, "sem_post fails");
}
}

private:
sem_t s_;
Expand Down

0 comments on commit 307206a

Please sign in to comment.