mariadb/bdb/mutex/mut_fcntl.c

185 lines
3.9 KiB
C
Raw Normal View History

2001-03-04 19:42:05 -05:00
/*-
* See the file LICENSE for redistribution information.
*
2002-10-30 15:57:05 +04:00
* Copyright (c) 1996-2002
2001-03-04 19:42:05 -05:00
* Sleepycat Software. All rights reserved.
*/
#include "db_config.h"
#ifndef lint
2002-10-30 15:57:05 +04:00
static const char revid[] = "$Id: mut_fcntl.c,v 11.21 2002/05/31 19:37:45 bostic Exp $";
2001-03-04 19:42:05 -05:00
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#endif
#include "db_int.h"
/*
* __db_fcntl_mutex_init --
* Initialize a DB mutex structure.
*
2002-10-30 15:57:05 +04:00
* PUBLIC: int __db_fcntl_mutex_init __P((DB_ENV *, DB_MUTEX *, u_int32_t));
2001-03-04 19:42:05 -05:00
*/
int
__db_fcntl_mutex_init(dbenv, mutexp, offset)
DB_ENV *dbenv;
2002-10-30 15:57:05 +04:00
DB_MUTEX *mutexp;
2001-03-04 19:42:05 -05:00
u_int32_t offset;
{
2002-10-30 15:57:05 +04:00
u_int32_t save;
/*
* 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);
2001-03-04 19:42:05 -05:00
memset(mutexp, 0, sizeof(*mutexp));
2002-10-30 15:57:05 +04:00
F_SET(mutexp, save);
2001-03-04 19:42:05 -05:00
/*
* This is where we decide to ignore locks we don't need to set -- if
* the application is private, we don't need any locks.
*/
if (F_ISSET(dbenv, DB_ENV_PRIVATE)) {
F_SET(mutexp, MUTEX_IGNORE);
return (0);
}
mutexp->off = offset;
2002-10-30 15:57:05 +04:00
#ifdef HAVE_MUTEX_SYSTEM_RESOURCES
2001-03-04 19:42:05 -05:00
mutexp->reg_off = INVALID_ROFF;
#endif
F_SET(mutexp, MUTEX_INITED);
return (0);
}
/*
* __db_fcntl_mutex_lock
* Lock on a mutex, blocking if necessary.
*
2002-10-30 15:57:05 +04:00
* PUBLIC: int __db_fcntl_mutex_lock __P((DB_ENV *, DB_MUTEX *));
2001-03-04 19:42:05 -05:00
*/
int
2002-10-30 15:57:05 +04:00
__db_fcntl_mutex_lock(dbenv, mutexp)
2001-03-04 19:42:05 -05:00
DB_ENV *dbenv;
2002-10-30 15:57:05 +04:00
DB_MUTEX *mutexp;
2001-03-04 19:42:05 -05:00
{
struct flock k_lock;
int locked, ms, waited;
2002-10-30 15:57:05 +04:00
if (F_ISSET(dbenv, DB_ENV_NOLOCKING))
2001-03-04 19:42:05 -05:00
return (0);
/* Initialize the lock. */
k_lock.l_whence = SEEK_SET;
k_lock.l_start = mutexp->off;
k_lock.l_len = 1;
for (locked = waited = 0;;) {
/*
* Wait for the lock to become available; wait 1ms initially,
* up to 1 second.
*/
for (ms = 1; mutexp->pid != 0;) {
waited = 1;
__os_yield(NULL, ms * USEC_PER_MS);
if ((ms <<= 1) > MS_PER_SEC)
ms = MS_PER_SEC;
}
/* Acquire an exclusive kernel lock. */
k_lock.l_type = F_WRLCK;
2002-10-30 15:57:05 +04:00
if (fcntl(dbenv->lockfhp->fd, F_SETLKW, &k_lock))
2001-03-04 19:42:05 -05:00
return (__os_get_errno());
/* If the resource is still available, it's ours. */
if (mutexp->pid == 0) {
locked = 1;
2002-10-30 15:57:05 +04:00
__os_id(&mutexp->pid);
2001-03-04 19:42:05 -05:00
}
/* Release the kernel lock. */
k_lock.l_type = F_UNLCK;
2002-10-30 15:57:05 +04:00
if (fcntl(dbenv->lockfhp->fd, F_SETLK, &k_lock))
2001-03-04 19:42:05 -05:00
return (__os_get_errno());
/*
* If we got the resource lock 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;
}
if (waited)
++mutexp->mutex_set_wait;
else
++mutexp->mutex_set_nowait;
return (0);
}
/*
* __db_fcntl_mutex_unlock --
* Release a lock.
*
2002-10-30 15:57:05 +04:00
* PUBLIC: int __db_fcntl_mutex_unlock __P((DB_ENV *, DB_MUTEX *));
2001-03-04 19:42:05 -05:00
*/
int
__db_fcntl_mutex_unlock(dbenv, mutexp)
DB_ENV *dbenv;
2002-10-30 15:57:05 +04:00
DB_MUTEX *mutexp;
2001-03-04 19:42:05 -05:00
{
2002-10-30 15:57:05 +04:00
if (F_ISSET(dbenv, DB_ENV_NOLOCKING))
2001-03-04 19:42:05 -05:00
return (0);
#ifdef DIAGNOSTIC
#define MSG "mutex_unlock: ERROR: released lock that was unlocked\n"
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
if (mutexp->pid == 0)
write(STDERR_FILENO, MSG, sizeof(MSG) - 1);
#endif
/*
* Release the resource. We don't have to acquire any locks because
* processes trying to acquire the lock are checking for a pid set to
* 0/non-0, not to any specific value.
*/
mutexp->pid = 0;
return (0);
}
/*
* __db_fcntl_mutex_destroy --
2002-10-30 15:57:05 +04:00
* Destroy a DB_MUTEX.
2001-03-04 19:42:05 -05:00
*
2002-10-30 15:57:05 +04:00
* PUBLIC: int __db_fcntl_mutex_destroy __P((DB_MUTEX *));
2001-03-04 19:42:05 -05:00
*/
int
__db_fcntl_mutex_destroy(mutexp)
2002-10-30 15:57:05 +04:00
DB_MUTEX *mutexp;
2001-03-04 19:42:05 -05:00
{
COMPQUIET(mutexp, NULL);
return (0);
}