www.pudn.com > MessageQueue.zip > event.c
/*
Copyright (C) 2004 Liu Ge
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "memmgr.h"
#include "event.h"
P_Event Event_Create(void)
{
P_Event pEvent = malloc( sizeof(St_Event) );
if( pEvent != NULL )
{
pEvent->isSignaled = FALSE;
List_init( pEvent->SuspendedThreads );
}
return pEvent;
}
BOOL Event_Destroy( P_Event pEvent )
{
Event_Signal( pEvent );
free( pEvent );
return TRUE;
}
BOOL Event_Wait( P_Event pEvent )
{
int iCurrent = Thread_GetCurrentThreadId();
__SystemLock();
if( !pEvent->isSignaled )
{ // The current thread will be suspended
P_IntListNode pNode = malloc(
sizeof(St_IntListNode) );
pNode->Data = iCurrent;
pNode->pNext = NULL;
List_add( pEvent->SuspendedThreads, pNode );
Thread_Suspend(iCurrent);
// The statement above will internally call __SystemUnlock()
}
else
{
__SystemUnlock();
}
return TRUE;
}
BOOL Event_Signal( P_Event pEvent )
{
P_IntListNode p;
__SystemLock();
pEvent->isSignaled = TRUE;
p = List_getFirst( pEvent->SuspendedThreads );
// Resume all threads waiting for the event
while( p != NULL )
{
int iThread = p->Data;
// pick off the first suspended thread
List_removeFirst( pEvent->SuspendedThreads );
free( p );
Thread_Resume( iThread );
// This statement above will internally call the __SystemUnlock function
__SystemLock();
p = List_getFirst( pEvent->SuspendedThreads );
}
__SystemUnlock();
return TRUE;
}
BOOL Event_Reset( P_Event pEvent )
{
__SystemLock();
pEvent->isSignaled = FALSE;
__SystemUnlock();
return TRUE;
}