Skip to content
Permalink
Browse files

lib/posix: correct the meaning of CONFIG_MAX_PTHREAD_COUNT

Current code implement CONFIG_MAX_PTHREAD_COUNT as the maximum number
of POSIX threads that can ever be created, rather than the maximum
number of active POSIX threads. Use pthread_state of struct posix_thread
to track the state of posix thread in posix_thread_pool so that we can
reuse the unused posix thread.

Fixes #15516.

Signed-off-by: Wentong Wu <wentong.wu@intel.com>
  • Loading branch information...
wentongwu authored and nashif committed May 11, 2019
1 parent 6f6b469 commit 6922d93d8e4fc2420149b0670a507ac7cbd40684
Showing with 21 additions and 10 deletions.
  1. +6 −6 include/posix/pthread.h
  2. +2 −2 lib/posix/Kconfig
  3. +13 −2 lib/posix/pthread.c
@@ -18,14 +18,14 @@
#include <string.h>

enum pthread_state {
/* The thread structure is unallocated and available for reuse. */
PTHREAD_TERMINATED = 0,
/* The thread is running and joinable. */
PTHREAD_JOINABLE = 0,
PTHREAD_JOINABLE,
/* The thread is running and detached. */
PTHREAD_DETACHED,
/* A joinable thread exited and its return code is available. */
PTHREAD_EXITED,
/* The thread structure is unallocated and available for reuse. */
PTHREAD_TERMINATED
PTHREAD_EXITED
};

struct posix_thread {
@@ -49,8 +49,8 @@ struct posix_thread {
};

/* Pthread detach/joinable */
#define PTHREAD_CREATE_JOINABLE 0
#define PTHREAD_CREATE_DETACHED 1
#define PTHREAD_CREATE_JOINABLE PTHREAD_JOINABLE
#define PTHREAD_CREATE_DETACHED PTHREAD_DETACHED

/* Pthread cancellation */
#define _PTHREAD_CANCEL_POS 0
@@ -30,11 +30,11 @@ config PTHREAD_IPC

if PTHREAD_IPC
config MAX_PTHREAD_COUNT
int "Maximum pthread count in POSIX application"
int "Maximum simultaneously active pthread count in POSIX application"
default 5
range 0 255
help
Mention maximum number of threads in POSIX compliant application.
Maximum number of simultaneously active threads in a POSIX application.

config SEM_VALUE_MAX
int "Maximum semaphore limit"
@@ -35,7 +35,7 @@ static const pthread_attr_t init_pthread_attrs = {
};

static struct posix_thread posix_thread_pool[CONFIG_MAX_PTHREAD_COUNT];
static u32_t pthread_num;
PTHREAD_MUTEX_DEFINE(pthread_pool_lock);

static bool is_posix_prio_valid(u32_t priority, int policy)
{
@@ -132,6 +132,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
void *(*threadroutine)(void *), void *arg)
{
s32_t prio;
u32_t pthread_num;
pthread_condattr_t cond_attr;
struct posix_thread *thread;

@@ -145,6 +146,17 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
return EINVAL;
}

pthread_mutex_lock(&pthread_pool_lock);
for (pthread_num = 0;
pthread_num < CONFIG_MAX_PTHREAD_COUNT; pthread_num++) {
thread = &posix_thread_pool[pthread_num];
if (thread->state == PTHREAD_TERMINATED) {
thread->state = PTHREAD_JOINABLE;
break;
}
}
pthread_mutex_unlock(&pthread_pool_lock);

if (pthread_num >= CONFIG_MAX_PTHREAD_COUNT) {
return EAGAIN;
}
@@ -166,7 +178,6 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,

pthread_cond_init(&thread->state_cond, &cond_attr);
sys_slist_init(&thread->key_list);
pthread_num++;

*newthread = (pthread_t) k_thread_create(&thread->thread, attr->stack,
attr->stacksize,

0 comments on commit 6922d93

Please sign in to comment.
You can’t perform that action at this time.