forked from Imagelibrary/rtems
Init.c: added timewait case, added broadcast case
This commit is contained in:
@@ -23,7 +23,8 @@ void *POSIX_Init(
|
|||||||
pthread_t thread_id;
|
pthread_t thread_id;
|
||||||
pthread_condattr_t attr;
|
pthread_condattr_t attr;
|
||||||
int pshared;
|
int pshared;
|
||||||
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
pthread_cond_t cond;
|
||||||
|
struct timespec timeout;
|
||||||
|
|
||||||
puts( "\n\n*** POSIX TEST 10 ***" );
|
puts( "\n\n*** POSIX TEST 10 ***" );
|
||||||
|
|
||||||
@@ -89,17 +90,21 @@ void *POSIX_Init(
|
|||||||
status = pthread_cond_destroy( &cond );
|
status = pthread_cond_destroy( &cond );
|
||||||
assert( !status );
|
assert( !status );
|
||||||
|
|
||||||
|
/* initiailize the attribute for the rest of the test */
|
||||||
|
|
||||||
puts( "Init: pthread_cond_init - attr" );
|
puts( "Init: pthread_cond_init - attr" );
|
||||||
status = pthread_cond_init( &Cond1_id, &attr );
|
status = pthread_cond_init( &Cond1_id, &attr );
|
||||||
assert( !status );
|
assert( !status );
|
||||||
|
|
||||||
/* create a thread */
|
/* signal task1 with a condition variable */
|
||||||
|
|
||||||
empty_line();
|
empty_line();
|
||||||
|
|
||||||
status = pthread_create( &Task_id, NULL, Task_1, NULL );
|
status = pthread_create( &Task_id, NULL, Task_1, NULL );
|
||||||
assert( !status );
|
assert( !status );
|
||||||
|
|
||||||
|
/* switch to task1 to allow it to wait for a condition variable */
|
||||||
|
|
||||||
puts( "Init: sleep to switch to Task_1" );
|
puts( "Init: sleep to switch to Task_1" );
|
||||||
sleep( 1 );
|
sleep( 1 );
|
||||||
|
|
||||||
@@ -112,16 +117,36 @@ void *POSIX_Init(
|
|||||||
status = pthread_create( &Task2_id, NULL, Task_2, NULL );
|
status = pthread_create( &Task2_id, NULL, Task_2, NULL );
|
||||||
assert( !status );
|
assert( !status );
|
||||||
|
|
||||||
|
/* switch to task1 and task2 to allow them to wait for broadcast signal */
|
||||||
|
|
||||||
puts( "Init: sleep - switch to Task_1 and Task_2" );
|
puts( "Init: sleep - switch to Task_1 and Task_2" );
|
||||||
sleep( 1 );
|
sleep( 1 );
|
||||||
|
|
||||||
|
/* broadcast a condition variable to task1 and task2 */
|
||||||
|
|
||||||
puts( "Init: pthread_cond_broadcast" );
|
puts( "Init: pthread_cond_broadcast" );
|
||||||
status = pthread_cond_broadcast( &Cond1_id );
|
status = pthread_cond_broadcast( &Cond1_id );
|
||||||
assert( !status );
|
assert( !status );
|
||||||
|
|
||||||
puts( "Init: sleep - switch to Task_1" );
|
puts( "Init: sleep - switch to Task_1" );
|
||||||
sleep( 1 );
|
sleep( 0 );
|
||||||
|
|
||||||
|
/* timedwait case - timeout */
|
||||||
|
|
||||||
|
status = pthread_mutex_lock( &Mutex_id );
|
||||||
|
assert( !status );
|
||||||
|
|
||||||
|
/* set timeout to 3 seconds */
|
||||||
|
|
||||||
|
timeout.tv_sec = 3;
|
||||||
|
|
||||||
|
puts( "Init: pthread_cond_timedwait for 3 seconds" );
|
||||||
|
status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
|
||||||
|
if ( status != ETIMEDOUT )
|
||||||
|
printf( "status = %d\n", status );
|
||||||
|
assert( status == ETIMEDOUT );
|
||||||
|
|
||||||
|
puts( "Init: timedout on pthread_cond_timedwait release mutex" );
|
||||||
/* exit this thread */
|
/* exit this thread */
|
||||||
|
|
||||||
puts( "*** END OF POSIX TEST 5 ***" );
|
puts( "*** END OF POSIX TEST 5 ***" );
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ void *POSIX_Init(
|
|||||||
pthread_t thread_id;
|
pthread_t thread_id;
|
||||||
pthread_condattr_t attr;
|
pthread_condattr_t attr;
|
||||||
int pshared;
|
int pshared;
|
||||||
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
pthread_cond_t cond;
|
||||||
|
struct timespec timeout;
|
||||||
|
|
||||||
puts( "\n\n*** POSIX TEST 10 ***" );
|
puts( "\n\n*** POSIX TEST 10 ***" );
|
||||||
|
|
||||||
@@ -89,17 +90,21 @@ void *POSIX_Init(
|
|||||||
status = pthread_cond_destroy( &cond );
|
status = pthread_cond_destroy( &cond );
|
||||||
assert( !status );
|
assert( !status );
|
||||||
|
|
||||||
|
/* initiailize the attribute for the rest of the test */
|
||||||
|
|
||||||
puts( "Init: pthread_cond_init - attr" );
|
puts( "Init: pthread_cond_init - attr" );
|
||||||
status = pthread_cond_init( &Cond1_id, &attr );
|
status = pthread_cond_init( &Cond1_id, &attr );
|
||||||
assert( !status );
|
assert( !status );
|
||||||
|
|
||||||
/* create a thread */
|
/* signal task1 with a condition variable */
|
||||||
|
|
||||||
empty_line();
|
empty_line();
|
||||||
|
|
||||||
status = pthread_create( &Task_id, NULL, Task_1, NULL );
|
status = pthread_create( &Task_id, NULL, Task_1, NULL );
|
||||||
assert( !status );
|
assert( !status );
|
||||||
|
|
||||||
|
/* switch to task1 to allow it to wait for a condition variable */
|
||||||
|
|
||||||
puts( "Init: sleep to switch to Task_1" );
|
puts( "Init: sleep to switch to Task_1" );
|
||||||
sleep( 1 );
|
sleep( 1 );
|
||||||
|
|
||||||
@@ -112,16 +117,36 @@ void *POSIX_Init(
|
|||||||
status = pthread_create( &Task2_id, NULL, Task_2, NULL );
|
status = pthread_create( &Task2_id, NULL, Task_2, NULL );
|
||||||
assert( !status );
|
assert( !status );
|
||||||
|
|
||||||
|
/* switch to task1 and task2 to allow them to wait for broadcast signal */
|
||||||
|
|
||||||
puts( "Init: sleep - switch to Task_1 and Task_2" );
|
puts( "Init: sleep - switch to Task_1 and Task_2" );
|
||||||
sleep( 1 );
|
sleep( 1 );
|
||||||
|
|
||||||
|
/* broadcast a condition variable to task1 and task2 */
|
||||||
|
|
||||||
puts( "Init: pthread_cond_broadcast" );
|
puts( "Init: pthread_cond_broadcast" );
|
||||||
status = pthread_cond_broadcast( &Cond1_id );
|
status = pthread_cond_broadcast( &Cond1_id );
|
||||||
assert( !status );
|
assert( !status );
|
||||||
|
|
||||||
puts( "Init: sleep - switch to Task_1" );
|
puts( "Init: sleep - switch to Task_1" );
|
||||||
sleep( 1 );
|
sleep( 0 );
|
||||||
|
|
||||||
|
/* timedwait case - timeout */
|
||||||
|
|
||||||
|
status = pthread_mutex_lock( &Mutex_id );
|
||||||
|
assert( !status );
|
||||||
|
|
||||||
|
/* set timeout to 3 seconds */
|
||||||
|
|
||||||
|
timeout.tv_sec = 3;
|
||||||
|
|
||||||
|
puts( "Init: pthread_cond_timedwait for 3 seconds" );
|
||||||
|
status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
|
||||||
|
if ( status != ETIMEDOUT )
|
||||||
|
printf( "status = %d\n", status );
|
||||||
|
assert( status == ETIMEDOUT );
|
||||||
|
|
||||||
|
puts( "Init: timedout on pthread_cond_timedwait release mutex" );
|
||||||
/* exit this thread */
|
/* exit this thread */
|
||||||
|
|
||||||
puts( "*** END OF POSIX TEST 5 ***" );
|
puts( "*** END OF POSIX TEST 5 ***" );
|
||||||
|
|||||||
Reference in New Issue
Block a user