Merge pull request #12056 from JulianHolzwarth/pr/posix/pthread/small_insert_fix
sys/posix/pthread/pthread.c: fix thread count limiting in pthread_create()
This commit is contained in:
commit
00e0a1cf13
@ -87,7 +87,7 @@ static void *pthread_start_routine(void *pt_)
|
|||||||
|
|
||||||
static int insert(pthread_thread_t *pt)
|
static int insert(pthread_thread_t *pt)
|
||||||
{
|
{
|
||||||
int result = -1;
|
int result = KERNEL_PID_UNDEF;
|
||||||
mutex_lock(&pthread_mutex);
|
mutex_lock(&pthread_mutex);
|
||||||
|
|
||||||
for (int i = 0; i < MAXTHREADS; i++){
|
for (int i = 0; i < MAXTHREADS; i++){
|
||||||
@ -160,7 +160,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr, void *(*sta
|
|||||||
pthread_start_routine,
|
pthread_start_routine,
|
||||||
pt,
|
pt,
|
||||||
"pthread");
|
"pthread");
|
||||||
if (pt->thread_pid == KERNEL_PID_UNDEF) {
|
if (!pid_is_valid(pt->thread_pid)) {
|
||||||
free(pt->stack);
|
free(pt->stack);
|
||||||
free(pt);
|
free(pt);
|
||||||
pthread_sched_threads[pthread_pid-1] = NULL;
|
pthread_sched_threads[pthread_pid-1] = NULL;
|
||||||
|
|||||||
16
tests/pthread_flood/Makefile
Normal file
16
tests/pthread_flood/Makefile
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
include ../Makefile.tests_common
|
||||||
|
|
||||||
|
BOARD_INSUFFICIENT_MEMORY := nucleo-f031k6
|
||||||
|
|
||||||
|
BOARD_BLACKLIST := arduino-duemilanove arduino-leonardo arduino-mega2560 \
|
||||||
|
arduino-nano arduino-uno jiminy-mega256rfr2 mega-xplained \
|
||||||
|
waspmote-pro
|
||||||
|
# arduino mega2560 uno duemilanove : unknown type name: clockid_t
|
||||||
|
# jiminy-mega256rfr2: unknown type name: clockid_t
|
||||||
|
# mega-xplained: unknown type name: clockid_t
|
||||||
|
|
||||||
|
USEMODULE += posix_headers
|
||||||
|
USEMODULE += pthread
|
||||||
|
CFLAGS += -DMAXTHREADS=8
|
||||||
|
|
||||||
|
include $(RIOTBASE)/Makefile.include
|
||||||
98
tests/pthread_flood/main.c
Normal file
98
tests/pthread_flood/main.c
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2019 Freie Universität Berlin,
|
||||||
|
*
|
||||||
|
* This file is subject to the terms and conditions of the GNU Lesser
|
||||||
|
* General Public License v2.1. See the file LICENSE in the top level
|
||||||
|
* directory for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ingroup tests
|
||||||
|
* @{
|
||||||
|
*
|
||||||
|
* @file
|
||||||
|
* @brief pthread test application
|
||||||
|
*
|
||||||
|
* Spawns pthreads till the scheduler's capacity is exhausted.
|
||||||
|
*
|
||||||
|
* @author Julian Holzwarth <julian.holzwarth@fu-berlin.de>
|
||||||
|
*
|
||||||
|
* @}
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "kernel_types.h"
|
||||||
|
#include "thread.h"
|
||||||
|
#include "pthread.h"
|
||||||
|
#include "mutex.h"
|
||||||
|
|
||||||
|
static char dummy_stack[MAXTHREADS][THREAD_STACKSIZE_IDLE];
|
||||||
|
static mutex_t testing_mutex;
|
||||||
|
|
||||||
|
static void *thread_func(void *arg)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
mutex_lock(&testing_mutex);
|
||||||
|
mutex_unlock(&testing_mutex);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 exit_loop = -1;
|
||||||
|
|
||||||
|
puts("[START] Spawning threads");
|
||||||
|
do {
|
||||||
|
pthread_attr_setstackaddr(&th_attr,
|
||||||
|
&(dummy_stack[pthread_cnt % MAXTHREADS]));
|
||||||
|
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(".");
|
||||||
|
}
|
||||||
|
} while (-1 != exit_loop);
|
||||||
|
volatile int numthread_check_after = sched_num_threads;
|
||||||
|
|
||||||
|
puts("");
|
||||||
|
if (numthread_check_after - numthread_check == pthread_cnt &&
|
||||||
|
numthread_check_after == MAXTHREADS) {
|
||||||
|
printf("[SUCCESS]\n");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
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++) {
|
||||||
|
if (pthread_ids[i] != 0) {
|
||||||
|
pthread_join(pthread_ids[i], NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
puts("test end");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
15
tests/pthread_flood/tests/01-run.py
Executable file
15
tests/pthread_flood/tests/01-run.py
Executable file
@ -0,0 +1,15 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from testrunner import run
|
||||||
|
|
||||||
|
|
||||||
|
def testfunc(child):
|
||||||
|
child.expect_exact(u'[START] Spawning threads')
|
||||||
|
child.expect(r'\.+')
|
||||||
|
child.expect("[SUCCESS]")
|
||||||
|
child.expect("test end")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(run(testfunc))
|
||||||
Loading…
x
Reference in New Issue
Block a user