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 //
22 // Decipher the platform-specific threading support.
23 // Set the ILMTHREAD_SEMAPHORE_* defines to indicate the corresponding
24 // implementation of the Semaphore class. Only one of these should be
25 // defined.
26 //
27 
28 #if ILMTHREAD_THREADING_ENABLED
29 # if ILMTHREAD_HAVE_POSIX_SEMAPHORES
30 # include <semaphore.h>
31 # define ILMTHREAD_SEMAPHORE_POSIX 1
32 # elif defined(__APPLE__)
33 # include <AvailabilityMacros.h>
34 # if MAC_OS_X_VERSION_MIN_REQUIRED > 1050 && !defined(__ppc__)
35 # include <dispatch/dispatch.h>
36 # define ILMTHREAD_SEMAPHORE_OSX 1
37 # else
38 # include <condition_variable>
39 # include <mutex>
40 # define ILMTHREAD_SEMAPHORE_OTHER 1
41 # endif
42 # elif (defined(_WIN32) || defined(_WIN64))
43 # ifdef NOMINMAX
44 # undef NOMINMAX
45 # endif
46 # define NOMINMAX
47 # include <windows.h>
48 # define ILMTHREAD_SEMAPHORE_WINDOWS 1
49 # else
50 # include <condition_variable>
51 # include <mutex>
52 # define ILMTHREAD_SEMAPHORE_OTHER 1
53 # endif
54 #else
55 # define ILMTHREAD_SEMAPHORE_DISABLED 1
56 #endif
57 
59 
61 {
62 public:
63  ILMTHREAD_EXPORT Semaphore (unsigned int value = 0);
64  ILMTHREAD_EXPORT virtual ~Semaphore ();
65 
66  ILMTHREAD_EXPORT void wait ();
67  ILMTHREAD_EXPORT bool tryWait ();
68  ILMTHREAD_EXPORT void post ();
69  ILMTHREAD_EXPORT int value () const;
70 
71 private:
72 
73 #if ILMTHREAD_SEMAPHORE_POSIX
74 
75  mutable sem_t _semaphore;
76 
77 #elif ILMTHREAD_SEMAPHORE_OSX
78 
79  mutable dispatch_semaphore_t _semaphore;
80 
81 #elif ILMTHREAD_SEMAPHORE_WINDOWS
82 
83  mutable HANDLE _semaphore;
84 
85 #elif ILMTHREAD_SEMAPHORE_OTHER
86 
87  //
88  // If the platform has threads but no semaphores,
89  // then we implement them ourselves using condition variables
90  //
91 
92  struct sema_t
93  {
94  unsigned int count;
95  unsigned long numWaiting;
96  std::mutex mutex;
97  std::condition_variable nonZero;
98  };
99 
100  mutable sema_t _semaphore;
101 
102 #endif
103 
104  void operator= (const Semaphore& s) = delete;
105  Semaphore (const Semaphore& s) = delete;
106  void operator= (Semaphore&& s) = delete;
107  Semaphore (Semaphore&& s) = delete;
108 };
109 
111 
112 #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()
LeafData & operator=(const LeafData &)=delete
Definition: core.h:1131