52 #include <sys/posix_shm.h>
64#elif defined(__APPLE__)
66#elif defined(NAME_MAX)
69 #error Unsupported platform
105 _mutex = CreateMutexA(NULL, FALSE, name.c_str());
111 int fd = shm_open(name.c_str(), O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
118 fd = shm_open(name.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
121 throw cbeam::error::system_error(
"cbeam::concurrency::named_recursive_mutex: Failed to open existing shared memory: " + name);
133 if (ftruncate(fd,
sizeof(pthread_mutex_t)) == -1)
140 void* addr = mmap(NULL,
sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
141 if (addr == MAP_FAILED)
149 _mutex =
reinterpret_cast<pthread_mutex_t*
>(addr);
151 pthread_mutexattr_t attr;
152 pthread_mutexattr_init(&attr);
153 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
154 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
156 if (pthread_mutex_init(_mutex, &attr) != 0)
158 munmap(addr,
sizeof(pthread_mutex_t));
162 pthread_mutexattr_destroy(&attr);
180 pthread_mutex_destroy(_mutex);
181 munmap(_mutex,
sizeof(pthread_mutex_t));
196 DWORD waitResult = WaitForSingleObject(_mutex, INFINITE);
205 if (pthread_mutex_lock(_mutex) != 0)
223 if (!ReleaseMutex(_mutex))
228 if (pthread_mutex_unlock(_mutex) != 0)
244#elif __linux__ || __APPLE__
245 pthread_mutex_t* _mutex;
named_recursive_mutex & operator=(const named_recursive_mutex &)=delete
named_recursive_mutex(const named_recursive_mutex &)=delete
named_recursive_mutex & operator=(named_recursive_mutex &&)=delete
void lock() const
Acquires the mutex lock.
Definition named_recursive_mutex.hpp:193
virtual ~named_recursive_mutex() noexcept
Destructor for named_recursive_mutex.
Definition named_recursive_mutex.hpp:172
void unlock() const
Releases the mutex lock.
Definition named_recursive_mutex.hpp:220
named_recursive_mutex(const std::string &name)
Constructs a named_recursive_mutex with a specified name.
Definition named_recursive_mutex.hpp:97
named_recursive_mutex(named_recursive_mutex &&)=delete
Custom exception class for handling system-level errors in a cross-platform manner.
Definition system_error.hpp:83
Provides concurrency primitives and abstractions for multithreaded programming. It features the power...
Definition message_manager.hpp:47
std::size_t get_max_shm_name_length()
Definition named_recursive_mutex.hpp:60
Header file to manage inclusion of windows.h with specific settings.