tests/pthread_flood: cleanup test
- Cleans up pthread initialization, checks for ret code !=0, instead of -1. For some platforms the first thread was not started and therefore `pthread_join` could not succeed. - Always specify stackaddr and stacksize to not use malloc - Formatting and uncrustify
This commit is contained in:
parent
e8a8d12d96
commit
eb107a08cb
@ -28,6 +28,7 @@
|
|||||||
#include "mutex.h"
|
#include "mutex.h"
|
||||||
|
|
||||||
static char dummy_stack[MAXTHREADS][THREAD_STACKSIZE_IDLE];
|
static char dummy_stack[MAXTHREADS][THREAD_STACKSIZE_IDLE];
|
||||||
|
static pthread_t pthread_ids[MAXTHREADS];
|
||||||
static mutex_t testing_mutex;
|
static mutex_t testing_mutex;
|
||||||
|
|
||||||
static void *thread_func(void *arg)
|
static void *thread_func(void *arg)
|
||||||
@ -43,56 +44,43 @@ int main(void)
|
|||||||
mutex_init(&testing_mutex);
|
mutex_init(&testing_mutex);
|
||||||
mutex_lock(&testing_mutex);
|
mutex_lock(&testing_mutex);
|
||||||
|
|
||||||
|
|
||||||
int pthread_cnt = 0;
|
|
||||||
|
|
||||||
pthread_t pthread_ids[MAXTHREADS];
|
|
||||||
|
|
||||||
pthread_attr_t th_attr;
|
pthread_attr_t th_attr;
|
||||||
pthread_attr_init(&th_attr);
|
pthread_attr_init(&th_attr);
|
||||||
|
|
||||||
if (-1 ==
|
int numthread_check = sched_num_threads;
|
||||||
pthread_create(&(pthread_ids[pthread_cnt % MAXTHREADS]), &th_attr, thread_func,
|
|
||||||
NULL)) {
|
|
||||||
puts("[ERROR] cannot create pthreads");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
volatile int numthread_check = sched_num_threads;
|
|
||||||
|
|
||||||
int exit_loop = -1;
|
puts("Spawning pthreads");
|
||||||
|
int pthread_cnt = 0;
|
||||||
puts("[START] Spawning threads");
|
for (uint8_t i = 0; i < MAXTHREADS; i++) {
|
||||||
do {
|
pthread_attr_setstackaddr(&th_attr, &(dummy_stack[i]));
|
||||||
pthread_attr_setstackaddr(&th_attr,
|
|
||||||
&(dummy_stack[pthread_cnt % MAXTHREADS]));
|
|
||||||
pthread_attr_setstacksize(&th_attr, THREAD_STACKSIZE_IDLE);
|
pthread_attr_setstacksize(&th_attr, THREAD_STACKSIZE_IDLE);
|
||||||
exit_loop = pthread_create(&(pthread_ids[pthread_cnt + 1 % MAXTHREADS]), &th_attr,
|
if (pthread_create(&(pthread_ids[i]), &th_attr, thread_func, NULL)) {
|
||||||
thread_func, NULL);
|
break;
|
||||||
if (exit_loop == 0) {
|
|
||||||
++pthread_cnt;
|
|
||||||
printf(".");
|
|
||||||
}
|
}
|
||||||
} while (-1 != exit_loop);
|
printf(".");
|
||||||
volatile int numthread_check_after = sched_num_threads;
|
pthread_cnt++;
|
||||||
|
}
|
||||||
puts("");
|
puts("");
|
||||||
if (numthread_check_after - numthread_check == pthread_cnt &&
|
|
||||||
numthread_check_after == MAXTHREADS) {
|
int numthread_check_after = sched_num_threads;
|
||||||
printf("[SUCCESS]\n");
|
|
||||||
}
|
if ((numthread_check_after - numthread_check) != pthread_cnt ||
|
||||||
else {
|
numthread_check_after != MAXTHREADS) {
|
||||||
printf("[ERROR] expected %d,", (MAXTHREADS - numthread_check));
|
printf("[ERROR] expected %d, ", (MAXTHREADS - numthread_check));
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("created %d pthreads\n", pthread_cnt);
|
printf("created %d pthreads\n", pthread_cnt);
|
||||||
printf("created %d threads\n", numthread_check_after - numthread_check);
|
printf("created %d threads\n", numthread_check_after - numthread_check);
|
||||||
|
|
||||||
mutex_unlock(&testing_mutex);
|
mutex_unlock(&testing_mutex);
|
||||||
|
|
||||||
for (int i = 0; i < pthread_cnt; i++) {
|
puts("wait for created pthreads to exit...");
|
||||||
|
for (uint8_t i = 0; i < pthread_cnt; i++) {
|
||||||
if (pthread_ids[i] != 0) {
|
if (pthread_ids[i] != 0) {
|
||||||
pthread_join(pthread_ids[i], NULL);
|
pthread_join(pthread_ids[i], NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
puts("test end");
|
puts("SUCCESS");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,10 +5,13 @@ from testrunner import run
|
|||||||
|
|
||||||
|
|
||||||
def testfunc(child):
|
def testfunc(child):
|
||||||
child.expect_exact(u'[START] Spawning threads')
|
child.expect_exact("Spawning pthreads")
|
||||||
child.expect(r'\.+')
|
child.expect(r"created (\d+) pthreads")
|
||||||
child.expect("[SUCCESS]")
|
pthread_num = int(child.match.group(1))
|
||||||
child.expect("test end")
|
child.expect(r"created (\d+) threads")
|
||||||
|
thread_num = int(child.match.group(1))
|
||||||
|
assert thread_num == pthread_num
|
||||||
|
child.expect_exact("SUCCESS")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user