* [LTP] [PATCH 2/5] open_posix: condvar/schedule: use SAFE_PFUNC
2016-02-19 10:03 [LTP] [PATCH 1/5] open_posix: add SAFE_PFUNC macro Jan Stancek
@ 2016-02-19 10:03 ` Jan Stancek
2016-02-22 13:42 ` Cyril Hrubis
2016-02-19 10:03 ` [LTP] [PATCH 3/5] open_posix: condvar/schedule: remove useless waiting Jan Stancek
` (3 subsequent siblings)
4 siblings, 1 reply; 13+ messages in thread
From: Jan Stancek @ 2016-02-19 10:03 UTC (permalink / raw)
To: ltp
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, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_setschedparam\n");
- exit(PTS_UNRESOLVED);
- }
- rc = pthread_getschedparam(pthread_self(), &policy, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_getschedparam\n");
- exit(PTS_UNRESOLVED);
- }
+ SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, ¶m));
+ SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
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, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_setschedparam\n");
- exit(PTS_UNRESOLVED);
- }
- rc = pthread_getschedparam(pthread_self(), &policy, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_getschedparam\n");
- exit(PTS_UNRESOLVED);
- }
+ SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, ¶m));
+ SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
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, ¤t_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, ¶m);
- 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, ¶m));
+ 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, ¶m);
- 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, ¶m));
+ 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, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_setschedparam\n");
- exit(PTS_UNRESOLVED);
- }
- rc = pthread_getschedparam(pthread_self(), &policy, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_getschedparam\n");
- exit(PTS_UNRESOLVED);
- }
+ SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, ¶m));
+ SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
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, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_setschedparam\n");
- exit(PTS_UNRESOLVED);
- }
- rc = pthread_getschedparam(pthread_self(), &policy, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_getschedparam\n");
- exit(PTS_UNRESOLVED);
- }
+ SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, ¶m));
+ SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
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, ¤t_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, ¶m);
- 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, ¶m));
+ 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, ¶m);
- 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, ¶m));
+ 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, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_setschedparam\n");
- exit(PTS_UNRESOLVED);
- }
- rc = pthread_getschedparam(pthread_self(), &policy, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_getschedparam\n");
- exit(PTS_UNRESOLVED);
- }
+ SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m));
+ SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
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, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_setschedparam\n");
- exit(PTS_UNRESOLVED);
- }
- rc = pthread_getschedparam(pthread_self(), &policy, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_getschedparam\n");
- exit(PTS_UNRESOLVED);
- }
+ SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m));
+ SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
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, ¤t_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, ¶m);
- 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, ¶m));
+ 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, ¶m);
- 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, ¶m));
+ 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, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_setschedparam\n");
- exit(PTS_UNRESOLVED);
- }
- rc = pthread_getschedparam(pthread_self(), &policy, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_getschedparam\n");
- exit(PTS_UNRESOLVED);
- }
+ SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m));
+ SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
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, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_setschedparam\n");
- exit(PTS_UNRESOLVED);
- }
- rc = pthread_getschedparam(pthread_self(), &policy, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_getschedparam\n");
- exit(PTS_UNRESOLVED);
- }
+ SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m));
+ SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
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, ¤t_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, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_setschedparam\n");
- exit(PTS_UNRESOLVED);
- }
- rc = pthread_getschedparam(pthread_self(), &policy, ¶m);
- if (rc != 0) {
- printf(ERROR_PREFIX "pthread_getschedparam\n");
- exit(PTS_UNRESOLVED);
- }
+ SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m));
+ SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
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, ¶m);
- 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, ¶m));
+ 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, ¶m);
- 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, ¶m));
+ 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
^ permalink raw reply related [flat|nested] 13+ messages in thread* [LTP] [PATCH 3/5] open_posix: condvar/schedule: remove useless waiting
2016-02-19 10:03 [LTP] [PATCH 1/5] open_posix: add SAFE_PFUNC macro Jan Stancek
2016-02-19 10:03 ` [LTP] [PATCH 2/5] open_posix: condvar/schedule: use SAFE_PFUNC Jan Stancek
@ 2016-02-19 10:03 ` Jan Stancek
2016-02-22 14:57 ` Cyril Hrubis
2016-02-19 10:03 ` [LTP] [PATCH 4/5] open_posix: condvar/schedule: remove duplicit setsched calls Jan Stancek
` (2 subsequent siblings)
4 siblings, 1 reply; 13+ messages in thread
From: Jan Stancek @ 2016-02-19 10:03 UTC (permalink / raw)
To: ltp
If high prio thread wakes up, test is done, there's no point waiting
another 3 seconds for low prio thread to end its busy loop.
If test PASS-es, we save 3 seconds per test.
Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
.../functional/threads/condvar/pthread_cond_wait_1.c | 12 ++++++------
.../functional/threads/condvar/pthread_cond_wait_2.c | 12 ++++++------
.../open_posix_testsuite/functional/threads/schedule/1-1.c | 10 +++++-----
.../open_posix_testsuite/functional/threads/schedule/1-2.c | 10 +++++-----
4 files changed, 22 insertions(+), 22 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 5b36fc24a3b8..dfbf44bdefd7 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
@@ -39,8 +39,8 @@ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
/* Flags that the threads use to indicate events */
-int woken_up = -1;
-int low_done = -1;
+static volatile int woken_up;
+static volatile int low_done;
void signal_handler(int sig)
{
@@ -85,7 +85,7 @@ void *hi_prio_thread(void *tmp)
/* This variable is unprotected because the scheduling removes
* the contention
*/
- if (low_done != 1)
+ if (!low_done)
woken_up = 1;
SAFE_PFUNC(pthread_mutex_unlock(&mutex));
@@ -109,7 +109,7 @@ void *low_prio_thread(void *tmp)
}
clock_gettime(CLOCK_REALTIME, &start_time);
- while (1) {
+ while (!woken_up) {
clock_gettime(CLOCK_REALTIME, ¤t_time);
if (timediff(current_time, start_time) > RUNTIME)
break;
@@ -142,8 +142,8 @@ int main()
SAFE_PFUNC(pthread_join(high_id, NULL));
SAFE_PFUNC(pthread_join(low_id, NULL));
- if (woken_up == -1) {
- printf("Test FAILED: high priority was not woken up\\n");
+ if (!woken_up) {
+ 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 7308bd6b98d1..c5670f8e26fd 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
@@ -39,8 +39,8 @@ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
/* Flags that the threads use to indicate events */
-int woken_up = -1;
-int low_done = -1;
+static volatile int woken_up;
+static volatile int low_done;
void signal_handler(int sig)
{
@@ -85,7 +85,7 @@ void *hi_prio_thread(void *tmp)
/* This variable is unprotected because the scheduling removes
* the contention
*/
- if (low_done != 1)
+ if (!low_done)
woken_up = 1;
SAFE_PFUNC(pthread_mutex_unlock(&mutex));
@@ -109,7 +109,7 @@ void *low_prio_thread(void *tmp)
}
clock_gettime(CLOCK_REALTIME, &start_time);
- while (1) {
+ while (!woken_up) {
clock_gettime(CLOCK_REALTIME, ¤t_time);
if (timediff(current_time, start_time) > RUNTIME)
break;
@@ -142,8 +142,8 @@ int main()
SAFE_PFUNC(pthread_join(high_id, NULL));
SAFE_PFUNC(pthread_join(low_id, NULL));
- if (woken_up == -1) {
- printf("Test FAILED: high priority was not woken up\\n");
+ if (!woken_up) {
+ 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 f1bf37292b53..a6b634c7c59d 100644
--- a/testcases/open_posix_testsuite/functional/threads/schedule/1-1.c
+++ b/testcases/open_posix_testsuite/functional/threads/schedule/1-1.c
@@ -37,8 +37,8 @@
#define RUNTIME 5
pthread_barrier_t barrier;
-volatile int woken_up = -1;
-volatile int low_done = -1;
+static volatile int woken_up;
+static volatile int low_done;
float timediff(struct timespec t2, struct timespec t1)
{
@@ -91,7 +91,7 @@ void *hi_prio_thread(void *tmp)
/* This variable is unprotected because the scheduling removes
* the contention
*/
- if (low_done != 1)
+ if (!low_done)
woken_up = 1;
pthread_exit(NULL);
@@ -113,7 +113,7 @@ void *low_prio_thread(void *tmp)
}
clock_gettime(CLOCK_REALTIME, &start_timespec);
- while (1) {
+ while (!woken_up) {
clock_gettime(CLOCK_REALTIME, ¤t_timespec);
if (timediff(current_timespec, start_timespec) > RUNTIME)
break;
@@ -149,7 +149,7 @@ int main()
SAFE_PFUNC(pthread_join(high_id, NULL));
SAFE_PFUNC(pthread_join(low_id, NULL));
- if (woken_up == -1) {
+ if (!woken_up) {
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 3d3427b8ec4b..f7b0bd89e081 100644
--- a/testcases/open_posix_testsuite/functional/threads/schedule/1-2.c
+++ b/testcases/open_posix_testsuite/functional/threads/schedule/1-2.c
@@ -39,8 +39,8 @@ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
-volatile int woken_up = -1;
-volatile int low_done = -1;
+static volatile int woken_up;
+static volatile int low_done;
float timediff(struct timespec t2, struct timespec t1)
{
@@ -78,7 +78,7 @@ void *hi_prio_thread(void *tmp)
/* This variable is unprotected because the scheduling removes
* the contention
*/
- if (low_done != 1)
+ if (!low_done)
woken_up = 1;
SAFE_PFUNC(pthread_mutex_unlock(&mutex));
@@ -105,7 +105,7 @@ void *low_prio_thread(void *tmp)
}
clock_gettime(CLOCK_REALTIME, &start_time);
- while (1) {
+ while (!woken_up) {
clock_gettime(CLOCK_REALTIME, ¤t_time);
if (timediff(current_time, start_time) > RUNTIME)
break;
@@ -168,7 +168,7 @@ int main()
SAFE_PFUNC(pthread_join(high_id, NULL));
SAFE_PFUNC(pthread_join(low_id, NULL));
- if (woken_up == -1) {
+ if (!woken_up) {
printf("High priority was not woken up. Test FAILED.\n");
exit(PTS_FAIL);
}
--
1.8.3.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* [LTP] [PATCH 4/5] open_posix: condvar/schedule: remove duplicit setsched calls
2016-02-19 10:03 [LTP] [PATCH 1/5] open_posix: add SAFE_PFUNC macro Jan Stancek
2016-02-19 10:03 ` [LTP] [PATCH 2/5] open_posix: condvar/schedule: use SAFE_PFUNC Jan Stancek
2016-02-19 10:03 ` [LTP] [PATCH 3/5] open_posix: condvar/schedule: remove useless waiting Jan Stancek
@ 2016-02-19 10:03 ` Jan Stancek
2016-02-22 15:38 ` Cyril Hrubis
2016-02-19 10:03 ` [LTP] [PATCH 5/5] open_posix: condvar/schedule: mask SIGALRM in SCHED_OTHER threads Jan Stancek
2016-02-22 13:35 ` [LTP] [PATCH 1/5] open_posix: add SAFE_PFUNC macro Cyril Hrubis
4 siblings, 1 reply; 13+ messages in thread
From: Jan Stancek @ 2016-02-19 10:03 UTC (permalink / raw)
To: ltp
pthread_attr_setschedparam's man page says:
In order for the parameter setting made by pthread_attr_setschedparam()
to have effect when calling pthread_create(3), the caller must use
pthread_attr_setinheritsched(3) to set the inherit-scheduler attribute
of the attributes object attr to PTHREAD_EXPLICIT_SCHED.
So add PTHREAD_EXPLICIT_SCHED to attr used during pthread_create and
remove duplicit pthread_setsched calls in each thread.
Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
.../functional/threads/condvar/pthread_cond_wait_1.c | 6 ++----
.../functional/threads/condvar/pthread_cond_wait_2.c | 6 ++----
testcases/open_posix_testsuite/functional/threads/schedule/1-1.c | 8 ++++----
testcases/open_posix_testsuite/functional/threads/schedule/1-2.c | 9 ++++-----
4 files changed, 12 insertions(+), 17 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 dfbf44bdefd7..a674efc406ef 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
@@ -61,9 +61,7 @@ void *hi_prio_thread(void *tmp)
int policy;
(void) tmp;
- param.sched_priority = HIGH_PRIORITY;
- SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, ¶m));
SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
if ((policy != POLICY) || (param.sched_priority != HIGH_PRIORITY)) {
printf("Error: the policy or priority not correct\n");
@@ -99,9 +97,7 @@ void *low_prio_thread(void *tmp)
int policy;
(void) tmp;
- param.sched_priority = LOW_PRIORITY;
- SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, ¶m));
SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
if ((policy != POLICY) || (param.sched_priority != LOW_PRIORITY)) {
printf("Error: the policy or priority not correct\n");
@@ -126,6 +122,7 @@ int main()
/* Create the higher priority thread */
SAFE_PFUNC(pthread_attr_init(&high_attr));
+ SAFE_PFUNC(pthread_attr_setinheritsched(&high_attr, PTHREAD_EXPLICIT_SCHED));
SAFE_PFUNC(pthread_attr_setschedpolicy(&high_attr, POLICY));
param.sched_priority = HIGH_PRIORITY;
SAFE_PFUNC(pthread_attr_setschedparam(&high_attr, ¶m));
@@ -133,6 +130,7 @@ int main()
/* Create the low priority thread */
SAFE_PFUNC(pthread_attr_init(&low_attr));
+ SAFE_PFUNC(pthread_attr_setinheritsched(&low_attr, PTHREAD_EXPLICIT_SCHED));
SAFE_PFUNC(pthread_attr_setschedpolicy(&low_attr, POLICY));
param.sched_priority = LOW_PRIORITY;
SAFE_PFUNC(pthread_attr_setschedparam(&low_attr, ¶m));
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 c5670f8e26fd..398b8db5448e 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
@@ -61,9 +61,7 @@ void *hi_prio_thread(void *tmp)
int policy;
(void) tmp;
- param.sched_priority = HIGH_PRIORITY;
- SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, ¶m));
SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
if ((policy != POLICY) || (param.sched_priority != HIGH_PRIORITY)) {
printf("Error: the policy or priority not correct\n");
@@ -99,9 +97,7 @@ void *low_prio_thread(void *tmp)
int policy;
(void) tmp;
- param.sched_priority = LOW_PRIORITY;
- SAFE_PFUNC(pthread_setschedparam(pthread_self(), POLICY, ¶m));
SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
if ((policy != POLICY) || (param.sched_priority != LOW_PRIORITY)) {
printf("Error: the policy or priority not correct\n");
@@ -126,6 +122,7 @@ int main()
/* Create the higher priority thread */
SAFE_PFUNC(pthread_attr_init(&high_attr));
+ SAFE_PFUNC(pthread_attr_setinheritsched(&high_attr, PTHREAD_EXPLICIT_SCHED));
SAFE_PFUNC(pthread_attr_setschedpolicy(&high_attr, POLICY));
param.sched_priority = HIGH_PRIORITY;
SAFE_PFUNC(pthread_attr_setschedparam(&high_attr, ¶m));
@@ -133,6 +130,7 @@ int main()
/* Create the low priority thread */
SAFE_PFUNC(pthread_attr_init(&low_attr));
+ SAFE_PFUNC(pthread_attr_setinheritsched(&low_attr, PTHREAD_EXPLICIT_SCHED));
SAFE_PFUNC(pthread_attr_setschedpolicy(&low_attr, POLICY));
param.sched_priority = LOW_PRIORITY;
SAFE_PFUNC(pthread_attr_setschedparam(&low_attr, ¶m));
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 a6b634c7c59d..c65e127bdf5b 100644
--- a/testcases/open_posix_testsuite/functional/threads/schedule/1-1.c
+++ b/testcases/open_posix_testsuite/functional/threads/schedule/1-1.c
@@ -70,8 +70,7 @@ void *hi_prio_thread(void *tmp)
void *previous_signal;
(void) tmp;
- param.sched_priority = HIGH_PRIORITY;
- SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m));
+
SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
if (policy != SCHED_RR || param.sched_priority != HIGH_PRIORITY) {
printf("Error: the policy or priority not correct\n");
@@ -104,8 +103,7 @@ void *low_prio_thread(void *tmp)
int policy;
(void) tmp;
- param.sched_priority = LOW_PRIORITY;
- SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m));
+
SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
if (policy != SCHED_RR || param.sched_priority != LOW_PRIORITY) {
printf("Error: the policy or priority not correct\n");
@@ -133,6 +131,7 @@ int main()
/* Create the higher priority */
SAFE_PFUNC(pthread_attr_init(&high_attr));
+ SAFE_PFUNC(pthread_attr_setinheritsched(&high_attr, PTHREAD_EXPLICIT_SCHED));
SAFE_PFUNC(pthread_attr_setschedpolicy(&high_attr, SCHED_RR));
param.sched_priority = HIGH_PRIORITY;
SAFE_PFUNC(pthread_attr_setschedparam(&high_attr, ¶m));
@@ -140,6 +139,7 @@ int main()
/* Create the low priority thread */
SAFE_PFUNC(pthread_attr_init(&low_attr));
+ SAFE_PFUNC(pthread_attr_setinheritsched(&low_attr, PTHREAD_EXPLICIT_SCHED));
SAFE_PFUNC(pthread_attr_setschedpolicy(&low_attr, SCHED_RR));
param.sched_priority = LOW_PRIORITY;
SAFE_PFUNC(pthread_attr_setschedparam(&low_attr, ¶m));
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 f7b0bd89e081..c85460e7cf89 100644
--- a/testcases/open_posix_testsuite/functional/threads/schedule/1-2.c
+++ b/testcases/open_posix_testsuite/functional/threads/schedule/1-2.c
@@ -61,8 +61,7 @@ void *hi_prio_thread(void *tmp)
int policy;
(void) tmp;
- param.sched_priority = HIGH_PRIORITY;
- SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m));
+
SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
if (policy != SCHED_RR) {
printf(ERROR_PREFIX "The policy is not correct\n");
@@ -92,8 +91,7 @@ void *low_prio_thread(void *tmp)
int policy;
(void) tmp;
- param.sched_priority = LOW_PRIORITY;
- SAFE_PFUNC(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m));
+
SAFE_PFUNC(pthread_getschedparam(pthread_self(), &policy, ¶m));
if (policy != SCHED_RR) {
printf(ERROR_PREFIX "Policy not correct\n");
@@ -137,6 +135,7 @@ int main()
/* create the higher priority */
SAFE_PFUNC(pthread_attr_init(&high_attr));
+ SAFE_PFUNC(pthread_attr_setinheritsched(&high_attr, PTHREAD_EXPLICIT_SCHED));
SAFE_PFUNC(pthread_attr_setschedpolicy(&high_attr, SCHED_RR));
param.sched_priority = HIGH_PRIORITY;
SAFE_PFUNC(pthread_attr_setschedparam(&high_attr, ¶m));
@@ -144,6 +143,7 @@ int main()
/* Create the low priority thread */
SAFE_PFUNC(pthread_attr_init(&low_attr));
+ SAFE_PFUNC(pthread_attr_setinheritsched(&low_attr, PTHREAD_EXPLICIT_SCHED));
SAFE_PFUNC(pthread_attr_setschedpolicy(&low_attr, SCHED_RR));
param.sched_priority = LOW_PRIORITY;
SAFE_PFUNC(pthread_attr_setschedparam(&low_attr, ¶m));
@@ -156,7 +156,6 @@ int main()
}
SAFE_PFUNC(pthread_mutex_lock(&cond_mutex));
-
alarm(2);
SAFE_PFUNC(pthread_cond_wait(&cond, &cond_mutex));
SAFE_PFUNC(pthread_mutex_unlock(&cond_mutex));
--
1.8.3.1
^ permalink raw reply related [flat|nested] 13+ messages in thread