[LTP] [PATCH 2/5] open_posix: condvar/schedule: use SAFE_PFUNC

Jan Stancek jstancek@redhat.com
Fri Feb 19 11:03:34 CET 2016


Replace common error checking with SAFE_PFUNC macro. Also remove
some obvious comments.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 .../threads/condvar/pthread_cond_wait_1.c          | 136 ++++-------------
 .../threads/condvar/pthread_cond_wait_2.c          | 135 +++-------------
 .../functional/threads/schedule/1-1.c              | 137 +++++------------
 .../functional/threads/schedule/1-2.c              | 169 ++++-----------------
 4 files changed, 115 insertions(+), 462 deletions(-)

diff --git a/testcases/open_posix_testsuite/functional/threads/condvar/pthread_cond_wait_1.c b/testcases/open_posix_testsuite/functional/threads/condvar/pthread_cond_wait_1.c
index 123d1e56d4cf..5b36fc24a3b8 100644
--- a/testcases/open_posix_testsuite/functional/threads/condvar/pthread_cond_wait_1.c
+++ b/testcases/open_posix_testsuite/functional/threads/condvar/pthread_cond_wait_1.c
@@ -4,18 +4,17 @@
  * This file is licensed under the GPL license.  For the full content
  * of this license, see the COPYING file at the top level of this
  * source tree.
-
+ *
  * Test that pthread_cond_signal()
  *   shall wakeup a high priority thread even when a low priority thread
  *   is running
-
+ *
  * Steps:
  * 1. Create a condition variable
  * 2. Create a high priority thread and make it wait on the cond
  * 3. Create a low priority thread and let it busy-loop
  * 4. Signal the cond in a signal handler and check that high
  *    priority thread got woken up
- *
  */
 
 #include <pthread.h>
@@ -25,6 +24,7 @@
 #include <unistd.h>
 #include <time.h>
 #include "posixtest.h"
+#include "safe_helpers.h"
 
 #define TEST "5-1"
 #define AREA "scheduler"
@@ -35,27 +35,19 @@
 #define RUNTIME       5
 #define POLICY        SCHED_RR
 
-/* mutex required by the cond variable */
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-/* condition variable that threads block on*/
 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 
 /* Flags that the threads use to indicate events */
 int woken_up = -1;
 int low_done = -1;
 
-/* Signal handler that handle the ALRM and wakes up
- * the high priority thread
- */
 void signal_handler(int sig)
 {
-	if (pthread_cond_signal(&cond) != 0) {
-		printf(ERROR_PREFIX "pthread_cond_signal\n");
-		exit(PTS_UNRESOLVED);
-	}
+	(void) sig;
+	SAFE_PFUNC(pthread_cond_signal(&cond));
 }
 
-/* Utility function to find difference between two time values */
 float timediff(struct timespec t2, struct timespec t1)
 {
 	float diff = t2.tv_sec - t1.tv_sec;
@@ -63,51 +55,32 @@ float timediff(struct timespec t2, struct timespec t1)
 	return diff;
 }
 
-void *hi_priority_thread(void *tmp)
+void *hi_prio_thread(void *tmp)
 {
 	struct sched_param param;
 	int policy;
-	int rc = 0;
 
+	(void) tmp;
 	param.sched_priority = HIGH_PRIORITY;
 
-	rc = pthread_setschedparam(pthread_self(), POLICY, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_getschedparam(pthread_self(), &policy, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_getschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, &param));
+	SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, &param));
 	if ((policy != POLICY) || (param.sched_priority != HIGH_PRIORITY)) {
 		printf("Error: the policy or priority not correct\n");
 		exit(PTS_UNRESOLVED);
 	}
 
-	/* Install a signal handler for ALRM */
 	if (signal(SIGALRM, signal_handler) != 0) {
 		perror(ERROR_PREFIX "signal:");
 		exit(PTS_UNRESOLVED);
 	}
 
-	/* acquire the mutex */
-	rc = pthread_mutex_lock(&mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_mutex_lock\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_mutex_lock(&mutex));
 
-	/* Setup an alarm to go off in 2 seconds */
 	alarm(2);
 
 	/* Block, to be woken up by the signal handler */
