4
// $Id: //poco/1.2/Foundation/src/NamedMutex_UNIX.cpp#2 $
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/NamedMutex_UNIX.h"
38
#include "Poco/Exception.h"
42
#if defined(sun) || defined(__APPLE__) || defined(__osf__)
43
#include <semaphore.h>
46
#include <sys/types.h>
55
#if defined(linux) || defined(__CYGWIN__)
60
unsigned short int* array;
61
struct seminfo* __buf;
73
NamedMutexImpl::NamedMutexImpl(const std::string& name):
76
std::string fileName = getFileName();
77
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
78
_sem = sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 1);
79
if ((long) _sem == (long) SEM_FAILED)
80
throw SystemException("cannot create named mutex (sem_open() failed)", _name);
82
int fd = open(fileName.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
86
throw SystemException("cannot create named mutex (lockfile)", _name);
87
key_t key = ftok(fileName.c_str(), 0);
89
throw SystemException("cannot create named mutex (ftok() failed)", _name);
90
_semid = semget(key, 1, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | IPC_CREAT | IPC_EXCL);
95
semctl(_semid, 0, SETVAL, arg);
97
else if (errno == EEXIST)
99
_semid = semget(key, 1, 0);
101
else throw SystemException("cannot create named mutex (semget() failed)", _name);
102
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
106
NamedMutexImpl::~NamedMutexImpl()
108
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
114
void NamedMutexImpl::lockImpl()
116
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
120
err = sem_wait(_sem);
122
while (err && errno == EINTR);
123
if (err) throw SystemException("cannot lock named mutex", _name);
128
op.sem_flg = SEM_UNDO;
132
err = semop(_semid, &op, 1);
134
while (err && errno == EINTR);
135
if (err) throw SystemException("cannot lock named mutex", _name);
140
bool NamedMutexImpl::tryLockImpl()
142
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
143
return sem_trywait(_sem) == 0;
148
op.sem_flg = SEM_UNDO | IPC_NOWAIT;
149
return semop(_semid, &op, 1) == 0;
154
void NamedMutexImpl::unlockImpl()
156
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
157
if (sem_post(_sem) != 0)
158
throw SystemException("cannot unlock named mutex", _name);
163
op.sem_flg = SEM_UNDO;
164
if (semop(_semid, &op, 1) != 0)
165
throw SystemException("cannot unlock named mutex", _name);
170
std::string NamedMutexImpl::getFileName()
172
#if defined(sun) || defined(__APPLE__) || defined(__QNX__)
173
std::string fn = "/";
175
std::string fn = "/tmp/";