8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-29 06:43:03 +01:00
firebird-mirror/src/jrd/GlobalRWLock.h

113 lines
3.0 KiB
C
Raw Normal View History

/*
* PROGRAM: JRD Access Method
* MODULE: GlobalRWLock.h
* DESCRIPTION: Global Read Write Lock
*
* The contents of this file are subject to the Initial
* Developer's 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.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* 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 Nickolay Samofatov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2006 Nickolay Samofatov
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s):
*
* Roman Simakov <roman-simakov@users.sourceforge.net>
*
*/
2009-02-10 09:59:55 +01:00
#ifndef GLOBAL_RW_LOCK
#define GLOBAL_RW_LOCK
#include "../common/classes/alloc.h"
#include "../jrd/jrd.h"
#include "../jrd/lck.h"
#include "../jrd/lck_proto.h"
2012-05-20 12:00:52 +02:00
#include "fb_types.h"
#include "os/pio.h"
#include "../common/classes/condition.h"
//#define COS_DEBUG
#ifdef COS_DEBUG
DEFINE_TRACE_ROUTINE(cos_trace);
#define COS_TRACE(args) cos_trace args
#define COS_TRACE_AST(message) gds__trace(message)
#else
#define COS_TRACE(args) /* nothing */
#define COS_TRACE_AST(message) /* nothing */
#endif
namespace Jrd {
enum lck_t;
2008-04-19 11:42:01 +02:00
class GlobalRWLock : public Firebird::PermanentStorage
{
public:
GlobalRWLock(thread_db* tdbb, MemoryPool& p,
lck_t lckType, bool lock_caching = true,
2014-07-17 20:48:46 +02:00
FB_SIZE_T lockLen = 0, const UCHAR* lockStr = NULL);
2007-05-24 14:49:06 +02:00
virtual ~GlobalRWLock();
2009-03-18 03:43:15 +01:00
// This function returns false if it cannot take the lock
bool lockWrite(thread_db* tdbb, SSHORT wait);
void unlockWrite(thread_db* tdbb, const bool release = false);
bool lockRead(thread_db* tdbb, SSHORT wait, const bool queueJump = false);
void unlockRead(thread_db* tdbb);
bool tryReleaseLock(thread_db* tdbb);
void shutdownLock(thread_db* tdbb);
protected:
// Flag to indicate that somebody is waiting via lock manager.
// If somebody uses lock manager, all concurrent requests should also
// go via lock manager to prevent starvation.
Lock* cachedLock;
// Load the object from shared location.
2009-06-08 11:57:29 +02:00
virtual bool fetch(thread_db* /*tdbb*/) { return true; }
virtual void invalidate(thread_db* /*tdbb*/)
{
fb_assert(readers == 0);
blocking = false;
}
virtual void blockingAstHandler(thread_db* tdbb);
2008-01-16 10:07:24 +01:00
Firebird::Mutex counterMutex; // Protects counter and blocking flag
2011-05-09 12:15:19 +02:00
private:
ULONG pendingLock;
2008-12-05 01:56:15 +01:00
ULONG readers;
Firebird::Condition noReaders; // Semaphore to wait all readers unlock to start relock
ULONG pendingWriters;
bool currentWriter;
Firebird::Condition writerFinished;
2009-04-04 18:39:31 +02:00
// true - unlock keep cached lock and release by AST.
// false - unlock releases cached lock if possible
bool lockCaching;
bool blocking; // Unprocessed AST pending
static int blocking_ast_cached_lock(void* ast_object);
};
2007-05-11 08:44:14 +02:00
}
2009-02-10 09:59:55 +01:00
#endif // GLOBAL_RW_LOCK