-	rc = pthread_cond_wait(&cond, &mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_cond_wait\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_cond_wait(&cond, &mutex));
 
 	/* This variable is unprotected because the scheduling removes
 	 * the contention
@@ -115,39 +88,26 @@ void *hi_priority_thread(void *tmp)
 	if (low_done != 1)
 		woken_up = 1;
 
-	rc = pthread_mutex_unlock(&mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_mutex_unlock\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_mutex_unlock(&mutex));
 	return NULL;
 }
 
-void *low_priority_thread(void *tmp)
+void *low_prio_thread(void *tmp)
 {
 	struct timespec start_time, current_time;
 	struct sched_param param;
 	int policy;
-	int rc = 0;
 
+	(void) tmp;
 	param.sched_priority = LOW_PRIORITY;
 
-	rc = pthread_setschedparam(pthread_self(), POLICY, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_getschedparam(pthread_self(), &policy, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_getschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, &param));
+	SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, &param));
 	if ((policy != POLICY) || (param.sched_priority != LOW_PRIORITY)) {
 		printf("Error: the policy or priority not correct\n");
 		exit(PTS_UNRESOLVED);
 	}
 
-	/* grab the start time and busy loop for 5 seconds */
 	clock_gettime(CLOCK_REALTIME, &start_time);
 	while (1) {
 		clock_gettime(CLOCK_REALTIME, &current_time);
@@ -163,69 +123,25 @@ int main()
 	pthread_t high_id, low_id;
 	pthread_attr_t high_attr, low_attr;
 	struct sched_param param;
-	int rc = 0;
 
 	/* Create the higher priority thread */
-	rc = pthread_attr_init(&high_attr);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_init\n");
-		exit(PTS_UNRESOLVED);
-	}
-
-	rc = pthread_attr_setschedpolicy(&high_attr, POLICY);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_init(&high_attr));
+	SAFE_PFUNC(pthread_attr_setschedpolicy(&high_attr, POLICY));
 	param.sched_priority = HIGH_PRIORITY;
-	rc = pthread_attr_setschedparam(&high_attr, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_create(&high_id, &high_attr, hi_priority_thread, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_create\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_setschedparam(&high_attr, &param));
+	SAFE_PFUNC(pthread_create(&high_id, &high_attr, hi_prio_thread, NULL));
 
 	/* Create the low priority thread */
-	rc = pthread_attr_init(&low_attr);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_init\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_attr_setschedpolicy(&low_attr, POLICY);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_init(&low_attr));
+	SAFE_PFUNC(pthread_attr_setschedpolicy(&low_attr, POLICY));
 	param.sched_priority = LOW_PRIORITY;
