semaphore_wrapper.h 1.09 KB
Newer Older
1
/**
2
 *  Copyright (c) 2021 by Contributors
3
4
 * @file semaphore_wrapper.h
 * @brief A simple corss platform semaphore wrapper
5
6
7
8
9
10
11
12
13
14
15
16
17
 */
#ifndef DGL_RUNTIME_SEMAPHORE_WRAPPER_H_
#define DGL_RUNTIME_SEMAPHORE_WRAPPER_H_

#ifdef _WIN32
#include <windows.h>
#else
#include <semaphore.h>
#endif

namespace dgl {
namespace runtime {

18
/**
19
 * @brief A simple crossplatform Semaphore wrapper
20
21
22
 */
class Semaphore {
 public:
23
  /**
24
   * @brief Semaphore constructor
25
26
   */
  Semaphore();
27
  /**
28
   * @brief blocking wait, decrease semaphore by 1
29
30
   */
  void Wait();
31
  /**
32
33
   * @brief timed wait, decrease semaphore by 1 or returns if times out
   * @param timeout The timeout value in milliseconds. If zero, wait
34
   * indefinitely.
35
36
   */
  bool TimedWait(int timeout);
37
  /**
38
   * @brief increase semaphore by 1
39
40
   */
  void Post();
41

42
43
44
45
46
47
 private:
#ifdef _WIN32
  HANDLE sem_;
#else
  sem_t sem_;
#endif
48
49
50
51
52
  enum {
    MILLISECONDS_PER_SECOND = 1000,
    NANOSECONDS_PER_MILLISECOND = 1000 * 1000,
    NANOSECONDS_PER_SECOND = 1000 * 1000 * 1000
  };
53
54
55
56
57
58
};

}  // namespace runtime
}  // namespace dgl

#endif  // DGL_RUNTIME_SEMAPHORE_WRAPPER_H_