Apama  10.15.3.4
sag_connectivity_threading.h
Go to the documentation of this file.
1 /*
2  * Title: sag_connectivity_threading.hpp
3  * Description: A cross-platform threading implementation for old compilers
4  * $Copyright (c) 2015-2016, 2020, 2022 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.$
5  * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG
6  */
7 
54 #ifndef _SAG_CONNECTIVITY_THREADS_HPP_
55 #define _SAG_CONNECTIVITY_THREADS_HPP_
56 
57 #if defined(_WIN32) || defined(__WIN32) || defined(WIN32)
58 
59 // Windows implementation using CriticalSections and _beginthreadex
60 
61 #include <stdarg.h>
62 #include <windef.h>
63 #include <winbase.h>
64 #include <process.h>
65 #include <io.h>
66 
68 #define SAG_MUTEX_LOCK(m) EnterCriticalSection(&m)
69 
70 #define SAG_MUTEX_UNLOCK(m) LeaveCriticalSection(&m)
71 
72 #define SAG_MUTEX_INIT(m) InitializeCriticalSection(&m)
73 
74 #define SAG_MUTEX_DESTROY(m) DeleteCriticalSection(&m)
75 
80 #define SAG_MUTEX_T CRITICAL_SECTION
81 
83 #define SAG_SEMA_WAIT(s) WaitForSingleObject(s, INFINITE)
84 
85 #define SAG_SEMA_POST(s) ReleaseSemaphore(s, 1, NULL)
86 
87 #define SAG_SEMA_INIT(s) s = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL)
88 
89 #define SAG_SEMA_DESTROY(s) CloseHandle(s)
90 
96 #define SAG_SEMA_T HANDLE
97 
103 #define SAG_THREAD_CREATE(id, func, params) (!(id = (HANDLE)_beginthreadex(NULL, 0, func, params, 0, NULL)))
104 
105 #define SAG_THREAD_JOIN(id) WaitForSingleObject(id, INFINITE)
106 
107 #define SAG_THREAD_RETURN return 0;
108 
109 #define SAG_THREAD_T HANDLE
110 
111 #define SAG_THREAD_RET_T unsigned int __stdcall
112 
113 #else // defined(_WIN32) || defined(__WIN32) || defined(WIN32)
114 
115 // Unix implementation using pthreads
116 
117 #include <pthread.h>
118 #include <semaphore.h>
119 #include <unistd.h>
120 #include <signal.h>
121 
122 
124 #define SAG_MUTEX_LOCK(m) pthread_mutex_lock(&m)
125 
126 #define SAG_MUTEX_UNLOCK(m) pthread_mutex_unlock(&m)
127 
128 #define SAG_MUTEX_INIT(m) pthread_mutex_init(&m, NULL)
129 
130 #define SAG_MUTEX_DESTROY(m) pthread_mutex_destroy(&m)
131 
137 #define SAG_MUTEX_T pthread_mutex_t
138 
140 #define SAG_SEMA_WAIT(s) {while (sem_wait(&s));}
141 
142 #define SAG_SEMA_POST(s) sem_post(&s)
143 
144 #define SAG_SEMA_INIT(s) sem_init(&s, 0, 0)
145 
146 #define SAG_SEMA_DESTROY(s) sem_destroy(&s)
147 
153 #define SAG_SEMA_T sem_t
154 
160 #define SAG_THREAD_CREATE(id, func, params) pthread_create(& id, NULL, func, params)
161 
162 #define SAG_THREAD_JOIN(id) pthread_join(id, NULL)
163 
164 #define SAG_THREAD_RETURN return NULL;
165 
166 #define SAG_THREAD_T pthread_t
167 
168 #define SAG_THREAD_RET_T void*
169 
170 
171 #endif // (else) defined(_WIN32) || defined(__WIN32) || defined(WIN32)
172 
173 #ifdef __cplusplus
174 
175 #include <stdexcept>
176 
177 namespace com {
178 namespace softwareag {
179 namespace _DATAT_INTERNAL_CPP_NAMESPACE {
180 
194 {
195 public:
198  :mx(mx), locked(false)
199  {
200  lock();
201  }
202 
205  {
206  if (locked)
207  unlock();
208  }
209 
211  void lock()
212  {
213  if (locked) throw std::runtime_error("Cannot lock a SAG_MUTEX_T that is already locked");
214  locked = true;
215  SAG_MUTEX_LOCK(mx);
216  }
218  void unlock()
219  {
220  if (!locked) throw std::runtime_error("Cannot unlock a SAG_MUTEX_T that is already unlocked");
221  locked = false;
222  SAG_MUTEX_UNLOCK(mx);
223  }
224 private:
226  SAG_LOCK_GUARD(const SAG_LOCK_GUARD &other);
230  SAG_LOCK_GUARD& operator=(const SAG_LOCK_GUARD &other);
232  SAG_LOCK_GUARD& operator=(SAG_LOCK_GUARD &&other);
233 
235  SAG_MUTEX_T &mx;
237  bool locked;
238 };
239 
240 }
241 namespace connectivity { using namespace _DATAT_INTERNAL_CPP_NAMESPACE; }
242 }} // com.softwareag.connectivity
243 
244 #endif // __cplusplus
245 
246 #endif // _SAG_CONNECTIVITY_THREADS_HPP_
#define SAG_MUTEX_LOCK(m)
Lock a mutex.
Definition: sag_connectivity_threading.h:124
SAG_LOCK_GUARD(SAG_MUTEX_T &mx)
Create a SAG_LOG_GUARD for a particular mutex.
Definition: sag_connectivity_threading.h:197
A simple RAII wrapper for SAG_MUTEX_T that locks the specified mutex in its constructor,...
Definition: sag_connectivity_threading.h:193
void lock()
Locks the mutex (throws if the mutex is already locked by this lock guard).
Definition: sag_connectivity_threading.h:211
#define SAG_MUTEX_T
A helper that provides cross-platform mutex functionality for use on old compilers.
Definition: sag_connectivity_threading.h:137
void unlock()
Unlocks the mutex (throws if the mutex is already unlocked by this lock guard).
Definition: sag_connectivity_threading.h:218
~SAG_LOCK_GUARD()
Destroy this lock guard.
Definition: sag_connectivity_threading.h:204
#define SAG_MUTEX_UNLOCK(m)
Unlock a mutex.
Definition: sag_connectivity_threading.h:126