Home

Resume

Blog

Teikitu


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


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

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Types                                                                                                                                                          */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

TgTYPE_STRUCT_ALIGN(STg2_UTM_AM_ST,16,)
{
    STg2_UTM_AM_PTR                             m_sTop;
};

TgTYPE_STRUCT(STg2_UTM_ST,)
{
    STg1_MP_CS                                  m_sLock;
    STg1_MP_SM                                  m_sStack_Count;
    P_STg2_UTM_Node                             m_psTop_Node;
};

TgCOMPILER_ASSERT(2*TgCOMPILE_POINTER_SIZE == sizeof(STg2_UTM_AM_ST), 0);
TgTYPE_STRUCT_ALIGN(STg2_UTM_AM_ST_ISO,TgCOMPILE_CACHE_LINE_SIZE,)
{
    STg2_UTM_AM_ST                              m_sStack;
#if TgCOMPILE_CACHE_LINE_SIZE > 2*TgCOMPILE_POINTER_SIZE
    TgUINT08                                    m_uiPad[TgCOMPILE_CACHE_LINE_SIZE - 2 * TgCOMPILE_POINTER_SIZE];
#endif
};


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

/* ---- Atomic Stack ------------------------------------------------------------------------------------------------------------------------------------------------------ */
/* Note: This implementation is not entirely "correct".  It can not guarantee perfect ordering.  Imagine two threads attempting an insert into the stack, with the */
/* first thread being the earlier arrival.  If the first thread were preempted during its insertion then the second thread would succeed in pushing onto the stack */
/* prior to the completion of the first thread. */

TgINLINE TgVOID                             tgCM_UTM_AM_ST_Init( PCU_STg2_UTM_AM_ST );
TgINLINE TgVOID                             tgCM_UTM_AM_ST_Init_PreLoad( PCU_STg2_UTM_AM_ST, P_TgVOID, C_TgSIZE, C_TgSIZE );
TgINLINE TgVOID                             tgCM_UTM_AM_ST_Free( PCU_STg2_UTM_AM_ST );

TgINLINE TgVOID                             tgCM_UTM_AM_ST_Push( PCU_STg2_UTM_AM_ST, PCU_STg2_UTM_Node );

TgINLINE P_STg2_UTM_Node                    tgCM_UTM_AM_ST_Pop( PCU_STg2_UTM_AM_ST );

TgINLINE P_STg2_UTM_Node                    tgCM_UTM_AM_ST_Pop_Wait_Yield( PCU_STg2_UTM_AM_ST );
TgINLINE P_STg2_UTM_Node                    tgCM_UTM_AM_ST_Pop_Wait_Spin( PCU_STg2_UTM_AM_ST );

TgINLINE TgBOOL                             tgCM_UTM_AM_ST_Is_Empty( PCU_STg2_UTM_AM_ST );

                                            /* Pop (clear) the entire stack returning the former stack head */
TgINLINE P_STg2_UTM_Node                    tgCM_UTM_AM_ST_Release( PCU_STg2_UTM_AM_ST );

                                            /* Push (append) an entire list of notes onto the stack */
TgINLINE TgVOID                             tgCM_UTM_AM_ST_Merge( PCU_STg2_UTM_AM_ST, P_STg2_UTM_Node, P_STg2_UTM_Node );


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

#if TgCOMPILE_THREAD

TgINLINE TgVOID                             tgCM_UTM_ST_Init( PCU_STg2_UTM_ST );
TgINLINE TgVOID                             tgCM_UTM_ST_Free( PCU_STg2_UTM_ST );

TgINLINE TgVOID                             tgCM_UTM_ST_Push( PCU_STg2_UTM_ST, PCU_STg2_UTM_Node );

TgINLINE P_STg2_UTM_Node                    tgCM_UTM_ST_Pop( PCU_STg2_UTM_ST );

TgINLINE P_STg2_UTM_Node                    tgCM_UTM_ST_Pop_Wait_Block( PCU_STg2_UTM_ST );

TgINLINE TgBOOL                             tgCM_UTM_ST_Is_Empty( PCU_STg2_UTM_ST );

/*# TgCOMPILE_THREAD */
#endif


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