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 | 0380cd595ee05773f78751aa7f2952b6ea105f7c (patch) | |
tree | eea9045c3749fdd5ad95ad272df396bd34280ab3 /works/life/operating-system-experiment/Semaphore.h | |
parent | 5018c2b40009258ed0691cbbb1dcc4d8ac4a809b (diff) | |
download | crupest-0380cd595ee05773f78751aa7f2952b6ea105f7c.tar.gz crupest-0380cd595ee05773f78751aa7f2952b6ea105f7c.tar.bz2 crupest-0380cd595ee05773f78751aa7f2952b6ea105f7c.zip |
import(life): ...
Diffstat (limited to 'works/life/operating-system-experiment/Semaphore.h')
-rw-r--r-- | works/life/operating-system-experiment/Semaphore.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/works/life/operating-system-experiment/Semaphore.h b/works/life/operating-system-experiment/Semaphore.h new file mode 100644 index 0000000..1d49b60 --- /dev/null +++ b/works/life/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 |