nlib
Barrier.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_BARRIER_H_
17 #define INCLUDE_NN_NLIB_THREADING_BARRIER_H_
18 
19 #include "nn/nlib/Config.h"
20 
21 NLIB_NAMESPACE_BEGIN
22 namespace threading {
23 
24 // code snippets:
25 // Initialization:
26 // Barrier barrier;
27 // barrier.Init(N);
28 // Thread1 .. ThreadN:
29 // # do its own job
30 // barrier.Wait(); # wait other threads
31 // # do its own job
32 // barrier.Wait(); # wait other threads again
34  public:
35  Barrier() NLIB_NOEXCEPT : is_initialized_(false) {}
37  if (is_initialized_) {
38  errno_t e = nlib_barrier_destroy(&barrier_);
39  NLIB_ASSERT_NOERR(e);
40  NLIB_UNUSED(e);
41  }
42  }
43  errno_t Init(unsigned int count) NLIB_NOEXCEPT {
44  if (is_initialized_) return EAGAIN;
45  errno_t e = nlib_barrier_init(&barrier_, count);
46  NLIB_ASSERT_NOERR(e);
47  if (e != 0) return e;
48  is_initialized_ = true;
49  return 0;
50  }
52  NLIB_ASSERT(is_initialized_);
53  errno_t e = nlib_barrier_wait(&barrier_);
54  NLIB_ASSERT_NOERR(e);
55  return e;
56  }
57 
58  private:
59  nlib_barrier barrier_;
60  bool is_initialized_;
61 };
62 
63 } // namespace threading
64 NLIB_NAMESPACE_END
65 
66 #endif // INCLUDE_NN_NLIB_THREADING_BARRIER_H_
errno_t nlib_barrier_destroy(nlib_barrier *barrier)
バリアオブジェクトを破壊します。
任意の数のスレッドを同期するために使用します。
Definition: Barrier.h:33
errno_t Init(unsigned int count) noexcept
バリアを初期化します。
Definition: Barrier.h:43
struct nlib_barrier_ nlib_barrier
バリアオブジェクトの型です。
Definition: Platform.h:1068
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:99
開発環境別の設定が書かれるファイルです。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:229
errno_t nlib_barrier_init(nlib_barrier *barrier, unsigned int count)
バリアオブジェクトを初期化します。
errno_t nlib_barrier_wait(nlib_barrier *barrier)
スレッドの待ち合わせを行います。
errno_t Wait() noexcept
スレッドの待ち合わせを行います。
Definition: Barrier.h:51
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37