winix/core/synchro.cpp

64 lines
653 B
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010-2014, Tomasz Sowa
* All rights reserved.
*
*/
#include <errno.h>
#include "synchro.h"
namespace Winix
{
Synchro::Synchro() : mutex(PTHREAD_MUTEX_INITIALIZER)
{
was_stop_signal = false;
ref = 0;
}
bool Synchro::Lock()
{
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;
}
void Synchro::Unlock()
{
if( ref > 0 )
{
ref -= 1;
}
else
{
pthread_mutex_unlock(&mutex);
}
}
} // namespace Winix