mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-18 00:15:27 +01:00
trunk/branch integration: Event & Mutex
This commit is contained in:
parent
05041e2689
commit
d6136d3088
@ -38,7 +38,13 @@
|
||||
|
||||
|
||||
#if defined(POCO_OS_FAMILY_WINDOWS)
|
||||
#if defined(_WIN32_WCE)
|
||||
#include "Mutex_WINCE.cpp"
|
||||
#else
|
||||
#include "Mutex_WIN32.cpp"
|
||||
#endif
|
||||
#elif defined(POCO_VXWORKS)
|
||||
#include "Mutex_VX.cpp"
|
||||
#else
|
||||
#include "Mutex_POSIX.cpp"
|
||||
#endif
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// Mutex_POSIX.cpp
|
||||
//
|
||||
// $Id: //poco/svn/Foundation/src/Mutex_POSIX.cpp#3 $
|
||||
// $Id: //poco/1.4/Foundation/src/Mutex_POSIX.cpp#4 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Threading
|
||||
@ -40,6 +40,10 @@
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#if defined(POCO_VXWORKS)
|
||||
#include <timers.h>
|
||||
#include <cstring>
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
|
||||
|
||||
@ -57,14 +61,21 @@ namespace Poco {
|
||||
|
||||
MutexImpl::MutexImpl()
|
||||
{
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
#if defined(PTHREAD_MUTEX_RECURSIVE_NP)
|
||||
pthread_mutexattr_settype_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
|
||||
#else
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
#if defined(POCO_VXWORKS)
|
||||
// This workaround is for VxWorks 5.x where
|
||||
// pthread_mutex_init() won't properly initialize the mutex
|
||||
// resulting in a subsequent freeze in pthread_mutex_destroy()
|
||||
// if the mutex has never been used.
|
||||
std::memset(&_mutex, 0, sizeof(_mutex));
|
||||
#endif
|
||||
if (pthread_mutex_init(&_mutex, &attr))
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
#if defined(PTHREAD_MUTEX_RECURSIVE_NP)
|
||||
pthread_mutexattr_settype_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
|
||||
#elif !defined(POCO_VXWORKS)
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
#endif
|
||||
if (pthread_mutex_init(&_mutex, &attr))
|
||||
{
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
throw SystemException("cannot create mutex");
|
||||
@ -75,14 +86,21 @@ MutexImpl::MutexImpl()
|
||||
|
||||
MutexImpl::MutexImpl(bool fast)
|
||||
{
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
#if defined(PTHREAD_MUTEX_RECURSIVE_NP)
|
||||
pthread_mutexattr_settype_np(&attr, fast ? PTHREAD_MUTEX_NORMAL_NP : PTHREAD_MUTEX_RECURSIVE_NP);
|
||||
#else
|
||||
pthread_mutexattr_settype(&attr, fast ? PTHREAD_MUTEX_NORMAL : PTHREAD_MUTEX_RECURSIVE);
|
||||
#if defined(POCO_VXWORKS)
|
||||
// This workaround is for VxWorks 5.x where
|
||||
// pthread_mutex_init() won't properly initialize the mutex
|
||||
// resulting in a subsequent freeze in pthread_mutex_destroy()
|
||||
// if the mutex has never been used.
|
||||
std::memset(&_mutex, 0, sizeof(_mutex));
|
||||
#endif
|
||||
if (pthread_mutex_init(&_mutex, &attr))
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
#if defined(PTHREAD_MUTEX_RECURSIVE_NP)
|
||||
pthread_mutexattr_settype_np(&attr, fast ? PTHREAD_MUTEX_NORMAL_NP : PTHREAD_MUTEX_RECURSIVE_NP);
|
||||
#elif !defined(POCO_VXWORKS)
|
||||
pthread_mutexattr_settype(&attr, fast ? PTHREAD_MUTEX_NORMAL : PTHREAD_MUTEX_RECURSIVE);
|
||||
#endif
|
||||
if (pthread_mutex_init(&_mutex, &attr))
|
||||
{
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
throw SystemException("cannot create mutex");
|
||||
@ -125,16 +143,24 @@ bool MutexImpl::tryLockImpl(long milliseconds)
|
||||
{
|
||||
int rc = pthread_mutex_trylock(&_mutex);
|
||||
if (rc == 0)
|
||||
return true;
|
||||
else if (rc != EBUSY)
|
||||
throw SystemException("cannot lock mutex");
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = sleepMillis * 1000;
|
||||
select(0, NULL, NULL, NULL, &tv);
|
||||
}
|
||||
while (!now.isElapsed(diff));
|
||||
return false;
|
||||
return true;
|
||||
else if (rc != EBUSY)
|
||||
throw SystemException("cannot lock mutex");
|
||||
#if defined(POCO_VXWORKS)
|
||||
struct timespec ts;
|
||||
ts.tv_sec = 0;
|
||||
ts.tv_nsec = sleepMillis*1000000;
|
||||
nanosleep(&ts, NULL);
|
||||
|
||||
#else
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = sleepMillis * 1000;
|
||||
select(0, NULL, NULL, NULL, &tv);
|
||||
#endif
|
||||
}
|
||||
while (!now.isElapsed(diff));
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -41,6 +41,8 @@
|
||||
#include "NamedEvent_WIN32U.cpp"
|
||||
#elif defined(POCO_OS_FAMILY_WINDOWS)
|
||||
#include "NamedEvent_WIN32.cpp"
|
||||
#elif defined(POCO_ANDROID)
|
||||
#include "NamedEvent_Android.cpp"
|
||||
#elif defined(POCO_OS_FAMILY_UNIX)
|
||||
#include "NamedEvent_UNIX.cpp"
|
||||
#else
|
||||
|
@ -39,7 +39,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
#include <semaphore.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
@ -57,13 +57,13 @@ namespace Poco {
|
||||
{
|
||||
int val;
|
||||
struct semid_ds* buf;
|
||||
unsigned short int* array;
|
||||
struct seminfo* __buf;
|
||||
};
|
||||
#elif defined(hpux)
|
||||
union semun
|
||||
{
|
||||
int val;
|
||||
unsigned short int* array;
|
||||
struct seminfo* __buf;
|
||||
};
|
||||
#elif defined(__hpux)
|
||||
union semun
|
||||
{
|
||||
int val;
|
||||
struct semid_ds* buf;
|
||||
ushort* array;
|
||||
};
|
||||
@ -71,13 +71,13 @@ namespace Poco {
|
||||
|
||||
|
||||
NamedEventImpl::NamedEventImpl(const std::string& name):
|
||||
_name(name)
|
||||
_name(name)
|
||||
{
|
||||
std::string fileName = getFileName();
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
_sem = sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 0);
|
||||
if ((long) _sem == (long) SEM_FAILED)
|
||||
throw SystemException("cannot create named event (sem_open() failed)", _name);
|
||||
std::string fileName = getFileName();
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
_sem = sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 0);
|
||||
if ((long) _sem == (long) SEM_FAILED)
|
||||
throw SystemException("cannot create named event (sem_open() failed)", _name);
|
||||
#else
|
||||
int fd = open(fileName.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||
if (fd != -1)
|
||||
@ -96,26 +96,26 @@ NamedEventImpl::NamedEventImpl(const std::string& name):
|
||||
}
|
||||
else if (errno == EEXIST)
|
||||
{
|
||||
_semid = semget(key, 1, 0);
|
||||
}
|
||||
else throw SystemException("cannot create named event (semget() failed)", _name);
|
||||
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
_semid = semget(key, 1, 0);
|
||||
}
|
||||
else throw SystemException("cannot create named event (semget() failed)", _name);
|
||||
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
}
|
||||
|
||||
|
||||
NamedEventImpl::~NamedEventImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
sem_close(_sem);
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
sem_close(_sem);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void NamedEventImpl::setImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
if (sem_post(_sem) != 0)
|
||||
throw SystemException("cannot set named event", _name);
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
if (sem_post(_sem) != 0)
|
||||
throw SystemException("cannot set named event", _name);
|
||||
#else
|
||||
struct sembuf op;
|
||||
op.sem_num = 0;
|
||||
@ -129,10 +129,10 @@ void NamedEventImpl::setImpl()
|
||||
|
||||
void NamedEventImpl::waitImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
int err;
|
||||
do
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
int err;
|
||||
do
|
||||
{
|
||||
err = sem_wait(_sem);
|
||||
}
|
||||
while (err && errno == EINTR);
|
||||
|
@ -41,6 +41,8 @@
|
||||
#include "NamedMutex_WIN32U.cpp"
|
||||
#elif defined(POCO_OS_FAMILY_WINDOWS)
|
||||
#include "NamedMutex_WIN32.cpp"
|
||||
#elif defined(POCO_ANDROID)
|
||||
#include "NamedMutex_Android.cpp"
|
||||
#elif defined(POCO_OS_FAMILY_UNIX)
|
||||
#include "NamedMutex_UNIX.cpp"
|
||||
#else
|
||||
|
@ -39,7 +39,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__)
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(_AIX)
|
||||
#include <semaphore.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
@ -57,13 +57,13 @@ namespace Poco {
|
||||
{
|
||||
int val;
|
||||
struct semid_ds* buf;
|
||||
unsigned short int* array;
|
||||
struct seminfo* __buf;
|
||||
};
|
||||
#elif defined(hpux)
|
||||
union semun
|
||||
{
|
||||
int val;
|
||||
unsigned short int* array;
|
||||
struct seminfo* __buf;
|
||||
};
|
||||
#elif defined(__hpux)
|
||||
union semun
|
||||
{
|
||||
int val;
|
||||
struct semid_ds* buf;
|
||||
ushort* array;
|
||||
};
|
||||
@ -71,13 +71,13 @@ namespace Poco {
|
||||
|
||||
|
||||
NamedMutexImpl::NamedMutexImpl(const std::string& name):
|
||||
_name(name)
|
||||
_name(name)
|
||||
{
|
||||
std::string fileName = getFileName();
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
_sem = sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 1);
|
||||
if ((long) _sem == (long) SEM_FAILED)
|
||||
throw SystemException("cannot create named mutex (sem_open() failed)", _name);
|
||||
std::string fileName = getFileName();
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
_sem = sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 1);
|
||||
if ((long) _sem == (long) SEM_FAILED)
|
||||
throw SystemException("cannot create named mutex (sem_open() failed)", _name);
|
||||
#else
|
||||
int fd = open(fileName.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||
if (fd != -1)
|
||||
@ -96,27 +96,27 @@ NamedMutexImpl::NamedMutexImpl(const std::string& name):
|
||||
}
|
||||
else if (errno == EEXIST)
|
||||
{
|
||||
_semid = semget(key, 1, 0);
|
||||
}
|
||||
else throw SystemException("cannot create named mutex (semget() failed)", _name);
|
||||
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
_semid = semget(key, 1, 0);
|
||||
}
|
||||
else throw SystemException("cannot create named mutex (semget() failed)", _name);
|
||||
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
}
|
||||
|
||||
|
||||
NamedMutexImpl::~NamedMutexImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
sem_close(_sem);
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
sem_close(_sem);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void NamedMutexImpl::lockImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
int err;
|
||||
do
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
int err;
|
||||
do
|
||||
{
|
||||
err = sem_wait(_sem);
|
||||
}
|
||||
while (err && errno == EINTR);
|
||||
@ -139,10 +139,10 @@ void NamedMutexImpl::lockImpl()
|
||||
|
||||
bool NamedMutexImpl::tryLockImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
return sem_trywait(_sem) == 0;
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
return sem_trywait(_sem) == 0;
|
||||
#else
|
||||
struct sembuf op;
|
||||
struct sembuf op;
|
||||
op.sem_num = 0;
|
||||
op.sem_op = -1;
|
||||
op.sem_flg = SEM_UNDO | IPC_NOWAIT;
|
||||
@ -153,9 +153,9 @@ bool NamedMutexImpl::tryLockImpl()
|
||||
|
||||
void NamedMutexImpl::unlockImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
if (sem_post(_sem) != 0)
|
||||
throw SystemException("cannot unlock named mutex", _name);
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
if (sem_post(_sem) != 0)
|
||||
throw SystemException("cannot unlock named mutex", _name);
|
||||
#else
|
||||
struct sembuf op;
|
||||
op.sem_num = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user