www.pudn.com > MessageQueue.zip > mutex.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 "mutex.h" 
 
P_Mutex Mutex_Create(void) 
{ 
    P_Mutex pMutex = malloc( sizeof(St_Mutex) ); 
    pMutex->iOwnerThread = -1; 
    pMutex->nReferenceCount = 0; 
 
    List_init( pMutex->SuspendedThreads ); 
 
    return pMutex; 
} 
 
BOOL Mutex_Destroy( P_Mutex pMutex ) 
{ 
    while( !List_isEmpty( pMutex->SuspendedThreads ) ) 
    { 
        Mutex_Release( pMutex ); 
    } 
 
    free( pMutex ); 
    return TRUE; 
} 
 
BOOL Mutex_Hold( P_Mutex pMutex ) 
{ 
    int iCurrent = Thread_GetCurrentThreadId(); 
 
    __SystemLock(); 
 
    if( pMutex->iOwnerThread == -1 ) 
	{ 
		//  hold this mutex 
		pMutex->iOwnerThread = iCurrent; 
		pMutex->nReferenceCount = 1; 
	} 
	else if( pMutex->iOwnerThread == iCurrent ) 
	{ 
		pMutex->nReferenceCount ++; 
	} 
	else  
    {   //  The current thread will be suspended 
        P_IntListNode pNode = malloc( 
            sizeof(St_IntListNode) ); 
 
        pNode->Data = iCurrent; 
        pNode->pNext = NULL; 
 
        List_add( pMutex->SuspendedThreads, pNode ); 
 
        Thread_Suspend(iCurrent);    
        //  The statement above will internally call __SystemUnlock() 
 
		//  hold this mutex 
		pMutex->iOwnerThread = iCurrent; 
		pMutex->nReferenceCount = 1; 
    } 
 
    __SystemUnlock(); 
 
    return TRUE; 
} 
 
BOOL Mutex_Release( P_Mutex pMutex ) 
{ 
    BOOL isDone = FALSE; 
    int iCurrent = Thread_GetCurrentThreadId(); 
 
    __SystemLock(); 
 
    if( pMutex->iOwnerThread == iCurrent ) 
    { 
		if( pMutex->nReferenceCount > 0 ) 
		{ 
			pMutex->nReferenceCount --; 
			if( pMutex->nReferenceCount == 0 ) 
			{ 
				P_IntListNode p = List_getFirst( pMutex->SuspendedThreads ); 
				pMutex->iOwnerThread = -1;   //  Release the mutex 
 
				if( p != NULL ) 
				{   //  resume the first waiting thread 
					int iThread = p->Data; 
 
					//  pick off the first suspended thread 
					List_removeFirst( pMutex->SuspendedThreads ); 
 
					free( p ); 
					Thread_Resume( iThread ); 
				} 
			} 
 
			isDone = TRUE; 
		} 
    } 
 
    __SystemUnlock(); 
 
    return isDone; 
}