1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
FALCON - The Falcon Programming Language.
FILE: baton_posix.cpp
Baton synchronization structure.
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: Sat, 14 Mar 2009 00:03:28 +0100
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
#include <falcon/memory.h>
#include <falcon/baton.h>
#include <falcon/fassert.h>
#include <falcon/mt_posix.h>
namespace Falcon {
typedef struct tag_POSIX_BATON_DATA
{
bool m_bBusy;
bool m_bBlocked;
pthread_t m_thBlocker;
pthread_mutex_t m_mtx;
pthread_cond_t m_cv;
} POSIX_BATON_DATA;
Baton::Baton( bool bBusy )
{
m_data = (POSIX_BATON_DATA *) memAlloc( sizeof( POSIX_BATON_DATA ) );
POSIX_BATON_DATA &p = *(POSIX_BATON_DATA *)m_data;
p.m_bBusy = bBusy;
p.m_bBlocked = false;
#ifdef NDEBUG
pthread_mutex_init( &p.m_mtx, 0 );
pthread_cond_init( &p.m_cv, 0 );
#else
int res = pthread_mutex_init( &p.m_mtx, 0 );
fassert( res == 0 );
res = pthread_cond_init( &p.m_cv, 0 );
fassert( res == 0 );
#endif
}
Baton::~Baton()
{
POSIX_BATON_DATA &p = *(POSIX_BATON_DATA *)m_data;
#ifdef NDEBUG
pthread_mutex_destroy( &p.m_mtx );
pthread_cond_destroy( &p.m_cv );
#else
int res = pthread_mutex_destroy( &p.m_mtx );
fassert( res == 0 );
res = pthread_cond_destroy( &p.m_cv );
fassert( res == 0 );
#endif
memFree( m_data );
}
void Baton::acquire()
{
POSIX_BATON_DATA &p = *(POSIX_BATON_DATA *)m_data;
mutex_lock( p.m_mtx );
if( p.m_bBlocked && ! pthread_equal( pthread_self(), p.m_thBlocker ) )
{
mutex_unlock( p.m_mtx );
// no problem if we release the mutex here: it's ok, as the semantic is just that of firing this
// notify if we're blocked.
onBlockedAcquire();
mutex_lock( p.m_mtx );
}
while( p.m_bBusy || (p.m_bBlocked && ! pthread_equal( pthread_self(), p.m_thBlocker ) ) )
{
cv_wait( p.m_cv, p.m_mtx );
}
p.m_bBusy = true;
p.m_bBlocked = false;
mutex_unlock( p.m_mtx );
}
bool Baton::busy()
{
POSIX_BATON_DATA &p = *(POSIX_BATON_DATA *)m_data;
mutex_lock( p.m_mtx );
bool bbret = p.m_bBusy;
mutex_unlock( p.m_mtx );
return bbret;
}
bool Baton::tryAcquire()
{
POSIX_BATON_DATA &p = *(POSIX_BATON_DATA *)m_data;
mutex_lock( p.m_mtx );
if( p.m_bBusy || (p.m_bBlocked && ! pthread_equal( pthread_self(), p.m_thBlocker ) ) )
{
mutex_unlock( p.m_mtx );
return false;
}
p.m_bBusy = true;
p.m_bBlocked = false;
mutex_unlock( p.m_mtx );
return true;
}
void Baton::release()
{
POSIX_BATON_DATA &p = *(POSIX_BATON_DATA *)m_data;
mutex_lock( p.m_mtx );
p.m_bBusy = false;
cv_broadcast( p.m_cv );
mutex_unlock( p.m_mtx );
}
void Baton::checkBlock()
{
POSIX_BATON_DATA &p = *(POSIX_BATON_DATA *)m_data;
mutex_lock( p.m_mtx );
p.m_bBusy = false;
cv_broadcast( p.m_cv );
bool bb = (p.m_bBlocked && ! pthread_equal( pthread_self(), p.m_thBlocker ) );
if ( bb )
{
mutex_unlock( p.m_mtx );
onBlockedAcquire();
mutex_lock( p.m_mtx );
}
while( p.m_bBusy || (p.m_bBlocked && ! pthread_equal( pthread_self(), p.m_thBlocker ) ) )
{
cv_wait( p.m_cv, p.m_mtx );
}
p.m_bBusy = true;
p.m_bBlocked = false;
mutex_unlock( p.m_mtx );
}
bool Baton::block()
{
POSIX_BATON_DATA &p = *(POSIX_BATON_DATA *)m_data;
mutex_lock( p.m_mtx );
if ( (!p.m_bBusy) || (p.m_bBlocked && ! pthread_equal( pthread_self(), p.m_thBlocker )) )
{
mutex_unlock( p.m_mtx );
return false;
}
if ( ! p.m_bBlocked )
{
p.m_bBlocked = true;
p.m_thBlocker = pthread_self();
}
mutex_unlock( p.m_mtx );
return true;
}
bool Baton::unblock()
{
POSIX_BATON_DATA &p = *(POSIX_BATON_DATA *)m_data;
mutex_lock( p.m_mtx );
if ( p.m_bBlocked && pthread_equal( pthread_self(), p.m_thBlocker ) )
{
p.m_bBlocked = false;
cv_broadcast( p.m_cv );
mutex_unlock( p.m_mtx );
return true;
}
mutex_unlock( p.m_mtx );
return false;
}
void Baton::onBlockedAcquire()
{
}
}
/* end of baton_posix.cpp */
|