aboutsummaryrefslogtreecommitdiff
path: root/store/works/life/operating-system-experiment/Semaphore.h
blob: 430c0364201131e80f82871e4320ba106dada050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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
  HANDLE handle_ = nullptr;
#else
  std::unique_ptr<sem_t> semaphore_;
#endif
};
} // namespace cru

#endif