2010-12-02 02:02:02 +01:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2012-04-28 22:57:57 +02:00
|
|
|
* Copyright (c) 2010-2012, Tomasz Sowa
|
2010-12-02 02:02:02 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-04-28 22:57:57 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include "synchro.h"
|
2010-12-02 02:02:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
Synchro::Synchro() : mutex(PTHREAD_MUTEX_INITIALIZER)
|
|
|
|
{
|
|
|
|
was_stop_signal = false;
|
2012-04-28 22:57:57 +02:00
|
|
|
ref = 0;
|
2010-12-02 02:02:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Synchro::Lock()
|
|
|
|
{
|
2012-04-28 22:57:57 +02:00
|
|
|
int res = pthread_mutex_lock(&mutex);
|
|
|
|
|
|
|
|
if( res == EDEADLK )
|
|
|
|
{
|
|
|
|
// Lock() method in this thread was called before
|
|
|
|
ref += 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ref = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res == 0;
|
2010-12-02 02:02:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-28 22:57:57 +02:00
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
void Synchro::Unlock()
|
|
|
|
{
|
2012-04-28 22:57:57 +02:00
|
|
|
if( ref > 0 )
|
|
|
|
{
|
|
|
|
ref -= 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
2010-12-02 02:02:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|