Last active
August 25, 2021 12:51
-
-
Save joycemaferko/78adb36308913f661209c4d8b930f43e to your computer and use it in GitHub Desktop.
RTEMS Test for pthread_cond_clockwait() (psxcond03)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2021 Matthew Joyce | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* 1. Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* 2. Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
* POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/* Defining in order to access pthread_cond_clockwait in pthread.h */ | |
#define _GNU_SOURCE | |
#ifdef HAVE_CONFIG_H | |
#include "config.h" | |
#endif | |
#define CONFIGURE_INIT | |
#include <pthread.h> | |
#include <sched.h> | |
#include <errno.h> | |
#include <rtems.h> | |
#include <tmacros.h> | |
#include "system.h" | |
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; | |
pthread_cond_t bad_cond; | |
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | |
pthread_mutex_t bad_mutex; | |
pthread_mutex_t new_mutex = PTHREAD_MUTEX_INITIALIZER; | |
clockid_t clock_id1 = CLOCK_MONOTONIC; | |
clockid_t clock_id2 = CLOCK_REALTIME; | |
clockid_t clock_id3 = CLOCK_MONOTONIC_RAW; | |
int count; | |
int eno; | |
int status; | |
const char rtems_test_name[] = "PSXCOND 3"; | |
void *POSIX_Init( | |
void *argument | |
) | |
{ | |
TEST_BEGIN(); | |
empty_line(); | |
pthread_t t1; | |
struct timespec abstime; | |
/* Expected pass: Clock Monotonic with sufficient abstime */ | |
printf("1st iteration Begin (Clock Monotonic)\n"); | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == 0 ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "1st iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Clock Monotonic with insufficient abstime */ | |
printf( "2nd iteration Begin (Clock Monotonic)\n" ); | |
abstime.tv_sec = 0; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == ETIMEDOUT ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "2nd iteration END\n" ); | |
empty_line(); | |
/* Expected pass: Clock Realtime with sufficient abstime */ | |
printf( "3rd iteration Begin (Clock Realtime)\n" ); | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec += 4; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == 0 ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "3rd iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Clock Realtime with insufficient abstime */ | |
printf( "4th iteration Begin (Clock Realtime)\n" ); | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec = 0; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == ETIMEDOUT ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "4th iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Unsupported Clock */ | |
printf( "5th iteration Begin (Unsupported Clock)\n" ); | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id3, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == EINVAL ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "5th iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Invalid Clock */ | |
printf( "6th iteration Begin (Invalid Clock)\n" ); | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, (int)NULL, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == EINVAL ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "6th iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Invalid Clock */ | |
printf( "7th iteration Begin (Invalid Abstime)\n" ); | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id1, NULL ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == EINVAL ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "7th iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Invalid Clock */ | |
printf( "8th iteration Begin (Invalid Cond)\n" ); | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( NULL, &mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == EINVAL ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "8th iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Invalid Mutex */ | |
printf( "9th iteration Begin (Invalid Mutex)\n" ); | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, NULL, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno != 0 ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "9th iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Uninitialized condition variable */ | |
printf( "10th iteration Begin (Uninitialized Condition Variable)\n" ); | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &bad_cond, &mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno != 0 ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "10th iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Uninitialized condition variable */ | |
printf( "11th iteration Begin (Uninitialized Mutex)\n" ); | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &bad_mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno != 0 ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "11th iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Uninitialized condition variable */ | |
printf( "12th iteration Begin (Uninitialized Condition Variable)\n" ); | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &bad_cond, &mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno != 0 ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "12th iteration END\n" ); | |
empty_line(); | |
/* Expected pass: Binding new mutex to condition variable */ | |
printf( "13th iteration Begin (New Mutex)\n" ); | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec += 4; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func2, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &new_mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == 0 ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "13th iteration END\n" ); | |
empty_line(); | |
/* Expected fail: Timeout (abstime < current time) */ | |
printf( "14th iteration Begin (abstime < current time)\n" ); | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec -= 1; | |
abstime.tv_nsec -= 1; | |
status = pthread_create( &t1, NULL, thread_func2, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &new_mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == ETIMEDOUT ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "14th iteration END\n" ); | |
empty_line(); | |
/* Expected Pass: Sufficient abstime to exceed sleep in thread_func3*/ | |
printf( "15th iteration Begin (sufficient abstime to exceed sleep)\n" ); | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec += 4; | |
abstime.tv_nsec += 1; | |
status = pthread_create( &t1, NULL, thread_func3, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &new_mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == 0 ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "15th iteration END\n" ); | |
empty_line(); | |
/* Expected Fail: Insufficient abstime to exceed sleep in thread_func3*/ | |
printf( "16th iteration Begin (insufficient abstime to exceed sleep)\n" ); | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec += 2; | |
abstime.tv_nsec += 1; | |
status = pthread_create( &t1, NULL, thread_func3, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &new_mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == ETIMEDOUT ); | |
printf( "Return value from cond_clockwait is %d\n", eno ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
printf( "16th iteration END\n" ); | |
empty_line(); | |
TEST_END(); | |
rtems_test_exit( 0 ); | |
return NULL; | |
} | |
void *thread_func( void *arg ) | |
{ | |
int status; | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
if (count == 0) { | |
status = pthread_cond_signal( &cond ); | |
rtems_test_assert( status == 0 ); | |
} | |
count +=1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
return NULL; | |
} | |
void *thread_func2( void *arg ) | |
{ | |
int status; | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
if ( count == 0 ) { | |
status = pthread_cond_signal( &cond ); | |
rtems_test_assert( status == 0 ); | |
} | |
count +=1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
return NULL; | |
} | |
void *thread_func3( void *arg ) | |
{ | |
int status; | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
/* Arbitrary sleep to test timeout functionality */ | |
sleep(3); | |
if ( count == 0 ) { | |
status = pthread_cond_signal( &cond ); | |
rtems_test_assert( status == 0 ); | |
} | |
count +=1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
return NULL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2021 Matthew Joyce | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* 1. Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* 2. Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
* POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/* Defining in order to access pthread_cond_clockwait in pthread.h */ | |
#define _GNU_SOURCE | |
#ifdef HAVE_CONFIG_H | |
#include "config.h" | |
#endif | |
#define CONFIGURE_INIT | |
#include <pthread.h> | |
#include <sched.h> | |
#include <errno.h> | |
#include <rtems.h> | |
#include <tmacros.h> | |
#include "system.h" | |
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; | |
pthread_cond_t bad_cond; | |
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | |
pthread_mutex_t bad_mutex; | |
pthread_mutex_t new_mutex = PTHREAD_MUTEX_INITIALIZER; | |
clockid_t clock_id1 = CLOCK_MONOTONIC; | |
clockid_t clock_id2 = CLOCK_REALTIME; | |
clockid_t clock_id3 = CLOCK_MONOTONIC_RAW; | |
int count; | |
int eno; | |
int status; | |
const char rtems_test_name[] = "PSXCOND 3"; | |
void *POSIX_Init( | |
void *argument | |
) | |
{ | |
TEST_BEGIN(); | |
pthread_t t1; | |
struct timespec abstime; | |
/* Expected pass: Clock Monotonic with sufficient abstime */ | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == 0 ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Clock Monotonic with insufficient abstime */ | |
abstime.tv_sec = 0; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == ETIMEDOUT ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected pass: Clock Realtime with sufficient abstime */ | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec += 4; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == 0 ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Clock Realtime with insufficient abstime */ | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec = 0; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == ETIMEDOUT ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Unsupported Clock */ | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id3, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == EINVAL ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Invalid Clock */ | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, (int)NULL, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == EINVAL ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Invalid Clock */ | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &mutex, clock_id1, NULL ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == EINVAL ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Invalid Clock */ | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( NULL, &mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == EINVAL ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Invalid Mutex */ | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, NULL, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno != 0 ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Uninitialized condition variable */ | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &bad_cond, &mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno != 0 ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Uninitialized condition variable */ | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &bad_mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno != 0 ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Uninitialized condition variable */ | |
abstime.tv_sec = 2; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &bad_cond, &mutex, clock_id1, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno != 0 ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected pass: Binding new mutex to condition variable */ | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec += 4; | |
abstime.tv_nsec = 1; | |
status = pthread_create( &t1, NULL, thread_func2, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &new_mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == 0 ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected fail: Timeout (abstime < current time) */ | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec -= 1; | |
abstime.tv_nsec -= 1; | |
status = pthread_create( &t1, NULL, thread_func2, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &new_mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == ETIMEDOUT ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected Pass: Sufficient abstime to exceed sleep in thread_func3*/ | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec += 4; | |
abstime.tv_nsec += 1; | |
status = pthread_create( &t1, NULL, thread_func3, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &new_mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == 0 ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
/* Expected Fail: Insufficient abstime to exceed sleep in thread_func3*/ | |
status = clock_gettime( clock_id2, &abstime ); | |
rtems_test_assert( status == 0 ); | |
abstime.tv_sec += 2; | |
abstime.tv_nsec += 1; | |
status = pthread_create( &t1, NULL, thread_func3, NULL ); | |
rtems_test_assert( status == 0 ); | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
while ( count == 0 ){ | |
eno = pthread_cond_clockwait( &cond, &new_mutex, clock_id2, &abstime ); | |
if ( eno != 0 ) { | |
break; | |
} | |
} | |
count -= 1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
rtems_test_assert( eno == ETIMEDOUT ); | |
status = pthread_join( t1, NULL ); | |
rtems_test_assert( status == 0 ); | |
TEST_END(); | |
rtems_test_exit( 0 ); | |
return NULL; | |
} | |
void *thread_func( void *arg ) | |
{ | |
int status; | |
status = pthread_mutex_lock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
if (count == 0) { | |
status = pthread_cond_signal( &cond ); | |
rtems_test_assert( status == 0 ); | |
} | |
count +=1; | |
status = pthread_mutex_unlock( &mutex ); | |
rtems_test_assert( status == 0 ); | |
return NULL; | |
} | |
void *thread_func2( void *arg ) | |
{ | |
int status; | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
if ( count == 0 ) { | |
status = pthread_cond_signal( &cond ); | |
rtems_test_assert( status == 0 ); | |
} | |
count +=1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
return NULL; | |
} | |
void *thread_func3( void *arg ) | |
{ | |
int status; | |
status = pthread_mutex_lock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
/* Arbitrary sleep to test timeout functionality */ | |
sleep(3); | |
if ( count == 0 ) { | |
status = pthread_cond_signal( &cond ); | |
rtems_test_assert( status == 0 ); | |
} | |
count +=1; | |
status = pthread_mutex_unlock( &new_mutex ); | |
rtems_test_assert( status == 0 ); | |
return NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment