8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-28 02:43:03 +01:00
firebird-mirror/src/jrd/rlck.cpp

152 lines
4.4 KiB
C++
Raw Normal View History

2001-05-23 15:26:42 +02:00
/*
* PROGRAM: JRD Access Method
* MODULE: rlck.cpp
2001-05-23 15:26:42 +02:00
* DESCRIPTION: Record and Relation Lock Manager
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
* 2001.07.06 Sean Leyne - Code Cleanup, removed "#ifdef READONLY_DATABASE"
* conditionals, as the engine now fully supports
* readonly databases.
2001-05-23 15:26:42 +02:00
*/
#include "firebird.h"
2004-03-22 12:38:23 +01:00
#include "../jrd/common.h"
2001-05-23 15:26:42 +02:00
#include "../jrd/tra.h"
#include "../jrd/lck.h"
#include "../jrd/err_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/rlck_proto.h"
using namespace Jrd;
using namespace Firebird;
Lock* RLCK_reserve_relation(thread_db* tdbb,
2008-01-16 10:48:41 +01:00
jrd_tra* transaction,
jrd_rel* relation,
bool write_flag)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* R L C K _ r e s e r v e _ r e l a t i o n
*
**************************************
*
* Functional description
* Lock a relation within a transaction. If the relation
* is already locked at a lower level, upgrade the lock.
*
**************************************/
2008-01-16 10:48:41 +01:00
SET_TDBB(tdbb);
2001-05-23 15:26:42 +02:00
if (transaction->tra_flags & TRA_system)
return NULL;
if (write_flag && (tdbb->getDatabase()->dbb_flags & DBB_read_only))
ERR_post(Arg::Gds(isc_read_only_database));
2001-05-23 15:26:42 +02:00
if (write_flag && (transaction->tra_flags & TRA_readonly))
ERR_post(Arg::Gds(isc_read_only_trans));
2008-01-16 10:48:41 +01:00
Lock* lock = RLCK_transaction_relation_lock(tdbb, transaction, relation);
// Next, figure out what kind of lock we need
USHORT level;
2001-05-23 15:26:42 +02:00
if (transaction->tra_flags & TRA_degree3) {
if (write_flag)
level = LCK_EX;
else
level = LCK_PR;
}
else {
if (write_flag)
level = LCK_SW;
else
level = LCK_none;
}
2008-01-16 10:48:41 +01:00
// If the lock is already "good enough", we're done
2001-05-23 15:26:42 +02:00
if (level <= lock->lck_logical)
return lock;
2008-01-16 10:48:41 +01:00
// Get lock
2003-12-31 06:36:12 +01:00
USHORT result;
2001-05-23 15:26:42 +02:00
if (lock->lck_logical)
result = LCK_convert(tdbb, lock, level, transaction->getLockWait());
2001-05-23 15:26:42 +02:00
else
result = LCK_lock(tdbb, lock, level, transaction->getLockWait());
2008-01-16 10:48:41 +01:00
if (!result)
ERR_punt();
return lock;
2001-05-23 15:26:42 +02:00
}
2008-01-16 10:48:41 +01:00
Lock* RLCK_transaction_relation_lock(thread_db* tdbb,
jrd_tra* transaction,
jrd_rel* relation)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* R L C K _ t r a n s a c t i o n _ r e l a t i o n _ l o c k
*
**************************************
*
* Functional description
* Take out a relation lock within the context of
2001-05-23 15:26:42 +02:00
* a transaction.
*
**************************************/
2008-01-16 10:48:41 +01:00
SET_TDBB(tdbb);
Lock* lock;
vec<Lock*>* vector = transaction->tra_relation_locks;
2008-12-22 10:00:05 +01:00
if (vector && (relation->rel_id < vector->count()) && (lock = (*vector)[relation->rel_id]))
2003-12-31 06:36:12 +01:00
{
2001-05-23 15:26:42 +02:00
return lock;
2003-12-31 06:36:12 +01:00
}
vector = transaction->tra_relation_locks =
vec<Lock*>::newVector(*transaction->tra_pool, transaction->tra_relation_locks,
relation->rel_id + 1);
2008-01-16 10:48:41 +01:00
const SSHORT relLockLen = relation->getRelLockKeyLength();
lock = FB_NEW_RPT(*transaction->tra_pool, relLockLen) Lock();
2008-01-16 13:22:11 +01:00
lock->lck_dbb = tdbb->getDatabase();
2006-05-22 00:07:35 +02:00
lock->lck_length = relLockLen;
relation->getRelLockKey(tdbb, &lock->lck_key.lck_string[0]);
2001-05-23 15:26:42 +02:00
lock->lck_type = LCK_relation;
lock->lck_owner_handle = LCK_get_owner_handle(tdbb, lock->lck_type);
2008-01-16 13:22:11 +01:00
lock->lck_parent = tdbb->getDatabase()->dbb_lock;
2008-01-16 10:48:41 +01:00
// the lck_object is used here to find the relation
// block from the lock block
lock->lck_object = relation;
2008-01-16 10:48:41 +01:00
// enter all relation locks into the intra-process lock manager and treat
// them as compatible within the attachment according to IPLM rules
2008-01-16 13:22:11 +01:00
lock->lck_compatible = tdbb->getAttachment();
2008-01-16 10:48:41 +01:00
// for relations locked within a transaction, add a second level of
// compatibility within the intra-process lock manager which specifies
// that relation locks are incompatible with locks taken out by other
// transactions, if a transaction is specified
lock->lck_compatible2 = transaction;
(*vector)[relation->rel_id] = lock;
2001-05-23 15:26:42 +02:00
return lock;
}