diff options
author | crupest <crupest@outlook.com> | 2021-06-11 10:43:00 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-11 10:43:00 +0800 |
commit | 864c09033f17904640fc17e571a50ab64f8f225d (patch) | |
tree | 0023bef179062292cb9e9fd3b455a7af8f4f5d91 /operating-system-experiment/Semaphore.h | |
parent | 10b7a5e29e48b874dbec2732c0396160bdcd5b01 (diff) | |
download | life-864c09033f17904640fc17e571a50ab64f8f225d.tar.gz life-864c09033f17904640fc17e571a50ab64f8f225d.tar.bz2 life-864c09033f17904640fc17e571a50ab64f8f225d.zip |
...
Diffstat (limited to 'operating-system-experiment/Semaphore.h')
-rw-r--r-- | operating-system-experiment/Semaphore.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/operating-system-experiment/Semaphore.h b/operating-system-experiment/Semaphore.h new file mode 100644 index 0000000..1d49b60 --- /dev/null +++ b/operating-system-experiment/Semaphore.h @@ -0,0 +1,46 @@ +#ifndef HEADER_SEMAPHORE_H +#define HEADER_SEMAPHORE_H + +#include "Base.h" + +#include <memory> + +#ifdef CRU_WINDOWS +#include <Windows.h> +#else +#include <semaphore.h> +#endif + +namespace cru { +class CRU_API Semaphore { +public: + explicit Semaphore(unsigned init_value = 1); + + Semaphore(const Semaphore &other) = delete; + Semaphore &operator=(const Semaphore &other) = delete; + + Semaphore(Semaphore &&other); + Semaphore &operator=(Semaphore &&other); + + ~Semaphore(); + +public: + void P(); + void V(); + + void Acquire(); + bool TryAcquire(); + void Release(); + +private: + void Destroy(); + +private: +#ifdef CRU_WINDOWS +#else + std::unique_ptr<sem_t> semaphore_; +#endif +}; +} // namespace cru + +#endif
\ No newline at end of file |