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>
This commit is contained in:
parent
6f6b46971a
commit
6922d93d8e
3 changed files with 21 additions and 10 deletions
|
@ -18,14 +18,14 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
enum pthread_state {
|
enum pthread_state {
|
||||||
|
/* The thread structure is unallocated and available for reuse. */
|
||||||
|
PTHREAD_TERMINATED = 0,
|
||||||
/* The thread is running and joinable. */
|
/* The thread is running and joinable. */
|
||||||
PTHREAD_JOINABLE = 0,
|
PTHREAD_JOINABLE,
|
||||||
/* The thread is running and detached. */
|
/* The thread is running and detached. */
|
||||||
PTHREAD_DETACHED,
|
PTHREAD_DETACHED,
|
||||||
/* A joinable thread exited and its return code is available. */
|
/* A joinable thread exited and its return code is available. */
|
||||||
PTHREAD_EXITED,
|
PTHREAD_EXITED
|
||||||
/* The thread structure is unallocated and available for reuse. */
|
|
||||||
PTHREAD_TERMINATED
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct posix_thread {
|
struct posix_thread {
|
||||||
|
@ -49,8 +49,8 @@ struct posix_thread {
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Pthread detach/joinable */
|
/* Pthread detach/joinable */
|
||||||
#define PTHREAD_CREATE_JOINABLE 0
|
#define PTHREAD_CREATE_JOINABLE PTHREAD_JOINABLE
|
||||||
#define PTHREAD_CREATE_DETACHED 1
|
#define PTHREAD_CREATE_DETACHED PTHREAD_DETACHED
|
||||||
|
|
||||||
/* Pthread cancellation */
|
/* Pthread cancellation */
|
||||||
#define _PTHREAD_CANCEL_POS 0
|
#define _PTHREAD_CANCEL_POS 0
|
||||||
|
|
|
@ -30,11 +30,11 @@ config PTHREAD_IPC
|
||||||
|
|
||||||
if PTHREAD_IPC
|
if PTHREAD_IPC
|
||||||
config MAX_PTHREAD_COUNT
|
config MAX_PTHREAD_COUNT
|
||||||
int "Maximum pthread count in POSIX application"
|
int "Maximum simultaneously active pthread count in POSIX application"
|
||||||
default 5
|
default 5
|
||||||
range 0 255
|
range 0 255
|
||||||
help
|
help
|
||||||
Mention maximum number of threads in POSIX compliant application.
|
Maximum number of simultaneously active threads in a POSIX application.
|
||||||
|
|
||||||
config SEM_VALUE_MAX
|
config SEM_VALUE_MAX
|
||||||
int "Maximum semaphore limit"
|
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 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)
|
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)
|
void *(*threadroutine)(void *), void *arg)
|
||||||
{
|
{
|
||||||
s32_t prio;
|
s32_t prio;
|
||||||
|
u32_t pthread_num;
|
||||||
pthread_condattr_t cond_attr;
|
pthread_condattr_t cond_attr;
|
||||||
struct posix_thread *thread;
|
struct posix_thread *thread;
|
||||||
|
|
||||||
|
@ -145,6 +146,17 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
|
||||||
return EINVAL;
|
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) {
|
if (pthread_num >= CONFIG_MAX_PTHREAD_COUNT) {
|
||||||
return EAGAIN;
|
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);
|
pthread_cond_init(&thread->state_cond, &cond_attr);
|
||||||
sys_slist_init(&thread->key_list);
|
sys_slist_init(&thread->key_list);
|
||||||
pthread_num++;
|
|
||||||
|
|
||||||
*newthread = (pthread_t) k_thread_create(&thread->thread, attr->stack,
|
*newthread = (pthread_t) k_thread_create(&thread->thread, attr->stack,
|
||||||
attr->stacksize,
|
attr->stacksize,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue