gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

240 lines
9.6KB

  1. /**********************************************************************
  2. * $Id: cpl_multiproc.h 49f2075cf4be6b103a5ab0b5a64f1ed3e2e7f46b 2018-11-27 21:21:53Z Robert Coup $
  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. #define CPL_MUTEX_REGULAR 2
  66. CPLMutex CPL_DLL *CPLCreateMutex( void ); /* returned acquired */
  67. CPLMutex CPL_DLL *CPLCreateMutexEx( int nOptions ); /* returned acquired */
  68. int CPL_DLL CPLCreateOrAcquireMutex( CPLMutex **, double dfWaitInSeconds );
  69. int CPL_DLL CPLCreateOrAcquireMutexEx( CPLMutex **, double dfWaitInSeconds, int nOptions );
  70. int CPL_DLL CPLAcquireMutex( CPLMutex *hMutex, double dfWaitInSeconds );
  71. void CPL_DLL CPLReleaseMutex( CPLMutex *hMutex );
  72. void CPL_DLL CPLDestroyMutex( CPLMutex *hMutex );
  73. void CPL_DLL CPLCleanupMasterMutex( void );
  74. CPLCond CPL_DLL *CPLCreateCond( void );
  75. void CPL_DLL CPLCondWait( CPLCond *hCond, CPLMutex* hMutex );
  76. void CPL_DLL CPLCondSignal( CPLCond *hCond );
  77. void CPL_DLL CPLCondBroadcast( CPLCond *hCond );
  78. void CPL_DLL CPLDestroyCond( CPLCond *hCond );
  79. /** Contrary to what its name suggests, CPLGetPID() actually returns the thread id */
  80. GIntBig CPL_DLL CPLGetPID( void );
  81. int CPL_DLL CPLGetCurrentProcessID( void );
  82. int CPL_DLL CPLCreateThread( CPLThreadFunc pfnMain, void *pArg );
  83. CPLJoinableThread CPL_DLL* CPLCreateJoinableThread( CPLThreadFunc pfnMain, void *pArg );
  84. void CPL_DLL CPLJoinThread(CPLJoinableThread* hJoinableThread);
  85. void CPL_DLL CPLSleep( double dfWaitInSeconds );
  86. const char CPL_DLL *CPLGetThreadingModel( void );
  87. int CPL_DLL CPLGetNumCPUs( void );
  88. typedef struct _CPLLock CPLLock;
  89. /* Currently LOCK_ADAPTIVE_MUTEX is Linux-only and LOCK_SPIN only available */
  90. /* on systems with pthread_spinlock API (so not MacOsX). If a requested type */
  91. /* isn't available, it fallbacks to LOCK_RECURSIVE_MUTEX */
  92. typedef enum
  93. {
  94. LOCK_RECURSIVE_MUTEX,
  95. LOCK_ADAPTIVE_MUTEX,
  96. LOCK_SPIN
  97. } CPLLockType;
  98. CPLLock CPL_DLL *CPLCreateLock( CPLLockType eType ); /* returned NON acquired */
  99. int CPL_DLL CPLCreateOrAcquireLock( CPLLock**, CPLLockType eType );
  100. int CPL_DLL CPLAcquireLock( CPLLock* );
  101. void CPL_DLL CPLReleaseLock( CPLLock* );
  102. void CPL_DLL CPLDestroyLock( CPLLock* );
  103. void CPL_DLL CPLLockSetDebugPerf( CPLLock*, int bEnableIn ); /* only available on x86/x86_64 with GCC for now */
  104. CPL_C_END
  105. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  106. /* Instantiates the mutex if not already done. The parameter x should be a (void**). */
  107. #define CPLMutexHolderD(x) CPLMutexHolder oHolder(x,1000.0,__FILE__,__LINE__);
  108. /* Instantiates the mutex with options if not already done. */
  109. /* The parameter x should be a (void**). */
  110. #define CPLMutexHolderExD(x, nOptions) CPLMutexHolder oHolder(x,1000.0,__FILE__,__LINE__,nOptions);
  111. /* This variant assumes the mutex has already been created. If not, it will */
  112. /* be a no-op. The parameter x should be a (void*) */
  113. #define CPLMutexHolderOptionalLockD(x) CPLMutexHolder oHolder(x,1000.0,__FILE__,__LINE__);
  114. /** Object to hold a mutex */
  115. class CPL_DLL CPLMutexHolder
  116. {
  117. private:
  118. CPLMutex *hMutex = nullptr;
  119. // Only used for debugging.
  120. const char *pszFile = nullptr;
  121. int nLine = 0;
  122. CPL_DISALLOW_COPY_ASSIGN(CPLMutexHolder)
  123. public:
  124. /** Instantiates the mutex if not already done. */
  125. explicit CPLMutexHolder( CPLMutex **phMutex, double dfWaitInSeconds = 1000.0,
  126. const char *pszFile = __FILE__,
  127. int nLine = __LINE__,
  128. int nOptions = CPL_MUTEX_RECURSIVE);
  129. /** This variant assumes the mutex has already been created. If not, it will
  130. * be a no-op */
  131. explicit CPLMutexHolder( CPLMutex* hMutex, double dfWaitInSeconds = 1000.0,
  132. const char *pszFile = __FILE__,
  133. int nLine = __LINE__ );
  134. ~CPLMutexHolder();
  135. };
  136. /* Instantiates the lock if not already done. The parameter x should be a (CPLLock**). */
  137. #define CPLLockHolderD(x, eType) CPLLockHolder oHolder(x,eType,__FILE__,__LINE__);
  138. /* This variant assumes the lock has already been created. If not, it will */
  139. /* be a no-op. The parameter should be (CPLLock*) */
  140. #define CPLLockHolderOptionalLockD(x) CPLLockHolder oHolder(x,__FILE__,__LINE__);
  141. /** Object to hold a lock */
  142. class CPL_DLL CPLLockHolder
  143. {
  144. private:
  145. CPLLock *hLock = nullptr;
  146. const char *pszFile = nullptr;
  147. int nLine = 0;
  148. CPL_DISALLOW_COPY_ASSIGN(CPLLockHolder)
  149. public:
  150. /** Instantiates the lock if not already done. */
  151. CPLLockHolder( CPLLock **phSpin, CPLLockType eType,
  152. const char *pszFile = __FILE__,
  153. int nLine = __LINE__);
  154. /** This variant assumes the lock has already been created. If not, it will
  155. * be a no-op */
  156. explicit CPLLockHolder( CPLLock* hSpin,
  157. const char *pszFile = __FILE__,
  158. int nLine = __LINE__ );
  159. ~CPLLockHolder();
  160. };
  161. #endif /* def __cplusplus */
  162. /* -------------------------------------------------------------------- */
  163. /* Thread local storage. */
  164. /* -------------------------------------------------------------------- */
  165. #define CTLS_RLBUFFERINFO 1 /* cpl_conv.cpp */
  166. #define CTLS_WIN32_COND 2 /* cpl_multiproc.cpp */
  167. #define CTLS_CSVTABLEPTR 3 /* cpl_csv.cpp */
  168. #define CTLS_CSVDEFAULTFILENAME 4 /* cpl_csv.cpp */
  169. #define CTLS_ERRORCONTEXT 5 /* cpl_error.cpp */
  170. /* 6: unused */
  171. #define CTLS_PATHBUF 7 /* cpl_path.cpp */
  172. #define CTLS_ABSTRACTARCHIVE_SPLIT 8 /* cpl_vsil_abstract_archive.cpp */
  173. #define CTLS_UNUSED4 9
  174. #define CTLS_CPLSPRINTF 10 /* cpl_string.h */
  175. #define CTLS_RESPONSIBLEPID 11 /* gdaldataset.cpp */
  176. #define CTLS_VERSIONINFO 12 /* gdal_misc.cpp */
  177. #define CTLS_VERSIONINFO_LICENCE 13 /* gdal_misc.cpp */
  178. #define CTLS_CONFIGOPTIONS 14 /* cpl_conv.cpp */
  179. #define CTLS_FINDFILE 15 /* cpl_findfile.cpp */
  180. #define CTLS_VSIERRORCONTEXT 16 /* cpl_vsi_error.cpp */
  181. #define CTLS_ERRORHANDLERACTIVEDATA 17 /* cpl_error.cpp */
  182. #define CTLS_MAX 32
  183. CPL_C_START
  184. void CPL_DLL * CPLGetTLS( int nIndex );
  185. void CPL_DLL * CPLGetTLSEx( int nIndex, int* pbMemoryErrorOccurred );
  186. void CPL_DLL CPLSetTLS( int nIndex, void *pData, int bFreeOnExit );
  187. /* Warning : the CPLTLSFreeFunc must not in any case directly or indirectly */
  188. /* use or fetch any TLS data, or a terminating thread will hang ! */
  189. typedef void (*CPLTLSFreeFunc)( void* pData );
  190. void CPL_DLL CPLSetTLSWithFreeFunc( int nIndex, void *pData, CPLTLSFreeFunc pfnFree );
  191. void CPL_DLL CPLSetTLSWithFreeFuncEx( int nIndex, void *pData, CPLTLSFreeFunc pfnFree, int* pbMemoryErrorOccurred );
  192. void CPL_DLL CPLCleanupTLS( void );
  193. CPL_C_END
  194. #endif /* CPL_MULTIPROC_H_INCLUDED_ */
上海开阖软件有限公司 沪ICP备12045867号-1