Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Util MP - Stack.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_ST_INL)
#define TGS_COMMON_UTIL_MP_ST_INL
#pragma once


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

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

/* ---- Atomic Unordered Stack -------------------------------------------------------------------------------------------------------------------------------------------- */

/* ---- tgCM_UTM_AM_ST_Init --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_AM_ST_Init( PCU_STg2_UTM_AM_ST psMP_ST )
{
    psMP_ST->m_sTop.m_pNode = nullptr;
    psMP_ST->m_sTop.m_nuiVal = 0;
}


/* ---- tgCM_UTM_AM_ST_Init_PreLoad ------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_AM_ST_Init_PreLoad( PCU_STg2_UTM_AM_ST psMP_ST, P_TgVOID pData, C_TgSIZE uiStride, C_TgSIZE nuiData)
{
    TgUINTPTR                           uiDataAddr;

    TgERROR( uiStride >= sizeof( STg2_UTM_Node ));

    psMP_ST->m_sTop.m_pNode = pData;
    psMP_ST->m_sTop.m_nuiVal = 0;

    uiDataAddr = (TgUINTPTR)pData;
    for (; psMP_ST->m_sTop.m_nuiVal < nuiData; ++psMP_ST->m_sTop.m_nuiVal)
    {
        *(P_TgUINTPTR)uiDataAddr = uiDataAddr + uiStride;
        uiDataAddr += uiStride;
    };
    uiDataAddr -= uiStride;
    *(P_TgUINTPTR)uiDataAddr = 0;
}


/* ---- tgCM_UTM_AM_ST_Free --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_AM_ST_Free( PCU_STg2_UTM_AM_ST UNUSED_PARAM psMP_ST )
{
}


/* ---- tgCM_UTM_AM_ST_Push --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_AM_ST_Push( PCU_STg2_UTM_AM_ST psMP_ST, PCU_STg2_UTM_Node psMP_Node )
{
    STg2_UTM_AM_PTR                     sOrig;

    do
    {
        sOrig.m_pNode = psMP_ST->m_sTop.m_pNode;
        sOrig.m_nuiVal = psMP_ST->m_sTop.m_nuiVal;
        psMP_Node->m_pNext_Node = psMP_ST->m_sTop.m_pNode;
    }
    while (TgTRUE != tgAMXX_XCMP2( (P_TgUINTXX)&(psMP_ST->m_sTop), sOrig.m_nuiVal + 1, (TgUINTPTR)psMP_Node, (P_TgUINTXX)&sOrig ));
}


/* ---- tgCM_UTM_AM_ST_Pop ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE P_STg2_UTM_Node tgCM_UTM_AM_ST_Pop( PCU_STg2_UTM_AM_ST psMP_ST )
{
    STg2_UTM_AM_PTR                     sOrig;
    volatile TgATOMIC_PVOID             pNext_Node;

    do
    {
        sOrig.m_pNode = psMP_ST->m_sTop.m_pNode;
        sOrig.m_nuiVal = psMP_ST->m_sTop.m_nuiVal;
        if (nullptr == sOrig.m_pNode)
        {
            return (nullptr);
        };
        pNext_Node = ((P_STg2_UTM_Node)sOrig.m_pNode)->m_pNext_Node;
    }
    while (TgTRUE != tgAMXX_XCMP2( (P_TgUINTXX)&(psMP_ST->m_sTop), sOrig.m_nuiVal + 1, (TgUINTPTR)pNext_Node, (P_TgUINTXX)&sOrig ));

    return ((P_STg2_UTM_Node)sOrig.m_pNode);
}


/* ---- tgCM_UTM_AM_ST_Release ------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE P_STg2_UTM_Node tgCM_UTM_AM_ST_Release( PCU_STg2_UTM_AM_ST psMP_ST )
{
    STg2_UTM_AM_PTR                     sOrig;
    volatile TgATOMIC_PVOID             pNext_Node;

    do
    {
        sOrig.m_pNode = psMP_ST->m_sTop.m_pNode;
        sOrig.m_nuiVal = psMP_ST->m_sTop.m_nuiVal;
        if (nullptr == sOrig.m_pNode)
        {
            return (nullptr);
        };
        pNext_Node = nullptr;
    }
    while (TgTRUE != tgAMXX_XCMP2( (P_TgUINTXX)&(psMP_ST->m_sTop), sOrig.m_nuiVal + 1, (TgUINTPTR)pNext_Node, (P_TgUINTXX)&sOrig ));

    return ((P_STg2_UTM_Node)sOrig.m_pNode);
}


/* ---- tgCM_UTM_AM_ST_Merge -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_AM_ST_Merge( PCU_STg2_UTM_AM_ST psMP_ST, P_STg2_UTM_Node psMP_Head, P_STg2_UTM_Node psMP_Tail )
{
    STg2_UTM_AM_PTR                     sOrig;

    do
    {
        sOrig.m_pNode = psMP_ST->m_sTop.m_pNode;
        sOrig.m_nuiVal = psMP_ST->m_sTop.m_nuiVal;
        psMP_Tail->m_pNext_Node = psMP_ST->m_sTop.m_pNode;
    }
    while (TgTRUE != tgAMXX_XCMP2( (P_TgUINTXX)&(psMP_ST->m_sTop), sOrig.m_nuiVal + 1, (TgUINTPTR)psMP_Head, (P_TgUINTXX)&sOrig ));
}


/* ---- tgCM_UTM_AM_ST_Pop_Wait_Yield ----------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE P_STg2_UTM_Node tgCM_UTM_AM_ST_Pop_Wait_Yield( PCU_STg2_UTM_AM_ST psMP_ST )
{
    P_STg2_UTM_Node                     pResult;

    while (1)
    {
        pResult = tgCM_UTM_AM_ST_Pop( psMP_ST );
        if (nullptr != pResult)
        {
            break;
        };
        tgTR_Yield();
    }

    return (pResult);
}


/* ---- tgCM_UTM_AM_ST_Pop_Wait_Spin ------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE P_STg2_UTM_Node tgCM_UTM_AM_ST_Pop_Wait_Spin( PCU_STg2_UTM_AM_ST psMP_ST )
{
    P_STg2_UTM_Node                     pResult;

    while (1)
    {
        pResult = tgCM_UTM_AM_ST_Pop( psMP_ST );
        if (nullptr != pResult)
        {
            break;
        };
        tgTR_Pause();
    }

    return (pResult);

}


/* ---- tgCM_UTM_AM_ST_Is_Empty ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL tgCM_UTM_AM_ST_Is_Empty( PCU_STg2_UTM_AM_ST psMP_ST )
{
    return (nullptr == psMP_ST->m_sTop.m_pNode ? TgTRUE : TgFALSE);
}


/* ---- Critical Section Stack -------------------------------------------------------------------------------------------------------------------------------------------- */

