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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1999-2002
* Sleepycat Software. All rights reserved.
*/
#include "db_config.h"
#ifndef lint
static const char revid[] = "$Id: mut_pthread.c,v 1.1.1.1 2003/11/20 22:13:37 toshok Exp $";
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#endif
#include "db_int.h"
#ifdef DIAGNOSTIC
#undef MSG1
#define MSG1 "mutex_lock: ERROR: lock currently in use: pid: %lu.\n"
#undef MSG2
#define MSG2 "mutex_unlock: ERROR: lock already unlocked\n"
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
#endif
#ifdef HAVE_MUTEX_SOLARIS_LWP
#define pthread_cond_signal _lwp_cond_signal
#define pthread_cond_wait _lwp_cond_wait
#define pthread_mutex_lock _lwp_mutex_lock
#define pthread_mutex_trylock _lwp_mutex_trylock
#define pthread_mutex_unlock _lwp_mutex_unlock
/*
* _lwp_self returns the LWP process ID which isn't a unique per-thread
* identifier. Use pthread_self instead, it appears to work even if we
* are not a pthreads application.
*/
#define pthread_mutex_destroy(x) 0
#endif
#ifdef HAVE_MUTEX_UI_THREADS
#define pthread_cond_signal cond_signal
#define pthread_cond_wait cond_wait
#define pthread_mutex_lock mutex_lock
#define pthread_mutex_trylock mutex_trylock
#define pthread_mutex_unlock mutex_unlock
#define pthread_self thr_self
#define pthread_mutex_destroy mutex_destroy
#endif
#define PTHREAD_UNLOCK_ATTEMPTS 5
/*
* __db_pthread_mutex_init --
* Initialize a DB_MUTEX.
*
* PUBLIC: int __db_pthread_mutex_init __P((DB_ENV *, DB_MUTEX *, u_int32_t));
*/
int
__db_pthread_mutex_init(dbenv, mutexp, flags)
DB_ENV *dbenv;
DB_MUTEX *mutexp;
u_int32_t flags;
{
u_int32_t save;
int ret;
ret = 0;
/*
* The only setting/checking of the MUTEX_MPOOL flags is in the mutex
* mutex allocation code (__db_mutex_alloc/free). Preserve only that
* flag. This is safe because even if this flag was never explicitly
* set, but happened to be set in memory, it will never be checked or
* acted upon.
*/
save = F_ISSET(mutexp, MUTEX_MPOOL);
memset(mutexp, 0, sizeof(*mutexp));
F_SET(mutexp, save);
/*
* If this is a thread lock or the process has told us that there are
* no other processes in the environment, use thread-only locks, they
* are faster in some cases.
*
* This is where we decide to ignore locks we don't need to set -- if
* the application isn't threaded, there aren't any threads to block.
*/
if (LF_ISSET(MUTEX_THREAD) || F_ISSET(dbenv, DB_ENV_PRIVATE)) {
if (!F_ISSET(dbenv, DB_ENV_THREAD)) {
F_SET(mutexp, MUTEX_IGNORE);
return (0);
}
}
#ifdef HAVE_MUTEX_PTHREADS
{
pthread_condattr_t condattr, *condattrp = NULL;
pthread_mutexattr_t mutexattr, *mutexattrp = NULL;
if (!LF_ISSET(MUTEX_THREAD)) {
ret = pthread_mutexattr_init(&mutexattr);
#ifndef HAVE_MUTEX_THREAD_ONLY
if (ret == 0)
ret = pthread_mutexattr_setpshared(
&mutexattr, PTHREAD_PROCESS_SHARED);
#endif
mutexattrp = &mutexattr;
}
if (ret == 0)
ret = pthread_mutex_init(&mutexp->mutex, mutexattrp);
if (mutexattrp != NULL)
pthread_mutexattr_destroy(mutexattrp);
if (ret == 0 && LF_ISSET(MUTEX_SELF_BLOCK)) {
if (!LF_ISSET(MUTEX_THREAD)) {
ret = pthread_condattr_init(&condattr);
#ifndef HAVE_MUTEX_THREAD_ONLY
if (ret == 0) {
condattrp = &condattr;
ret = pthread_condattr_setpshared(
&condattr, PTHREAD_PROCESS_SHARED);
}
#endif
}
if (ret == 0)
ret = pthread_cond_init(&mutexp->cond, condattrp);
F_SET(mutexp, MUTEX_SELF_BLOCK);
if (condattrp != NULL)
(void)pthread_condattr_destroy(condattrp);
}
}
#endif
#ifdef HAVE_MUTEX_SOLARIS_LWP
/*
* XXX
* Gcc complains about missing braces in the static initializations of
* lwp_cond_t and lwp_mutex_t structures because the structures contain
* sub-structures/unions and the Solaris include file that defines the
* initialization values doesn't have surrounding braces. There's not
* much we can do.
*/
if (LF_ISSET(MUTEX_THREAD)) {
static lwp_mutex_t mi = DEFAULTMUTEX;
mutexp->mutex = mi;
} else {
static lwp_mutex_t mi = SHAREDMUTEX;
mutexp->mutex = mi;
}
if (LF_ISSET(MUTEX_SELF_BLOCK)) {
if (LF_ISSET(MUTEX_THREAD)) {
static lwp_cond_t ci = DEFAULTCV;
mutexp->cond = ci;
} else {
static lwp_cond_t ci = SHAREDCV;
mutexp->cond = ci;
}
F_SET(mutexp, MUTEX_SELF_BLOCK);
}
#endif
#ifdef HAVE_MUTEX_UI_THREADS
{
int type;
type = LF_ISSET(MUTEX_THREAD) ? USYNC_THREAD : USYNC_PROCESS;
ret = mutex_init(&mutexp->mutex, type, NULL);
if (ret == 0 && LF_ISSET(MUTEX_SELF_BLOCK)) {
ret = cond_init(&mutexp->cond, type, NULL);
F_SET(mutexp, MUTEX_SELF_BLOCK);
}}
#endif
mutexp->spins = __os_spin(dbenv);
#ifdef HAVE_MUTEX_SYSTEM_RESOURCES
mutexp->reg_off = INVALID_ROFF;
#endif
if (ret == 0)
F_SET(mutexp, MUTEX_INITED);
else
__db_err(dbenv,
"unable to initialize mutex: %s", strerror(ret));
return (ret);
}
/*
* __db_pthread_mutex_lock
* Lock on a mutex, logically blocking if necessary.
*
* PUBLIC: int __db_pthread_mutex_lock __P((DB_ENV *, DB_MUTEX *));
*/
int
__db_pthread_mutex_lock(dbenv, mutexp)
DB_ENV *dbenv;
DB_MUTEX *mutexp;
{
u_int32_t nspins;
int i, ret, waited;
if (F_ISSET(dbenv, DB_ENV_NOLOCKING) || F_ISSET(mutexp, MUTEX_IGNORE))
return (0);
/* Attempt to acquire the resource for N spins. */
for (nspins = mutexp->spins; nspins > 0; --nspins)
if (pthread_mutex_trylock(&mutexp->mutex) == 0)
break;
if (nspins == 0 && (ret = pthread_mutex_lock(&mutexp->mutex)) != 0)
goto err;
if (F_ISSET(mutexp, MUTEX_SELF_BLOCK)) {
for (waited = 0; mutexp->locked != 0; waited = 1) {
ret = pthread_cond_wait(&mutexp->cond, &mutexp->mutex);
/*
* !!!
* Solaris bug workaround:
* pthread_cond_wait() sometimes returns ETIME -- out
* of sheer paranoia, check both ETIME and ETIMEDOUT.
* We believe this happens when the application uses
* SIGALRM for some purpose, e.g., the C library sleep
* call, and Solaris delivers the signal to the wrong
* LWP.
*/
if (ret != 0 && ret != EINTR &&
#ifdef ETIME
ret != ETIME &&
#endif
ret != ETIMEDOUT) {
(void)pthread_mutex_unlock(&mutexp->mutex);
return (ret);
}
}
if (waited)
++mutexp->mutex_set_wait;
else
++mutexp->mutex_set_nowait;
#ifdef DIAGNOSTIC
mutexp->locked = (u_int32_t)pthread_self();
#else
mutexp->locked = 1;
#endif
/*
* According to HP-UX engineers contacted by Netscape,
* pthread_mutex_unlock() will occasionally return EFAULT
* for no good reason on mutexes in shared memory regions,
* and the correct caller behavior is to try again. Do
* so, up to PTHREAD_UNLOCK_ATTEMPTS consecutive times.
* Note that we don't bother to restrict this to HP-UX;
* it should be harmless elsewhere. [#2471]
*/
i = PTHREAD_UNLOCK_ATTEMPTS;
do {
ret = pthread_mutex_unlock(&mutexp->mutex);
} while (ret == EFAULT && --i > 0);
if (ret != 0)
goto err;
} else {
if (nspins == mutexp->spins)
++mutexp->mutex_set_nowait;
else if (nspins > 0) {
++mutexp->mutex_set_spin;
mutexp->mutex_set_spins += mutexp->spins - nspins;
} else
++mutexp->mutex_set_wait;
#ifdef DIAGNOSTIC
if (mutexp->locked) {
char msgbuf[128];
(void)snprintf(msgbuf,
sizeof(msgbuf), MSG1, (u_long)mutexp->locked);
(void)write(STDERR_FILENO, msgbuf, strlen(msgbuf));
}
mutexp->locked = (u_int32_t)pthread_self();
#else
mutexp->locked = 1;
#endif
}
return (0);
err: __db_err(dbenv, "unable to lock mutex: %s", strerror(ret));
return (ret);
}
/*
* __db_pthread_mutex_unlock --
* Release a lock.
*
* PUBLIC: int __db_pthread_mutex_unlock __P((DB_ENV *, DB_MUTEX *));
*/
int
__db_pthread_mutex_unlock(dbenv, mutexp)
DB_ENV *dbenv;
DB_MUTEX *mutexp;
{
int i, ret;
if (F_ISSET(dbenv, DB_ENV_NOLOCKING) || F_ISSET(mutexp, MUTEX_IGNORE))
return (0);
#ifdef DIAGNOSTIC
if (!mutexp->locked)
(void)write(STDERR_FILENO, MSG2, sizeof(MSG2) - 1);
#endif
if (F_ISSET(mutexp, MUTEX_SELF_BLOCK)) {
if ((ret = pthread_mutex_lock(&mutexp->mutex)) != 0)
goto err;
mutexp->locked = 0;
if ((ret = pthread_cond_signal(&mutexp->cond)) != 0)
return (ret);
} else
mutexp->locked = 0;
/* See comment above; workaround for [#2471]. */
i = PTHREAD_UNLOCK_ATTEMPTS;
do {
ret = pthread_mutex_unlock(&mutexp->mutex);
} while (ret == EFAULT && --i > 0);
return (ret);
err: __db_err(dbenv, "unable to unlock mutex: %s", strerror(ret));
return (ret);
}
/*
* __db_pthread_mutex_destroy --
* Destroy a DB_MUTEX.
*
* PUBLIC: int __db_pthread_mutex_destroy __P((DB_MUTEX *));
*/
int
__db_pthread_mutex_destroy(mutexp)
DB_MUTEX *mutexp;
{
int ret;
if (F_ISSET(mutexp, MUTEX_IGNORE))
return (0);
if ((ret = pthread_mutex_destroy(&mutexp->mutex)) != 0)
__db_err(NULL, "unable to destroy mutex: %s", strerror(ret));
return (ret);
}
|