nlib
Semaphore.h
[詳解]
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_THREADING_SEMAPHORE_H_
17 #define INCLUDE_NN_NLIB_THREADING_SEMAPHORE_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/DateTime.h"
21 
22 NLIB_NAMESPACE_BEGIN
23 namespace threading {
24 
26  public:
27  typedef nlib_semaphore* NativeHandle;
28 
29  Semaphore() NLIB_NOEXCEPT : obj_() {}
31  errno_t Init(int initial_count) NLIB_NOEXCEPT {
32  return nlib_semaphore_init(&obj_, initial_count);
33  }
34  errno_t Release(int* previous_count) NLIB_NOEXCEPT {
35  return nlib_semaphore_post(&obj_, previous_count);
36  }
37  errno_t Release(int release_count, int* previous_count) NLIB_NOEXCEPT {
38  return nlib_semaphore_post_ex(&obj_, release_count, previous_count);
39  }
42  return nlib_semaphore_trywait(&obj_);
43  }
45  return nlib_semaphore_trywait_for(&obj_, timeout.ToTimeValue().tick);
46  }
47  NativeHandle GetNativeHandle() NLIB_NOEXCEPT { return &obj_; }
48 
49  private:
50  nlib_semaphore obj_;
52 };
53 
54 } // namespace threading
55 NLIB_NAMESPACE_END
56 
57 NLIB_NAMESPACE_BEGIN
58 namespace threading {
59 
60 class ScopedSemaphore {
61  public:
62  explicit NLIB_ALWAYS_INLINE ScopedSemaphore(Semaphore& rhs) NLIB_NOEXCEPT // NOLINT
63  : semaphore_(rhs) {
64  semaphore_.Acquire();
65  }
66  NLIB_ALWAYS_INLINE ~ScopedSemaphore() NLIB_NOEXCEPT {
67  semaphore_.Release(nullptr);
68  }
69 
70  private:
71  Semaphore& semaphore_;
72  NLIB_DISALLOW_COPY_AND_ASSIGN(ScopedSemaphore);
73 };
74 
75 } // namespace threading
76 NLIB_NAMESPACE_END
77 
78 #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:97
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:179
sem_t nlib_semaphore
セマフォオブジェクトの型です。
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:44
errno_t Release(int *previous_count) noexcept
セマフォを1つリリースします。
Definition: Semaphore.h:34
errno_t Release(int release_count, int *previous_count) noexcept
セマフォを複数個リリースします。 アトミックには実行されないかもしれません。
Definition: Semaphore.h:37
セマフォを実装しています。
Definition: Semaphore.h:25
errno_t nlib_semaphore_trywait(nlib_semaphore *sem)
セマフォカウントが0でなければ、セマフォカウントを1減少させる。
errno_t Init(int initial_count) noexcept
セマフォを初期化します。
Definition: Semaphore.h:31
時刻や時間を扱うためのクラスが定義されています。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:105
errno_t nlib_semaphore_init(nlib_semaphore *sem, int initial_count)
sem で指定されるセマフォオブジェクトを初期化する。
開発環境別の設定が書かれるファイルです。
errno_t TryAcquire() noexcept
セマフォのロックを試みます。
Definition: Semaphore.h:41
errno_t nlib_semaphore_wait(nlib_semaphore *sem)
セマフォカウントが0でなくなるまで待って、セマフォカウントを1減少させる。
errno_t nlib_semaphore_post(nlib_semaphore *sem, int *previous_count)
セマフォカウントを1つ増加させる。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:245
時間を表すクラスです。
Definition: DateTime.h:97
errno_t nlib_semaphore_destroy(nlib_semaphore *sem)
セマフォオブジェクトを破壊する。
errno_t Acquire() noexcept
セマフォを1つロックします。
Definition: Semaphore.h:40
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37