Samchon Framework for CPP  1.0.0
UniqueAcquire.hpp
1 #pragma once
2 
3 #include <samchon/library/Semaphore.hpp>
4 
5 namespace samchon
6 {
7 namespace library
8 {
40  {
41  private:
46 
50  bool locked;
51 
52  public:
53  /* -----------------------------------------------------------
54  CONSTRUCTORS
55  ----------------------------------------------------------- */
62  UniqueAcquire(Semaphore &semaphore, bool lock = true)
63  {
64  this->semaphore = &semaphore;
65  this->locked = false;
66 
67  if (lock == true)
68  this->acquire();
69  };
70 
81  UniqueAcquire(const UniqueAcquire &obj) = delete;
82 
89  {
90  //MOVE
91  this->semaphore = obj.semaphore;
92  this->locked = obj.locked;
93 
94  //TRUNCATE
95  obj.semaphore = nullptr;
96  obj.locked = false;
97  };
98 
104  {
105  if (locked)
106  semaphore->release();
107  };
108 
109  /* -----------------------------------------------------------
110  LOCKERS
111  ----------------------------------------------------------- */
115  void acquire()
116  {
117  if (locked)
118  return;
119 
120  semaphore->acquire();
121  locked = true;
122  };
123 
127  void release()
128  {
129  if (locked == false)
130  return;
131 
132  semaphore->release();
133  locked = false;
134  };
135 
139  //auto tryAcquire() -> bool;
140  };
141 };
142 };
UniqueAcquire(UniqueAcquire &&obj)
Move Constructor.
UniqueAcquire(Semaphore &semaphore, bool lock=true)
Construct from semaphore.
void release()
Release an admission.
bool locked
Whether the semaphore was acquired by the UniqueLock.
void acquire()
Acquire admission.
Definition: Semaphore.hpp:128
Unique acquire from a Semaphore.
~UniqueAcquire()
Default Destructor.
void acquire()
Acquire admission.
void release()
Release an admission.
Definition: Semaphore.hpp:174
Semaphore * semaphore
Managed semaphore.