Apama  10.7.2.2
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 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  * @Version: $Id: sag_connectivity_threading.h 366779 2020-02-05 10:42:04Z matj $
7  */
8 
55 #ifndef _SAG_CONNECTIVITY_THREADS_HPP_
56 #define _SAG_CONNECTIVITY_THREADS_HPP_
57 
58 #if defined(_WIN32) || defined(__WIN32) || defined(WIN32)
59 
60 // Windows implementation using CriticalSections and _beginthreadex
61 
62 #include <stdarg.h>
63 #include <windef.h>
64 #include <winbase.h>
65 #include <process.h>
66 #include <io.h>
67 
69 #define SAG_MUTEX_LOCK(m) EnterCriticalSection(&m)
70 
71 #define SAG_MUTEX_UNLOCK(m) LeaveCriticalSection(&m)
72 
73 #define SAG_MUTEX_INIT(m) InitializeCriticalSection(&m)
74 
75 #define SAG_MUTEX_DESTROY(m) DeleteCriticalSection(&m)
76 
81 #define SAG_MUTEX_T CRITICAL_SECTION
82 
84 #define SAG_SEMA_WAIT(s) WaitForSingleObject(s, INFINITE)
85 
86 #define SAG_SEMA_POST(s) ReleaseSemaphore(s, 1, NULL)
87 
88 #define SAG_SEMA_INIT(s) s = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL)
89 
90 #define SAG_SEMA_DESTROY(s) CloseHandle(s)
91 
97 #define SAG_SEMA_T HANDLE
98 
104 #define SAG_THREAD_CREATE(id, func, params) (!(id = (HANDLE)_beginthreadex(NULL, 0, func, params, 0, NULL)))
105 
106 #define SAG_THREAD_JOIN(id) WaitForSingleObject(id, INFINITE)
107 
108 #define SAG_THREAD_RETURN return 0;
109 
110 #define SAG_THREAD_T HANDLE
111 
112 #define SAG_THREAD_RET_T unsigned int __stdcall
113 
114 #else // defined(_WIN32) || defined(__WIN32) || defined(WIN32)
115 
116 // Unix implementation using pthreads
117 
118 #include <pthread.h>
119 #include <semaphore.h>
120 #include <unistd.h>
121 #include <signal.h>
122 
123 
125 #define SAG_MUTEX_LOCK(m) pthread_mutex_lock(&m)
126 
127 #define SAG_MUTEX_UNLOCK(m) pthread_mutex_unlock(&m)
128 
129 #define SAG_MUTEX_INIT(m) pthread_mutex_init(&m, NULL)
130 
131 #define SAG_MUTEX_DESTROY(m) pthread_mutex_destroy(&m)
132 
138 #define SAG_MUTEX_T pthread_mutex_t
139 
141 #define SAG_SEMA_WAIT(s) {while (sem_wait(&s));}
142 
143 #define SAG_SEMA_POST(s) sem_post(&s)
144 
145 #define SAG_SEMA_INIT(s) sem_init(&s, 0, 0)
146 
147 #define SAG_SEMA_DESTROY(s) sem_destroy(&s)
148 
154 #define SAG_SEMA_T sem_t
155 
161 #define SAG_THREAD_CREATE(id, func, params) pthread_create(& id, NULL, func, params)
162 
163 #define SAG_THREAD_JOIN(id) pthread_join(id, NULL)
164 
165 #define SAG_THREAD_RETURN return NULL;
166 
167 #define SAG_THREAD_T pthread_t
168 
169 #define SAG_THREAD_RET_T void*
170 
171 
172 #endif // (else) defined(_WIN32) || defined(__WIN32) || defined(WIN32)
173 
174 #ifdef __cplusplus
175 
176 #include <stdexcept>
177 
178 namespace com {
179 namespace softwareag {
180 namespace _DATAT_INTERNAL_CPP_NAMESPACE {
181 
195 {
196 public:
199  :mx(mx), locked(false)
200  {
201  lock();
202  }
203 
206  {
207  if (locked)
208  unlock();
209  }
210 
212  void lock()
213  {
214  if (locked) throw std::runtime_error("Cannot lock a SAG_MUTEX_T that is already locked");
215  locked = true;
216  SAG_MUTEX_LOCK(mx);
217  }
219  void unlock()
220  {
221  if (!locked) throw std::runtime_error("Cannot unlock a SAG_MUTEX_T that is already unlocked");
222  locked = false;
223  SAG_MUTEX_UNLOCK(mx);
224  }
225 private:
227  SAG_LOCK_GUARD(const SAG_LOCK_GUARD &other);
231  SAG_LOCK_GUARD& operator=(const SAG_LOCK_GUARD &other);
233  SAG_LOCK_GUARD& operator=(SAG_LOCK_GUARD &&other);
234 
236  SAG_MUTEX_T &mx;
238  bool locked;
239 };
240 
241 }
242 namespace connectivity { using namespace _DATAT_INTERNAL_CPP_NAMESPACE; }
243 }} // com.softwareag.connectivity
244 
245 #endif // __cplusplus
246 
247 #endif // _SAG_CONNECTIVITY_THREADS_HPP_
#define SAG_MUTEX_LOCK(m)
Lock a mutex.
Definition: sag_connectivity_threading.h:125
SAG_LOCK_GUARD(SAG_MUTEX_T &mx)
Create a SAG_LOG_GUARD for a particular mutex.
Definition: sag_connectivity_threading.h:198
A simple RAII wrapper for SAG_MUTEX_T that locks the specified mutex in its constructor,...
Definition: sag_connectivity_threading.h:194
void lock()
Locks the mutex (throws if the mutex is already locked by this lock guard).
Definition: sag_connectivity_threading.h:212
#define SAG_MUTEX_T
A helper that provides cross-platform mutex functionality for use on old compilers.
Definition: sag_connectivity_threading.h:138
void unlock()
Unlocks the mutex (throws if the mutex is already unlocked by this lock guard).
Definition: sag_connectivity_threading.h:219
~SAG_LOCK_GUARD()
Destroy this lock guard.
Definition: sag_connectivity_threading.h:205
#define SAG_MUTEX_UNLOCK(m)
Unlock a mutex.
Definition: sag_connectivity_threading.h:127