saturnng/libChf/test06.c

123 lines
2.8 KiB
C
Raw Normal View History

2022-03-21 11:05:59 +01:00
/* $Id: test06.c,v 2.1 2000/05/29 13:10:55 cibrario Rel $
Chf test program.
Structured condition handling - single and multithreaded
$Log: test06.c,v $
Revision 2.1 2000/05/29 13:10:55 cibrario
*** empty log message ***
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <setjmp.h>
#ifdef _REENTRANT
# include <pthread.h>
2022-03-21 11:05:59 +01:00
#endif
#define CHF_MODULE_ID 255
2022-03-21 11:05:59 +01:00
#define CHF_EXTENDED_INFO
#include "Chf.h"
#define H_STACK_SIZE 10
#define C_STACK_SIZE 30
2022-03-21 11:05:59 +01:00
void* task( void* arg )
2022-03-21 11:05:59 +01:00
{
volatile int phase = 0;
ChfTry
{
phase = 1;
ChfCondition 20, CHF_SUCCESS ChfEnd;
ChfSignal();
phase = 2;
ChfCondition 20, CHF_INFO ChfEnd;
ChfSignal();
phase = 3;
ChfCondition 20, CHF_WARNING ChfEnd;
ChfSignal();
phase = 4;
ChfCondition 20, CHF_ERROR ChfEnd;
ChfSignal();
phase = 5;
ChfCondition 20, CHF_FATAL ChfEnd;
ChfSignal();
/* Should not be reached */
return ( void* )EXIT_FAILURE;
}
2022-03-21 11:05:59 +01:00
ChfCatch
{
/* Catched an exception; check descriptor */
const ChfDescriptor* d = ChfGetTopCondition();
if ( d == NULL || ChfGetNextDescriptor( d ) != NULL || ChfGetModuleId( d ) != CHF_MODULE_ID || ChfGetConditionCode( d ) != 20 )
return ( void* )EXIT_FAILURE;
}
2022-03-21 11:05:59 +01:00
ChfEndTry;
/* Check that the condition stack actually is empty after catch */
ChfTry { const volatile ChfDescriptor* e = ChfGetTopCondition(); }
2022-03-21 11:05:59 +01:00
ChfCatch
{
const ChfDescriptor* d = ChfGetTopCondition();
if ( d == NULL || ChfGetNextDescriptor( d ) != NULL || ChfGetModuleId( d ) != CHF_SET ||
ChfGetConditionCode( d ) != CHF_F_BAD_STATE )
return ( void* )EXIT_FAILURE;
}
2022-03-21 11:05:59 +01:00
ChfEndTry;
return ( void* )EXIT_SUCCESS;
2022-03-21 11:05:59 +01:00
}
#define N_THREADS 50
2022-03-21 11:05:59 +01:00
int main( int argc, char* argv[] )
2022-03-21 11:05:59 +01:00
{
int st;
int i;
void* ret;
2022-03-21 11:05:59 +01:00
#ifdef _REENTRANT
pthread_t t[ N_THREADS ];
2022-03-21 11:05:59 +01:00
#endif
puts( "test06" );
2022-03-21 11:05:59 +01:00
/* Initialization */
if ( st = ChfMsgcatInit( argv[ 0 ], CHF_DEFAULT, "./test01.cat", C_STACK_SIZE, H_STACK_SIZE, EXIT_FAILURE ) )
exit( st );
2022-03-21 11:05:59 +01:00
#ifdef _REENTRANT
/* Create */
for ( i = 0; i < N_THREADS; i++ )
if ( pthread_create( &( t[ i ] ), NULL, task, ( void* )i ) ) {
perror( "pthread_create" );
exit( EXIT_FAILURE );
}
2022-03-21 11:05:59 +01:00
/* Join */
for ( i = 0; i < N_THREADS; i++ ) {
if ( pthread_join( t[ i ], &ret ) ) {
perror( "pthread_join" );
exit( EXIT_FAILURE );
} else if ( ( int )ret != EXIT_SUCCESS )
exit( ( int )ret );
2022-03-21 11:05:59 +01:00
}
st = EXIT_SUCCESS;
#else
st = ( int )task( ( void* )0 );
2022-03-21 11:05:59 +01:00
#endif
/* Exit Chf */
ChfExit();
exit( st );
2022-03-21 11:05:59 +01:00
}