GDAL

cpl_multiproc.h
1/**********************************************************************
2 *
3 * Project: CPL - Common Portability Library
4 * Purpose: CPL Multi-Threading, and process handling portability functions.
5 * Author: Frank Warmerdam, warmerdam@pobox.com
6 *
7 **********************************************************************
8 * Copyright (c) 2002, Frank Warmerdam
9 * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
10 *
11 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14#ifndef CPL_MULTIPROC_H_INCLUDED_
15#define CPL_MULTIPROC_H_INCLUDED_
16
17#include "cpl_port.h"
18
19/*
20** There are three primary implementations of the multi-process support
21** controlled by one of CPL_MULTIPROC_WIN32, CPL_MULTIPROC_PTHREAD or
22** CPL_MULTIPROC_STUB being defined. If none are defined, the stub
23** implementation will be used.
24*/
25
26#if defined(_WIN32) && !defined(CPL_MULTIPROC_STUB)
27#define CPL_MULTIPROC_WIN32
28/* MinGW can have pthread support, so disable it to avoid issues */
29/* in cpl_multiproc.cpp */
30#undef CPL_MULTIPROC_PTHREAD
31#endif
32
33#if !defined(CPL_MULTIPROC_WIN32) && !defined(CPL_MULTIPROC_PTHREAD) && \
34 !defined(CPL_MULTIPROC_STUB) && !defined(CPL_MULTIPROC_NONE)
35#define CPL_MULTIPROC_STUB
36#endif
37
38 CPL_C_START
39
40 typedef void (*CPLThreadFunc)(void *);
41
42 void CPL_DLL *CPLLockFile(const char *pszPath, double dfWaitInSeconds);
43 void CPL_DLL CPLUnlockFile(void *hLock);
44
45 typedef struct _CPLMutex CPLMutex;
46 typedef struct _CPLCond CPLCond;
47 typedef struct _CPLJoinableThread CPLJoinableThread;
48
49/* Options for CPLCreateMutexEx() and CPLCreateOrAcquireMutexEx() */
50#define CPL_MUTEX_RECURSIVE 0
51#define CPL_MUTEX_ADAPTIVE 1
52#define CPL_MUTEX_REGULAR 2
53
54CPLMutex CPL_DLL *CPLCreateMutex(void); /* returned acquired */
55CPLMutex CPL_DLL *CPLCreateMutexEx(int nOptions); /* returned acquired */
56 int CPL_DLL CPLCreateOrAcquireMutex(CPLMutex **, double dfWaitInSeconds);
57 int CPL_DLL CPLCreateOrAcquireMutexEx(CPLMutex **, double dfWaitInSeconds,
58 int nOptions);
59 int CPL_DLL CPLAcquireMutex(CPLMutex *hMutex, double dfWaitInSeconds);
60 void CPL_DLL CPLReleaseMutex(CPLMutex *hMutex);
61 void CPL_DLL CPLDestroyMutex(CPLMutex *hMutex);
62 void CPL_DLL CPLCleanupMasterMutex(void);
63
64CPLCond CPL_DLL *CPLCreateCond(void);
65 void CPL_DLL CPLCondWait(CPLCond *hCond, CPLMutex *hMutex);
66
67 typedef enum
68{
69 COND_TIMED_WAIT_COND,
70 COND_TIMED_WAIT_TIME_OUT,
71 COND_TIMED_WAIT_OTHER
72} CPLCondTimedWaitReason;
73
74CPLCondTimedWaitReason CPL_DLL CPLCondTimedWait(CPLCond *hCond,
75 CPLMutex *hMutex,
76 double dfWaitInSeconds);
77 void CPL_DLL CPLCondSignal(CPLCond *hCond);
78 void CPL_DLL CPLCondBroadcast(CPLCond *hCond);
79 void CPL_DLL CPLDestroyCond(CPLCond *hCond);
80
83 GIntBig CPL_DLL CPLGetPID(void);
84 int CPL_DLL CPLGetCurrentProcessID(void);
85 int CPL_DLL CPLCreateThread(CPLThreadFunc pfnMain, void *pArg);
86CPLJoinableThread CPL_DLL *CPLCreateJoinableThread(CPLThreadFunc pfnMain,
87 void *pArg);
88 void CPL_DLL CPLJoinThread(CPLJoinableThread *hJoinableThread);
89 void CPL_DLL CPLSleep(double dfWaitInSeconds);
90
91 const char CPL_DLL *CPLGetThreadingModel(void);
92
93 int CPL_DLL CPLGetNumCPUs(void);
94 int CPL_DLL CPLGetCurrentThreadCount(void);
95
96 typedef struct _CPLLock CPLLock;
97
98/* Currently LOCK_ADAPTIVE_MUTEX is Linux-only and LOCK_SPIN only available */
99/* on systems with pthread_spinlock API (so not MacOsX). If a requested type */
100/* isn't available, it fallbacks to LOCK_RECURSIVE_MUTEX */
101 typedef enum
102{
103 LOCK_RECURSIVE_MUTEX,
104 LOCK_ADAPTIVE_MUTEX,
105 LOCK_SPIN
106} CPLLockType;
107
108CPLLock CPL_DLL *CPLCreateLock(CPLLockType eType); /* returned NON acquired */
109 int CPL_DLL CPLCreateOrAcquireLock(CPLLock **, CPLLockType eType);
110 int CPL_DLL CPLAcquireLock(CPLLock *);
111 void CPL_DLL CPLReleaseLock(CPLLock *);
112 void CPL_DLL CPLDestroyLock(CPLLock *);
113 void CPL_DLL CPLLockSetDebugPerf(
114 CPLLock *,
115 int bEnableIn); /* only available on x86/x86_64 with GCC for now */
116
117 CPL_C_END
118
119#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
120
121/* Instantiates the mutex if not already done. The parameter x should be a
122 * (void**). */
123#define CPLMutexHolderD(x) CPLMutexHolder oHolder(x, 1000.0, __FILE__, __LINE__)
124
125/* Instantiates the mutex with options if not already done. */
126/* The parameter x should be a (void**). */
127#define CPLMutexHolderExD(x, nOptions) \
128 CPLMutexHolder oHolder(x, 1000.0, __FILE__, __LINE__, nOptions)
129
130/* This variant assumes the mutex has already been created. If not, it will */
131/* be a no-op. The parameter x should be a (void*) */
132#define CPLMutexHolderOptionalLockD(x) \
133 CPLMutexHolder oHolder(x, 1000.0, __FILE__, __LINE__)
134
136 class CPL_DLL CPLMutexHolder
137{
138 private:
139 CPLMutex *hMutex = nullptr;
140 // Only used for debugging.
141 const char *pszFile = nullptr;
142 int nLine = 0;
143
144 CPL_DISALLOW_COPY_ASSIGN(CPLMutexHolder)
145
146 public:
148 explicit CPLMutexHolder(CPLMutex **phMutex, double dfWaitInSeconds = 1000.0,
149 const char *pszFile = __FILE__,
150 int nLine = __LINE__,
151 int nOptions = CPL_MUTEX_RECURSIVE);
152
155 explicit CPLMutexHolder(CPLMutex *hMutex, double dfWaitInSeconds = 1000.0,
156 const char *pszFile = __FILE__,
157 int nLine = __LINE__);
158
159 ~CPLMutexHolder();
160};
161
162/* Instantiates the lock if not already done. The parameter x should be a
163 * (CPLLock**). */
164#define CPLLockHolderD(x, eType) \
165 CPLLockHolder oHolder(x, eType, __FILE__, __LINE__);
166
167/* This variant assumes the lock has already been created. If not, it will */
168/* be a no-op. The parameter should be (CPLLock*) */
169#define CPLLockHolderOptionalLockD(x) \
170 CPLLockHolder oHolder(x, __FILE__, __LINE__);
171
173 class CPL_DLL CPLLockHolder
174{
175 private:
176 CPLLock *hLock = nullptr;
177 const char *pszFile = nullptr;
178 int nLine = 0;
179
180 CPL_DISALLOW_COPY_ASSIGN(CPLLockHolder)
181
182 public:
184 CPLLockHolder(CPLLock **phSpin, CPLLockType eType,
185 const char *pszFile = __FILE__, int nLine = __LINE__);
186
189 explicit CPLLockHolder(CPLLock *hSpin, const char *pszFile = __FILE__,
190 int nLine = __LINE__);
191
192 ~CPLLockHolder();
193};
194
195#endif /* def __cplusplus */
196
197/* -------------------------------------------------------------------- */
198/* Thread local storage. */
199/* -------------------------------------------------------------------- */
200
201#define CTLS_RLBUFFERINFO 1 /* cpl_conv.cpp */
202#define CTLS_WIN32_COND 2 /* cpl_multiproc.cpp */
203#define CTLS_CSVTABLEPTR 3 /* cpl_csv.cpp */
204#define CTLS_CSVDEFAULTFILENAME 4 /* cpl_csv.cpp */
205#define CTLS_ERRORCONTEXT 5 /* cpl_error.cpp */
206#define CTLS_VSICURL_CACHEDCONNECTION 6 /* cpl_vsil_curl.cpp */
207#define CTLS_PATHBUF 7 /* cpl_path.cpp */
208#define CTLS_ABSTRACTARCHIVE_SPLIT 8 /* cpl_vsil_abstract_archive.cpp */
209#define CTLS_GDALOPEN_ANTIRECURSION 9 /* gdaldataset.cpp */
210#define CTLS_CPLSPRINTF 10 /* cpl_string.h */
211#define CTLS_RESPONSIBLEPID 11 /* gdaldataset.cpp */
212#define CTLS_VERSIONINFO 12 /* gdal_misc.cpp */
213#define CTLS_VERSIONINFO_LICENCE 13 /* gdal_misc.cpp */
214#define CTLS_CONFIGOPTIONS 14 /* cpl_conv.cpp */
215#define CTLS_FINDFILE 15 /* cpl_findfile.cpp */
216#define CTLS_VSIERRORCONTEXT 16 /* cpl_vsi_error.cpp */
217/* 17: unused */
218#define CTLS_PROJCONTEXTHOLDER 18 /* ogr_proj_p.cpp */
219#define CTLS_GDALDEFAULTOVR_ANTIREC 19 /* gdaldefaultoverviews.cpp */
220#define CTLS_HTTPFETCHCALLBACK 20 /* cpl_http.cpp */
221
222#define CTLS_MAX 32
223
224 CPL_C_START
225 void CPL_DLL *CPLGetTLS(int nIndex);
226 void CPL_DLL *CPLGetTLSEx(int nIndex, int *pbMemoryErrorOccurred);
227 void CPL_DLL CPLSetTLS(int nIndex, void *pData, int bFreeOnExit);
228
229/* Warning : the CPLTLSFreeFunc must not in any case directly or indirectly */
230/* use or fetch any TLS data, or a terminating thread will hang ! */
231 typedef void (*CPLTLSFreeFunc)(void *pData);
232 void CPL_DLL CPLSetTLSWithFreeFunc(int nIndex, void *pData,
233 CPLTLSFreeFunc pfnFree);
234 void CPL_DLL CPLSetTLSWithFreeFuncEx(int nIndex, void *pData,
235 CPLTLSFreeFunc pfnFree,
236 int *pbMemoryErrorOccurred);
237
238 void CPL_DLL CPLCleanupTLS(void);
239 CPL_C_END
240
241#endif /* CPL_MULTIPROC_H_INCLUDED_ */
CPLLockHolder
Object to hold a lock.
Definition cpl_multiproc.h:174
CPLMutexHolder
Object to hold a mutex.
Definition cpl_multiproc.h:137
cpl_port.h
Core portability definitions for CPL.
CPL_C_END
#define CPL_C_END
Macro to end a block of C symbols.
Definition cpl_port.h:289
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition cpl_port.h:285
CPL_DISALLOW_COPY_ASSIGN
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition cpl_port.h:936
GIntBig
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:205