Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 48dc17ef authored by Sharvil Nanavati's avatar Sharvil Nanavati
Browse files

Expose semaphore's file descriptor.

|semaphore_get_fd| can be used by callers to determine if it's safe
to call |semaphore_wait| and |semaphore_post| without blocking.

Change-Id: Icf758f03ea2aa85cddcb4ab3f7906b2c587fb8ac
parent 933e4d6e
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -43,3 +43,15 @@ bool semaphore_try_wait(semaphore_t *semaphore);

// Increments the value of |semaphore|. |semaphore| may not be NULL.
void semaphore_post(semaphore_t *semaphore);

// Returns a file descriptor representing this semaphore. The caller may
// only perform one operation on the file descriptor: select(2). If |select|
// indicates the fd is readable, the caller may call |semaphore_wait|
// without blocking. If select indicates the fd is writable, the caller may
// call |semaphore_post| without blocking. Note that there may be a race
// condition between calling |select| and |semaphore_wait| or |semaphore_post|
// which results in blocking behaviour.
//
// The caller must not close the returned file descriptor. |semaphore| may not
// be NULL.
int semaphore_get_fd(const semaphore_t *semaphore);
+6 −0
Original line number Diff line number Diff line
@@ -93,3 +93,9 @@ void semaphore_post(semaphore_t *semaphore) {
  if (eventfd_write(semaphore->fd, 1ULL) == -1)
    ALOGE("%s unable to post to semaphore: %s", __func__, strerror(errno));
}

int semaphore_get_fd(const semaphore_t *semaphore) {
  assert(semaphore != NULL);
  assert(semaphore->fd != -1);
  return semaphore->fd;
}