#if TgCOMPILE_THREAD

/* ---- tgCM_UTM_ST_Init ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_ST_Init( PCU_STg2_UTM_ST psMP_ST )
{
    TgVERIFY( KTgS_OK == tgCM_MP_CS_Init( &psMP_ST->m_sLock ) );
    TgVERIFY( KTgS_OK == tgCM_MP_SM_Init( &psMP_ST->m_sStack_Count, (TgUINT32)KTgMAX_S32 ) );
    psMP_ST->m_psTop_Node = nullptr;
}


/* ---- tgCM_UTM_ST_Free ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_ST_Free( PCU_STg2_UTM_ST psMP_ST )
{
    TgERROR(nullptr == psMP_ST->m_psTop_Node);
    tgCM_MP_SM_Free( &psMP_ST->m_sStack_Count );
    tgCM_MP_CS_Free( &psMP_ST->m_sLock );
}


/* ---- tgCM_UTM_ST_Push ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgCM_UTM_ST_Push( PCU_STg2_UTM_ST psMP_ST, PCU_STg2_UTM_Node psMP_Node )
{
    tgCM_MP_CS_Enter_Block( &psMP_ST->m_sLock );

    psMP_Node->m_pNext_Node = (TgATOMIC_PVOID)psMP_ST->m_psTop_Node;
    psMP_ST->m_psTop_Node = psMP_Node;

    tgCM_MP_CS_Exit( &psMP_ST->m_sLock );

    tgCM_MP_SM_Signal( &psMP_ST->m_sStack_Count, 1 );
}


/* ---- tgCM_UTM_ST_Pop ------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE P_STg2_UTM_Node tgCM_UTM_ST_Pop( PCU_STg2_UTM_ST psMP_ST )
{
    P_STg2_UTM_Node                     psTop_Node;

    if (TgFAILED( tgCM_MP_SM_Wait( &psMP_ST->m_sStack_Count ) ))
    {
        return (nullptr);
    };

    tgCM_MP_CS_Enter_Block( &psMP_ST->m_sLock );
    TgERROR(nullptr != psMP_ST->m_psTop_Node);

    psTop_Node = psMP_ST->m_psTop_Node;
    psMP_ST->m_psTop_Node = (P_STg2_UTM_Node)psTop_Node->m_pNext_Node;

    tgCM_MP_CS_Exit( &psMP_ST->m_sLock );

    return (psTop_Node);
}


/* ---- tgCM_UTM_ST_Pop_Wait_Block -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE P_STg2_UTM_Node tgCM_UTM_ST_Pop_Wait_Block( PCU_STg2_UTM_ST psMP_ST )
{
    P_STg2_UTM_Node                     psTop_Node;

    tgCM_MP_SM_Wait_Block( &psMP_ST->m_sStack_Count );

    tgCM_MP_CS_Enter_Block( &psMP_ST->m_sLock );
    TgERROR(nullptr != psMP_ST->m_psTop_Node);

    psTop_Node = psMP_ST->m_psTop_Node;
    psMP_ST->m_psTop_Node = (P_STg2_UTM_Node)psTop_Node->m_pNext_Node;

    tgCM_MP_CS_Exit( &psMP_ST->m_sLock );

    return (psTop_Node);
}


/* ---- tgCM_UTM_ST_Is_Empty -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL tgCM_UTM_ST_Is_Empty( PCU_STg2_UTM_ST psMP_ST )
{
    return (nullptr == psMP_ST->m_psTop_Node ? TgTRUE : TgFALSE);
}

/*# TgCOMPILE_THREAD */
#endif


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