www.pudn.com > MessageQueue.zip > semaphor.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 "semaphor.h"
P_Semaphore Semaphore_Create(int nInitialCount)
{
P_Semaphore pSemaphore = malloc( sizeof(St_Semaphore) );
if( pSemaphore != NULL )
{
pSemaphore->nCount = nInitialCount;
List_init( pSemaphore->SuspendedThreads );
}
return pSemaphore;
}
BOOL Semaphore_Destroy( P_Semaphore pSemaphore )
{
while( pSemaphore->nCount != 0 )
{
Semaphore_Signal( pSemaphore );
}
free( pSemaphore );
return TRUE;
}
void Semaphore_Wait( P_Semaphore pSemaphore )
{
int iCurrentThread = Thread_GetCurrentThreadId();
__SystemLock();
pSemaphore->nCount --;
if( pSemaphore->nCount < 0 )
{ // The current thread will be suspended
P_IntListNode pNode = malloc(
sizeof(St_IntListNode) );
pNode->Data = iCurrentThread;
pNode->pNext = NULL;
List_add( pSemaphore->SuspendedThreads, pNode );
Thread_Suspend(iCurrentThread);
// The statement above will internally call __SystemUnlock()
}
else
{
__SystemUnlock();
}
}
void Semaphore_Signal( P_Semaphore pSemaphore )
{
__SystemLock();
pSemaphore->nCount ++;
if( pSemaphore->nCount <= 0 )
{
P_IntListNode p = List_getFirst( pSemaphore->SuspendedThreads );
// Resume all threads waiting for the event
if( p != NULL )
{
int iThread = p->Data;
// pick off the first suspended thread
List_removeFirst( pSemaphore->SuspendedThreads );
free( p );
p = List_getFirst( pSemaphore->SuspendedThreads );
Thread_Resume( iThread );
}
}
__SystemUnlock();
}