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
|
/*
FALCON - The Falcon Programming Language.
FILE: vmsema.cpp
Short description
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: gio nov 11 2004
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/** \file
Short description
*/
#include "vmsema.h"
#include <falcon/vmcontext.h>
namespace Falcon {
void VMSemaphore::post( VMachine *vm, int32 count )
{
m_count += count;
while( m_count > 0 && ! m_waiting.empty() )
{
VMContext *ctx = (VMContext *) m_waiting.front();
ctx->signaled();
vm->reschedule( ctx );
m_waiting.popFront();
m_count --;
}
}
void VMSemaphore::wait( VMachine *vm, numeric to )
{
if ( m_count == 0 ) {
m_waiting.pushBack( vm->m_currentContext );
vm->m_currentContext->waitOn( this, to );
// by default will be zero; 1 if correctly awaken
vm->regA().setBoolean( false );
// now we can change the context
vm->rotateContext();
}
else {
vm->regA().setBoolean( true );
m_count --;
}
}
void VMSemaphore::unsubscribe( VMContext *ctx )
{
ListElement *elem = m_waiting.begin();
while( elem != 0 )
{
VMContext *cty = (VMContext *) elem->data();
if ( ctx == cty )
{
m_waiting.erase( elem );
return;
}
elem = elem->next();
}
}
VMSemaphore *VMSemaphore::clone() const
{
// ! not supported.
return 0;
}
}
/* end of vmsema.cpp */
|