한 걸음씩..

[VxWorks] Critical Section 구현 본문

프로그래밍

[VxWorks] Critical Section 구현

smdy0426 2013. 7. 15. 15:29
반응형

/************************************

*    VxWorks에서 Critical Section이 없기 때문에

*    세마포어를 이용하여 구현

*    이걸 왜 만들었더라? 그냥 세마포어 쓰면 될 껄..

************************************/





/*세마포어를 크리티컬 섹션이란 이름으로 재 정의*/

typedef SEM_ID CRITICAL_SECTION;

typedef SEM_ID *LPTCRITICAL_SECTION;


/* 함수 원형 작성 */

BOOL CreateCriticalSection( LPTCRITICAL_SECTION CriticalSection );

BOOL DeleteCriticalSection( LPTCRITICAL_SECTION CriticalSection );

BOOL LockCriticalSection( LPTCRITICAL_SECTION CriticalSection );

BOOL UnlockCriticalSection( LPTCRITICAL_SECTION CriticalSection );


BOOL CreateCriticalSection( LPTCRITICAL_SECTION CriticalSection )

{

*CriticalSection = semMCreate( SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE );

if( *CriticalSection == 0 )

{

printf( "Semaphore Fail in CreateCriticalSection \n" );

return FALSE;

}

return TRUE;


}


BOOL DeleteCriticalSection( LPTCRITICAL_SECTION CriticalSection )

{

if( semDelete( *CriticalSection ) == ERROR )

{

printf( " Semaphore Error in DeleteCriticalSection\n" );

return FALSE;

}

 *CriticalSection = 0;

return TRUE;

}


BOOL LockCriticalSection( LPTCRITICAL_SECTION CriticalSection )

{

if( semTake( *CriticalSection, WAIT_FOREVER ) == ERROR )

{

printf( "Semaphore Error in LockCriticalSection\n" );

return FALSE;

}


return TRUE;

}


BOOL UnlockCriticalSection( LPTCRITICAL_SECTION CriticalSection )

{

if( semGive( *CriticalSection ) == ERROR )

{

printf( "Semaphore Error in UnlockCriticalSection\n" );

return FALSE;

}


return TRUE;

}

반응형