ttmaththreads.h 4.9 KB

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