File: mapthread.c

package info (click to toggle)
mapserver 7.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,620 kB
  • sloc: ansic: 176,974; cpp: 58,161; python: 2,969; xml: 1,676; cs: 997; yacc: 856; lex: 766; java: 588; perl: 428; makefile: 374; sh: 184; tcl: 158; ruby: 55
file content (318 lines) | stat: -rw-r--r-- 11,546 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/******************************************************************************
 * $Id$
 *
 * Project:  UMN MapServer
 * Purpose:  Support code for abstracting thread issues.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 1996-2005 Regents of the University of Minnesota.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of this Software or works derived from this Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

/******************************************************************************

            THREAD-SAFE SUPPORT IN MAPSERVER
            ================================

If thread safety is enabled the USE_THREAD macro will be defined.

Thread API (mapthread.h/c)
--------------------------

This API is made available to avoid having dependencies on different
thread libraries in lots of places in MapServer.  It is intended to provide
minimal services required by mapserver and isn't intended to be broadly useful.
It should be available for Win32 and pthreads environments.  It should be
possible to implement for other thread libraries if needed.

  int msGetThreadId():
    Returns the current threads integer id.  This can be used for making
        some information thread specific, as has been done for the global
        error context in maperror.c.

  void msAcquireLock(int):
        Acquires the indicated Mutex.  If it is already held by another thread
        then this thread will block till the other thread releases it.  If
        this thread already holds the mutex then behaviour is undefined.  If
        the mutex id is not valid (not in the range 0 to TLOCK_STATIC_MAX)
        then results are undefined.

  void msReleaseLock(int):
        Releases the indicated mutex.  If the lock id is invalid, or if the
        mutex is not currently held by this thread then results are undefined.

It is incredibly important to ensure that any mutex that is acquired is
released as soon as possible.  Any flow of control that could result in a
mutex not being release is going to be a disaster.

The mutex numbers are defined in mapthread.h with the TLOCK_* codes.  If you
need a new mutex, add a #define in mapthread.h for it.  Currently there is
no "dynamic" mutex allocation, but this could be added.


Making Things Thread-safe
-------------------------

Generally, to make MapServer thread-safe it is necessary to ensure that
different threads aren't read and updating common datastructures at the same
time and that all appropriate state be kept thread specific.

Generally this will mean:
  o The previously global error status (errorObj ms_error) is now thread
    specific.  Use msGetErrorObj() to get the current threads error state.

  o Use of subcomponents that are not thread safe need to be protected by
    a Mutex (lock).

Currently a mutex is used for the entire map file parsing operation
(msLoadMap() in mapfile.c) since the yacc parser uses a number of global
variables.

It is also done with pj_init() from PROJ.4 since this does not appear to be
thread safe.  It isn't yet clear if pj_transform() is thread safe.

It is expected that mutexes will need to be employed in a variety of other
places to ensure serialized access to risky functionality.  This may apply
to sublibraries like GDAL for instance.

If a new section that is not thread-safe is identified (and assuming it
can't be internally modified to make it thread-safe easily), it is necessary
to define a new mutex (#define a TLOCK code in mapthread.h), and then
surround the resource with acquire and release lock calls.

eg.
    msAcquireLock( TLOCK_PROJ );
    if( !(p->proj = pj_init(p->numargs, p->args)) ) {
        msReleaseLock( TLOCK_PROJ );
        msSetError(MS_PROJERR, pj_strerrno(pj_errno),
                   "msProcessProjection()");
        return(-1);
    }

    msReleaseLock( TLOCK_PROJ );

It is imperative that any acquired locks be released on all possible
control paths or else the MapServer will lock up as other thread try to
acquire the lock and block forever.


Other Thread-safe Issues
------------------------

Some issues are not easily corrected with Mutexes or other similar
mechanisms.  The following restrictions currently apply to MapServer when
trying to use it in thread-safe mode.  Note that failure to adhere to these
constraints will not usually generate nice error messages, instead operation
will just fail sometimes.

1) It is currently assumed that a mapObj belongs only to one thread at a time.
That is, there is no effort to syncronize access to a mapObj itself.

2) Stuff that results in a chdir() call are problematic.  In particular, the
.map file SHAPEPATH directive should not be used.  Use full paths to data
files instead.

******************************************************************************/


#include <assert.h>
#include "mapserver.h"
#include "mapthread.h"



#if defined(USE_THREAD)
static int thread_debug = 0;

static char *lock_names[] = {
  NULL, "PARSER", "GDAL", "ERROROBJ", "PROJ", "TTF", "POOL", "SDE",
  "ORACLE", "OWS", "LAYER_VTABLE", "IOCONTEXT", "TMPFILE", "DEBUGOBJ", "OGR", "TIME", "FRIBIDI", "WXS", "GEOS", NULL
};
#endif

