From 79fdf3f2c9723e30565b22e18f5345eb7d08467c Mon Sep 17 00:00:00 2001 From: Erik Zandvliet Date: Mon, 12 Jan 2015 10:12:22 +0100 Subject: [PATCH] Allows waiting for the creation of a semaphore (if it does not exist yet) for a maximum of 5 seconds --- lib/shared_memory.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/shared_memory.cpp b/lib/shared_memory.cpp index 45caf592..280d374a 100644 --- a/lib/shared_memory.cpp +++ b/lib/shared_memory.cpp @@ -96,15 +96,25 @@ namespace IPC { ///\param value The initial value of the semaphore if O_CREAT is given in oflag, ignored otherwise void semaphore::open(const char * name, int oflag, mode_t mode, unsigned int value) { close(); + int timer = 0; + while (!(*this) && timer++ < 10){ #ifdef __CYGWIN__ - mySem = CreateSemaphore(0, value, 1 , std::string("Global\\" + std::string(name)).c_str()); + mySem = CreateSemaphore(0, value, 1 , std::string("Global\\" + std::string(name)).c_str()); #else - if (oflag & O_CREAT) { - mySem = sem_open(name, oflag, mode, value); - } else { - mySem = sem_open(name, oflag); - } + if (oflag & O_CREAT) { + mySem = sem_open(name, oflag, mode, value); + } else { + mySem = sem_open(name, oflag); + } #endif + if (!(*this)){ + if (errno == ENOENT){ + Util::wait(500); + }else{ + break; + } + } + } if (!(*this)){ DEBUG_MSG(DLVL_VERYHIGH, "Attempt to open semaphore %s: %s", name, strerror(errno)); }