From d2db11479fee516433ec36d206adc17b5d40cbcf Mon Sep 17 00:00:00 2001 From: kishoramballi Date: Mon, 11 May 2009 11:52:48 +0000 Subject: [PATCH] version 1.0 --- src/storage/Util.cxx | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/storage/Util.cxx diff --git a/src/storage/Util.cxx b/src/storage/Util.cxx new file mode 100644 index 00000000..cbf61164 --- /dev/null +++ b/src/storage/Util.cxx @@ -0,0 +1,53 @@ +#include +#include + +DbRetVal GlobalUniqueID::create() +{ + int key = Conf::config.getShmIDKey(); + int id = os::shm_create(key, MAX_UNIQUE_ID *sizeof(int), 0666); + if (-1 == id) { + printError(ErrOS, "Unable to create shared memory"); + return ErrOS; + } + ptr = os::shm_attach(id, NULL, 0); + if ((void*)-1 == ptr) { + printError(ErrOS, "Unable to attach shared memory"); + return ErrOS; + } + memset(ptr, 0, MAX_UNIQUE_ID *sizeof(int)); +} + +DbRetVal GlobalUniqueID::open() +{ + int key = Conf::config.getShmIDKey(); + int id = os::shm_open(key, MAX_UNIQUE_ID *sizeof(int), 0666); + if (-1 == id) { + printError(ErrOS, "Unable to create shared memory"); + } + ptr = os::shm_attach(id, NULL, 0); + if ((void*)-1 == ptr) { + printError(ErrOS, "Unable to create shared memory"); + } + return OK; +} + +DbRetVal GlobalUniqueID::destroy() +{ + int key = Conf::config.getShmIDKey(); + int id = os::shm_open(key, MAX_UNIQUE_ID *sizeof(int), 0666); + if (-1 == id) { + printError(ErrOS, "Unable to open shared memory"); + return ErrOS; + } + os::shmctl(id, IPC_RMID); +} + +int GlobalUniqueID::getID(UniqueIDType type) +{ + int *id = (int*)(((char*)ptr) + sizeof(int) * type); + int oldVal = *id; + int ret = Mutex::CAS(id, oldVal, oldVal+1); + if (ret) return -1; + return *id; +} + -- 2.11.4.GIT