mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-23 00:07:59 +02:00
trunk/branch integration: Event & Mutex
This commit is contained in:
@@ -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,11 +61,18 @@ namespace Poco {
|
||||
|
||||
MutexImpl::MutexImpl()
|
||||
{
|
||||
#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
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
#if defined(PTHREAD_MUTEX_RECURSIVE_NP)
|
||||
pthread_mutexattr_settype_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
|
||||
#else
|
||||
#elif !defined(POCO_VXWORKS)
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
#endif
|
||||
if (pthread_mutex_init(&_mutex, &attr))
|
||||
@@ -75,11 +86,18 @@ MutexImpl::MutexImpl()
|
||||
|
||||
MutexImpl::MutexImpl(bool fast)
|
||||
{
|
||||
#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
|
||||
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
|
||||
#elif !defined(POCO_VXWORKS)
|
||||
pthread_mutexattr_settype(&attr, fast ? PTHREAD_MUTEX_NORMAL : PTHREAD_MUTEX_RECURSIVE);
|
||||
#endif
|
||||
if (pthread_mutex_init(&_mutex, &attr))
|
||||
@@ -128,10 +146,18 @@ bool MutexImpl::tryLockImpl(long milliseconds)
|
||||
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;
|
||||
|
@@ -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>
|
||||
@@ -60,7 +60,7 @@ namespace Poco {
|
||||
unsigned short int* array;
|
||||
struct seminfo* __buf;
|
||||
};
|
||||
#elif defined(hpux)
|
||||
#elif defined(__hpux)
|
||||
union semun
|
||||
{
|
||||
int val;
|
||||
@@ -74,7 +74,7 @@ NamedEventImpl::NamedEventImpl(const std::string& name):
|
||||
_name(name)
|
||||
{
|
||||
std::string fileName = getFileName();
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#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);
|
||||
@@ -99,13 +99,13 @@ NamedEventImpl::NamedEventImpl(const std::string& name):
|
||||
_semid = semget(key, 1, 0);
|
||||
}
|
||||
else throw SystemException("cannot create named event (semget() failed)", _name);
|
||||
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
}
|
||||
|
||||
|
||||
NamedEventImpl::~NamedEventImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
sem_close(_sem);
|
||||
#endif
|
||||
}
|
||||
@@ -113,7 +113,7 @@ NamedEventImpl::~NamedEventImpl()
|
||||
|
||||
void NamedEventImpl::setImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
if (sem_post(_sem) != 0)
|
||||
throw SystemException("cannot set named event", _name);
|
||||
#else
|
||||
@@ -129,7 +129,7 @@ void NamedEventImpl::setImpl()
|
||||
|
||||
void NamedEventImpl::waitImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
int err;
|
||||
do
|
||||
{
|
||||
|
@@ -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>
|
||||
@@ -60,7 +60,7 @@ namespace Poco {
|
||||
unsigned short int* array;
|
||||
struct seminfo* __buf;
|
||||
};
|
||||
#elif defined(hpux)
|
||||
#elif defined(__hpux)
|
||||
union semun
|
||||
{
|
||||
int val;
|
||||
@@ -74,7 +74,7 @@ NamedMutexImpl::NamedMutexImpl(const std::string& name):
|
||||
_name(name)
|
||||
{
|
||||
std::string fileName = getFileName();
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#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);
|
||||
@@ -99,13 +99,13 @@ NamedMutexImpl::NamedMutexImpl(const std::string& name):
|
||||
_semid = semget(key, 1, 0);
|
||||
}
|
||||
else throw SystemException("cannot create named mutex (semget() failed)", _name);
|
||||
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
}
|
||||
|
||||
|
||||
NamedMutexImpl::~NamedMutexImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
sem_close(_sem);
|
||||
#endif
|
||||
}
|
||||
@@ -113,7 +113,7 @@ NamedMutexImpl::~NamedMutexImpl()
|
||||
|
||||
void NamedMutexImpl::lockImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
int err;
|
||||
do
|
||||
{
|
||||
@@ -139,7 +139,7 @@ void NamedMutexImpl::lockImpl()
|
||||
|
||||
bool NamedMutexImpl::tryLockImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
return sem_trywait(_sem) == 0;
|
||||
#else
|
||||
struct sembuf op;
|
||||
@@ -153,7 +153,7 @@ bool NamedMutexImpl::tryLockImpl()
|
||||
|
||||
void NamedMutexImpl::unlockImpl()
|
||||
{
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__)
|
||||
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
|
||||
if (sem_post(_sem) != 0)
|
||||
throw SystemException("cannot unlock named mutex", _name);
|
||||
#else
|
||||
|
Reference in New Issue
Block a user