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