Files
winix/core/synchro.cpp
Tomasz Sowa fc33b4f882 added: a deadlock counter to Synchro class
now we can use Lock() more than one in the same thread
       and next Unlock() will recognize it
       sample:
       Lock(); // first lock -- resources locked
       Lock(); // second lock -- skipped (counter incremented)
       ...
       Unlock(); // first unlock -- skipped (because counter greater than zero)
       Unlock(); // second unlock -- actually unlocking


git-svn-id: svn://ttmath.org/publicrep/winix/trunk@830 e52654a7-88a9-db11-a3e9-0013d4bc506e
2012-04-28 20:57:57 +00:00

58 lines
611 B
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010-2012, Tomasz Sowa
* All rights reserved.
*
*/
#include <errno.h>
#include "synchro.h"
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);
}
}