summaryrefslogtreecommitdiffstats
path: root/lib/base/crit.cpp
blob: 3f705ed31fff72c2e2fabfb2ab72a97c88bb4ca5 (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/*
 * crit.c: Critical section abstraction. Used in threaded servers to protect
 *         areas where two threads can interfere with each other.
 *
 *         Condvars are condition variables that are used for thread-thread 
 *         synchronization.
 * 
 * Rob McCool
 */

#include "systems.h"

#include "netsite.h"
#include "crit.h"
#include "pool.h"

/* #include "base/dbtbase.h" */

#ifdef USE_NSPR
/*
#include "prmon.h"
#include "private/primpl.h"
*/
#include "nspr.h"

#include "prthread.h"
#include "prlock.h"
#include "prcvar.h"
#include "prinrval.h"

/*
 * Defined to replace PR_Monitor() with PR_Lock().
 */
typedef struct critical {
	PRLock		*lock;
	PRUint32	count;
	PRThread	*owner;
} critical_t;

typedef struct condvar {
	critical_t	*lock;
	PRCondVar	*cvar;
} condvar_t;

#endif
/* -------------------------- critical sections --------------------------- */

/* Useful for ASSERTs only. Returns non-zero if the current thread is the owner.
 */
NSAPI_PUBLIC int crit_owner_is_me(CRITICAL id)
{
#ifdef USE_NSPR
    critical_t *crit = (critical_t*)id;

    return (crit->owner == PR_GetCurrentThread());
#else
    return 1;
#endif
}

NSAPI_PUBLIC CRITICAL crit_init(void)
{
#ifdef USE_NSPR
    critical_t *crit = (critical_t*)PERM_MALLOC(sizeof(critical_t)) ;

    if (crit) {
        if (!(crit->lock = PR_NewLock())) {
            PERM_FREE(crit);
            return NULL;
        }
        crit->count = 0;
        crit->owner = 0;
    }
    return (void *) crit;
#else
    return NULL;
#endif
}

NSAPI_PUBLIC void crit_enter(CRITICAL id)
{
#ifdef USE_NSPR
    critical_t *crit = (critical_t*)id;
    PRThread *me = PR_GetCurrentThread();

    if ( crit->owner == me) {
        PR_ASSERT(crit->count > 0);
        crit->count++;
    } 
    else {
        PR_Lock(crit->lock);
        PR_ASSERT(crit->count == 0);
        crit->count = 1;
        crit->owner = me;
    }
#endif
}

NSAPI_PUBLIC void crit_exit(CRITICAL id)
{
#ifdef USE_NSPR
    critical_t	*crit = (critical_t*)id;

    if (crit->owner != PR_GetCurrentThread()) 
        return;

    if ( --crit->count == 0) {
        crit->owner = 0;
        PR_Unlock(crit->lock);
    }
#endif
}

NSAPI_PUBLIC void crit_terminate(CRITICAL id)
{
#ifdef USE_NSPR
    critical_t	*crit = (critical_t*)id;

    PR_DestroyLock((PRLock*)crit->lock);
    PERM_FREE(crit);
#endif
}


/* ------------------------- condition variables -------------------------- */


NSAPI_PUBLIC CONDVAR condvar_init(CRITICAL id)
{
#ifdef USE_NSPR
    critical_t	*crit = (critical_t*)id;

    condvar_t *cvar = (condvar_t*)PERM_MALLOC(sizeof(condvar_t)) ;

    if (crit) {
        cvar->lock = crit;
        if ((cvar->cvar = PR_NewCondVar((PRLock *)cvar->lock->lock)) == 0) {
            PERM_FREE(cvar);
            return NULL;
        }
    }
    return (void *) cvar;
#endif
}

NSAPI_PUBLIC void condvar_wait(CONDVAR _cv)
{
#ifdef USE_NSPR
    condvar_t *cv = (condvar_t *)_cv;
    /* Save away recursion count so we can restore it after the wait */
    int saveCount = cv->lock->count;
    PRThread *saveOwner = cv->lock->owner;

    PR_ASSERT(cv->lock->owner == PR_GetCurrentThread());
    cv->lock->count = 0;
    cv->lock->owner = 0;

    PR_WaitCondVar(cv->cvar, PR_INTERVAL_NO_TIMEOUT);

    cv->lock->count = saveCount;
    cv->lock->owner = saveOwner;
#endif
}


NSAPI_PUBLIC void condvar_timed_wait(CONDVAR _cv, long secs)
{
#ifdef USE_NSPR
    condvar_t *cv = (condvar_t *)_cv;
    /* Save away recursion count so we can restore it after the wait */
    int saveCount = cv->lock->count;
    PRThread *saveOwner = cv->lock->owner;
 
    PR_ASSERT(cv->lock->owner == PR_GetCurrentThread());
    cv->lock->count = 0;
    cv->lock->owner = 0;
    
    PRIntervalTime timeout = PR_INTERVAL_NO_TIMEOUT;
    if (secs > 0) 
	timeout = PR_SecondsToInterval(secs); 
    PR_WaitCondVar(cv->cvar, timeout);
 
    cv->lock->count = saveCount;
    cv->lock->owner = saveOwner;
#endif
}



NSAPI_PUBLIC void condvar_notify(CONDVAR _cv)
{
#ifdef USE_NSPR
    condvar_t *cv = (condvar_t *)_cv;
    PR_ASSERT(cv->lock->owner == PR_GetCurrentThread());
    PR_NotifyCondVar(cv->cvar);
#endif
}

NSAPI_PUBLIC void condvar_notifyAll(CONDVAR _cv)
{
#ifdef USE_NSPR
    condvar_t *cv = (condvar_t *)_cv;
    PR_ASSERT(cv->lock->owner == PR_GetCurrentThread());
    PR_NotifyAllCondVar(cv->cvar);
#endif
}

NSAPI_PUBLIC void condvar_terminate(CONDVAR _cv)
{
#ifdef USE_NSPR
    condvar_t *cv = (condvar_t *)_cv;
    PR_DestroyCondVar(cv->cvar);
    PERM_FREE(cv);
#endif
}

/* ----------------------- Counting semaphores ---------------------------- */
/* These are currently not listed in crit.h because they aren't yet used; 
 * although they do work.
 */

/*
 * XXXMB - these should be in NSPR.  
 *
 */

#if defined(SOLARIS) && defined(HW_THREADS)
#include <synch.h>
typedef sema_t counting_sem_t;
#elif defined(IRIX) && defined(HW_THREADS)
#include <ulocks.h>
typedef usema_t *counting_sem_t;
#else
typedef struct counting_sem_t {
	CRITICAL lock;
	CRITICAL cv_lock;
	CONDVAR cv;
	int count;
	int max;
} counting_sem_t;
#endif

NSAPI_PUBLIC COUNTING_SEMAPHORE
cs_init(int initial_count)
{
	counting_sem_t *cs = (counting_sem_t *)PERM_MALLOC(sizeof(counting_sem_t));
#if defined(SOLARIS) && defined(HW_THREADS)
    if ( sema_init(cs, initial_count, USYNC_THREAD, NULL) < 0) {
		ereport(LOG_FAILURE, XP_GetAdminStr(DBT_csInitFailureS_), system_errmsg());
		PERM_FREE(cs);
		return NULL;
	}

	return (COUNTING_SEMAPHORE)cs;
#elif defined(IRIX) && defined(HW_THREADS)
	usptr_t *arena;

	usconfig(CONF_INITSIZE, 64*1024);
	if ( (arena = usinit("/tmp/cs.locks")) == NULL) 
		return NULL;

	if ( (cs = (counting_sem_t *)usnewsema(arena, 0)) == NULL)
		return NULL;

	return cs;
	
#else

	cs->count = initial_count;
	cs->lock = crit_init();
	cs->cv_lock = crit_init();
	cs->cv = condvar_init(cs->cv_lock);

	return (COUNTING_SEMAPHORE)cs;
#endif
}

NSAPI_PUBLIC void
cs_terminate(COUNTING_SEMAPHORE csp)
{
	counting_sem_t *cs = (counting_sem_t *)csp;

#if defined(SOLARIS) && defined(HW_THREADS)
    if ( sema_destroy(cs) < 0 ) {
		ereport(LOG_FAILURE, XP_GetAdminStr(DBT_csTerminateFailureS_), system_errmsg());
	}
    PERM_FREE(cs);
	return;
#elif defined(IRIX) && defined(HW_THREADS)
	/* usfreesema() */
	return;
#else
	condvar_terminate(cs->cv);
	crit_terminate(cs->cv_lock);
	crit_terminate(cs->lock);
	PERM_FREE(cs);

	return;
#endif
}

NSAPI_PUBLIC int
cs_wait(COUNTING_SEMAPHORE csp)
{
	counting_sem_t *cs = (counting_sem_t *)csp;
#if defined(SOLARIS) && defined(HW_THREADS)
	int ret;

    if ( (ret = sema_wait(cs)) < 0 ) {
		ereport(LOG_FAILURE, XP_GetAdminStr(DBT_csWaitFailureS_), system_errmsg());
		return -1;
	}
	return ret;
#elif defined(IRIX) && defined(HW_THREADS)
	uspsema(cs);
	return 0;
#else
	crit_enter(cs->lock);
	while ( cs->count == 0 ) {
		crit_enter(cs->cv_lock);
		crit_exit(cs->lock);
		condvar_wait(cs->cv);
		crit_exit(cs->cv_lock);
		crit_enter(cs->lock);
	}
	--(cs->count);
	crit_exit(cs->lock);

	return 0;
#endif
}

NSAPI_PUBLIC int
cs_trywait(COUNTING_SEMAPHORE csp)
{
	counting_sem_t *cs = (counting_sem_t *)csp;

#if defined(SOLARIS) && defined(HW_THREADS)
	int ret;
	ret = sema_trywait(cs)?-1:0;
    return ret;
#elif defined(IRIX) && defined(HW_THREADS)
	int ret;
	ret = uscpsema(cs);
	return (ret == 1)?0:-1;
#else
	crit_enter(cs->lock);
	if (cs->count > 0) {
		--(cs->count);
		crit_exit(cs->lock);
        return 0;
    }
    crit_exit(cs->lock);
	return -1;

#endif
}

NSAPI_PUBLIC int 
cs_release(COUNTING_SEMAPHORE csp)
{
	counting_sem_t *cs = (counting_sem_t *)csp;

#if defined(SOLARIS) && defined(HW_THREADS)
	int ret;
    if ( (ret = sema_post(cs)) < 0 ) {
		ereport(LOG_FAILURE, XP_GetAdminStr(DBT_csPostFailureS_), system_errmsg());
		return -1;
	}
	return ret;
#elif defined(IRIX) && defined(HW_THREADS)
	usvsema(cs);
	return 0;
#else
	crit_enter(cs->lock);
	++(cs->count);
	if (cs->count == 1) {
		crit_enter(cs->cv_lock);
		condvar_notify(cs->cv);
		crit_exit(cs->cv_lock);
	}
	crit_exit(cs->lock);

	return 0;
#endif
}