HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IlmThreadSemaphore.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 #ifndef INCLUDED_ILM_THREAD_SEMAPHORE_H
7 #define INCLUDED_ILM_THREAD_SEMAPHORE_H
8 
9 //-----------------------------------------------------------------------------
10 //
11 // class Semaphore -- a wrapper class for
12 // system-dependent counting semaphores
13 //
14 //-----------------------------------------------------------------------------
15 
16 #include "IlmThreadExport.h"
17 
18 #include "IlmThreadConfig.h"
19 #include "IlmThreadNamespace.h"
20 
21 #if ILMTHREAD_THREADING_ENABLED
22 # if ILMTHREAD_HAVE_POSIX_SEMAPHORES
23 # include <semaphore.h>
24 # elif defined(__APPLE__)
25 # include <dispatch/dispatch.h>
26 # elif (defined (_WIN32) || defined (_WIN64))
27 # ifdef NOMINMAX
28 # undef NOMINMAX
29 # endif
30 # define NOMINMAX
31 # include <windows.h>
32 # else
33 # include <mutex>
34 # include <condition_variable>
35 # endif
36 #endif
37 
39 
40 
42 {
43  public:
44 
45  ILMTHREAD_EXPORT Semaphore (unsigned int value = 0);
46  ILMTHREAD_EXPORT virtual ~Semaphore();
47 
48  ILMTHREAD_EXPORT void wait();
49  ILMTHREAD_EXPORT bool tryWait();
50  ILMTHREAD_EXPORT void post();
51  ILMTHREAD_EXPORT int value() const;
52 
53  private:
54 
55 #if ILMTHREAD_HAVE_POSIX_SEMAPHORES
56 
57  mutable sem_t _semaphore;
58 
59 #elif defined(__APPLE__)
60  mutable dispatch_semaphore_t _semaphore;
61 
62 #elif (defined (_WIN32) || defined (_WIN64))
63 
64  mutable HANDLE _semaphore;
65 
66 #elif ILMTHREAD_THREADING_ENABLED
67  //
68  // If the platform has threads but no semapohores,
69  // then we implement them ourselves using condition variables
70  //
71 
72  struct sema_t
73  {
74  unsigned int count;
75  unsigned long numWaiting;
76  std::mutex mutex;
77  std::condition_variable nonZero;
78  };
79 
80  mutable sema_t _semaphore;
81 
82 #endif
83 
84  void operator = (const Semaphore& s) = delete;
85  Semaphore (const Semaphore& s) = delete;
86  void operator = (Semaphore&& s) = delete;
87  Semaphore (Semaphore&& s) = delete;
88 };
89 
90 
92 
93 #endif // INCLUDED_ILM_THREAD_SEMAPHORE_H
#define ILMTHREAD_EXPORT
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLdouble s
Definition: glad.h:3009
#define ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER
#define ILMTHREAD_INTERNAL_NAMESPACE_HEADER_EXIT
#define ILMTHREAD_EXPORT_TYPE
*tasks wait()
Definition: core.h:1131
GLint GLsizei count
Definition: glcorearb.h:405