nlib
Semaphore.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_THREADING_SEMAPHORE_H_
4 #define INCLUDE_NN_NLIB_THREADING_SEMAPHORE_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/DateTime.h"
8 
9 NLIB_NAMESPACE_BEGIN
10 namespace threading {
11 
12 class Semaphore {
13  public:
14  typedef nlib_semaphore* NativeHandle;
15 
16  Semaphore() NLIB_NOEXCEPT : obj_() {}
18  errno_t Init(int initial_count) NLIB_NOEXCEPT {
19  return nlib_semaphore_init(&obj_, initial_count);
20  }
21  errno_t Release(int* previous_count) NLIB_NOEXCEPT {
22  return nlib_semaphore_post(&obj_, previous_count);
23  }
24  errno_t Release(int release_count, int* previous_count) NLIB_NOEXCEPT {
25  return nlib_semaphore_post_ex(&obj_, release_count, previous_count);
26  }
29  return nlib_semaphore_trywait(&obj_);
30  }
32  return nlib_semaphore_trywait_for(&obj_, timeout.ToTimeValue().tick);
33  }
34  NativeHandle GetNativeHandle() NLIB_NOEXCEPT { return &obj_; }
35 
36  private:
37  nlib_semaphore obj_;
38 };
39 
40 } // namespace threading
41 NLIB_NAMESPACE_END
42 
43 NLIB_NAMESPACE_BEGIN
44 namespace threading {
45 
46 class ScopedSemaphore {
47  public:
48  explicit NLIB_ALWAYS_INLINE ScopedSemaphore(Semaphore& rhs) NLIB_NOEXCEPT // NOLINT
49  : semaphore_(rhs) {
50  semaphore_.Acquire();
51  }
52  NLIB_ALWAYS_INLINE ~ScopedSemaphore() NLIB_NOEXCEPT {
53  semaphore_.Release(NULL);
54  }
55 
56  private:
57  Semaphore& semaphore_;
58  NLIB_DISALLOW_COPY_AND_ASSIGN(ScopedSemaphore);
59 };
60 
61 } // namespace threading
62 NLIB_NAMESPACE_END
63 
64 #endif // INCLUDE_NN_NLIB_THREADING_SEMAPHORE_H_
errno_t nlib_semaphore_post_ex(nlib_semaphore *sem, int release_count, int *previous_count)
Increments the semaphore count by the amount specified by releaseCount.
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:69
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:145
sem_t nlib_semaphore
The type for a semaphore object.
NLIB_CHECK_RESULT errno_t nlib_semaphore_trywait_for(nlib_semaphore *sem, nlib_duration duration)
Decrements the semaphore count by 1 if the count is not 0. If 0, waits for the period specified by du...
errno_t TryAcquire(const nlib_ns::TimeSpan &timeout) noexcept
Locks one semaphore with timeout.
Definition: Semaphore.h:31
errno_t Release(int *previous_count) noexcept
Releases one semaphore.
Definition: Semaphore.h:21
errno_t Release(int release_count, int *previous_count) noexcept
Releases multiple semaphores. May not be executed atomically.
Definition: Semaphore.h:24
Implements a semaphore.
Definition: Semaphore.h:12
NLIB_CHECK_RESULT errno_t nlib_semaphore_trywait(nlib_semaphore *sem)
Decrements the semaphore count by 1 if the count is not 0.
errno_t Init(int initial_count) noexcept
Initializes the semaphore.
Definition: Semaphore.h:18
Defines the class for handling times and durations.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
errno_t nlib_semaphore_init(nlib_semaphore *sem, int initial_count)
Initializes the semaphore object specified by sem.
A file that contains the configuration information for each development environment.
errno_t TryAcquire() noexcept
Tries to lock a semaphore.
Definition: Semaphore.h:28
errno_t nlib_semaphore_wait(nlib_semaphore *sem)
Waits until the semaphore count is no longer 0 and decrements the semaphore count by 1...
errno_t nlib_semaphore_post(nlib_semaphore *sem, int *previous_count)
Increments the semaphore count by 1.
The class for representing the time.
Definition: DateTime.h:93
errno_t nlib_semaphore_destroy(nlib_semaphore *sem)
Destroys the semaphore count.
errno_t Acquire() noexcept
Locks one semaphore.
Definition: Semaphore.h:27
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24