41 #include "boost/bind.hpp"
42 #include "boost/format.hpp"
52 namespace ex = lsst::pex::exceptions;
55 namespace lsst {
namespace ap {
namespace detail {
72 template class ChunkRef<detail::ObjAllocator, Object>;
98 BootstrapLock::BootstrapLock(
char const *
const name) :
_name(name), _fd(-1) {
100 assert(name != 0 && name[0] ==
'/' &&
"BootstrapLock: illegal name");
107 while ((fd = ::shm_open(name, O_RDONLY | O_CREAT | O_EXCL, S_IRUSR | S_IRGRP | S_IROTH)) == -1) {
110 (
boost::format(
"Unable to obtain shared memory bootstrap lock %1%") % name).str());
128 template <
typename ManagerT>
129 ManagerT *
getSingleton(
char const *
const shmObjName,
char const *
const shmLockName) {
132 static ManagerT * singleton = 0;
135 if (singleton != 0) {
142 std::size_t
const pageSize =
static_cast<std::size_t
>(::getpagesize());
143 std::size_t
const numBytes = (ManagerT::size() + pageSize - 1) & ~(pageSize - 1);
146 int fd = ::shm_open(shmObjName, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
151 if (errno != EEXIST) {
153 (
boost::format(
"shm_open(): failed to create shared memory object %1%, errno: %2%")
154 % shmObjName % errno).str());
157 fd = ::shm_open(shmObjName, O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
160 (
boost::format(
"shm_open(): failed to open existing shared memory object %1%, errno: %2%")
161 % shmObjName % errno).str());
165 void * mem = ::mmap(0, numBytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
168 (
boost::format(
"mmap(): failed to map %1% bytes of shared memory object %2%, errno: %3%")
169 % numBytes % shmObjName % errno).str());
171 singleton =
static_cast<ManagerT *
>(mem);
177 ScopeGuard g1(boost::bind(::shm_unlink, shmObjName));
181 if (::ftruncate(fd, numBytes) != 0) {
184 "ftruncate(): failed to set size of shared memory object %1% to %2% bytes, errno: %3%")
185 % shmObjName % numBytes % errno).str());
189 void * mem = ::mmap(0, numBytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
192 (
boost::format(
"mmap(): failed to map %1% bytes of shared memory object %2%, errno: %3%")
193 % numBytes % shmObjName % errno).str());
195 ScopeGuard g3(boost::bind(::munmap, mem, numBytes));
197 new (mem) ManagerT();
198 singleton =
static_cast<ManagerT *
>(mem);
207 ::mlock(static_cast<void *>(singleton), numBytes);
217 #if defined(__GNUC__) && __GNUC__ > 3
218 # pragma GCC visibility push(hidden)
220 template ObjChunkMgr * getSingleton<ObjChunkMgr>(
char const *
const,
char const *
const);
223 #if defined(__GNUC__) && __GNUC__ > 3
224 # pragma GCC visibility pop
231 static char const *
const sSharedObjLock =
"/ap_shlck";
232 static char const *
const sSharedPrefix =
"/ap_";
241 std::string actualName(sSharedPrefix);
243 return detail::getSingleton<detail::ObjChunkMgr>(actualName.c_str(), sSharedObjLock);
252 std::string actualName(sSharedPrefix);
254 int res = ::shm_unlink(actualName.c_str());
258 if (res != 0 && errno != ENOENT && errno != EINVAL) {
260 (
boost::format(
"shm_unlink(): failed to unlink shared memory object %1%, errno: %2%")
261 % name % errno).str());
ManagerT * getSingleton(char const *const shmObjName, char const *const shmLockName)
Class for managing chunks of Object instances in shared memory.
A manager for a set of chunks of a single type.
Wraps the C library timespec struct.
Helper class for managing chunks of a particular type.
std::string const & _name
Utility class for automatically invoking a function when leaving a scope.
SharedObjectChunkManager(std::string const &name)
A thread-safe memory block allocator that uses a Bitset to track which blocks (out of a fixed size po...
ChunkManagerImpl< SharedMutex, Object > ObjChunkMgr
static std::size_t size()
Returns the size in bytes of the underlying chunk manager and pool of memory blocks.
Mutual exclusion lock suitable for initializing shared memory objects.
#define LSST_EXCEPT(type,...)
A wrapper for a process private POSIX mutual exclusion lock.
Chunk manager helper class implementations.
BlockAllocator< SharedMutex, Object > ObjAllocator
static void destroyInstance(std::string const &name)
static std::size_t size()
Chunk class implementation.
static Manager * instance(std::string const &name)
Grants access to a mutex, enforcing the RAII principle.
Include files required for standard LSST Exception handling.
Class and helper functions related to spatial partitioning.
Utility class for automatically invoking a function when leaving a scope.