55 #ifndef _SAG_CONNECTIVITY_THREADS_HPP_
56 #define _SAG_CONNECTIVITY_THREADS_HPP_
58 #if defined(_WIN32) || defined(__WIN32) || defined(WIN32)
69 #define SAG_MUTEX_LOCK(m) EnterCriticalSection(&m)
71 #define SAG_MUTEX_UNLOCK(m) LeaveCriticalSection(&m)
73 #define SAG_MUTEX_INIT(m) InitializeCriticalSection(&m)
75 #define SAG_MUTEX_DESTROY(m) DeleteCriticalSection(&m)
81 #define SAG_MUTEX_T CRITICAL_SECTION
84 #define SAG_SEMA_WAIT(s) WaitForSingleObject(s, INFINITE)
86 #define SAG_SEMA_POST(s) ReleaseSemaphore(s, 1, NULL)
88 #define SAG_SEMA_INIT(s) s = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL)
90 #define SAG_SEMA_DESTROY(s) CloseHandle(s)
97 #define SAG_SEMA_T HANDLE
104 #define SAG_THREAD_CREATE(id, func, params) (!(id = (HANDLE)_beginthreadex(NULL, 0, func, params, 0, NULL)))
106 #define SAG_THREAD_JOIN(id) WaitForSingleObject(id, INFINITE)
108 #define SAG_THREAD_RETURN return 0;
110 #define SAG_THREAD_T HANDLE
112 #define SAG_THREAD_RET_T unsigned int __stdcall
114 #else // defined(_WIN32) || defined(__WIN32) || defined(WIN32)
119 #include <semaphore.h>
125 #define SAG_MUTEX_LOCK(m) pthread_mutex_lock(&m)
127 #define SAG_MUTEX_UNLOCK(m) pthread_mutex_unlock(&m)
129 #define SAG_MUTEX_INIT(m) pthread_mutex_init(&m, NULL)
131 #define SAG_MUTEX_DESTROY(m) pthread_mutex_destroy(&m)
138 #define SAG_MUTEX_T pthread_mutex_t
141 #define SAG_SEMA_WAIT(s) {while (sem_wait(&s));}
143 #define SAG_SEMA_POST(s) sem_post(&s)
145 #define SAG_SEMA_INIT(s) sem_init(&s, 0, 0)
147 #define SAG_SEMA_DESTROY(s) sem_destroy(&s)
154 #define SAG_SEMA_T sem_t
161 #define SAG_THREAD_CREATE(id, func, params) pthread_create(& id, NULL, func, params)
163 #define SAG_THREAD_JOIN(id) pthread_join(id, NULL)
165 #define SAG_THREAD_RETURN return NULL;
167 #define SAG_THREAD_T pthread_t
169 #define SAG_THREAD_RET_T void*
172 #endif // (else) defined(_WIN32) || defined(__WIN32) || defined(WIN32)
179 namespace softwareag {
180 namespace connectivity {
199 :mx(mx), locked(false)
214 if (locked)
throw std::runtime_error(
"Cannot lock a SAG_MUTEX_T that is already locked");
221 if (!locked)
throw std::runtime_error(
"Cannot unlock a SAG_MUTEX_T that is already unlocked");
243 #endif // __cplusplus
245 #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
Definition: sag_connectivity_threading.h:178
A simple RAII wrapper for SAG_MUTEX_T that locks the specified mutex in its constructor, and guarantees that it will be unlocked when this object goes out of scope unless it has been unlocked already by that point.
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