8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-31 06:43:02 +01:00
firebird-mirror/src/jrd/trace/TraceConfigStorage.h
2010-10-12 08:02:57 +00:00

164 lines
3.3 KiB
C++

/*
* PROGRAM: Firebird Trace Services
* MODULE: TraceConfigStorage.h
* DESCRIPTION: Trace API shared configurations storage
*
* 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 Khorsun Vladyslav
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2008 Khorsun Vladyslav <hvlad@users.sourceforge.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*/
#ifndef JRD_TRACECONFIGSTORAGE_H
#define JRD_TRACECONFIGSTORAGE_H
#include "../../common/classes/array.h"
#include "../../common/classes/fb_string.h"
#include "../../common/classes/init.h"
#include "../../common/isc_s_proto.h"
#include "../../jrd/trace/TraceSession.h"
namespace Jrd {
struct TraceCSHeader : public MemoryHeader
{
volatile ULONG change_number;
volatile ULONG session_number;
ULONG cnt_uses;
char cfg_file_name[MAXPATHLEN];
};
class ConfigStorage : public Firebird::GlobalStorage, public SharedMemory<TraceCSHeader>
{
public:
ConfigStorage();
~ConfigStorage();
void addSession(Firebird::TraceSession& session);
bool getNextSession(Firebird::TraceSession& session);
void removeSession(ULONG id);
void restart();
void updateSession(Firebird::TraceSession& session);
ULONG getChangeNumber() const
{ return sh_mem_header ? sh_mem_header->change_number : 0; }
void acquire();
void release();
private:
void mutexBug(int osErrorCode, const char* text);
bool initialize(bool);
void checkFile();
void checkDirty()
{
if (m_dirty)
{
//_commit(m_cfg_file);
m_dirty = false;
}
}
void setDirty()
{
if (!m_dirty)
{
sh_mem_header->change_number++;
m_dirty = true;
}
}
// items in every session record at sessions file
enum ITEM
{
tagID = 1, // session ID
tagName, // session Name
tagUserName, // creator user name
tagFlags, // session flags
tagConfig, // configuration
tagStartTS, // date+time when started
tagLogFile, // log file name, if any
tagEnd
};
void putItem(ITEM tag, ULONG len, const void* data);
bool getItemLength(ITEM& tag, ULONG& len);
int m_cfg_file;
bool m_dirty;
};
class StorageInstance : private Firebird::InstanceControl
{
private:
Firebird::Mutex initMtx;
ConfigStorage* storage;
public:
void dtor()
{
delete storage;
storage = 0;
}
StorageInstance() :
storage(NULL)
{}
ConfigStorage* getStorage()
{
if (!storage)
{
Firebird::MutexLockGuard guard(initMtx);
if (!storage)
{
storage = new ConfigStorage;
new Firebird::InstanceControl::InstanceLink<StorageInstance>(this);
}
}
return storage;
}
};
class StorageGuard
{
public:
explicit StorageGuard(ConfigStorage* storage) :
m_storage(storage)
{
m_storage->acquire();
}
~StorageGuard()
{
m_storage->release();
}
private:
ConfigStorage* m_storage;
};
}
#endif