/************************************************************************/
/* ==================================================================== */
/*                               PTHREADS                               */
/* ==================================================================== */
/************************************************************************/

#if defined(USE_THREAD) && !defined(_WIN32)

#include "pthread.h"

static int mutexes_initialized = 0;
static pthread_mutex_t mutex_locks[TLOCK_MAX];

/************************************************************************/
/*                            msThreadInit()                            */
/************************************************************************/

void msThreadInit()

{
  static pthread_mutex_t core_lock = PTHREAD_MUTEX_INITIALIZER;

  if( thread_debug )
    fprintf( stderr, "msThreadInit() (posix)\n" );

  pthread_mutex_lock( &core_lock );

  for( ; mutexes_initialized < TLOCK_STATIC_MAX; mutexes_initialized++ )
    pthread_mutex_init( mutex_locks + mutexes_initialized, NULL );

  pthread_mutex_unlock( &core_lock );
}

/************************************************************************/
/*                           msGetThreadId()                            */
/************************************************************************/

void* msGetThreadId()

{
  return (void*) pthread_self();
}

/************************************************************************/
/*                           msAcquireLock()                            */
/************************************************************************/

void msAcquireLock( int nLockId )

{
  if( mutexes_initialized == 0 )
    msThreadInit();

  assert( nLockId >= 0 && nLockId < mutexes_initialized );

  if( thread_debug )
    fprintf( stderr, "msAcquireLock(%d/%s) (posix)\n",
             nLockId, lock_names[nLockId] );

  pthread_mutex_lock( mutex_locks + nLockId );
}

/************************************************************************/
/*                           msReleaseLock()                            */
/************************************************************************/

void msReleaseLock( int nLockId )

{
  assert( mutexes_initialized > 0 );
  assert( nLockId >= 0 && nLockId < mutexes_initialized );

  if( thread_debug )
    fprintf( stderr, "msReleaseLock(%d/%s) (posix)\n",
             nLockId, lock_names[nLockId] );

  pthread_mutex_unlock( mutex_locks + nLockId );
}

#endif /* defined(USE_THREAD) && !defined(_WIN32) */

/************************************************************************/
/* ==================================================================== */
/*                          WIN32 THREADS                               */
/* ==================================================================== */
/************************************************************************/

#if defined(USE_THREAD) && defined(_WIN32)

#include <windows.h>

static int mutexes_initialized = 0;
static HANDLE mutex_locks[TLOCK_MAX];

/************************************************************************/
/*                            msThreadInit()                            */
/************************************************************************/

void msThreadInit()

{
  /* static pthread_mutex_t core_lock = PTHREAD_MUTEX_INITIALIZER; */
  static HANDLE core_lock = NULL;

  if( mutexes_initialized >= TLOCK_STATIC_MAX )
    return;

  if( thread_debug )
    fprintf( stderr, "msThreadInit() (win32)\n" );

  if( core_lock == NULL )
    core_lock = CreateMutex( NULL, TRUE, NULL );
  else
    WaitForSingleObject( core_lock, INFINITE );

  for( ; mutexes_initialized < TLOCK_STATIC_MAX; mutexes_initialized++ )
    mutex_locks[mutexes_initialized] = CreateMutex( NULL, FALSE, NULL );

  ReleaseMutex( core_lock );
}

/************************************************************************/
/*                           msGetThreadId()                            */
/************************************************************************/

void* msGetThreadId()

{
  return GetCurrentThreadId();
}

/************************************************************************/
/*                           msAcquireLock()                            */
/************************************************************************/

void msAcquireLock( int nLockId )

{
  if( mutexes_initialized == 0 )
    msThreadInit();

  assert( nLockId >= 0 && nLockId < mutexes_initialized );

  if( thread_debug )
    fprintf( stderr, "msAcquireLock(%d/%s) (win32)\n",
             nLockId, lock_names[nLockId] );

  WaitForSingleObject( mutex_locks[nLockId], INFINITE );
}

/************************************************************************/
/*                           msReleaseLock()                            */
/************************************************************************/

void msReleaseLock( int nLockId )

{
  assert( mutexes_initialized > 0 );
  assert( nLockId >= 0 && nLockId < mutexes_initialized );

  if( thread_debug )
    fprintf( stderr, "msReleaseLock(%d/%s) (win32)\n",
             nLockId, lock_names[nLockId] );

  ReleaseMutex( mutex_locks[nLockId] );
}

#endif /* defined(USE_THREAD) && defined(_WIN32) */