only remove named mutex semaphore if owned (see #271)

This commit is contained in:
Alex Fabijanic 2017-10-12 15:05:27 -05:00
parent d33db357ca
commit d578109d4d
2 changed files with 8 additions and 2 deletions

View File

@ -46,6 +46,7 @@ private:
sem_t* _sem;
#else
int _semid; // semaphore id
bool _owned;
#endif
};

View File

@ -72,12 +72,17 @@ NamedMutexImpl::NamedMutexImpl(const std::string& name):
union semun arg;
arg.val = 1;
semctl(_semid, 0, SETVAL, arg);
_owned = true;
return;
}
else if (errno == EEXIST)
{
_semid = semget(key, 1, 0);
_owned = false;
if (_semid >= 0) return;
}
else throw SystemException(Poco::format("cannot create named mutex %s (semget() failed, errno=%d)", fileName, errno), _name);
throw SystemException(Poco::format("cannot create named mutex %s (semget() failed, errno=%d)", fileName, errno), _name);
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__EMSCRIPTEN__)
}
@ -87,7 +92,7 @@ NamedMutexImpl::~NamedMutexImpl()
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__EMSCRIPTEN__)
sem_close(_sem);
#else
semctl(_semid, 0, IPC_RMID, 0);
if (_owned) semctl(_semid, 0, IPC_RMID, 0);
#endif
}