-	rc = pthread_attr_setschedparam(&low_attr, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_create(&low_id, &low_attr, low_priority_thread, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_create\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_setschedparam(&low_attr, &param));
+	SAFE_PFUNC(pthread_create(&low_id, &low_attr, low_prio_thread, NULL));
 
 	/* Wait for the threads to exit */
-	rc = pthread_join(high_id, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_join\n");
-		exit(PTS_UNRESOLVED);
-	}
-
-	rc = pthread_join(low_id, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_join\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_join(high_id, NULL));
+	SAFE_PFUNC(pthread_join(low_id, NULL));
 
-	/* Check the result */
 	if (woken_up == -1) {
 		printf("Test FAILED: high priority was not woken up\\n");
 		exit(PTS_FAIL);
diff --git a/testcases/open_posix_testsuite/functional/threads/condvar/pthread_cond_wait_2.c b/testcases/open_posix_testsuite/functional/threads/condvar/pthread_cond_wait_2.c
index bf2b1f217e84..7308bd6b98d1 100644
--- a/testcases/open_posix_testsuite/functional/threads/condvar/pthread_cond_wait_2.c
+++ b/testcases/open_posix_testsuite/functional/threads/condvar/pthread_cond_wait_2.c
@@ -4,7 +4,7 @@
  * This file is licensed under the GPL license.  For the full content
  * of this license, see the COPYING file at the top level of this
  * source tree.
-
+ *
  * Test that pthread_cond_broadcast()
  *   shall wakeup a high priority thread even when a low priority thread
  *   is running
@@ -15,8 +15,6 @@
  * 3. Create a low priority thread and let it busy-loop
  * 4. Broadcast the cond in a signal handler and check that high
  *    priority thread got woken up
-
- *
  */
 
 #include <pthread.h>
@@ -26,6 +24,7 @@
 #include <unistd.h>
 #include <time.h>
 #include "posixtest.h"
+#include "safe_helpers.h"
 
 #define TEST "5-1"
 #define AREA "scheduler"
@@ -36,27 +35,19 @@
 #define RUNTIME       5
 #define POLICY        SCHED_RR
 
-/* mutex required by the cond variable */
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-/* condition variable that threads block on*/
 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 
 /* Flags that the threads use to indicate events */
 int woken_up = -1;
 int low_done = -1;
 
-/* Signal handler that handle the ALRM and wakes up
- * the high priority thread
- */
 void signal_handler(int sig)
 {
-	if (pthread_cond_broadcast(&cond) != 0) {
-		printf(ERROR_PREFIX "pthread_cond_broadcast\n");
-		exit(PTS_UNRESOLVED);
-	}
+	(void) sig;
+	SAFE_PFUNC(pthread_cond_broadcast(&cond));
 }
 
-/* Utility function to find difference between two time values */
 float timediff(struct timespec t2, struct timespec t1)
 {
 	float diff = t2.tv_sec - t1.tv_sec;
@@ -64,51 +55,32 @@ float timediff(struct timespec t2, struct timespec t1)
 	return diff;
 }
 
-void *hi_priority_thread(void *tmp)
+void *hi_prio_thread(void *tmp)
 {
 	struct sched_param param;
 	int policy;
-	int rc = 0;
 
+	(void) tmp;
 	param.sched_priority = HIGH_PRIORITY;
 
-	rc = pthread_setschedparam(pthread_self(), POLICY, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_getschedparam(pthread_self(), &policy, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_getschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, &param));
+	SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, &param));
 	if ((policy != POLICY) || (param.sched_priority != HIGH_PRIORITY)) {
 		printf("Error: the policy or priority not correct\n");
 		exit(PTS_UNRESOLVED);
 	}
 
-	/* Install a signal handler for ALRM */
 	if (signal(SIGALRM, signal_handler) != 0) {
 		perror(ERROR_PREFIX "signal:");
 		exit(PTS_UNRESOLVED);
 	}
 
-	/* acquire the mutex */
-	rc = pthread_mutex_lock(&mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_mutex_lock\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_mutex_lock(&mutex));
 
-	/* Setup an alarm to go off in 2 seconds */
 	alarm(2);
 
 	/* Block, to be woken up by the signal handler */
-	rc = pthread_cond_wait(&cond, &mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_cond_wait\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_cond_wait(&cond, &mutex));
 
 	/* This variable is unprotected because the scheduling removes
 	 * the contention
@@ -116,39 +88,26 @@ void *hi_priority_thread(void *tmp)
 	if (low_done != 1)
 		woken_up = 1;
 
-	rc = pthread_mutex_unlock(&mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_mutex_unlock\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_mutex_unlock(&mutex));
 	return NULL;
 }
 
-void *low_priority_thread(void *tmp)
+void *low_prio_thread(void *tmp)
 {
 	struct timespec start_time, current_time;
 	struct sched_param param;
 	int policy;
-	int rc = 0;
 
+	(void) tmp;
 	param.sched_priority = LOW_PRIORITY;
 
-	rc = pthread_setschedparam(pthread_self(), POLICY, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_getschedparam(pthread_self(), &policy, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_getschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, &param));
+	SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, &param));
 	if ((policy != POLICY) || (param.sched_priority != LOW_PRIORITY)) {
 		printf("Error: the policy or priority not correct\n");
 		exit(PTS_UNRESOLVED);
 	}
 
-	/* grab the start time and busy loop for 5 seconds */
 	clock_gettime(CLOCK_REALTIME, &start_time);
 	while (1) {
 		clock_gettime(CLOCK_REALTIME, &current_time);
@@ -164,69 +123,25 @@ int main()
 	pthread_t high_id, low_id;
 	pthread_attr_t high_attr, low_attr;
 	struct sched_param param;
-	int rc = 0;
 
 	/* Create the higher priority thread */
-	rc = pthread_attr_init(&high_attr);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_init\n");
-		exit(PTS_UNRESOLVED);
-	}
-
-	rc = pthread_attr_setschedpolicy(&high_attr, POLICY);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_init(&high_attr));
+	SAFE_PFUNC(pthread_attr_setschedpolicy(&high_attr, POLICY));
 	param.sched_priority = HIGH_PRIORITY;
