nlib
Semaphore.h
[詳解]
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)
セマフォカウントをreleaseCount 増加させる。
#define NLIB_ALWAYS_INLINE
コンパイラに関数をインライン展開するように強く示します。
Definition: Platform_unix.h:69
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:145
sem_t nlib_semaphore
セマフォオブジェクトの型です。
NLIB_CHECK_RESULT errno_t nlib_semaphore_trywait_for(nlib_semaphore *sem, nlib_duration duration)
セマフォカウントが0でなければ、セマフォカウントを1減少させる。0の場合はduration の期間だけ待つ。 ...
errno_t TryAcquire(const nlib_ns::TimeSpan &timeout) noexcept
セマフォをタイムアウトつきで1つロックします。
Definition: Semaphore.h:31
errno_t Release(int *previous_count) noexcept
セマフォを1つリリースします。
Definition: Semaphore.h:21
errno_t Release(int release_count, int *previous_count) noexcept
セマフォを複数個リリースします。 アトミックには実行されないかもしれません。
Definition: Semaphore.h:24
セマフォを実装しています。
Definition: Semaphore.h:12
NLIB_CHECK_RESULT errno_t nlib_semaphore_trywait(nlib_semaphore *sem)
セマフォカウントが0でなければ、セマフォカウントを1減少させる。
errno_t Init(int initial_count) noexcept
セマフォを初期化します。
Definition: Semaphore.h:18
時刻や時間を扱うためのクラスが定義されています。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:86
errno_t nlib_semaphore_init(nlib_semaphore *sem, int initial_count)
sem で指定されるセマフォオブジェクトを初期化する。
開発環境別の設定が書かれるファイルです。
errno_t TryAcquire() noexcept
セマフォのロックを試みます。
Definition: Semaphore.h:28
errno_t nlib_semaphore_wait(nlib_semaphore *sem)
セマフォカウントが0でなくなるまで待って、セマフォカウントを1減少させる。
errno_t nlib_semaphore_post(nlib_semaphore *sem, int *previous_count)
セマフォカウントを1つ増加させる。
時間を表すクラスです。
Definition: DateTime.h:93
errno_t nlib_semaphore_destroy(nlib_semaphore *sem)
セマフォオブジェクトを破壊する。
errno_t Acquire() noexcept
セマフォを1つロックします。
Definition: Semaphore.h:27
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24