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:
Francisco Molina 2020-07-21 17:08:50 +02:00
parent e8a8d12d96
commit eb107a08cb
No known key found for this signature in database
GPG Key ID: 3E94EAC3DBDEEDA8
2 changed files with 29 additions and 38 deletions

View File

@ -28,6 +28,7 @@
#include "mutex.h"
static char dummy_stack[MAXTHREADS][THREAD_STACKSIZE_IDLE];
static pthread_t pthread_ids[MAXTHREADS];
static mutex_t testing_mutex;
static void *thread_func(void *arg)
@ -43,56 +44,43 @@ int main(void)
mutex_init(&testing_mutex);
mutex_lock(&testing_mutex);
int pthread_cnt = 0;
pthread_t pthread_ids[MAXTHREADS];
pthread_attr_t th_attr;
pthread_attr_init(&th_attr);
if (-1 ==
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 numthread_check = sched_num_threads;
int exit_loop = -1;
puts("[START] Spawning threads");
do {
pthread_attr_setstackaddr(&th_attr,
&(dummy_stack[pthread_cnt % MAXTHREADS]));
puts("Spawning pthreads");
int pthread_cnt = 0;
for (uint8_t i = 0; i < MAXTHREADS; i++) {
pthread_attr_setstackaddr(&th_attr, &(dummy_stack[i]));
pthread_attr_setstacksize(&th_attr, THREAD_STACKSIZE_IDLE);
exit_loop = pthread_create(&(pthread_ids[pthread_cnt + 1 % MAXTHREADS]), &th_attr,
thread_func, NULL);
if (exit_loop == 0) {
++pthread_cnt;
printf(".");
if (pthread_create(&(pthread_ids[i]), &th_attr, thread_func, NULL)) {
break;
}
} while (-1 != exit_loop);
volatile int numthread_check_after = sched_num_threads;
printf(".");
pthread_cnt++;
}
puts("");
if (numthread_check_after - numthread_check == pthread_cnt &&
numthread_check_after == MAXTHREADS) {
printf("[SUCCESS]\n");
}
else {
printf("[ERROR] expected %d,", (MAXTHREADS - numthread_check));
int numthread_check_after = sched_num_threads;
if ((numthread_check_after - numthread_check) != pthread_cnt ||
numthread_check_after != MAXTHREADS) {
printf("[ERROR] expected %d, ", (MAXTHREADS - numthread_check));
}
printf("created %d pthreads\n", pthread_cnt);
printf("created %d threads\n", numthread_check_after - numthread_check);
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) {
pthread_join(pthread_ids[i], NULL);
}
}
puts("test end");
puts("SUCCESS");
return 0;
}

View File

@ -5,10 +5,13 @@ from testrunner import run
def testfunc(child):
child.expect_exact(u'[START] Spawning threads')
child.expect(r'\.+')
child.expect("[SUCCESS]")
child.expect("test end")
child.expect_exact("Spawning pthreads")
child.expect(r"created (\d+) pthreads")
pthread_num = int(child.match.group(1))
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__":