Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Util MP - Spin Lock.inl
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, 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:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution 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.
    The name of the author may not 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_UTIL_MP_SN_INL)
#define TGS_COMMON_UTIL_MP_SN_INL
#pragma once


/* == Common ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Functions                                                                                                                                                      */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgCM_UTM_SN_Init ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_SN_Init( PCU_STg2_UTM_SN psMP_SN )
{
    psMP_SN->m_iData = 0;
    return (KTgS_OK);
}


/* ---- tgCM_UTM_SN_Free ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_SN_Free( PCU_STg2_UTM_SN psMP_SN )
{
    TgERROR(psMP_SN->m.niServed == psMP_SN->m.iRequest);
}


/* ---- tgCM_UTM_SN_Lock_Yield ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_SN_Lock_Yield( PCU_STg2_UTM_SN psMP_SN )
{
    TgSINT32                            iRequest;

    iRequest = tgAM32_XADD( &psMP_SN->m.iRequest, 1 );

    while (psMP_SN->m.niServed != iRequest)
    {
        tgTR_Yield();
    };
}


/* ---- tgCM_UTM_SN_Lock_Spin ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_SN_Lock_Spin( PCU_STg2_UTM_SN psMP_SN )
{
    TgSINT32                            iRequest;

    iRequest = tgAM32_XADD( &psMP_SN->m.iRequest, 1 );

    while (psMP_SN->m.niServed != iRequest)
    {
        tgTR_Pause();
    };
}


/* ---- tgCM_UTM_SN_Lock_Test ------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_SN_Lock_Test( PCU_STg2_UTM_SN psMP_SN )
{
    STg2_UTM_SN                         sData, sNewData;
    TgSINT32                            iRequest;

    iRequest = psMP_SN->m.iRequest;
    sData.m.niServed = iRequest;
    sData.m.iRequest = iRequest;

    sNewData.m.niServed = iRequest;
    sNewData.m.iRequest = iRequest + 1;

    return ((sData.m_iData == tgAM64_XCMP( &psMP_SN->m_iData, sNewData.m_iData, sData.m_iData )) ? KTgS_OK : KTgE_FAIL);
}


/* ---- tgCM_UTM_SN_Signal ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_SN_Signal( PCU_STg2_UTM_SN psMP_SN )
{
    ++psMP_SN->m.niServed;
    TgERROR( psMP_SN->m.niServed <= psMP_SN->m.iRequest );
}


/* ---- tgCM_UTM_SNC_Init ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgRESULT tgCM_UTM_SNC_Init( PCU_STg2_UTM_SNC psMP_SNC )
{
    tgCM_UTM_SN_Init( &psMP_SNC->m_sData0 );
    psMP_SNC->m_sData1.m_iData = 0;
    return (KTgS_OK);
}


/* ---- tgCM_UTM_SNC_Free ----------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_SNC_Free( PCU_STg2_UTM_SNC psMP_SNC )
{
    tgCM_UTM_SN_Free( &psMP_SNC->m_sData0 );
}


/* ---- tgCM_UTM_SNC_Lock_Yield ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_SNC_Lock_Yield( PCU_STg2_UTM_SNC psMP_SNC )
{
    tgCM_UTM_SN_Lock_Yield( &psMP_SNC->m_sData0 );
}


/* ---- tgCM_UTM_SNC_Lock_Spin ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_SNC_Lock_Spin( PCU_STg2_UTM_SNC psMP_SNC )
{
    tgCM_UTM_SN_Lock_Spin( &psMP_SNC->m_sData0 );
}


/* ---- tgCM_UTM_SNC_Lock_Test ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
CLANG_WARN_DISABLE_PUSH(cast-qual)
TgINLINE TgRESULT tgCM_UTM_SNC_Lock_Test( PCU_STg2_UTM_SNC psMP_SNC )
{
    STg2_UTM_SNC                        sData, sNewData;

    do
    {
        TgMEMCPY( &sData, sizeof( STg2_UTM_SNC ), psMP_SNC, sizeof(STg2_UTM_SNC) );

        if (0 != sData.m_sData1.m.niAttempt || (sData.m_sData0.m.iRequest != sData.m_sData0.m.niServed))
        {
            sNewData.m_sData0.m.niServed = sData.m_sData0.m.niServed;
            sNewData.m_sData0.m.iRequest = sData.m_sData0.m.iRequest;
            sNewData.m_sData1.m_iData = 0;
            sNewData.m_sData1.m.niAttempt = sData.m_sData1.m.niAttempt + 1;
        }
        else
        {
            sNewData.m_sData0.m.niServed = sData.m_sData0.m.iRequest;
            sNewData.m_sData0.m.iRequest = sData.m_sData0.m.iRequest + 1;
            sNewData.m_sData1.m_iData = 0;
        };
    }
    while (TgTRUE != tgAM64_XCMP2( &(psMP_SNC->m_sData0.m_iData), sNewData.m_sData1.m_iData, sNewData.m_sData0.m_iData, (P_TgSINT64)&sData.m_sData0.m_iData ));

    return (0 == sNewData.m_sData1.m.niAttempt ? KTgS_OK : KTgE_FAIL);
}
CLANG_WARN_DISABLE_POP(cast-qual)


/* ---- tgCM_UTM_SNC_Signal --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
CLANG_WARN_DISABLE_PUSH(cast-qual)
TgINLINE TgSINT32 tgCM_UTM_SNC_Signal( PCU_STg2_UTM_SNC psMP_SNC )
{
    STg2_UTM_SNC                        sData, sNewData;
    TgSINT32                            niAttempt;

    do
    {
        TgMEMCPY( &sData, sizeof( STg2_UTM_SNC ), psMP_SNC, sizeof( STg2_UTM_SNC ) );
        sNewData.m_sData0.m.niServed = sData.m_sData0.m.niServed + (0 == sData.m_sData1.m.niAttempt ? 1 : 0);
        sNewData.m_sData0.m.iRequest = sData.m_sData0.m.iRequest;
        sNewData.m_sData1.m_iData = 0;
        sNewData.m_sData1.m.niAttempt = 0;

        niAttempt = sData.m_sData1.m.niAttempt;
    }
    while (TgTRUE != tgAM64_XCMP2( &(psMP_SNC->m_sData0.m_iData), sNewData.m_sData1.m_iData, sNewData.m_sData0.m_iData, (P_TgSINT64)&sData.m_sData0.m_iData ));

    return (niAttempt);
}
CLANG_WARN_DISABLE_POP(cast-qual)


/* ====================================================================================================================================================================== */
#endif