-	rc = pthread_attr_setschedparam(&high_attr, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_create(&high_id, &high_attr, hi_priority_thread, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_create\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_setschedparam(&high_attr, &param));
+	SAFE_PFUNC(pthread_create(&high_id, &high_attr, hi_prio_thread, NULL));
 
 	/* Create the low priority thread */
-	rc = pthread_attr_init(&low_attr);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_init\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_attr_setschedpolicy(&low_attr, POLICY);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_init(&low_attr));
+	SAFE_PFUNC(pthread_attr_setschedpolicy(&low_attr, POLICY));
 	param.sched_priority = LOW_PRIORITY;
-	rc = pthread_attr_setschedparam(&low_attr, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_create(&low_id, &low_attr, low_priority_thread, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_create\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_setschedparam(&low_attr, &param));
+	SAFE_PFUNC(pthread_create(&low_id, &low_attr, low_prio_thread, NULL));
 
 	/* Wait for the threads to exit */
-	rc = pthread_join(high_id, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_join\n");
-		exit(PTS_UNRESOLVED);
-	}
-
-	rc = pthread_join(low_id, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_join\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_join(high_id, NULL));
+	SAFE_PFUNC(pthread_join(low_id, NULL));
 
-	/* Check the result */
 	if (woken_up == -1) {
 		printf("Test FAILED: high priority was not woken up\\n");
 		exit(PTS_FAIL);
diff --git a/testcases/open_posix_testsuite/functional/threads/schedule/1-1.c b/testcases/open_posix_testsuite/functional/threads/schedule/1-1.c
index 73be55b98c0c..f1bf37292b53 100644
--- a/testcases/open_posix_testsuite/functional/threads/schedule/1-1.c
+++ b/testcases/open_posix_testsuite/functional/threads/schedule/1-1.c
@@ -16,17 +16,17 @@
  * 4. Setup a signal handler for ALRM
  * 5. Call the final barrier_wait in the signal handler
  * 6. Check that the higher priority thread got woken up
- *
  */
 
 #define _XOPEN_SOURCE 600
-#include "posixtest.h"
 #include <pthread.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <signal.h>
 #include <unistd.h>
 #include <sys/time.h>
+#include "posixtest.h"
+#include "safe_helpers.h"
 
 #define TEST "5-4"
 #define AREA "scheduler"
@@ -40,7 +40,6 @@ pthread_barrier_t barrier;
 volatile int woken_up = -1;
 volatile int low_done = -1;
 
-/* Utility function to find difference between two time values */
 float timediff(struct timespec t2, struct timespec t1)
 {
 	float diff = t2.tv_sec - t1.tv_sec;
@@ -48,44 +47,37 @@ float timediff(struct timespec t2, struct timespec t1)
 	return diff;
 }
 
-/* This signal handler will wakeup the high priority thread by
- * calling barrier wait
- */
-void signal_handler(int sig)
+int my_pthread_barrier_wait(pthread_barrier_t *p)
 {
-	int rc = 0;
+	int rc;
 
-	rc = pthread_barrier_wait(&barrier);
-	if ((rc != 0) && (rc != PTHREAD_BARRIER_SERIAL_THREAD)) {
-		printf(ERROR_PREFIX "pthread_barrier_wait in handler\n");
-		exit(PTS_UNRESOLVED);
-	}
+	rc = pthread_barrier_wait(p);
+	if (rc == PTHREAD_BARRIER_SERIAL_THREAD)
+		rc = 0;
+	return rc;
+}
+
+void signal_handler(int sig)
+{
+	(void) sig;
+	SAFE_PFUNC(my_pthread_barrier_wait(&barrier));
 }
 
-void *hi_priority_thread(void *tmp)
+void *hi_prio_thread(void *tmp)
 {
 	struct sched_param param;
 	int policy;
-	int rc = 0;
 	void *previous_signal;
 
+	(void) tmp;
 	param.sched_priority = HIGH_PRIORITY;
-	rc = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_getschedparam(pthread_self(), &policy, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_getschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, &param));
+	SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, &param));
 	if (policy != SCHED_RR || param.sched_priority != HIGH_PRIORITY) {
 		printf("Error: the policy or priority not correct\n");
 		exit(PTS_UNRESOLVED);
 	}
 
-	/* setup a signal handler for ALRM */
 	previous_signal = signal(SIGALRM, signal_handler);
 	if (previous_signal == SIG_ERR) {
 		perror(ERROR_PREFIX "signal");
@@ -94,11 +86,7 @@ void *hi_priority_thread(void *tmp)
 
 	alarm(2);
 
-	rc = pthread_barrier_wait(&barrier);
-	if ((rc != 0) && (rc != PTHREAD_BARRIER_SERIAL_THREAD)) {
-		printf(ERROR_PREFIX "pthread_barrier_wait\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(my_pthread_barrier_wait(&barrier));
 
 	/* This variable is unprotected because the scheduling removes
 	 * the contention
@@ -109,30 +97,21 @@ void *hi_priority_thread(void *tmp)
 	pthread_exit(NULL);
 }
 
-void *low_priority_thread(void *tmp)
+void *low_prio_thread(void *tmp)
 {
 	struct timespec start_timespec, current_timespec;
 	struct sched_param param;
-	int rc = 0;
 	int policy;
 
+	(void) tmp;
 	param.sched_priority = LOW_PRIORITY;
-	rc = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_getschedparam(pthread_self(), &policy, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_getschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, &param));
+	SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, &param));
 	if (policy != SCHED_RR || param.sched_priority != LOW_PRIORITY) {
 		printf("Error: the policy or priority not correct\n");
 		exit(PTS_UNRESOLVED);
 	}
 
-	/* grab the start time and busy loop for RUNTIME seconds */
 	clock_gettime(CLOCK_REALTIME, &start_timespec);
 	while (1) {
 		clock_gettime(CLOCK_REALTIME, &current_timespec);
@@ -149,75 +128,27 @@ int main()
 	pthread_t high_id, low_id;
 	pthread_attr_t low_attr, high_attr;
 	struct sched_param param;
-	int rc = 0;
 
-	/* Initialize the barrier */
-	rc = pthread_barrier_init(&barrier, NULL, 2);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_barrier_init\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_barrier_init(&barrier, NULL, 2));
 
 	/* Create the higher priority */
-	rc = pthread_attr_init(&high_attr);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_init\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_attr_setschedpolicy(&high_attr, SCHED_RR);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_init(&high_attr));
+	SAFE_PFUNC(pthread_attr_setschedpolicy(&high_attr, SCHED_RR));
 	param.sched_priority = HIGH_PRIORITY;
-	rc = pthread_attr_setschedparam(&high_attr, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_create(&high_id, &high_attr, hi_priority_thread, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_create\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_setschedparam(&high_attr, &param));
+	SAFE_PFUNC(pthread_create(&high_id, &high_attr, hi_prio_thread, NULL));
 
 	/* Create the low priority thread */
-	rc = pthread_attr_init(&low_attr);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_init\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_attr_setschedpolicy(&low_attr, SCHED_RR);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_init(&low_attr));
+	SAFE_PFUNC(pthread_attr_setschedpolicy(&low_attr, SCHED_RR));
 	param.sched_priority = LOW_PRIORITY;
-	rc = pthread_attr_setschedparam(&low_attr, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_create(&low_id, &low_attr, low_priority_thread, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_create\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_setschedparam(&low_attr, &param));
+	SAFE_PFUNC(pthread_create(&low_id, &low_attr, low_prio_thread, NULL));
 
 	/* Wait for the threads to exit */
-	rc = pthread_join(high_id, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_join\n");
-		exit(PTS_UNRESOLVED);
-	}
-
-	rc = pthread_join(low_id, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_join\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_join(high_id, NULL));
+	SAFE_PFUNC(pthread_join(low_id, NULL));
 
-	/* Check the result */
 	if (woken_up == -1) {
 		printf("High priority was not woken up. Test FAILED\n");
 		exit(PTS_FAIL);
diff --git a/testcases/open_posix_testsuite/functional/threads/schedule/1-2.c b/testcases/open_posix_testsuite/functional/threads/schedule/1-2.c
index cfc46088d316..3d3427b8ec4b 100644
--- a/testcases/open_posix_testsuite/functional/threads/schedule/1-2.c
+++ b/testcases/open_posix_testsuite/functional/threads/schedule/1-2.c
@@ -15,7 +15,6 @@
  * 3. Create a low priority thread and let it busy-loop
  * 4. Unlock the mutex and make sure that the higher priority thread
  *    got woken up
- *
  */
 
 #include <pthread.h>
@@ -25,6 +24,7 @@
 #include <unistd.h>
 #include <sys/time.h>
 #include "posixtest.h"
+#include "safe_helpers.h"
 
 #define TEST "5-5"
 #define AREA "scheduler"
@@ -35,17 +35,13 @@
 #define LOW_PRIORITY 5
 #define RUNTIME 5
 
-/* mutex  high priority thread will wait on*/
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-/* mutex required by the cond variable */
 pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
-/* condition variable that threads block on*/
 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 
 volatile int woken_up = -1;
 volatile int low_done = -1;
 
-/* Utility function to find difference between two time values */
 float timediff(struct timespec t2, struct timespec t1)
 {
 	float diff = t2.tv_sec - t1.tv_sec;
@@ -53,37 +49,21 @@ float timediff(struct timespec t2, struct timespec t1)
 	return diff;
 }
 
-/* This signal handler will wakeup the main thread by sending a signal
- * to a condition variable that the main thread is waiting on.
- */
 void signal_handler(int sig)
 {
-	int rc = 0;
-
-	rc = pthread_cond_signal(&cond);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_cond_signal\n");
-		exit(PTS_UNRESOLVED);
-	}
+	(void) sig;
+	SAFE_PFUNC(pthread_cond_signal(&cond));
 }
 
-void *hi_priority_thread(void *tmp)
+void *hi_prio_thread(void *tmp)
 {
 	struct sched_param param;
 	int policy;
-	int rc = 0;
 
+	(void) tmp;
 	param.sched_priority = HIGH_PRIORITY;
-	rc = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_getschedparam(pthread_self(), &policy, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_getschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, &param));
+	SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, &param));
 	if (policy != SCHED_RR) {
 		printf(ERROR_PREFIX "The policy is not correct\n");
 		exit(PTS_UNRESOLVED);
@@ -93,12 +73,7 @@ void *hi_priority_thread(void *tmp)
 		exit(PTS_UNRESOLVED);
 	}
 
-	/* acquire the mutex */
-	rc = pthread_mutex_lock(&mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_mutex_lock\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_mutex_lock(&mutex));
 
 	/* This variable is unprotected because the scheduling removes
 	 * the contention
@@ -106,32 +81,20 @@ void *hi_priority_thread(void *tmp)
 	if (low_done != 1)
 		woken_up = 1;
 
-	rc = pthread_mutex_unlock(&mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_mutex_unlock\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_mutex_unlock(&mutex));
 	pthread_exit(NULL);
 }
 
-void *low_priority_thread(void *tmp)
+void *low_prio_thread(void *tmp)
 {
 	struct timespec current_time, start_time;
 	struct sched_param param;
-	int rc = 0;
 	int policy;
 
+	(void) tmp;
 	param.sched_priority = LOW_PRIORITY;
-	rc = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_getschedparam(pthread_self(), &policy, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_getschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, &param));
+	SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, &param));
 	if (policy != SCHED_RR) {
 		printf(ERROR_PREFIX "Policy not correct\n");
 		exit(PTS_UNRESOLVED);
@@ -141,7 +104,6 @@ void *low_priority_thread(void *tmp)
 		exit(PTS_UNRESOLVED);
 	}
 
-	/* Grab the start time and busy loop for 5 seconds */
 	clock_gettime(CLOCK_REALTIME, &start_time);
 	while (1) {
 		clock_gettime(CLOCK_REALTIME, &current_time);
@@ -157,20 +119,11 @@ int main()
 	pthread_t high_id, low_id;
 	pthread_attr_t low_attr, high_attr;
 	struct sched_param param;
-	int rc = 0;
 	int policy;
 
 	param.sched_priority = MID_PRIORITY;
-	rc = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_getschedparam(pthread_self(), &policy, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_getschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, &param));
+	SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, &param));
 	if (policy != SCHED_RR) {
 		printf(ERROR_PREFIX "The policy is not correct\n");
 		exit(PTS_UNRESOLVED);
@@ -180,57 +133,21 @@ int main()
 		exit(PTS_UNRESOLVED);
 	}
 
-	rc = pthread_mutex_lock(&mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_mutex_lock\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_mutex_lock(&mutex));
 
 	/* create the higher priority */
-	rc = pthread_attr_init(&high_attr);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_init\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_attr_setschedpolicy(&high_attr, SCHED_RR);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_init(&high_attr));
+	SAFE_PFUNC(pthread_attr_setschedpolicy(&high_attr, SCHED_RR));
 	param.sched_priority = HIGH_PRIORITY;
-	rc = pthread_attr_setschedparam(&high_attr, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_create(&high_id, &high_attr, hi_priority_thread, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_create\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_setschedparam(&high_attr, &param));
+	SAFE_PFUNC(pthread_create(&high_id, &high_attr, hi_prio_thread, NULL));
 
 	/* Create the low priority thread */
-	rc = pthread_attr_init(&low_attr);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_init\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_attr_setschedpolicy(&low_attr, SCHED_RR);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedpolicy\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_init(&low_attr));
+	SAFE_PFUNC(pthread_attr_setschedpolicy(&low_attr, SCHED_RR));
 	param.sched_priority = LOW_PRIORITY;
-	rc = pthread_attr_setschedparam(&low_attr, &param);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_attr_setschedparam\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_create(&low_id, &low_attr, low_priority_thread, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_create\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_attr_setschedparam(&low_attr, &param));
+	SAFE_PFUNC(pthread_create(&low_id, &low_attr, low_prio_thread, NULL));
 
 	/* setup a signal handler which will wakeup main later */
 	if (signal(SIGALRM, signal_handler) == SIG_ERR) {
@@ -238,45 +155,19 @@ int main()
 		exit(PTS_UNRESOLVED);
 	}
 
