GEOS  3.8.0dev
ttmaththreads.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TTMath Bignum Library
3  * and is distributed under the 3-Clause BSD Licence.
4  * Author: Tomasz Sowa <t.sowa@ttmath.org>
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 
62 namespace ttmath
63 {
64 
65 
66 #ifdef TTMATH_WIN32_THREADS
67 
68  /*
69  we use win32 threads
70  */
71 
72 
79  #define TTMATH_MULTITHREADS_HELPER
80 
81 
85  class ThreadLock
86  {
87  HANDLE mutex_handle;
88 
89 
90  void CreateName(char * buffer) const
91  {
92  #ifdef _MSC_VER
93  #pragma warning (disable : 4996)
94  // warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead.
95  #endif
96 
97  sprintf(buffer, "TTMATH_LOCK_%ul", (unsigned long)GetCurrentProcessId());
98 
99  #ifdef _MSC_VER
100  #pragma warning (default : 4996)
101  #endif
102  }
103 
104 
105  public:
106 
107  bool Lock()
108  {
109  char buffer[50];
110 
111  CreateName(buffer);
112  mutex_handle = CreateMutexA(0, false, buffer);
113 
114  if( mutex_handle == 0 )
115  return false;
116 
117  WaitForSingleObject(mutex_handle, INFINITE);
118 
119  return true;
120  }
121 
122 
123  ThreadLock()
124  {
125  mutex_handle = 0;
126  }
127 
128 
129  ~ThreadLock()
130  {
131  if( mutex_handle != 0 )
132  {
133  ReleaseMutex(mutex_handle);
134  CloseHandle(mutex_handle);
135  }
136  }
137  };
138 
139 #endif // #ifdef TTMATH_WIN32_THREADS
140 
141 
142 
143 
144 
145 #ifdef TTMATH_POSIX_THREADS
146 
147  /*
148  we use posix threads
149  */
150 
151 
157  #define TTMATH_MULTITHREADS_HELPER \
158  namespace ttmath \
159  { \
160  pthread_mutex_t ttmath_mutex = PTHREAD_MUTEX_INITIALIZER; \
161  }
162 
163 
167  extern pthread_mutex_t ttmath_mutex;
168 
169 
188  class ThreadLock
189  {
190  public:
191 
197  bool Lock()
198  {
199  if( pthread_mutex_lock(&ttmath_mutex) != 0 )
200  return false;
201 
202  return true;
203  }
204 
205 
206  ~ThreadLock()
207  {
208  pthread_mutex_unlock(&ttmath_mutex);
209  }
210  };
211 
212 #endif // #ifdef TTMATH_POSIX_THREADS
213 
214 
215 
216 
217 #if !defined(TTMATH_POSIX_THREADS) && !defined(TTMATH_WIN32_THREADS)
218 
225  #define TTMATH_MULTITHREADS_HELPER
226 
227 
233  {
234  public:
235 
236  bool Lock()
237  {
238  return false;
239  }
240  };
241 
242 
243 #endif // #if !defined(TTMATH_POSIX_THREADS) && !defined(TTMATH_WIN32_THREADS)
244 
245 
246 
247 
248 
249 } // namespace
250 
251 #endif
252 
Definition: ttmaththreads.h:232
a namespace for the TTMath library
Definition: ttmath.h:62
constants used in the library