extern "C"
{
#include <unistd.h>
}
#if !defined(__COMMON_HPP)
#include <corelinux/Common.hpp>
#endif
#if !defined(__EVENTSEMAPHOREGROUP_HPP)
#include <corelinux/EventSemaphoreGroup.hpp>
#endif
#if !defined(__EVENTSEMAPHORE_HPP)
#include <corelinux/EventSemaphore.hpp>
#endif
#if !defined(__THREAD_HPP)
#include <corelinux/Thread.hpp>
#endif
#if !defined(__EVENTCONTEXT_HPP)
#include <EventContext.hpp>
#endif
using namespace corelinux;
#include <iostream>
#include <exception>
int main( void );
void handleAssertion( AssertionCref );
void handleException( ExceptionCref );
Int eventTest( EventSemaphoreGroupPtr );
static SemaphoreIdentifier gSemId(0);
const ThreadIdentifier badThread(-1);
int main( void )
{
cout << endl;
EventSemaphorePtr aEventSem( NULLPTR );
EventSemaphoreGroup aGroup( 1 );
try
{
aEventSem = EventSemaphorePtr
(
aGroup.createSemaphore( gSemId, FAIL_IF_EXISTS )
);
EventContext aContext1(eventTest,&aGroup);
ThreadIdentifier aThread1( Thread::startThread(aContext1) );
if( aThread1 == badThread )
{
cout << "Thread error!" << endl;
}
else
{
cout << "Thread 1 created!" << endl;
}
sleep(1);
EventContext aContext2(eventTest,&aGroup);
ThreadIdentifier aThread2( Thread::startThread(aContext2) );
if( aThread2 == badThread )
{
cout << "Thread error!" << endl;
}
else
{
cout << "Thread 2 created!" << endl;
}
sleep(1);
while (1)
{
cout << "Release event semaphore" << endl;
aEventSem->release();
sleep(2);
aEventSem->post();
}
cout << "Cleaning up" << endl;
cout << "Return code from thread1 wait = " <<
Thread::waitForThread(aThread1) << endl;
cout << "Return code from thread2 wait = " <<
Thread::waitForThread(aThread2) << endl;
aGroup.destroySemaphore( aEventSem );
aEventSem = NULLPTR;
}
catch( NullPointerException aException )
{
cerr << "Received NullPointerException!" << endl;
handleException(aException);
}
catch( AssertionRef aAssert )
{
handleAssertion(aAssert);
}
catch( ExceptionRef aException )
{
handleException(aException);
}
catch( std::exception & e )
{
cerr << e.what() << endl;
}
catch( ... )
{
cerr << "Unknown exception." << endl;
}
if( aEventSem != NULLPTR )
{
aGroup.destroySemaphore( aEventSem );
aEventSem = NULLPTR;
}
else
{
;
}
return 0;
}
Int eventTest( EventSemaphoreGroupPtr aGroupPtr )
{
Int rc(-1);
if( aGroupPtr != NULLPTR )
{
EventSemaphorePtr aEventSem( NULLPTR );
try
{
aEventSem = EventSemaphorePtr
(
aGroupPtr->createSemaphore( gSemId, FAIL_IF_NOTEXISTS )
);
while (1)
{
rc = aEventSem->lockWithWait();
if ( rc == SUCCESS )
cout << "Semaphore obtained" << endl;
}
aGroupPtr->destroySemaphore( aEventSem );
aEventSem = NULLPTR;
rc = 0;
}
catch( ExceptionRef aException )
{
handleException(aException);
}
catch( ... )
{
cerr << "Unknown exception received" << endl;
}
if( aEventSem != NULLPTR )
{
aGroupPtr->destroySemaphore( aEventSem );
aEventSem = NULLPTR;
}
else
{
;
}
}
else
{
cout << "Ugly situation here" << endl;
}
return rc;
}
void handleAssertion( AssertionCref aAssert )
{
cerr << aAssert.getFile() << ":" << aAssert.getLine() << ":" <<
"Assertion: ";
if( aAssert.getType() == Assertion::NEVERGETHERE )
{
cerr << "NEVER_GET_HERE";
}
else
{
if( aAssert.getType() == Assertion::REQUIRE )
{
cerr << "REQUIRE";
}
else if( aAssert.getType() == Assertion::ENSURE )
{
cerr << "ENSURE";
}
else if( aAssert.getType() == Assertion::CHECK )
{
cerr << "CHECK";
}
else
{
cerr << "ASSERT";
}
cerr << "( " << aAssert.getWhy() << " )";
}
cerr << endl;
}
void handleException( ExceptionCref aExcp )
{
cerr << aExcp.getFile() << ":" << aExcp.getLine() << ":" <<
"Exception: " << aExcp.getWhy() << endl;
}