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 
25 class Semaphore {
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_;
51 };
52 
53 } // namespace threading
54 NLIB_NAMESPACE_END
55 
56 NLIB_NAMESPACE_BEGIN
57 namespace threading {
58 
59 class ScopedSemaphore {
60  public:
61  explicit NLIB_ALWAYS_INLINE ScopedSemaphore(Semaphore& rhs) NLIB_NOEXCEPT // NOLINT
62  : semaphore_(rhs) {
63  semaphore_.Acquire();
64  }
65  NLIB_ALWAYS_INLINE ~ScopedSemaphore() NLIB_NOEXCEPT {
66  semaphore_.Release(NULL);
67  }
68 
69  private:
70  Semaphore& semaphore_;
71  NLIB_DISALLOW_COPY_AND_ASSIGN(ScopedSemaphore);
72 };
73 
74 } // namespace threading
75 NLIB_NAMESPACE_END
76 
77 #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:97
#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:163
sem_t nlib_semaphore
The type for a semaphore object.
NLIB_CHECK_RESULT 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:44
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
NLIB_CHECK_RESULT 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:99
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.
The class for representing the time.
Definition: DateTime.h:106
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