Debian Codegraph @

extern/ttmath/ttmaththreads.h

raw
1 /*
2 * This file is a part of TTMath Bignum Library
3 * and is distributed under the (new) BSD licence.
4 * Author: Tomasz Sowa <[email protected]>
5 */
6
7 /*
8 * Copyright (c) 2006-2009, Tomasz Sowa
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * * Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * * Neither the name Tomasz Sowa nor the names of contributors to this
22 * project may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38
39
40 #ifndef headerfilettmaththreads
41 #define headerfilettmaththreads
42
43 #include "ttmathtypes.h"
44
45 #ifdef TTMATH_WIN32_THREADS
46 #include <windows.h>
47 #include <cstdio>
48 #endif
49
50 #ifdef TTMATH_POSIX_THREADS
51 #include <pthread.h>
52 #endif
53
54
55
56 /*!
57 \file ttmaththreads.h
58 \brief Some objects used in multithreads environment
59 */
60
61
62 /*
63 this is a simple skeleton of a program in multithreads environment:
64
65 #define TTMATH_MULTITHREADS
66 #include<ttmath/ttmath.h>
67
68 TTMATH_MULTITHREADS_HELPER
69
70 int main()
71 {
72 [...]
73 }
74
75 make sure that macro TTMATH_MULTITHREADS is defined and (somewhere in *.cpp file)
76 use TTMATH_MULTITHREADS_HELPER macro (outside of any classes/functions/namespaces scope)
77 */
78
79
80 namespace ttmath
81 {
82
83
84 #ifdef TTMATH_WIN32_THREADS
85
86 /*
87 we use win32 threads
88 */
89
90
91 /*!
92 in multithreads environment you should use TTMATH_MULTITHREADS_HELPER macro
93 somewhere in *.cpp file
94
95 (at the moment in win32 this macro does nothing)
96 */
97 #define TTMATH_MULTITHREADS_HELPER
98
99
100 /*!
101 objects of this class are used to synchronize
102 */
103 class ThreadLock
104 {
105 HANDLE mutex_handle;
106
107
108 void CreateName(char * buffer) const
109 {
110 #ifdef _MSC_VER
111 #pragma warning (disable : 4996)
112 // warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead.
113 #endif
114
115 sprintf(buffer, "TTMATH_LOCK_%ul", (unsigned long)GetCurrentProcessId());
116
117 #ifdef _MSC_VER
118 #pragma warning (default : 4996)
119 #endif
120 }
121
122
123 public:
124
125 bool Lock()
126 {
127 char buffer[50];
128
129 CreateName(buffer);
130 mutex_handle = CreateMutexA(0, false, buffer);
131
132 if( mutex_handle == 0 )
133 return false;
134
135 WaitForSingleObject(mutex_handle, INFINITE);
136
137 return true;
138 }
139
140
141 ThreadLock()
142 {
143 mutex_handle = 0;
144 }
145
146
147 ~ThreadLock()
148 {
149 if( mutex_handle != 0 )
150 {
151 ReleaseMutex(mutex_handle);
152 CloseHandle(mutex_handle);
153 }
154 }
155 };
156
157 #endif // #ifdef TTMATH_WIN32_THREADS
158
159
160
161
162
163 #ifdef TTMATH_POSIX_THREADS
164
165 /*
166 we use posix threads
167 */
168
169
170 /*!
171 in multithreads environment you should use TTMATH_MULTITHREADS_HELPER macro
172 somewhere in *.cpp file
173 (this macro defines a pthread_mutex_t object used by TTMath library)
174 */
175 #define TTMATH_MULTITHREADS_HELPER \
176 namespace ttmath \
177 { \
178 pthread_mutex_t ttmath_mutex = PTHREAD_MUTEX_INITIALIZER; \
179 }
180
181
182 /*!
183 ttmath_mutex will be defined by TTMATH_MULTITHREADS_HELPER macro
184 */
185 extern pthread_mutex_t ttmath_mutex;
186
187
188 /*!
189 objects of this class are used to synchronize
190 */
191 class ThreadLock
192 {
193 public:
194
195 bool Lock()
196 {
197 if( pthread_mutex_lock(&ttmath_mutex) != 0 )
198 return false;
199
200 return true;
201 }
202
203
204 ~ThreadLock()
205 {
206 pthread_mutex_unlock(&ttmath_mutex);
207 }
208 };
209
210 #endif // #ifdef TTMATH_POSIX_THREADS
211
212
213
214
215 #if !defined(TTMATH_POSIX_THREADS) && !defined(TTMATH_WIN32_THREADS)
216
217 /*!
218 we don't use win32 and pthreads
219 */
220
221 /*!
222 */
223 #define TTMATH_MULTITHREADS_HELPER
224
225
226 /*!
227 objects of this class are used to synchronize
228 actually we don't synchronize, the method Lock() returns always 'false'
229 */
230 class ThreadLock
231 {
232 public:
233
234 bool Lock()
235 {
236 return false;
237 }
238 };
239
240
241 #endif // #if !defined(TTMATH_POSIX_THREADS) && !defined(TTMATH_WIN32_THREADS)
242
243
244
245
246
247 } // namespace
248
249 #endif
250