GDAL
cpl_multiproc.h
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 
31 #ifndef _CPL_MULTIPROC_H_INCLUDED_
32 #define _CPL_MULTIPROC_H_INCLUDED_
33 
34 #include "cpl_port.h"
35 
36 /*
37 ** There are three primary implementations of the multi-process support
38 ** controlled by one of CPL_MULTIPROC_WIN32, CPL_MULTIPROC_PTHREAD or
39 ** CPL_MULTIPROC_STUB being defined. If none are defined, the stub
40 ** implementation will be used.
41 */
42 
43 #if defined(WIN32) && !defined(CPL_MULTIPROC_STUB)
44 # define CPL_MULTIPROC_WIN32
45 /* MinGW can have pthread support, so disable it to avoid issues */
46 /* in cpl_multiproc.cpp */
47 # undef CPL_MULTIPROC_PTHREAD
48 #endif
49 
50 #if !defined(CPL_MULTIPROC_WIN32) && !defined(CPL_MULTIPROC_PTHREAD) \
51  && !defined(CPL_MULTIPROC_STUB) && !defined(CPL_MULTIPROC_NONE)
52 # define CPL_MULTIPROC_STUB
53 #endif
54 
55 CPL_C_START
56 
57 typedef void (*CPLThreadFunc)(void *);
58 
59 void CPL_DLL *CPLLockFile( const char *pszPath, double dfWaitInSeconds );
60 void CPL_DLL CPLUnlockFile( void *hLock );
61 
62 #ifdef DEBUG
63 typedef struct _CPLMutex CPLMutex;
64 typedef struct _CPLCond CPLCond;
65 typedef struct _CPLJoinableThread CPLJoinableThread;
66 #else
67 #define CPLMutex void
68 #define CPLCond void
69 #define CPLJoinableThread void
70 #endif
71 
72 /* Options for CPLCreateMutexEx() and CPLCreateOrAcquireMutexEx() */
73 #define CPL_MUTEX_RECURSIVE 0
74 #define CPL_MUTEX_ADAPTIVE 1
75 
76 CPLMutex CPL_DLL *CPLCreateMutex( void ); /* returned acquired */
77 CPLMutex CPL_DLL *CPLCreateMutexEx( int nOptions ); /* returned acquired */
78 int CPL_DLL CPLCreateOrAcquireMutex( CPLMutex **, double dfWaitInSeconds );
79 int CPL_DLL CPLCreateOrAcquireMutexEx( CPLMutex **, double dfWaitInSeconds, int nOptions );
80 int CPL_DLL CPLAcquireMutex( CPLMutex *hMutex, double dfWaitInSeconds );
81 void CPL_DLL CPLReleaseMutex( CPLMutex *hMutex );
82 void CPL_DLL CPLDestroyMutex( CPLMutex *hMutex );
83 void CPL_DLL CPLCleanupMasterMutex( void );
84 
85 CPLCond CPL_DLL *CPLCreateCond( void );
86 void CPL_DLL CPLCondWait( CPLCond *hCond, CPLMutex* hMutex );
87 void CPL_DLL CPLCondSignal( CPLCond *hCond );
88 void CPL_DLL CPLCondBroadcast( CPLCond *hCond );
89 void CPL_DLL CPLDestroyCond( CPLCond *hCond );
90 
91 GIntBig CPL_DLL CPLGetPID( void );
92 int CPL_DLL CPLCreateThread( CPLThreadFunc pfnMain, void *pArg );
93 CPLJoinableThread CPL_DLL* CPLCreateJoinableThread( CPLThreadFunc pfnMain, void *pArg );
94 void CPL_DLL CPLJoinThread(CPLJoinableThread* hJoinableThread);
95 void CPL_DLL CPLSleep( double dfWaitInSeconds );
96 
97 const char CPL_DLL *CPLGetThreadingModel( void );
98 
99 int CPL_DLL CPLGetNumCPUs( void );
100 
101 
102 typedef struct _CPLLock CPLLock;
103 
104 /* Currently LOCK_ADAPTIVE_MUTEX is Linux-only and LOCK_SPIN only available */
105 /* on systems with pthread_spinlock API (so not MacOsX). If a requested type */
106 /* isn't available, it fallbacks to LOCK_RECURSIVE_MUTEX */
107 typedef enum
108 {
109  LOCK_RECURSIVE_MUTEX,
110  LOCK_ADAPTIVE_MUTEX,
111  LOCK_SPIN
112 } CPLLockType;
113 
114 CPLLock CPL_DLL *CPLCreateLock( CPLLockType eType ); /* returned NON acquired */
115 int CPL_DLL CPLCreateOrAcquireLock( CPLLock**, CPLLockType eType );
116 int CPL_DLL CPLAcquireLock( CPLLock* );
117 void CPL_DLL CPLReleaseLock( CPLLock* );
118 void CPL_DLL CPLDestroyLock( CPLLock* );
119 void CPL_DLL CPLLockSetDebugPerf( CPLLock*, int bEnableIn ); /* only available on x86/x86_64 with GCC for now */
120 
121 
122 CPL_C_END
123 
124 #ifdef __cplusplus
125 
126 /* Instantiates the mutex if not already done. The parameter x should be a (void**) */
127 #define CPLMutexHolderD(x) CPLMutexHolder oHolder(x,1000.0,__FILE__,__LINE__);
128 
129 /* Instantiates the mutex with options if not already done. */
130 /* The parameter x should be a (void**) */
131 #define CPLMutexHolderExD(x, nOptions) CPLMutexHolder oHolder(x,1000.0,__FILE__,__LINE__,nOptions);
132 
133 /* This variant assumes the the mutex has already been created. If not, it will */
134 /* be a no-op. The parameter x should be a (void*) */
135 #define CPLMutexHolderOptionalLockD(x) CPLMutexHolder oHolder(x,1000.0,__FILE__,__LINE__);
136 
137 class CPL_DLL CPLMutexHolder
138 {
139  private:
140  CPLMutex *hMutex;
141  const char *pszFile;
142  int nLine;
143 
144  public:
145 
146  /* Instantiates the mutex if not already done */
147  CPLMutexHolder( CPLMutex **phMutex, double dfWaitInSeconds = 1000.0,
148  const char *pszFile = __FILE__,
149  int nLine = __LINE__,
150  int nOptions = CPL_MUTEX_RECURSIVE);
151 
152  /* This variant assumes the the mutex has already been created. If not, it will */
153  /* be a no-op */
154  CPLMutexHolder( CPLMutex* hMutex, double dfWaitInSeconds = 1000.0,
155  const char *pszFile = __FILE__,
156  int nLine = __LINE__ );
157 
158  ~CPLMutexHolder();
159 };
160 
161 /* Instantiates the lock if not already done. The parameter x should be a (CPLLock**) */
162 #define CPLLockHolderD(x, eType) CPLLockHolder oHolder(x,eType,__FILE__,__LINE__);
163 
164 /* This variant assumes the the lock has already been created. If not, it will */
165 /* be a no-op. The parameter should be (CPLLock*) */
166 #define CPLLockHolderOptionalLockD(x) CPLLockHolder oHolder(x,__FILE__,__LINE__);
167 
168 class CPL_DLL CPLLockHolder
169 {
170  private:
171  CPLLock *hLock;
172  const char *pszFile;
173  int nLine;
174 
175  public:
176 
177  /* Instantiates the lock if not already done */
178  CPLLockHolder( CPLLock **phSpin, CPLLockType eType,
179  const char *pszFile = __FILE__,
180  int nLine = __LINE__);
181 
182  /* This variant assumes the the lock has already been created. If not, it will */
183  /* be a no-op */
184  CPLLockHolder( CPLLock* hSpin,
185  const char *pszFile = __FILE__,
186  int nLine = __LINE__ );
187 
188  ~CPLLockHolder();
189 };
190 
191 
192 #endif /* def __cplusplus */
193 
194 /* -------------------------------------------------------------------- */
195 /* Thread local storage. */
196 /* -------------------------------------------------------------------- */
197 
198 #define CTLS_RLBUFFERINFO 1 /* cpl_conv.cpp */
199 #define CTLS_WIN32_COND 2 /* cpl_multiproc.cpp */
200 #define CTLS_CSVTABLEPTR 3 /* cpl_csv.cpp */
201 #define CTLS_CSVDEFAULTFILENAME 4 /* cpl_csv.cpp */
202 #define CTLS_ERRORCONTEXT 5 /* cpl_error.cpp */
203 #define CTLS_GDALDATASET_REC_PROTECT_MAP 6 /* gdaldataset.cpp */
204 #define CTLS_PATHBUF 7 /* cpl_path.cpp */
205 #define CTLS_UNUSED3 8
206 #define CTLS_UNUSED4 9
207 #define CTLS_CPLSPRINTF 10 /* cpl_string.h */
208 #define CTLS_RESPONSIBLEPID 11 /* gdaldataset.cpp */
209 #define CTLS_VERSIONINFO 12 /* gdal_misc.cpp */
210 #define CTLS_VERSIONINFO_LICENCE 13 /* gdal_misc.cpp */
211 #define CTLS_CONFIGOPTIONS 14 /* cpl_conv.cpp */
212 #define CTLS_FINDFILE 15 /* cpl_findfile.cpp */
213 
214 #define CTLS_MAX 32
215 
216 CPL_C_START
217 void CPL_DLL * CPLGetTLS( int nIndex );
218 void CPL_DLL CPLSetTLS( int nIndex, void *pData, int bFreeOnExit );
219 
220 /* Warning : the CPLTLSFreeFunc must not in any case directly or indirectly */
221 /* use or fetch any TLS data, or a terminating thread will hang ! */
222 typedef void (*CPLTLSFreeFunc)( void* pData );
223 void CPL_DLL CPLSetTLSWithFreeFunc( int nIndex, void *pData, CPLTLSFreeFunc pfnFree );
224 
225 void CPL_DLL CPLCleanupTLS( void );
226 CPL_C_END
227 
228 #endif /* _CPL_MULTIPROC_H_INCLUDED_ */
Definition: cpl_multiproc.h:137
Core portability definitions for CPL.
Definition: cpl_multiproc.h:168

Generated for GDAL by doxygen 1.8.11.