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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1996, 1997
* Sleepycat Software. All rights reserved.
*/
#include "config.h"
#ifndef lint
static const char sccsid[] = "@(#)mutex.c 10.32 (Sleepycat) 1/16/98";
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#endif
#include "db_int.h"
#include "common_ext.h"
#ifdef HAVE_SPINLOCKS
#ifdef HAVE_FUNC_AIX
#define TSL_INIT(x)
#define TSL_SET(x) (!_check_lock(x, 0, 1))
#define TSL_UNSET(x) _clear_lock(x, 0)
#endif
#ifdef HAVE_ASSEM_MC68020_GCC
#include "68020.gcc"
#endif
#if defined(HAVE_FUNC_MSEM)
/*
* XXX
* Should we not use MSEM_IF_NOWAIT and let the system block for us?
* I've no idea if this will block all threads in the process or not.
*/
#define TSL_INIT(x) msem_init(x, MSEM_UNLOCKED)
#define TSL_SET(x) (!msem_lock(x, MSEM_IF_NOWAIT))
#define TSL_UNSET(x) msem_unlock(x, 0)
#endif
#ifdef HAVE_FUNC_SGI
#define TSL_INIT(x) init_lock(x)
#define TSL_SET(x) (!acquire_lock(x))
#define TSL_UNSET(x) release_lock(x)
#endif
#ifdef HAVE_FUNC_SOLARIS
/*
* Semaphore calls don't work on Solaris 5.5.
*
* #define TSL_INIT(x) sema_init(x, 1, USYNC_PROCESS, NULL)
* #define TSL_SET(x) (sema_wait(x) == 0)
* #define TSL_UNSET(x) sema_post(x)
*/
#define TSL_INIT(x)
#define TSL_SET(x) (_lock_try(x))
#define TSL_UNSET(x) _lock_clear(x)
#endif
#ifdef HAVE_ASSEM_SCO_CC
#include "sco.cc"
#endif
#ifdef HAVE_ASSEM_SPARC_GCC
#include "sparc.gcc"
#endif
#ifdef HAVE_ASSEM_UTS4_CC
#define TSL_INIT(x)
#define TSL_SET(x) (!uts_lock(x, 1))
#define TSL_UNSET(x) (*(x) = 0)
#endif
#ifdef HAVE_ASSEM_X86_GCC
#include "x86.gcc"
#endif
#if defined(_WIN32)
/* DBDB this needs to be byte-aligned!! */
#define TSL_INIT(tsl)
#define TSL_SET(tsl) (!InterlockedExchange((PLONG)tsl, 1))
#define TSL_UNSET(tsl) (*(tsl) = 0)
#endif
#ifdef macintosh
/* Mac spinlocks are simple because we cannot possibly be preempted. */
#define TSL_INIT(tsl)
#define TSL_SET(tsl) (*(tsl) = 1)
#define TSL_UNSET(tsl) (*(tsl) = 0)
#endif
#endif /* HAVE_SPINLOCKS */
/*
* __db_mutex_init --
* Initialize a DB mutex structure.
*
* PUBLIC: void __db_mutex_init __P((db_mutex_t *, u_int32_t));
*/
void
__db_mutex_init(mp, off)
db_mutex_t *mp;
u_int32_t off;
{
#ifdef DEBUG
if ((ALIGNTYPE)mp & (MUTEX_ALIGNMENT - 1)) {
(void)fprintf(stderr,
"MUTEX ERROR: mutex NOT %d-byte aligned!\n",
MUTEX_ALIGNMENT);
abort();
}
#endif
memset(mp, 0, sizeof(db_mutex_t));
#ifdef HAVE_SPINLOCKS
COMPQUIET(off, 0);
TSL_INIT(&mp->tsl_resource);
mp->spins = __os_spin();
#else
mp->off = off;
#endif
}
#define MS(n) ((n) * 1000) /* Milliseconds to micro-seconds. */
#define SECOND (MS(1000)) /* A second's worth of micro-seconds. */
/*
* __db_mutex_lock
* Lock on a mutex, logically blocking if necessary.
*
* PUBLIC: int __db_mutex_lock __P((db_mutex_t *, int));
*/
int
__db_mutex_lock(mp, fd)
db_mutex_t *mp;
int fd;
{
u_long usecs;
#ifdef HAVE_SPINLOCKS
int nspins;
COMPQUIET(fd, 0);
for (usecs = MS(10);;) {
/* Try and acquire the uncontested resource lock for N spins. */
for (nspins = mp->spins; nspins > 0; --nspins)
if (TSL_SET(&mp->tsl_resource)) {
#ifdef DEBUG
if (mp->pid != 0) {
(void)fprintf(stderr,
"MUTEX ERROR: __db_mutex_lock: lock currently locked\n");
abort();
}
mp->pid = getpid();
#endif
if (usecs == MS(10))
++mp->mutex_set_nowait;
else
++mp->mutex_set_wait;
return (0);
}
/* Yield the processor; wait 10ms initially, up to 1 second. */
if (__db_yield == NULL || __db_yield() != 0) {
(void)__db_sleep(0, usecs);
if ((usecs <<= 1) > SECOND)
usecs = SECOND;
}
}
/* NOTREACHED */
#else /* !HAVE_SPINLOCKS */
struct flock k_lock;
pid_t mypid;
int locked;
/* Initialize the lock. */
k_lock.l_whence = SEEK_SET;
k_lock.l_start = mp->off;
k_lock.l_len = 1;
for (locked = 0, mypid = getpid();;) {
/*
* Wait for the lock to become available; wait 10ms initially,
* up to 1 second.
*/
for (usecs = MS(10); mp->pid != 0;)
if (__db_yield == NULL || __db_yield() != 0) {
(void)__db_sleep(0, usecs);
if ((usecs <<= 1) > SECOND)
usecs = SECOND;
}
/* Acquire an exclusive kernel lock. */
k_lock.l_type = F_WRLCK;
if (fcntl(fd, F_SETLKW, &k_lock))
return (errno);
/* If the resource tsl is still available, it's ours. */
if (mp->pid == 0) {
locked = 1;
mp->pid = mypid;
}
/* Release the kernel lock. */
k_lock.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &k_lock))
return (errno);
/*
* If we got the resource tsl we're done.
*
* !!!
* We can't check to see if the lock is ours, because we may
* be trying to block ourselves in the lock manager, and so
* the holder of the lock that's preventing us from getting
* the lock may be us! (Seriously.)
*/
if (locked)
break;
}
return (0);
#endif /* !HAVE_SPINLOCKS */
}
/*
* __db_mutex_unlock --
* Release a lock.
*
* PUBLIC: int __db_mutex_unlock __P((db_mutex_t *, int));
*/
int
__db_mutex_unlock(mp, fd)
db_mutex_t *mp;
int fd;
{
#ifdef DEBUG
if (mp->pid == 0) {
(void)fprintf(stderr,
"MUTEX ERROR: __db_mutex_unlock: lock already unlocked\n");
abort();
}
#endif
#ifdef HAVE_SPINLOCKS
COMPQUIET(fd, 0);
#ifdef DEBUG
mp->pid = 0;
#endif
/* Release the resource tsl. */
TSL_UNSET(&mp->tsl_resource);
#else
/*
* Release the resource tsl. We don't have to acquire any locks
* because processes trying to acquire the lock are checking for
* a pid of 0, not a specific value.
*/
mp->pid = 0;
#endif
return (0);
}
|