⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 semphore.c

📁 一个操作系统源代码 用于嵌入式设备 在Vc++环境下仿真 成功移植到多款处理器上
💻 C
字号:
////////////////////////////////////////////////////////////////////////////////
// File Name	:		semphore.c
// Create Date	:		2001-6-13 am 09:02:31
// Written by	:		
// Decription	:	
//------------------------------------------------------------------------------
// Copyright:   CNASIC Proprietary Material
//              Copyright (c) 2001, All Rights Reserved By:
//              National ASIC System Engineering Research Center (CNASIC) 
//				SouthEast University
//
//              DISTRIBUTION PROHIBITED without written authorization from CNASIC
//------------------------------------------------------------------------------
// Modification History
//
// 2001/10/31	by longn_qi		revise.
//
////////////////////////////////////////////////////////////////////////////////#include <kernel\ros33\ros33.h>
#include <kernel\ros33\ros33.h>

#define _SIZE_T_DEFINED

#include "internal.h"

//****************************************************************************
//***    3.3 Synchronization and Communication Functions                   ***
//****************************************************************************

//********************************************************************
//  Signal Semaphore
//  [Parameters]
//      ID  semid   SemaphoreID
//  [Return Parameters]
//      ER  ercd    ErrorCode
//********************************************************************
ER sig_sem( ID semid )
{
	ID			idTask;

#if ENABLE_SYSCALL_STATISTIC	
	gSyscallSta[15].sta++;
#endif

    if(semid <= 0)
    {
        return E_ID;
    }
    
    if(semid > SMPH_NUM)
    {
        return E_NOEXS;
    }

    if( aSmphcb[semid].ubSmphCnt >= aSmphcb[semid].ubSmphMax )
    {
        return E_QOVR;
    }

    if( aSmphcb[semid].idTask != 0 ) 
	{      
		// exist semaphore wait task
        idTask = aSmphcb[semid].idTask;

        aTcb[idTask].ubStatus &= ~TTS_WAI;
        aTcb[idTask].ubWaitStat &= ~TTW_SEM;

        AppendTaskToReadyQueue( idTask );

		ContextSwitch();
    } 
    else 
    {
        aSmphcb[semid].ubSmphCnt++;
    }

    return E_OK;
}


//********************************************************************
//  Wait on Semaphore
//  [Parameters]
//      ID  semid   SemaphoreID
//      TMO tmout   Timeout
//  [Return Parameters]
//      ER  ercd    ErrorCode
//********************************************************************
ER twai_sem( ID semid, TMO tmout )
{
#if ENABLE_SYSCALL_STATISTIC	
	gSyscallSta[18].sta++;
#endif

    if((g_ubIntNestCnt != 0)    // issued from task-independent portions
    || (g_ubSysStat & TSS_DDSP)) // task in dispatch disabled state
    {   
        return E_CTX;
    }

    if(semid <= 0)
    {
        return E_ID;
    }
    
    if(semid > SMPH_NUM)
    {
        return E_NOEXS;
    }
    if(tmout <= -2)
    {
        return E_PAR;
    }

    //  wait single object
    if( aSmphcb[semid].idTask )
        return E_OBJ;

    if( aSmphcb[semid].ubSmphCnt == 0 ) 
    {
        if (tmout == TMO_POL)
        {
            return E_TMOUT;
        }

        // wait status

		aTcb[idCurTask].ubStatus |= TTS_WAI;  
        aTcb[idCurTask].ubWaitStat |= TTW_SEM;
        aSmphcb[semid].idTask = idCurTask;
		        
		RemoveTaskFromReadyQueue( aTcb[idCurTask].bPriority, idCurTask );
        /* set time out */
        if(tmout != TMO_FEVR)
			SetTimer(	hwndClient, 
						idCurTask, 
						tmout, 
						( TIMERPROC ) PollingTimerHandler );

        ContextSwitch();
    } 
	else 
	{
        aSmphcb[semid].ubSmphCnt--;
    }
    return E_OK;
}

ER ini_sem( ID semid, UH smphMax, UH smphCnt )
{
    if( semid <= 0 || semid > SMPH_NUM )
        return E_ID;

    if( smphCnt > smphMax )
        return E_PAR;
    
	aSmphcb[semid].ubSmphMax = smphMax;
    aSmphcb[semid].ubSmphCnt = smphCnt;
	aSmphcb[semid].idTask = 0;

	return E_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -