From: gowrishankar <[email protected]> In a scenario where a thread other than high priority noise thread is scheduled after the barrier, a priority inversion will not occur, defeating the purpose of test. We need to tightly synchronize the threads so that the noise thread always begins the test and gets preempted by the low prio thread through the PI logic.
This change is applied on testpi-4. Signed-off-by: Gowrishankar<[email protected]> Tested-by: Gowrishankar<[email protected]> Acked-by: Darren Hart <[email protected]> --- testcases/realtime/func/pi-tests/testpi-4.c | 24 +++++++++++++++++++++++- 1 files changed, 23 insertions(+), 1 deletions(-) diff --git a/testcases/realtime/func/pi-tests/testpi-4.c b/testcases/realtime/func/pi-tests/testpi-4.c index 3bd5d77..63f8430 100644 --- a/testcases/realtime/func/pi-tests/testpi-4.c +++ b/testcases/realtime/func/pi-tests/testpi-4.c @@ -30,7 +30,9 @@ * * * HISTORY - * + * 2010-06-29 Thread synchronization changes by using + * conditional variables by Gowrishankar. + * by Gowrishankar <[email protected]> * *****************************************************************************/ @@ -74,6 +76,8 @@ int gettid(void) typedef void *(*entrypoint_t)(void *); pthread_mutex_t *glob_mutex; +static pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER; void *func_nonrt(void *arg) { @@ -85,8 +89,15 @@ void *func_nonrt(void *arg) pthread_mutex_lock(glob_mutex); printf("Thread %d at start pthread pol %d pri %d - Got global lock\n", tid, pthr->policy, pthr->priority); + + /* Wait for other RT threads to start up */ pthread_barrier_wait(&barrier); + /* Wait for the high priority noise thread to start and signal us */ + pthread_mutex_lock(&cond_mutex); + pthread_cond_wait(&cond_var, &cond_mutex); + pthread_mutex_unlock(&cond_mutex); + for (i = 0; i < 10000; i++) { if (i%100 == 0) { printf("Thread %d loop %d pthread pol %d pri %d\n", @@ -137,6 +148,14 @@ void *func_noise(void *arg) pthr->priority); pthread_barrier_wait(&barrier); + /* Give the other threads time to wait on the condition variable.*/ + usleep(1000); + + /* Noise thread begins the test */ + pthread_mutex_lock(&cond_mutex); + pthread_cond_broadcast(&cond_var); + pthread_mutex_unlock(&cond_mutex); + for (i = 0; i < 10000; i++) { if (i%100 == 0) { printf("Noise Thread %d loop %d pthread pol %d "\ @@ -200,5 +219,8 @@ int main(int argc, char *argv[]) join_threads(); printf("Done\n"); + pthread_mutex_destroy(glob_mutex); + pthread_mutex_destroy(&cond_mutex); + pthread_cond_destroy(&cond_var); return 0; } -- 1.7.1 ------------------------------------------------------------------------------ This SF.net email is sponsored by Sprint What will you do first with EVO, the first 4G phone? Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list
