Apama  9.10.0.4.289795
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 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 272471 2016-01-08 15:52:33Z cree $
7  */
8 
53 #ifndef _SAG_CONNECTIVITY_THREADS_HPP_
54 #define _SAG_CONNECTIVITY_THREADS_HPP_
55 
56 #if defined(_WIN32) || defined(__WIN32) || defined(WIN32)
57 
58 // Windows implementation using CriticalSections and _beginthreadex
59 
60 #include <stdarg.h>
61 #include <windef.h>
62 #include <winbase.h>
63 #include <process.h>
64 #include <io.h>
65 
67 #define SAG_MUTEX_LOCK(m) EnterCriticalSection(&m)
68 #define SAG_MUTEX_UNLOCK(m) LeaveCriticalSection(&m)
70 #define SAG_MUTEX_INIT(m) InitializeCriticalSection(&m)
72 #define SAG_MUTEX_DESTROY(m) DeleteCriticalSection(&m)
74 #define SAG_MUTEX_T CRITICAL_SECTION
76 
78 #define SAG_SEMA_WAIT(s) WaitForSingleObject(s, INFINITE)
79 #define SAG_SEMA_POST(s) ReleaseSemaphore(s, 1, NULL)
81 #define SAG_SEMA_INIT(s) s = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL)
83 #define SAG_SEMA_DESTROY(s) CloseHandle(s)
85 #define SAG_SEMA_T HANDLE
87 
89 #define SAG_THREAD_CREATE(id, func, params) (!(id = (HANDLE)_beginthreadex(NULL, 0, func, params, 0, NULL)))
90 #define SAG_THREAD_JOIN(id) WaitForSingleObject(id, INFINITE)
92 #define SAG_THREAD_RETURN return 0;
94 #define SAG_THREAD_T HANDLE
96 #define SAG_THREAD_RET_T unsigned int __stdcall
98 
99 #else
100 
101 // Unix implementation using pthreads
102 
103 #include <pthread.h>
104 #include <semaphore.h>
105 #include <unistd.h>
106 #include <signal.h>
107 
108 
110 #define SAG_MUTEX_LOCK(m) pthread_mutex_lock(&m)
111 #define SAG_MUTEX_UNLOCK(m) pthread_mutex_unlock(&m)
113 #define SAG_MUTEX_INIT(m) pthread_mutex_init(&m, NULL)
115 #define SAG_MUTEX_DESTROY(m) pthread_mutex_destroy(&m)
117 #define SAG_MUTEX_T pthread_mutex_t
119 
121 #define SAG_SEMA_WAIT(s) {while (sem_wait(&s));}
122 #define SAG_SEMA_POST(s) sem_post(&s)
124 #define SAG_SEMA_INIT(s) sem_init(&s, 0, 0)
126 #define SAG_SEMA_DESTROY(s) sem_destroy(&s)
128 #define SAG_SEMA_T sem_t
130 
132 #define SAG_THREAD_CREATE(id, func, params) pthread_create(& id, NULL, func, params)
133 #define SAG_THREAD_JOIN(id) pthread_join(id, NULL)
135 #define SAG_THREAD_RETURN return NULL;
137 #define SAG_THREAD_T pthread_t
139 #define SAG_THREAD_RET_T void*
141 
142 
143 #endif
144 
145 
146 #endif // _SAG_CONNECTIVITY_THREADS_HPP_