// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//  »Project«   Talina Gaming System (TgS) (∂)
//  »File«      TgS (XB2) Common - STg1_MP_SM.inl
//  »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
//  »Version«   4.0
// ------------------------------------------------------------------------------------------------------------------------------ //
//  Copyright: © 2002-2010, Andrew Aye.  All Rights Reserved.
//  This software is free for non-commercial use. Redistribution and use in source and binary forms, with or without modification,
//  are permitted provided that the following conditions are met: 
//    Redistributions of source code must retain this copyright notice, this list of conditions and the following disclaimers. 
//    Redistributions in binary form must reproduce this copyright notice, this list of conditions and the following
//      disclaimers in the documentation and other materials provided with the distribution. 
//  Neither the names of the copyright owner nor the names of its contributors may be used to endorse or promote products derived
//  from this software without specific prior written permission. 
//  The intellectual property rights of the algorithms used reside with Andrew Aye.  You may not use this software, in whole or
//  in part, in support of any commercial product without the express written consent of the author.
//  There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
#if !defined(_TGS_COMMON_CTG1_MP_SM_INL_)
#define _TGS_COMMON_CTG1_MP_SM_INL_
#pragma once


// START TGS ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

TgINLINE TgRESULT tgCM_MP_SM_Init( PCU_STg1_MP_SM ptgSM, C_TgUINT32 uiMax )
{
    return ((NULL == (ptgSM->m_hSemaphore = CreateSemaphore( NULL, 0, uiMax, NULL )) ) ? TgE_FAIL : TgS_OK);
}


TgINLINE TgVOID tgCM_MP_SM_Free( PCU_STg1_MP_SM ptgSM )
{
    if (0 != ptgSM->m_hSemaphore)
    {
        TgVERIFY( 0 != CloseHandle( ptgSM->m_hSemaphore ) );
        ptgSM->m_hSemaphore = 0;
    };
}


TgINLINE TgVOID tgCM_MP_SM_Wait_Block( PCU_STg1_MP_SM ptgSM )
{
    while (WAIT_OBJECT_0 != WaitForSingleObject( ptgSM->m_hSemaphore, 0xFFFFFFFF ));
}


TgINLINE TgRESULT tgCM_MP_SM_Wait( PCU_STg1_MP_SM ptgSM )
{
    return (WAIT_OBJECT_0 == WaitForSingleObject( ptgSM->m_hSemaphore, 0 ) ? TgS_OK : TgE_FAIL);
}


TgINLINE TgVOID tgCM_MP_SM_Signal( PCU_STg1_MP_SM ptgSM, C_TgUINT32 nuiSignal )
{
    TgVERIFY( 0 != ReleaseSemaphore( ptgSM->m_hSemaphore, nuiSignal, NULL ) );
}


#endif //  END  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////