nlib
Barrier.h
Go to the documentation of this file.
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)
Destroys a barrier object.
Synchronizes an arbitrary number of threads.
Definition: Barrier.h:33
errno_t Init(unsigned int count) noexcept
Initializes a barrier.
Definition: Barrier.h:43
struct nlib_barrier_ nlib_barrier
The type for a barrier object.
Definition: Platform.h:1068
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
A file that contains the configuration information for each development environment.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:229
errno_t nlib_barrier_init(nlib_barrier *barrier, unsigned int count)
Initializes a barrier object.
errno_t nlib_barrier_wait(nlib_barrier *barrier)
Waits for a thread.
errno_t Wait() noexcept
Waits for a thread.
Definition: Barrier.h:51
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37