lock_util.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*-
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * See the file LICENSE for redistribution information.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner * Copyright (c) 1996, 1997, 1998
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Sleepycat Software. All rights reserved.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "config.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#ifndef lint
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic const char sccsid[] = "@(#)lock_util.c 10.10 (Sleepycat) 9/20/98";
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* not lint */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#ifndef NO_SYSTEM_INCLUDES
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <sys/types.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <string.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "db_int.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "shqueue.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "db_page.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "db_shash.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "hash.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "lock.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * __lock_cmp --
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * This function is used to compare a DBT that is about to be entered
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * into a hash table with an object already in the hash table. Note
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * that it just returns true on equal and 0 on not-equal. Therefore
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * this function cannot be used as a sort function; its purpose is to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * be used as a hash comparison function.
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * PUBLIC: int __lock_cmp __P((const DBT *, DB_LOCKOBJ *));
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz */
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainzint
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz__lock_cmp(dbt, lock_obj)
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz const DBT *dbt;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz DB_LOCKOBJ *lock_obj;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz{
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz void *obj_data;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if (lock_obj->type != DB_LOCK_OBJTYPE)
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz return (0);
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz obj_data = SH_DBT_PTR(&lock_obj->lockobj);
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz return (dbt->size == lock_obj->lockobj.size &&
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz memcmp(dbt->data, obj_data, dbt->size) == 0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * PUBLIC: int __lock_locker_cmp __P((u_int32_t, DB_LOCKOBJ *));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin__lock_locker_cmp(locker, lock_obj)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin u_int32_t locker;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin DB_LOCKOBJ *lock_obj;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin void *obj_data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (lock_obj->type != DB_LOCK_LOCKER)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin obj_data = SH_DBT_PTR(&lock_obj->lockobj);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (memcmp(&locker, obj_data, sizeof(u_int32_t)) == 0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The next two functions are the hash functions used to store objects in the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * lock hash table. They are hashing the same items, but one (__lock_ohash)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * takes a DBT (used for hashing a parameter passed from the user) and the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * other (__lock_lhash) takes a DB_LOCKOBJ (used for hashing something that is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * already in the lock manager). In both cases, we have a special check to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * fast path the case where we think we are doing a hash on a DB page/fileid
* pair. If the size is right, then we do the fast hash.
*
* We know that DB uses DB_LOCK_ILOCK types for its lock objects. The first
* four bytes are the 4-byte page number and the next DB_FILE_ID_LEN bytes
* are a unique file id, where the first 4 bytes on UNIX systems are the file
* inode number, and the first 4 bytes on Windows systems are the FileIndexLow
* bytes. So, we use the XOR of the page number and the first four bytes of
* the file id to produce a 32-bit hash value.
*
* We have no particular reason to believe that this algorithm will produce
* a good hash, but we want a fast hash more than we want a good one, when
* we're coming through this code path.
*/
#define FAST_HASH(P) { \
u_int32_t __h; \
u_int8_t *__cp, *__hp; \
__hp = (u_int8_t *)&__h; \
__cp = (u_int8_t *)(P); \
__hp[0] = __cp[0] ^ __cp[4]; \
__hp[1] = __cp[1] ^ __cp[5]; \
__hp[2] = __cp[2] ^ __cp[6]; \
__hp[3] = __cp[3] ^ __cp[7]; \
return (__h); \
}
/*
* __lock_ohash --
*
* PUBLIC: u_int32_t __lock_ohash __P((const DBT *));
*/
u_int32_t
__lock_ohash(dbt)
const DBT *dbt;
{
if (dbt->size == sizeof(DB_LOCK_ILOCK))
FAST_HASH(dbt->data);
return (__ham_func5(dbt->data, dbt->size));
}
/*
* __lock_lhash --
*
* PUBLIC: u_int32_t __lock_lhash __P((DB_LOCKOBJ *));
*/
u_int32_t
__lock_lhash(lock_obj)
DB_LOCKOBJ *lock_obj;
{
u_int32_t tmp;
void *obj_data;
obj_data = SH_DBT_PTR(&lock_obj->lockobj);
if (lock_obj->type == DB_LOCK_LOCKER) {
memcpy(&tmp, obj_data, sizeof(u_int32_t));
return (tmp);
}
if (lock_obj->lockobj.size == sizeof(DB_LOCK_ILOCK))
FAST_HASH(obj_data);
return (__ham_func5(obj_data, lock_obj->lockobj.size));
}
/*
* __lock_locker_hash --
* Hash function for entering lockers into the hash table. Since these
* are simply 32-bit unsigned integers, just return the locker value.
*
* PUBLIC: u_int32_t __lock_locker_hash __P((u_int32_t));
*/
u_int32_t
__lock_locker_hash(locker)
u_int32_t locker;
{
return (locker);
}