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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/*
FALCON - Falcon advanced simple text evaluator.
FILE: systhread_posix.cpp
System dependent MT provider - posix specific.
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: Wed, 09 Apr 2008 21:32:31 +0200
-------------------------------------------------------------------
(C) Copyright 2008: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/** \file
System dependent MT provider - posix specific.
*/
#include <falcon/memory.h>
#include "waitable.h"
#include "systhread.h"
#include "systhread_posix.h"
#include "threading_mod.h"
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <errno.h>
// #define TRACE_ON
#ifdef TRACE_ON
#include <stdio.h>
#define TRACE printf
#else
inline void _trace( const char* fmt, ...) { }
#define TRACE _trace
#endif
namespace Falcon {
namespace Ext {
//======================================================
// Waitable
//
void WaitableProvider::init( Waitable *wo )
{
wo->m_sysData = new POSIX_WAITABLE(wo);
}
void WaitableProvider::destroy( Waitable *wo )
{
delete ( POSIX_WAITABLE *) wo->m_sysData;
}
void WaitableProvider::signal( Waitable *wo )
{
broadcast( wo );
/*
POSIX_WAITABLE *pwo = (POSIX_WAITABLE *) wo->m_sysData;
POSIX_TH *pt = 0;
pthread_mutex_lock( &pwo->m_mtx );
if ( ! pwo->m_waiting.empty() )
{
pt = (POSIX_TH *) pwo->m_waiting.front();
pwo->m_waiting.popFront();
pthread_mutex_unlock( &pwo->m_mtx );
pthread_mutex_lock( &pt->m_mtx );
if ( --pt->m_refCount == 0 )
{
pthread_mutex_unlock( &pt->m_mtx );
delete pt;
}
else {
pt->m_bSignaled = true;
pthread_cond_signal( &pt->m_condSignaled );
pthread_mutex_unlock( &pt->m_mtx );
}
}
else
pthread_mutex_unlock( &pwo->m_mtx );
*/
}
void WaitableProvider::broadcast( Waitable *wo )
{
TRACE( "Broadcasting %p\n", wo );
POSIX_WAITABLE *pwo = (POSIX_WAITABLE *) wo->m_sysData;
POSIX_THI_DATA *pt = 0;
//pwo->m_waitable->m_mtx.lock();
while( ! pwo->m_waiting.empty() )
{
pt = (POSIX_THI_DATA *) pwo->m_waiting.front();
pwo->m_waiting.popFront();
//pwo->m_waitable->m_mtx.unlock();
TRACE( "%p: Notifying thread %p\n", wo, pt );
pthread_mutex_lock( &pt->m_mtx );
if ( --pt->m_refCount == 0 )
{
pthread_mutex_unlock( &pt->m_mtx );
delete pt;
}
else {
pt->m_bSignaled = true;
pthread_cond_signal( &pt->m_condSignaled );
pthread_mutex_unlock( &pt->m_mtx );
}
//pwo->m_waitable->m_mtx.lock();
}
//pwo->m_waitable->m_mtx.unlock();
TRACE( "%p: End broadcast\n", wo );
}
void WaitableProvider::lock( Waitable *wo )
{
wo->m_mtx.lock();
}
bool WaitableProvider::acquireInternal( Waitable *wo )
{
return wo->acquireInternal();
}
void WaitableProvider::unlock( Waitable *wo )
{
wo->m_mtx.unlock();
}
void WaitableProvider::interruptWaits( ThreadImpl *runner )
{
POSIX_THI_DATA *pth = (POSIX_THI_DATA *) runner->sysData();
pthread_mutex_lock( &pth->m_mtx );
if ( ! pth->m_bInterrupted )
{
pth->m_bInterrupted = true;
pth->m_bSignaled = true;;
pthread_cond_signal( &pth->m_condSignaled );
}
pthread_mutex_unlock( &pth->m_mtx );
}
int WaitableProvider::waitForObjects( const ThreadImpl *runner, int32 count, Waitable **objects, int64 time )
{
struct timespec ts;
POSIX_THI_DATA *data = (POSIX_THI_DATA *) runner->sysData();
// first, let's see if we have some data ready
// in case of no wait, just check for availability
TRACE( "Entering wait %p\n", data );
for ( int32 i = 0; i < count; i++ )
{
// try-acquire semantic.
if ( objects[i]->acquire() )
{
TRACE( "%p: Acquired item %d\n", data, i );
return i;
}
}
// noway.
if ( time == 0 ) {
TRACE( "%p: Forfaiting (try-aqcuire fail)\n", data );
return -1;
}
// else, if we have to wait for sometime, get the time now.
// ... in this case, prepare for absolute time wait.
if ( time > 0 )
{
struct timeval tp;
gettimeofday(&tp, NULL);
ts.tv_sec = tp.tv_sec;
ts.tv_nsec = tp.tv_usec * 1000;
ts.tv_sec += time/ 1000000;
ts.tv_nsec += (time%1000000)*1000;
if( ts.tv_nsec > 1000000000 )
{
ts.tv_nsec -= 1000000000;
ts.tv_sec ++;
}
}
// acquire with notify-back semanitic.
data->m_bSignaled = false;
// now wait till we can acquire something.
int acquired = -1;
bool bComplete = false;
TRACE( "%p: Starting notify-back loop\n", data );
while( true )
{
TRACE( "%p: Notify-back loop begin\n", data );
// try again to acquire everything.
for ( int32 i = 0; i < count; i++ )
{
POSIX_WAITABLE *pwo = (POSIX_WAITABLE *) objects[i]->m_sysData;
// try-acquire semantic.
if ( pwo->waitOnThis( data ) )
{
TRACE( "%p: Success on waitOnThis %d\n", data, i );
acquired = i;
bComplete = true;
break;
}
TRACE( "%p: Wating on %d\n", data, i );
}
if ( bComplete )
break;
//wait to be signaled
pthread_mutex_lock( &data->m_mtx );
TRACE( "%p: Starting notify-back wait\n", data );
while( ! data->m_bSignaled )
{
TRACE( "%p: Notify-back loop\n", data );
if ( time > 0 )
{
if ( pthread_cond_timedwait( &data->m_condSignaled, &data->m_mtx, &ts ) == ETIMEDOUT )
{
// we didn't make it on time. We must stop waiting and return.
TRACE( "%p: Timeout on notify-back\n", data );
bComplete = true;
break;
}
}
else{
pthread_cond_wait( &data->m_condSignaled, &data->m_mtx );
}
}
data->m_bSignaled = false; // reset for the next loop, if we have one.
// if we have been interrupted; we should simply return -2 and go away.
if ( data->m_bInterrupted )
{
TRACE( "%p: Thread wait interrupted\n", data );
data->m_bInterrupted = false; // reset
pthread_mutex_unlock( &data->m_mtx );
acquired = -2;
break;
}
pthread_mutex_unlock( &data->m_mtx );
if ( bComplete )
break;
}
TRACE( "%p: Exiting loop with acquired %d\n", data, acquired );
// anyhow, unwait everything
if( count > 1 && acquired <= 0 )
{
// but only if we have more than one thing to wait on, or if we failed to acquire
// otherwise we know we've been removed from the waiting list
for ( int32 i = 0; i < count; i++ )
{
TRACE( "%p: Canceling wait from object %d\n", data, objects );
POSIX_WAITABLE *pwo = (POSIX_WAITABLE *) objects[i]->m_sysData;
pwo->cancelWait( data );
}
}
return acquired;
}
//==============================================
//==============================================
POSIX_WAITABLE::POSIX_WAITABLE( Waitable *wo )
{
m_waitable = wo;
//pthread_mutex_init( &m_mtx, NULL );
}
POSIX_WAITABLE::~POSIX_WAITABLE()
{
//pthread_mutex_destroy( &m_mtx );
}
bool POSIX_WAITABLE::waitOnThis( POSIX_THI_DATA *th )
{
TRACE( "waitOnThis %p\n", th );
WaitableProvider::lock( m_waitable );
TRACE( "%p: Trying to acquire\n", th );
if( WaitableProvider::acquireInternal( m_waitable ) )
{
WaitableProvider::unlock( m_waitable );
TRACE( "%p: Acquired\n", th );
return true;
}
TRACE( "%p: Acquired failed, proceeding\n", th );
// check if we're still in the waiting list.
ListElement *le = m_waiting.begin();
while( le != 0 )
{
if ( th == le->data() )
{
WaitableProvider::unlock( m_waitable );
TRACE( "%p: Already waiting\n", th );
return false;
}
le = le->next();
}
// we're not; add us
TRACE( "%p: Goiong to wait\n", th );
// this is just to ensure correct visibility
pthread_mutex_lock( &th->m_mtx );
th->m_refCount++;
pthread_mutex_unlock( &th->m_mtx );
m_waiting.pushBack( th );
WaitableProvider::unlock( m_waitable );
return false;
}
void POSIX_WAITABLE::cancelWait( POSIX_THI_DATA *th )
{
WaitableProvider::lock( m_waitable );
ListElement *le = m_waiting.begin();
while( le != 0 )
{
if ( th == le->data() )
{
m_waiting.erase( le );
WaitableProvider::unlock( m_waitable );
pthread_mutex_lock( &th->m_mtx );
if ( --th->m_refCount == 0 )
{
pthread_mutex_unlock( &th->m_mtx );
delete th;
}
else
pthread_mutex_unlock( &th->m_mtx );
return;
}
le = le->next();
}
WaitableProvider::unlock( m_waitable );
}
//======================================================
// Thread
//
POSIX_THI_DATA::POSIX_THI_DATA()
{
pthread_cond_init( &m_condSignaled, NULL );
pthread_mutex_init( &m_mtx, NULL );
m_refCount = 1;
m_bSignaled = false;
m_bInterrupted = false;
}
POSIX_THI_DATA::~POSIX_THI_DATA()
{
pthread_cond_destroy( &m_condSignaled );
pthread_mutex_destroy( &m_mtx );
}
void* createSysData() {
return new POSIX_THI_DATA;
}
void disposeSysData( void *data )
{
if ( data != 0 )
{
POSIX_THI_DATA* ptr = (POSIX_THI_DATA*) data;
delete ptr;
}
}
}
}
/* end of systhread_posix.cpp */
|