cpl_multiproc.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**********************************************************************
  2. * $Id: cpl_multiproc.h 28470 2015-02-12 21:01:32Z rouault $
  3. *
  4. * Project: CPL - Common Portability Library
  5. * Purpose: CPL Multi-Threading, and process handling portability functions.
  6. * Author: Frank Warmerdam, warmerdam@pobox.com
  7. *
  8. **********************************************************************
  9. * Copyright (c) 2002, Frank Warmerdam
  10. * Copyright (c) 2008-2013, Even Rouault <even dot rouault at mines-paris dot org>
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. * DEALINGS IN THE SOFTWARE.
  29. ****************************************************************************/
  30. #ifndef _CPL_MULTIPROC_H_INCLUDED_
  31. #define _CPL_MULTIPROC_H_INCLUDED_
  32. #include "cpl_port.h"
  33. /*
  34. ** There are three primary implementations of the multi-process support
  35. ** controlled by one of CPL_MULTIPROC_WIN32, CPL_MULTIPROC_PTHREAD or
  36. ** CPL_MULTIPROC_STUB being defined. If none are defined, the stub
  37. ** implementation will be used.
  38. */
  39. #if defined(WIN32) && !defined(CPL_MULTIPROC_STUB)
  40. # define CPL_MULTIPROC_WIN32
  41. /* MinGW can have pthread support, so disable it to avoid issues */
  42. /* in cpl_multiproc.cpp */
  43. # undef CPL_MULTIPROC_PTHREAD
  44. #endif
  45. #if !defined(CPL_MULTIPROC_WIN32) && !defined(CPL_MULTIPROC_PTHREAD) \
  46. && !defined(CPL_MULTIPROC_STUB) && !defined(CPL_MULTIPROC_NONE)
  47. # define CPL_MULTIPROC_STUB
  48. #endif
  49. CPL_C_START
  50. typedef void (*CPLThreadFunc)(void *);
  51. void CPL_DLL *CPLLockFile( const char *pszPath, double dfWaitInSeconds );
  52. void CPL_DLL CPLUnlockFile( void *hLock );
  53. #ifdef DEBUG
  54. typedef struct _CPLMutex CPLMutex;
  55. typedef struct _CPLCond CPLCond;
  56. typedef struct _CPLJoinableThread CPLJoinableThread;
  57. #else
  58. #define CPLMutex void
  59. #define CPLCond void
  60. #define CPLJoinableThread void
  61. #endif
  62. /* Options for CPLCreateMutexEx() and CPLCreateOrAcquireMutexEx() */
  63. #define CPL_MUTEX_RECURSIVE 0
  64. #define CPL_MUTEX_ADAPTIVE 1
  65. CPLMutex CPL_DLL *CPLCreateMutex( void ); /* returned acquired */
  66. CPLMutex CPL_DLL *CPLCreateMutexEx( int nOptions ); /* returned acquired */
  67. int CPL_DLL CPLCreateOrAcquireMutex( CPLMutex **, double dfWaitInSeconds );
  68. int CPL_DLL CPLCreateOrAcquireMutexEx( CPLMutex **, double dfWaitInSeconds, int nOptions );
  69. int CPL_DLL CPLAcquireMutex( CPLMutex *hMutex, double dfWaitInSeconds );
  70. void CPL_DLL CPLReleaseMutex( CPLMutex *hMutex );
  71. void CPL_DLL CPLDestroyMutex( CPLMutex *hMutex );
  72. void CPL_DLL CPLCleanupMasterMutex( void );
  73. CPLCond CPL_DLL *CPLCreateCond( void );
  74. void CPL_DLL CPLCondWait( CPLCond *hCond, CPLMutex* hMutex );
  75. void CPL_DLL CPLCondSignal( CPLCond *hCond );
  76. void CPL_DLL CPLCondBroadcast( CPLCond *hCond );
  77. void CPL_DLL CPLDestroyCond( CPLCond *hCond );
  78. GIntBig CPL_DLL CPLGetPID( void );
  79. int CPL_DLL CPLCreateThread( CPLThreadFunc pfnMain, void *pArg );
  80. CPLJoinableThread CPL_DLL* CPLCreateJoinableThread( CPLThreadFunc pfnMain, void *pArg );
  81. void CPL_DLL CPLJoinThread(CPLJoinableThread* hJoinableThread);
  82. void CPL_DLL CPLSleep( double dfWaitInSeconds );
  83. const char CPL_DLL *CPLGetThreadingModel( void );
  84. int CPL_DLL CPLGetNumCPUs( void );
  85. typedef struct _CPLLock CPLLock;
  86. /* Currently LOCK_ADAPTIVE_MUTEX is Linux-only and LOCK_SPIN only available */
  87. /* on systems with pthread_spinlock API (so not MacOsX). If a requested type */
  88. /* isn't available, it fallbacks to LOCK_RECURSIVE_MUTEX */
  89. typedef enum
  90. {
  91. LOCK_RECURSIVE_MUTEX,
  92. LOCK_ADAPTIVE_MUTEX,
  93. LOCK_SPIN
  94. } CPLLockType;
  95. CPLLock CPL_DLL *CPLCreateLock( CPLLockType eType ); /* returned NON acquired */
  96. int CPL_DLL CPLCreateOrAcquireLock( CPLLock**, CPLLockType eType );
  97. int CPL_DLL CPLAcquireLock( CPLLock* );
  98. void CPL_DLL CPLReleaseLock( CPLLock* );
  99. void CPL_DLL CPLDestroyLock( CPLLock* );
  100. void CPL_DLL CPLLockSetDebugPerf( CPLLock*, int bEnableIn ); /* only available on x86/x86_64 with GCC for now */
  101. CPL_C_END
  102. #ifdef __cplusplus
  103. /* Instanciates the mutex if not already done. The parameter x should be a (void**) */
  104. #define CPLMutexHolderD(x) CPLMutexHolder oHolder(x,1000.0,__FILE__,__LINE__);
  105. /* Instanciates the mutex with options if not already done. */
  106. /* The parameter x should be a (void**) */
  107. #define CPLMutexHolderExD(x, nOptions) CPLMutexHolder oHolder(x,1000.0,__FILE__,__LINE__,nOptions);
  108. /* This variant assumes the the mutex has already been created. If not, it will */
  109. /* be a no-op. The parameter x should be a (void*) */
  110. #define CPLMutexHolderOptionalLockD(x) CPLMutexHolder oHolder(x,1000.0,__FILE__,__LINE__);
  111. class CPL_DLL CPLMutexHolder
  112. {
  113. private:
  114. CPLMutex *hMutex;
  115. const char *pszFile;
  116. int nLine;
  117. public:
  118. /* Instanciates the mutex if not already done */
  119. CPLMutexHolder( CPLMutex **phMutex, double dfWaitInSeconds = 1000.0,
  120. const char *pszFile = __FILE__,
  121. int nLine = __LINE__,
  122. int nOptions = CPL_MUTEX_RECURSIVE);
  123. /* This variant assumes the the mutex has already been created. If not, it will */
  124. /* be a no-op */
  125. CPLMutexHolder( CPLMutex* hMutex, double dfWaitInSeconds = 1000.0,
  126. const char *pszFile = __FILE__,
  127. int nLine = __LINE__ );
  128. ~CPLMutexHolder();
  129. };
  130. /* Instanciates the lock if not already done. The parameter x should be a (CPLLock**) */
  131. #define CPLLockHolderD(x, eType) CPLLockHolder oHolder(x,eType,__FILE__,__LINE__);
  132. /* This variant assumes the the lock has already been created. If not, it will */
  133. /* be a no-op. The parameter should be (CPLLock*) */
  134. #define CPLLockHolderOptionalLockD(x) CPLLockHolder oHolder(x,__FILE__,__LINE__);
  135. class CPL_DLL CPLLockHolder
  136. {
  137. private:
  138. CPLLock *hLock;
  139. const char *pszFile;
  140. int nLine;
  141. public:
  142. /* Instanciates the lock if not already done */
  143. CPLLockHolder( CPLLock **phSpin, CPLLockType eType,
  144. const char *pszFile = __FILE__,
  145. int nLine = __LINE__);
  146. /* This variant assumes the the lock has already been created. If not, it will */
  147. /* be a no-op */
  148. CPLLockHolder( CPLLock* hSpin,
  149. const char *pszFile = __FILE__,
  150. int nLine = __LINE__ );
  151. ~CPLLockHolder();
  152. };
  153. #endif /* def __cplusplus */
  154. /* -------------------------------------------------------------------- */
  155. /* Thread local storage. */
  156. /* -------------------------------------------------------------------- */
  157. #define CTLS_RLBUFFERINFO 1 /* cpl_conv.cpp */
  158. #define CTLS_WIN32_COND 2 /* cpl_multiproc.cpp */
  159. #define CTLS_CSVTABLEPTR 3 /* cpl_csv.cpp */
  160. #define CTLS_CSVDEFAULTFILENAME 4 /* cpl_csv.cpp */
  161. #define CTLS_ERRORCONTEXT 5 /* cpl_error.cpp */
  162. #define CTLS_GDALDATASET_REC_PROTECT_MAP 6 /* gdaldataset.cpp */
  163. #define CTLS_PATHBUF 7 /* cpl_path.cpp */
  164. #define CTLS_UNUSED3 8
  165. #define CTLS_UNUSED4 9
  166. #define CTLS_CPLSPRINTF 10 /* cpl_string.h */
  167. #define CTLS_RESPONSIBLEPID 11 /* gdaldataset.cpp */
  168. #define CTLS_VERSIONINFO 12 /* gdal_misc.cpp */
  169. #define CTLS_VERSIONINFO_LICENCE 13 /* gdal_misc.cpp */
  170. #define CTLS_CONFIGOPTIONS 14 /* cpl_conv.cpp */
  171. #define CTLS_FINDFILE 15 /* cpl_findfile.cpp */
  172. #define CTLS_MAX 32
  173. CPL_C_START
  174. void CPL_DLL * CPLGetTLS( int nIndex );
  175. void CPL_DLL CPLSetTLS( int nIndex, void *pData, int bFreeOnExit );
  176. /* Warning : the CPLTLSFreeFunc must not in any case directly or indirectly */
  177. /* use or fetch any TLS data, or a terminating thread will hang ! */
  178. typedef void (*CPLTLSFreeFunc)( void* pData );
  179. void CPL_DLL CPLSetTLSWithFreeFunc( int nIndex, void *pData, CPLTLSFreeFunc pfnFree );
  180. void CPL_DLL CPLCleanupTLS( void );
  181. CPL_C_END
  182. #endif /* _CPL_MULTIPROC_H_INCLUDED_ */