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
|
2024-03-26 13:36:50 +01:00
|
|
|
# include <pthread.h>
|
2022-03-21 11:05:59 +01:00
|
|
|
#endif
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
#define CHF_MODULE_ID 255
|
2022-03-21 11:05:59 +01:00
|
|
|
#define CHF_EXTENDED_INFO
|
|
|
|
#include "Chf.h"
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
#define H_STACK_SIZE 10
|
|
|
|
#define C_STACK_SIZE 30
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
void* task( void* arg )
|
2022-03-21 11:05:59 +01:00
|
|
|
{
|
|
|
|
volatile int phase = 0;
|
|
|
|
|
|
|
|
ChfTry
|
2024-03-26 13:36:50 +01:00
|
|
|
{
|
|
|
|
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
|
2024-03-26 13:36:50 +01:00
|
|
|
{
|
|
|
|
/* 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 */
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfTry { const volatile ChfDescriptor* e = ChfGetTopCondition(); }
|
2022-03-21 11:05:59 +01:00
|
|
|
ChfCatch
|
2024-03-26 13:36:50 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
return ( void* )EXIT_SUCCESS;
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
#define N_THREADS 50
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
int main( int argc, char* argv[] )
|
2022-03-21 11:05:59 +01:00
|
|
|
{
|
|
|
|
int st;
|
|
|
|
int i;
|
2024-03-26 13:36:50 +01:00
|
|
|
void* ret;
|
2022-03-21 11:05:59 +01:00
|
|
|
|
|
|
|
#ifdef _REENTRANT
|
2024-03-26 13:36:50 +01:00
|
|
|
pthread_t t[ N_THREADS ];
|
2022-03-21 11:05:59 +01:00
|
|
|
#endif
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
puts( "test06" );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
|
|
|
/* Initialization */
|
2024-03-26 13:36:50 +01:00
|
|
|
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 */
|
2024-03-26 13:36:50 +01:00
|
|
|
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 */
|
2024-03-26 13:36:50 +01:00
|
|
|
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
|
2024-03-26 13:36:50 +01:00
|
|
|
st = ( int )task( ( void* )0 );
|
2022-03-21 11:05:59 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Exit Chf */
|
|
|
|
ChfExit();
|
2024-03-26 13:36:50 +01:00
|
|
|
exit( st );
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|