git-svn-id: svn://db.shs.com.ru/pip@282 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -38,29 +38,19 @@
|
||||
* */
|
||||
|
||||
|
||||
PIMutex::PIMutex() {
|
||||
#ifdef WINDOWS
|
||||
mutex = CreateMutex(0, false, 0);
|
||||
#else
|
||||
pthread_mutexattr_t attr;
|
||||
memset(&attr, 0, sizeof(attr));
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
||||
memset(&mutex, 0, sizeof(mutex));
|
||||
pthread_mutex_init(&mutex, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
#endif
|
||||
locked = false;
|
||||
PIMutex::PIMutex(): inited_(false) {
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
PIMutex::~PIMutex() {
|
||||
#ifdef WINDOWS
|
||||
if (mutex != 0) CloseHandle(mutex);
|
||||
#else
|
||||
pthread_mutex_destroy(&mutex);
|
||||
#endif
|
||||
locked = false;
|
||||
destroy();
|
||||
}
|
||||
|
||||
|
||||
PIMutex & PIMutex::operator =(const PIMutex & other) {
|
||||
init();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -99,3 +89,34 @@ bool PIMutex::tryLock() {
|
||||
bool PIMutex::isLocked() const {
|
||||
return locked;
|
||||
}
|
||||
|
||||
|
||||
void PIMutex::init() {
|
||||
if (inited_) destroy();
|
||||
#ifdef WINDOWS
|
||||
mutex = CreateMutex(0, false, 0);
|
||||
#else
|
||||
pthread_mutexattr_t attr;
|
||||
memset(&attr, 0, sizeof(attr));
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
||||
memset(&mutex, 0, sizeof(mutex));
|
||||
pthread_mutex_init(&mutex, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
#endif
|
||||
locked = false;
|
||||
inited_ = true;
|
||||
}
|
||||
|
||||
|
||||
void PIMutex::destroy() {
|
||||
if (inited_) {
|
||||
#ifdef WINDOWS
|
||||
if (mutex) CloseHandle(mutex);
|
||||
mutex = 0;
|
||||
#else
|
||||
pthread_mutex_destroy(&mutex);
|
||||
#endif
|
||||
}
|
||||
locked = inited_ = false;
|
||||
}
|
||||
|
||||
@@ -33,9 +33,12 @@ class PIP_EXPORT PIMutex
|
||||
public:
|
||||
|
||||
//! Constructs unlocked mutex
|
||||
PIMutex();
|
||||
explicit PIMutex();
|
||||
|
||||
~PIMutex();
|
||||
|
||||
|
||||
PIMutex & operator =(const PIMutex & other);
|
||||
|
||||
|
||||
//! \brief Lock mutex
|
||||
@@ -56,12 +59,16 @@ public:
|
||||
bool isLocked() const;
|
||||
|
||||
private:
|
||||
void init();
|
||||
void destroy();
|
||||
|
||||
#ifdef WINDOWS
|
||||
void *
|
||||
#else
|
||||
pthread_mutex_t
|
||||
#endif
|
||||
mutex;
|
||||
bool inited_;
|
||||
volatile bool locked;
|
||||
|
||||
};
|
||||
|
||||
@@ -48,7 +48,7 @@ public:
|
||||
void enqueue(const Tin &v) {enqueue(v, 0);}
|
||||
EVENT_HANDLER2(void, enqueue, const Tin &, v, bool *, overload) {
|
||||
mutex.lock();
|
||||
// piCout << "enque" << max_size << in.size();
|
||||
//piCoutObj << "enque" << overload;//max_size << in.size();
|
||||
if (max_size == 0 || in.size() < max_size) {
|
||||
in.enqueue(v);
|
||||
if (overload) *overload = false;
|
||||
@@ -119,15 +119,17 @@ protected:
|
||||
private:
|
||||
void begin() {cnt = 0;}
|
||||
void run() {
|
||||
//piCoutObj << "run ...";
|
||||
mutex.lock();
|
||||
if (in.isEmpty()) {
|
||||
mutex.unlock();
|
||||
piMSleep(10);
|
||||
//piCoutObj << "run in empty";
|
||||
return;
|
||||
}
|
||||
if (next_overload && wait_next_pipe) {
|
||||
mutex.unlock();
|
||||
piCoutObj << &next_overload;
|
||||
//piCoutObj << "wait" << &next_overload;
|
||||
calculated(last, &next_overload);
|
||||
piMSleep(10);
|
||||
} else {
|
||||
@@ -141,10 +143,11 @@ private:
|
||||
mutex_l.unlock();
|
||||
cnt++;
|
||||
// next_overload = true;
|
||||
piCoutObj << &next_overload;
|
||||
//piCoutObj << "calc ok" << &next_overload;
|
||||
calculated(r, &next_overload);
|
||||
}
|
||||
}
|
||||
//piCoutObj << "run ok";
|
||||
}
|
||||
PIMutex mutex;
|
||||
PIMutex mutex_l;
|
||||
|
||||
Reference in New Issue
Block a user