semaphore_wrapper.h 1.09 KB
Newer Older
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
/*!
 *  Copyright (c) 2021 by Contributors
 * \file semaphore_wrapper.h
 * \brief A simple corss platform semaphore wrapper
 */
#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 {

/*!
 * \brief A simple crossplatform Semaphore wrapper
 */
class Semaphore {
 public:
  /*!
   * \brief Semaphore constructor
   */
  Semaphore();
  /*!
   * \brief blocking wait, decrease semaphore by 1
   */
  void Wait();
31
32
  /*!
   * \brief timed wait, decrease semaphore by 1 or returns if times out
33
34
   * \param timeout The timeout value in milliseconds. If zero, wait
   * indefinitely.
35
36
   */
  bool TimedWait(int timeout);
37
38
39
40
  /*!
   * \brief increase semaphore by 1
   */
  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_