From: Cyril H. <su...@li...> - 2013-10-17 08:51:17
|
The branch, master, has been updated via efddf81c171823091af8777b22e6386abc05a7ee (commit) via a2b2b352c9af90c27b58a5e6bcc4dfb8022f3b1a (commit) via 683835e0b151b3d0b7998cc15f0e615820b6d07f (commit) from 48d49bb21eaa84b554938054df77807241d5ed5b (commit) - Log ----------------------------------------------------------------- commit efddf81c171823091af8777b22e6386abc05a7ee Author: Jan Stancek <jst...@re...> Date: Thu Oct 3 10:22:50 2013 +0200 pthread_attr_setschedpolicy/2-1.c: bind threads to single CPU To make sure that threads are blocked on mutex, bind all to single CPU. Because they all use SCHED_FIFO policy with main thread having the lowest priority, main will progress only after three created threads block on mutex. Signed-off-by: Jan Stancek <jst...@re...> commit a2b2b352c9af90c27b58a5e6bcc4dfb8022f3b1a Author: Jan Stancek <jst...@re...> Date: Thu Oct 3 10:10:30 2013 +0200 pthread_attr_setschedpolicy/2-1.c: fix race at thread startup Test can hang during startup in following scenario: main new thread --------------------------------------------------+------------------------------------- int create_thread(int prio, pthread_t * tid) | ... | pthread_create(tid, &attr, thread_func, NULL);| while (!thread_started) { | | void *thread_func(void *data) | thread_started = 1; | pthread_cond_signal(&cond); pthread_mutex_lock(&c_mutex); | 132: pthread_cond_wait(&cond, &c_mutex); | |65: pthread_mutex_lock(&mutex); | (gdb) bt #0 0x00007fd588808f6d in __lll_lock_wait () from /lib64/libpthread.so.0 #1 0x00007fd588804d31 in _L_lock_790 () from /lib64/libpthread.so.0 #2 0x00007fd588804c37 in pthread_mutex_lock () from /lib64/libpthread.so.0 #3 0x0000000000400bba in thread_func (data=0x0) at ../../../conformance/interfaces/pthread_attr_setschedpolicy/2-1.c:65 #4 0x00007fd588802de3 in start_thread () from /lib64/libpthread.so.0 #5 0x00007fd5885300dd in clone () from /lib64/libc.so.6 (gdb) t 2 (gdb) bt #0 0x00007fd5888066f5 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0 #1 0x0000000000400d9b in create_thread (prio=5, tid=0x7fffdb562678) at ../../../conformance/interfaces/pthread_attr_setschedpolicy/2-1.c:132 #2 0x0000000000400e82 in main () at ../../../conformance/interfaces/pthread_attr_setschedpolicy/2-1.c:174 Fix this by using same c_mutex in thread_func for updating thread_started and signalling cond. Signed-off-by: Jan Stancek <jst...@re...> commit 683835e0b151b3d0b7998cc15f0e615820b6d07f Author: Jan Stancek <jst...@re...> Date: Thu Oct 3 10:02:29 2013 +0200 pthread_attr_setschedpolicy/2-1.c: simplify error handling Remove all gotos and if there's error in any of pthread functions exit immediately. Signed-off-by: Jan Stancek <jst...@re...> ----------------------------------------------------------------------- Summary of changes: .../interfaces/pthread_attr_setschedpolicy/2-1.c | 135 +++++++++----------- 1 files changed, 58 insertions(+), 77 deletions(-) diff --git a/testcases/open_posix_testsuite/conformance/interfaces/pthread_attr_setschedpolicy/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/pthread_attr_setschedpolicy/2-1.c index 6088466..af8e3e4 100644 --- a/testcases/open_posix_testsuite/conformance/interfaces/pthread_attr_setschedpolicy/2-1.c +++ b/testcases/open_posix_testsuite/conformance/interfaces/pthread_attr_setschedpolicy/2-1.c @@ -20,17 +20,21 @@ * Date: 20/05/2011 */ +#define _GNU_SOURCE #include <stdio.h> #include <string.h> #include <pthread.h> #include <unistd.h> +#include <stdlib.h> #include <errno.h> #include <posixtest.h> +#include <affinity.h> /* Priorities for the threads, must be unique, non-zero, and ordered */ #define PRIO_HIGH 20 #define PRIO_MED 10 #define PRIO_LOW 5 +#define PRIO_MAIN 1 static int priorities[3]; @@ -40,8 +44,10 @@ static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int thread_started; -#define ERR_MSG(f, rc) printf("Failed: function: %s status: %s(%u)\n", \ - f, strerror(rc), rc) +#define FAIL_AND_EXIT(f, rc) { \ + printf("Failed: function: %s status: %s(%u)\n", f, strerror(rc), rc); \ + exit(PTS_UNRESOLVED); \ +} static void *thread_func(void *data) { @@ -50,23 +56,23 @@ static void *thread_func(void *data) int rc; rc = pthread_getschedparam(pthread_self(), &policy, &sp); - if (rc) { - ERR_MSG("pthread_getschedparam()", rc); - goto done; - } + if (rc) + FAIL_AND_EXIT("pthread_getschedparam()", rc); + rc = pthread_mutex_lock(&c_mutex); + if (rc) + FAIL_AND_EXIT("pthread_mutex_lock()", rc); thread_started = 1; rc = pthread_cond_signal(&cond); - if (rc) { - ERR_MSG("pthread_cond_signal()", rc); - goto done; - } + if (rc) + FAIL_AND_EXIT("pthread_cond_signal()", rc); + rc = pthread_mutex_unlock(&c_mutex); + if (rc) + FAIL_AND_EXIT("pthread_mutex_unlock()", rc); rc = pthread_mutex_lock(&mutex); - if (rc) { - ERR_MSG("pthread_mutex_lock()", rc); - goto done; - } + if (rc) + FAIL_AND_EXIT("pthread_mutex_lock()", rc); /* Stuff the priority in execution order */ if (!priorities[0]) @@ -77,79 +83,56 @@ static void *thread_func(void *data) priorities[2] = sp.sched_priority; rc = pthread_mutex_unlock(&mutex); - if (rc) { - ERR_MSG("pthread_mutex_unlock()", rc); - goto done; - } + if (rc) + FAIL_AND_EXIT("pthread_mutex_unlock()", rc); -done: return (void *)(long)rc; } static int create_thread(int prio, pthread_t * tid) { int rc; - char *func; struct sched_param sp; pthread_attr_t attr; - func = "pthread_attr_init()"; rc = pthread_attr_init(&attr); if (rc != 0) - goto done; + FAIL_AND_EXIT("pthread_attr_init()", rc); - func = "pthread_attr_setschedpolicy()"; rc = pthread_attr_setschedpolicy(&attr, SCHED_FIFO); if (rc != 0) - goto error; + FAIL_AND_EXIT("pthread_attr_setschedpolicy()", rc); - func = "pthread_attr_setinheritsched()"; rc = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); if (rc != 0) - goto error; + FAIL_AND_EXIT("pthread_attr_setinheritsched()", rc); - func = "pthread_attr_setschedparam()"; sp.sched_priority = prio; rc = pthread_attr_setschedparam(&attr, &sp); if (rc != 0) - goto error; + FAIL_AND_EXIT("pthread_attr_setschedparam()", rc); thread_started = 0; rc = pthread_create(tid, &attr, thread_func, NULL); - if (rc) { - ERR_MSG("pthread_create()", rc); - goto error; - } + if (rc) + FAIL_AND_EXIT("pthread_create()", rc); + rc = pthread_mutex_lock(&c_mutex); + if (rc) + FAIL_AND_EXIT("pthread_mutex_lock()", rc); while (!thread_started) { - func = "pthread_mutex_lock()"; - rc = pthread_mutex_lock(&c_mutex); - if (rc) - goto error; - - func = "pthread_cond_wait()"; rc = pthread_cond_wait(&cond, &c_mutex); if (rc) - goto unlock; - - func = "pthread_mutex_unlock()"; - rc = pthread_mutex_unlock(&c_mutex); - if (rc) - goto error; + FAIL_AND_EXIT("pthread_cond_wait()", rc); } + rc = pthread_mutex_unlock(&c_mutex); + if (rc) + FAIL_AND_EXIT("pthread_mutex_unlock()", rc); pthread_attr_destroy(&attr); return 0; - -unlock: - (void)pthread_mutex_unlock(&c_mutex); -error: - pthread_attr_destroy(&attr); -done: - ERR_MSG(func, rc); - return -1; } int main(void) @@ -162,52 +145,51 @@ int main(void) pthread_t t1; pthread_t t2; pthread_t t3; + struct sched_param sp; status = PTS_UNRESOLVED; + + rc = set_affinity(0); + if (rc) + FAIL_AND_EXIT("set_affinity", errno); + + sp.sched_priority = PRIO_MAIN; + rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp); + if (rc) + FAIL_AND_EXIT("pthread_setschedparam()", rc); + rc = pthread_mutex_lock(&mutex); - if (rc) { - ERR_MSG("pthread_mutex_lock()", rc); - goto done; - } + if (rc) + FAIL_AND_EXIT("pthread_mutex_lock()", rc); rc = create_thread(PRIO_LOW, &t3); if (rc) - goto done; + FAIL_AND_EXIT("create_thread LOW", rc); rc = create_thread(PRIO_MED, &t2); if (rc) - goto done; + FAIL_AND_EXIT("create_thread MED", rc); rc = create_thread(PRIO_HIGH, &t1); if (rc) - goto done; + FAIL_AND_EXIT("create_thread HIGH", rc); rc = pthread_mutex_unlock(&mutex); if (rc) - ERR_MSG("pthread_mutex_unlock()", rc); + FAIL_AND_EXIT("pthread_mutex_unlock()", rc); rc = pthread_join(t1, &r1); - if (rc) { - ERR_MSG("pthread_join(t1)", rc); - goto done; - } + if (rc) + FAIL_AND_EXIT("pthread_join(t1)", rc); rc = pthread_join(t2, &r2); - if (rc) { - ERR_MSG("pthread_join(t2)", rc); - goto done; - } + if (rc) + FAIL_AND_EXIT("pthread_join(t2)", rc); rc = pthread_join(t3, &r3); - if (rc) { - ERR_MSG("pthread_join(t3)", rc); - goto done; - } - - /* Threads fail? */ - if ((long)r1 || (long)r2 || (long)r2) - goto done; + if (rc) + FAIL_AND_EXIT("pthread_join(t3)", rc); /* priorities must be high to low */ status = PTS_FAIL; @@ -223,7 +205,6 @@ int main(void) else status = PTS_PASS; -done: if (status == PTS_PASS) printf("Test PASSED\n"); hooks/post-receive -- ltp |