-	rc = pthread_mutex_lock(&cond_mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_mutex_lock\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_mutex_lock(&cond_mutex));
 
 	alarm(2);
-	rc = pthread_cond_wait(&cond, &cond_mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_cond_wait\n");
-		exit(PTS_UNRESOLVED);
-	}
-	rc = pthread_mutex_unlock(&cond_mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_mutex_unlock\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_cond_wait(&cond, &cond_mutex));
+	SAFE_PFUNC(pthread_mutex_unlock(&cond_mutex));
 
 	/* Wake the other high priority thread up */
-	rc = pthread_mutex_unlock(&mutex);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_mutex_unlock\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_mutex_unlock(&mutex));
 
 	/* Wait for the threads to exit */
-	rc = pthread_join(high_id, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_join\n");
-		exit(PTS_UNRESOLVED);
-	}
-
-	rc = pthread_join(low_id, NULL);
-	if (rc != 0) {
-		printf(ERROR_PREFIX "pthread_join\n");
-		exit(PTS_UNRESOLVED);
-	}
+	SAFE_PFUNC(pthread_join(high_id, NULL));
+	SAFE_PFUNC(pthread_join(low_id, NULL));
 
-	/* Check the result */
 	if (woken_up == -1) {
 		printf("High priority was not woken up. Test FAILED.\n");
 		exit(PTS_FAIL);
-- 
1.8.3.1



More information about the Ltp mailing list