| 1 |
/** BEGIN COPYRIGHT BLOCK |
| 2 |
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission. |
| 3 |
* Copyright (C) 2005 Red Hat, Inc. |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* License: GPL (version 3 or any later version). |
| 7 |
* See LICENSE for details. |
| 8 |
* END COPYRIGHT BLOCK **/ |
| 9 |
|
| 10 |
#ifdef HAVE_CONFIG_H |
| 11 |
#include <config.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
#ifndef BASE_CRIT_H |
| 15 |
#define BASE_CRIT_H |
| 16 |
|
| 17 |
#ifndef NOINTNSAPI |
| 18 |
#define INTNSAPI |
| 19 |
#endif /* !NOINTNSAPI */ |
| 20 |
|
| 21 |
/* |
| 22 |
* crit.h: Critical section abstraction. Used in threaded servers to protect |
| 23 |
* areas where two threads can interfere with each other. |
| 24 |
* |
| 25 |
* Condvars are condition variables that are used for thread-thread |
| 26 |
* synchronization. |
| 27 |
* |
| 28 |
* Rob McCool |
| 29 |
*/ |
| 30 |
|
| 31 |
#ifndef NETSITE_H |
| 32 |
#include "netsite.h" |
| 33 |
#endif /* !NETSITE_H */ |
| 34 |
|
| 35 |
/* Define C++ interface */ |
| 36 |
#ifdef __cplusplus |
| 37 |
|
| 38 |
#include "prlog.h" /* NSPR */ |
| 39 |
|
| 40 |
#ifndef prmon_h___ |
| 41 |
#include "prmon.h" |
| 42 |
#endif /* !prmon_h___ */ |
| 43 |
|
| 44 |
class NSAPI_PUBLIC CriticalSection |
| 45 |
{ |
| 46 |
public: |
| 47 |
CriticalSection(); |
| 48 |
~CriticalSection(); |
| 49 |
void Acquire() { PR_EnterMonitor(_crtsec); } |
| 50 |
void Release() { PR_ExitMonitor(_crtsec); } |
| 51 |
|
| 52 |
private: |
| 53 |
PRMonitor *_crtsec; |
| 54 |
}; |
| 55 |
|
| 56 |
inline CriticalSection::CriticalSection() : _crtsec(0) |
| 57 |
{ |
| 58 |
_crtsec = PR_NewMonitor(); |
| 59 |
PR_ASSERT(_crtsec); |
| 60 |
} |
| 61 |
|
| 62 |
inline CriticalSection::~CriticalSection() |
| 63 |
{ |
| 64 |
if (_crtsec) |
| 65 |
PR_DestroyMonitor(_crtsec); |
| 66 |
} |
| 67 |
|
| 68 |
class SafeLock |
| 69 |
{ |
| 70 |
public: |
| 71 |
SafeLock(CriticalSection &); // acquire lock |
| 72 |
~SafeLock(); // release lock |
| 73 |
private: |
| 74 |
CriticalSection &lock; |
| 75 |
}; |
| 76 |
|
| 77 |
inline SafeLock::SafeLock(CriticalSection &_lock) : lock(_lock) |
| 78 |
{ |
| 79 |
lock.Acquire(); |
| 80 |
} |
| 81 |
|
| 82 |
inline SafeLock::~SafeLock() |
| 83 |
{ |
| 84 |
lock.Release(); |
| 85 |
} |
| 86 |
#endif /* __cplusplus */ |
| 87 |
|
| 88 |
/* --- Begin function prototypes --- */ |
| 89 |
|
| 90 |
#ifdef INTNSAPI |
| 91 |
|
| 92 |
NSPR_BEGIN_EXTERN_C |
| 93 |
|
| 94 |
/* ASSERT function only */ |
| 95 |
NSAPI_PUBLIC int crit_owner_is_me(CRITICAL id); |
| 96 |
|
| 97 |
/* |
| 98 |
* INTcrit_init creates and returns a new critical section variable. At the |
| 99 |
* time of creation no one has entered it. |
| 100 |
*/ |
| 101 |
NSAPI_PUBLIC CRITICAL INTcrit_init(void); |
| 102 |
|
| 103 |
/* |
| 104 |
* INTcrit_enter enters a critical section. If someone is already in the |
| 105 |
* section, the calling thread is blocked until that thread exits. |
| 106 |
*/ |
| 107 |
NSAPI_PUBLIC void INTcrit_enter(CRITICAL id); |
| 108 |
|
| 109 |
|
| 110 |
/* |
| 111 |
* INTcrit_exit exits a critical section. If another thread is blocked waiting |
| 112 |
* to enter, it will be unblocked and given ownership of the section. |
| 113 |
*/ |
| 114 |
NSAPI_PUBLIC void INTcrit_exit(CRITICAL id); |
| 115 |
|
| 116 |
|
| 117 |
/* |
| 118 |
* INTcrit_terminate removes a previously allocated critical section variable. |
| 119 |
*/ |
| 120 |
NSAPI_PUBLIC void INTcrit_terminate(CRITICAL id); |
| 121 |
|
| 122 |
|
| 123 |
/* |
| 124 |
* INTcondvar_init initializes and returns a new condition variable. You |
| 125 |
* must provide a critical section to be associated with this condition |
| 126 |
* variable. |
| 127 |
*/ |
| 128 |
NSAPI_PUBLIC CONDVAR INTcondvar_init(CRITICAL id); |
| 129 |
|
| 130 |
|
| 131 |
/* |
| 132 |
* INTcondvar_wait blocks on the given condition variable. The calling thread |
| 133 |
* will be blocked until another thread calls INTcondvar_notify on this variable. |
| 134 |
* The caller must have entered the critical section associated with this |
| 135 |
* condition variable prior to waiting for it. |
| 136 |
*/ |
| 137 |
NSAPI_PUBLIC void INTcondvar_wait(CONDVAR cv); |
| 138 |
NSAPI_PUBLIC void condvar_timed_wait(CONDVAR _cv, long secs); |
| 139 |
|
| 140 |
|
| 141 |
/* |
| 142 |
* INTcondvar_notify awakens any threads blocked on the given condition |
| 143 |
* variable. The caller must have entered the critical section associated |
| 144 |
* with this variable first. |
| 145 |
*/ |
| 146 |
NSAPI_PUBLIC void INTcondvar_notify(CONDVAR cv); |
| 147 |
|
| 148 |
/* |
| 149 |
* INTcondvar_notifyAll awakens all threads blocked on the given condition |
| 150 |
* variable. The caller must have entered the critical section associated |
| 151 |
* with this variable first. |
| 152 |
*/ |
| 153 |
NSAPI_PUBLIC void INTcondvar_notifyAll(CONDVAR cv); |
| 154 |
|
| 155 |
/* |
| 156 |
* INTcondvar_terminate frees the given previously allocated condition variable |
| 157 |
*/ |
| 158 |
NSAPI_PUBLIC void INTcondvar_terminate(CONDVAR cv); |
| 159 |
|
| 160 |
|
| 161 |
/* |
| 162 |
* Create a counting semaphore. |
| 163 |
* Return non-zero on success, 0 on failure. |
| 164 |
*/ |
| 165 |
NSAPI_PUBLIC COUNTING_SEMAPHORE INTcs_init(int initial_count); |
| 166 |
|
| 167 |
/* |
| 168 |
* Destroy a counting semaphore |
| 169 |
*/ |
| 170 |
NSAPI_PUBLIC void INTcs_terminate(COUNTING_SEMAPHORE csp); |
| 171 |
|
| 172 |
/* |
| 173 |
* Wait to "enter" the semaphore. |
| 174 |
* Return 0 on success, -1 on failure. |
| 175 |
*/ |
| 176 |
NSAPI_PUBLIC int INTcs_wait(COUNTING_SEMAPHORE csp); |
| 177 |
|
| 178 |
/* |
| 179 |
* Enter the semaphore if the count is > 0. Otherwise return -1. |
| 180 |
* |
| 181 |
*/ |
| 182 |
NSAPI_PUBLIC int INTcs_trywait(COUNTING_SEMAPHORE csp); |
| 183 |
|
| 184 |
/* |
| 185 |
* Release the semaphore- allowing a thread to enter. |
| 186 |
* Return 0 on success, -1 on failure. |
| 187 |
*/ |
| 188 |
NSAPI_PUBLIC int INTcs_release(COUNTING_SEMAPHORE csp); |
| 189 |
|
| 190 |
NSPR_END_EXTERN_C |
| 191 |
|
| 192 |
/* --- End function prototypes --- */ |
| 193 |
|
| 194 |
#define crit_init INTcrit_init |
| 195 |
#define crit_enter INTcrit_enter |
| 196 |
#define crit_exit INTcrit_exit |
| 197 |
#define crit_terminate INTcrit_terminate |
| 198 |
#define condvar_init INTcondvar_init |
| 199 |
#define condvar_wait INTcondvar_wait |
| 200 |
#define condvar_notify INTcondvar_notify |
| 201 |
#define condvar_notifyAll INTcondvar_notifyAll |
| 202 |
#define condvar_terminate INTcondvar_terminate |
| 203 |
#define cs_init INTcs_init |
| 204 |
#define cs_terminate INTcs_terminate |
| 205 |
#define cs_wait INTcs_wait |
| 206 |
#define cs_trywait INTcs_trywait |
| 207 |
#define cs_release INTcs_release |
| 208 |
|
| 209 |
#endif /* INTNSAPI */ |
| 210 |
|
| 211 |
#endif /* !BASE_CRIT_H */ |