2022-03-21 11:05:59 +01:00
|
|
|
/* $Id: test05.c,v 2.1 2000/05/29 13:10:46 cibrario Rel $
|
|
|
|
Chf test program.
|
|
|
|
Condition & Handler stacks oveflow checks - single and multithreaded
|
|
|
|
|
|
|
|
$Log: test05.c,v $
|
|
|
|
Revision 2.1 2000/05/29 13:10:46 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
|
|
|
|
|
|
|
/* Dummy handler; pushed only to verify that the handler stack overflow
|
|
|
|
checks are correct.
|
|
|
|
*/
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfAction h1( const ChfDescriptor* c, const ChfState s, ChfPointer p ) { return CHF_RESIGNAL; }
|
2022-03-21 11:05:59 +01:00
|
|
|
|
|
|
|
/* Overflow check handler; it unwinds if the CHF_F_HDLR_STACK_FULL
|
|
|
|
condition is signalled exactly after H_STACK_SIZE-2 invocations
|
|
|
|
of ChfPushHandler(), it resignals a modified condition if the
|
|
|
|
condition is signalled too early
|
|
|
|
*/
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfAction h2( const ChfDescriptor* c, const ChfState s, ChfPointer p )
|
2022-03-21 11:05:59 +01:00
|
|
|
{
|
2024-03-26 13:36:50 +01:00
|
|
|
int push_count = *( ( int* )p );
|
2022-03-21 11:05:59 +01:00
|
|
|
ChfAction action;
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
if ( s == CHF_SIGNALING ) {
|
|
|
|
if ( ChfGetModuleId( c ) == CHF_SET && ChfGetConditionCode( c ) == CHF_F_HDLR_STACK_FULL ) {
|
|
|
|
/* Handler stack is full; check correctness of the descriptor */
|
|
|
|
if ( push_count == H_STACK_SIZE - 2 && ChfGetNextDescriptor( c ) == NULL && ChfGetSeverity( c ) == CHF_FATAL )
|
|
|
|
action = CHF_UNWIND;
|
|
|
|
else {
|
|
|
|
ChfCondition 11, CHF_FATAL, push_count, H_STACK_SIZE - 2 ChfEnd;
|
|
|
|
action = CHF_RESIGNAL;
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2024-03-26 13:36:50 +01:00
|
|
|
action = CHF_RESIGNAL;
|
2022-03-21 11:05:59 +01:00
|
|
|
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Overflow check handler; it unwinds if the CHF_F_COND_STACK_FULL
|
|
|
|
condition is signalled exactly after C_STACK_SIZE invocations
|
|
|
|
of ChfCondition, it resignals a modified condition if the
|
|
|
|
condition is signalled too early
|
|
|
|
*/
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfAction h3( const ChfDescriptor* c, const ChfState s, ChfPointer p )
|
2022-03-21 11:05:59 +01:00
|
|
|
{
|
2024-03-26 13:36:50 +01:00
|
|
|
int push_count = *( ( int* )p );
|
2022-03-21 11:05:59 +01:00
|
|
|
ChfAction action;
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
if ( s == CHF_SIGNALING ) {
|
|
|
|
if ( ChfGetModuleId( c ) == CHF_SET && ChfGetConditionCode( c ) == CHF_F_COND_STACK_FULL ) {
|
|
|
|
/* Handler stack is full; check correctness of the descriptor */
|
|
|
|
if ( push_count == C_STACK_SIZE && ChfGetNextDescriptor( c ) == NULL && ChfGetSeverity( c ) == CHF_FATAL )
|
|
|
|
action = CHF_UNWIND;
|
|
|
|
else {
|
|
|
|
ChfCondition 12, CHF_FATAL, push_count, C_STACK_SIZE ChfEnd;
|
|
|
|
action = CHF_RESIGNAL;
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2024-03-26 13:36:50 +01:00
|
|
|
action = CHF_RESIGNAL;
|
2022-03-21 11:05:59 +01:00
|
|
|
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
void* task( void* arg )
|
2022-03-21 11:05:59 +01:00
|
|
|
{
|
|
|
|
int push_count = 0;
|
|
|
|
sigjmp_buf jb;
|
|
|
|
|
|
|
|
/* The sleep() is here to increase contention between threads */
|
2024-03-26 13:36:50 +01:00
|
|
|
sleep( 1 );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
printf( "\tThread %d\n", ( int )arg );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
|
|
|
/* Check handler stack overflow checks */
|
2024-03-26 13:36:50 +01:00
|
|
|
if ( sigsetjmp( jb, 1 ) == 0 ) {
|
|
|
|
int i;
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* Push the handler */
|
|
|
|
ChfPushHandler( h2, jb, ( ChfPointer )( &push_count ) );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* The sleep() is here to increase contention between threads */
|
|
|
|
sleep( 1 );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* Push dummy handlers until an error should occur */
|
|
|
|
for ( ; push_count < H_STACK_SIZE - 1; push_count++ )
|
|
|
|
ChfPushHandler( h1, NULL, NULL );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* No error? Bad! */
|
|
|
|
return ( void* )EXIT_FAILURE;
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Flow control returns here if 'handler stack full' was signalled
|
|
|
|
at the correct place.
|
|
|
|
Check condition stack overflow checks
|
|
|
|
*/
|
|
|
|
push_count = 0;
|
2024-03-26 13:36:50 +01:00
|
|
|
if ( sigsetjmp( jb, 1 ) == 0 ) {
|
|
|
|
int i;
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* Push the handler */
|
|
|
|
ChfPushHandler( h3, jb, ( ChfPointer )( &push_count ) );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* The sleep() is here to increase contention between threads */
|
|
|
|
sleep( 1 );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* Push dummy conditions until an error should occur */
|
|
|
|
for ( ; push_count <= C_STACK_SIZE; push_count++ )
|
|
|
|
ChfCondition 1, CHF_INFO ChfEnd;
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* No error? Bad! */
|
|
|
|
return ( void* )EXIT_FAILURE;
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Flow control returns here if 'condition stack full' was signalled
|
|
|
|
at the correct place.
|
|
|
|
Check condition stack overflow again, to ensure that no spurious
|
|
|
|
conditions were left out in the previous check.
|
|
|
|
*/
|
|
|
|
push_count = 0;
|
2024-03-26 13:36:50 +01:00
|
|
|
if ( sigsetjmp( jb, 1 ) == 0 ) {
|
|
|
|
int i;
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* Push the handler */
|
|
|
|
ChfPushHandler( h3, jb, ( ChfPointer )( &push_count ) );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* The sleep() is here to increase contention between threads */
|
|
|
|
sleep( 1 );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* Push dummy conditions until an error should occur */
|
|
|
|
for ( ; push_count <= C_STACK_SIZE; push_count++ )
|
|
|
|
ChfCondition 1, CHF_INFO ChfEnd;
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
/* No error? Bad! */
|
|
|
|
return ( void* )EXIT_FAILURE;
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
|
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( "test05" );
|
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
|
|
|
}
|