2022-03-21 11:05:59 +01:00
|
|
|
/* $Id: test04.c,v 2.1 2000/05/29 13:10:38 cibrario Rel $
|
|
|
|
Chf test program.
|
|
|
|
General condition handling - single and multithreaded
|
|
|
|
|
|
|
|
$Log: test04.c,v $
|
|
|
|
Revision 2.1 2000/05/29 13:10:38 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
|
|
|
struct tdata_s {
|
2022-03-21 11:05:59 +01:00
|
|
|
const ChfDescriptor *d, *e;
|
|
|
|
int phase;
|
|
|
|
};
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfAction h1( const ChfDescriptor* c, const ChfState s, ChfPointer p )
|
2022-03-21 11:05:59 +01:00
|
|
|
{
|
2024-03-26 13:36:50 +01:00
|
|
|
struct tdata_s* tdata_p = ( struct tdata_s* )p;
|
2022-03-21 11:05:59 +01:00
|
|
|
ChfAction action;
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
if ( c != tdata_p->e || ChfGetNextDescriptor( c ) != tdata_p->d ) {
|
|
|
|
ChfCondition 10, CHF_FATAL ChfEnd;
|
|
|
|
action = CHF_RESIGNAL;
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2024-03-26 13:36:50 +01:00
|
|
|
action = CHF_CONTINUE;
|
2022-03-21 11:05:59 +01:00
|
|
|
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
struct tdata_s* tdata_p = ( struct tdata_s* )p;
|
2022-03-21 11:05:59 +01:00
|
|
|
ChfAction action;
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
switch ( s ) {
|
|
|
|
case CHF_SIGNALING:
|
|
|
|
{
|
|
|
|
if ( c != tdata_p->e || ChfGetNextDescriptor( c ) != tdata_p->d || ( tdata_p->phase != 2 && tdata_p->phase != 4 ) ) {
|
|
|
|
ChfCondition 10, CHF_FATAL ChfEnd;
|
|
|
|
action = CHF_RESIGNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
action = ( ChfGetConditionCode( c ) != 8 ? CHF_CONTINUE : CHF_UNWIND );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CHF_UNWINDING:
|
|
|
|
{
|
|
|
|
if ( tdata_p->phase != 4 )
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
tdata_p->phase = 5;
|
|
|
|
action = CHF_CONTINUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
struct tdata_s* tdata_p = ( struct tdata_s* )p;
|
2022-03-21 11:05:59 +01:00
|
|
|
ChfAction action;
|
|
|
|
|
|
|
|
/* This handler must be invoked only during the first signal */
|
2024-03-26 13:36:50 +01:00
|
|
|
if ( tdata_p->phase != 3 )
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
|
|
|
|
switch ( s ) {
|
|
|
|
case CHF_SIGNALING:
|
|
|
|
{
|
|
|
|
if ( ChfGetConditionCode( c ) != 9 || ChfGetNextDescriptor( c ) != NULL ) {
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
tdata_p->phase = 4;
|
|
|
|
action = CHF_CONTINUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfAction h4( const ChfDescriptor* c, const ChfState s, ChfPointer p )
|
2022-03-21 11:05:59 +01:00
|
|
|
{
|
2024-03-26 13:36:50 +01:00
|
|
|
struct tdata_s* tdata_p = ( struct tdata_s* )p;
|
2022-03-21 11:05:59 +01:00
|
|
|
ChfAction action;
|
|
|
|
|
|
|
|
/* This handler must be invoked only during the first signal */
|
2024-03-26 13:36:50 +01:00
|
|
|
if ( tdata_p->phase != 2 )
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
|
|
|
|
switch ( s ) {
|
|
|
|
case CHF_SIGNALING:
|
|
|
|
{
|
|
|
|
if ( c != tdata_p->e || ChfGetNextDescriptor( c ) != tdata_p->d ) {
|
|
|
|
ChfCondition 10, CHF_FATAL ChfEnd;
|
|
|
|
action = CHF_RESIGNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
/* This generates a new group and signals it */
|
|
|
|
tdata_p->phase = 3;
|
|
|
|
ChfCondition 9, CHF_INFO ChfEnd;
|
|
|
|
ChfSignal();
|
|
|
|
|
|
|
|
if ( tdata_p->phase != 4 )
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
tdata_p->phase = 5;
|
|
|
|
|
|
|
|
if ( c != tdata_p->e || ChfGetNextDescriptor( c ) != tdata_p->d ) {
|
|
|
|
ChfCondition 10, CHF_FATAL ChfEnd;
|
|
|
|
action = CHF_RESIGNAL;
|
|
|
|
} else
|
|
|
|
action = CHF_CONTINUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
volatile struct tdata_s tdata;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* Push the handler */
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfPushHandler( h1, NULL, ( ChfPointer )( &tdata ) );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
|
|
|
/* Generate a condition group and signal it */
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfCondition 6, CHF_INFO, ( int )arg ChfEnd;
|
2022-03-21 11:05:59 +01:00
|
|
|
tdata.d = ChfGetTopCondition();
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfCondition 7, CHF_INFO, ( int )arg ChfEnd;
|
2022-03-21 11:05:59 +01:00
|
|
|
tdata.e = ChfGetTopCondition();
|
|
|
|
|
|
|
|
/* 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
|
|
|
ChfSignal();
|
|
|
|
|
|
|
|
/* Pop the handler */
|
|
|
|
ChfPopHandler();
|
|
|
|
|
|
|
|
/* Generate a new condition group with (apparently) wrong linkage
|
|
|
|
and signal it; this checks that the handler has actually been
|
|
|
|
removed.
|
|
|
|
*/
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfCondition 6, CHF_INFO, ( int )arg ChfEnd;
|
2022-03-21 11:05:59 +01:00
|
|
|
tdata.d = NULL;
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfCondition 7, CHF_INFO, ( int )arg ChfEnd;
|
2022-03-21 11:05:59 +01:00
|
|
|
tdata.e = NULL;
|
|
|
|
ChfSignal();
|
|
|
|
|
|
|
|
/* Conditional unwind test */
|
|
|
|
{
|
2024-03-26 13:36:50 +01:00
|
|
|
sigjmp_buf jb;
|
|
|
|
|
|
|
|
tdata.phase = 0;
|
|
|
|
if ( setjmp( jb ) == 0 ) {
|
|
|
|
ChfPushHandler( h2, jb, ( ChfPointer )( &tdata ) );
|
|
|
|
|
|
|
|
/* Generate a condition group and signal it */
|
|
|
|
tdata.phase = 1;
|
|
|
|
ChfCondition 6, CHF_INFO, ( int )arg ChfEnd;
|
|
|
|
tdata.d = ChfGetTopCondition();
|
|
|
|
ChfCondition 7, CHF_INFO, ( int )arg ChfEnd;
|
|
|
|
tdata.e = ChfGetTopCondition();
|
|
|
|
|
|
|
|
/* This does not trigger an unwind */
|
|
|
|
tdata.phase = 2;
|
|
|
|
ChfSignal();
|
|
|
|
|
|
|
|
tdata.phase = 3;
|
|
|
|
ChfCondition 6, CHF_INFO, ( int )arg ChfEnd;
|
|
|
|
tdata.d = ChfGetTopCondition();
|
|
|
|
ChfCondition 8, CHF_INFO, ( int )arg ChfEnd;
|
|
|
|
tdata.e = ChfGetTopCondition();
|
|
|
|
|
|
|
|
/* This MUST trigger an unwind */
|
|
|
|
tdata.phase = 4;
|
|
|
|
ChfSignal();
|
|
|
|
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Unwind */
|
|
|
|
if ( tdata.phase != 5 )
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
|
|
|
|
ChfPopHandler();
|
|
|
|
}
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Condition generation and signal while a signal is in progress;
|
|
|
|
this requires two handlers.
|
|
|
|
*/
|
|
|
|
{
|
2024-03-26 13:36:50 +01:00
|
|
|
tdata.phase = 0;
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfPushHandler( h3, NULL, ( ChfPointer )&tdata );
|
|
|
|
ChfPushHandler( h4, NULL, ( ChfPointer )&tdata );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
tdata.phase = 1;
|
|
|
|
ChfCondition 6, CHF_INFO, ( int )arg ChfEnd;
|
|
|
|
tdata.d = ChfGetTopCondition();
|
|
|
|
ChfCondition 7, CHF_INFO, ( int )arg ChfEnd;
|
|
|
|
tdata.e = ChfGetTopCondition();
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
tdata.phase = 2;
|
|
|
|
ChfSignal();
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
if ( tdata.phase != 5 )
|
|
|
|
exit( EXIT_FAILURE );
|
2022-03-21 11:05:59 +01:00
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
ChfPopHandler();
|
|
|
|
ChfPopHandler();
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|
|
|
|
|
2024-03-26 13:36:50 +01:00
|
|
|
return ( void* )0;
|
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( "test04" );
|
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", 50, 10, 1 ) )
|
|
|
|
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 );
|
|
|
|
}
|
2022-03-21 11:05:59 +01:00
|
|
|
#else
|
2024-03-26 13:36:50 +01:00
|
|
|
task( ( void* )0 );
|
2022-03-21 11:05:59 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Exit Chf */
|
|
|
|
ChfExit();
|
2024-03-26 13:36:50 +01:00
|
|
|
exit( EXIT_SUCCESS );
|
2022-03-21 11:05:59 +01:00
|
|
|
}
|