nlib
Semaphore.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_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  }
43  return nlib_semaphore_trywait_for(&obj_, timeout.ToTimeValue().tick);
44  }
45  NativeHandle GetNativeHandle() NLIB_NOEXCEPT { return &obj_; }
46 
47  private:
48  nlib_semaphore obj_;
50 };
51 
52 } // namespace threading
53 NLIB_NAMESPACE_END
54 
55 NLIB_NAMESPACE_BEGIN
56 namespace threading {
57 
58 class ScopedSemaphore {
59  public:
60  explicit NLIB_ALWAYS_INLINE ScopedSemaphore(Semaphore& rhs) NLIB_NOEXCEPT : semaphore_(rhs) {
61  semaphore_.Acquire();
62  }
63  NLIB_ALWAYS_INLINE ~ScopedSemaphore() NLIB_NOEXCEPT { semaphore_.Release(nullptr); }
64 
65  private:
66  Semaphore& semaphore_;
67  NLIB_DISALLOW_COPY_AND_ASSIGN(ScopedSemaphore);
68 };
69 
70 } // namespace threading
71 NLIB_NAMESPACE_END
72 
73 #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:95
#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:183
sem_t nlib_semaphore
The type for a semaphore object.
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:42
errno_t Release(int *previous_count) noexcept
Releases one semaphore.
Definition: Semaphore.h:34
errno_t Release(int release_count, int *previous_count) noexcept
Releases multiple semaphores. May not be executed atomically.
Definition: Semaphore.h:37
Implements a semaphore.
Definition: Semaphore.h:25
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:31
Defines the class for handling times and durations.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:109
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:41
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.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:250
The class for representing the time.
Definition: DateTime.h:97
errno_t nlib_semaphore_destroy(nlib_semaphore *sem)
Destroys the semaphore count.
errno_t Acquire() noexcept
Locks one semaphore.
Definition: Semaphore.h:40
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37