From 3638e5129ebca52190976ebd1f600af078996449 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sun, 12 May 2019 15:04:46 +0800 Subject: [PATCH] [pthreads] The fields definition are more like those of newlib/glibc. --- components/libc/pthreads/pthread.c | 20 ++++++++--------- components/libc/pthreads/pthread.h | 23 ++++++++++---------- components/libc/pthreads/pthread_attr.c | 29 ++++++++++++++----------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/components/libc/pthreads/pthread.c b/components/libc/pthreads/pthread.c index 55f46c0caa..a1072670af 100644 --- a/components/libc/pthreads/pthread.c +++ b/components/libc/pthreads/pthread.c @@ -34,7 +34,7 @@ static void _pthread_destroy(_pthread_data_t *ptd) rt_sem_delete(ptd->joinable_sem); /* release thread resource */ - if (ptd->attr.stack_base == RT_NULL) + if (ptd->attr.stackaddr == RT_NULL) { /* release thread allocated stack */ rt_free(ptd->tid->stack_addr); @@ -119,13 +119,13 @@ int pthread_create(pthread_t *tid, } rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++); - if (ptd->attr.stack_base == 0) + if (ptd->attr.stackaddr == 0) { - stack = (void *)rt_malloc(ptd->attr.stack_size); + stack = (void *)rt_malloc(ptd->attr.stacksize); } else { - stack = (void *)(ptd->attr.stack_base); + stack = (void *)(ptd->attr.stackaddr); } if (stack == RT_NULL) @@ -139,7 +139,7 @@ int pthread_create(pthread_t *tid, ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread)); if (ptd->tid == RT_NULL) { - if (ptd->attr.stack_base == 0) + if (ptd->attr.stackaddr == 0) rt_free(stack); rt_free(ptd); @@ -151,7 +151,7 @@ int pthread_create(pthread_t *tid, ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO); if (ptd->joinable_sem == RT_NULL) { - if (ptd->attr.stack_base != 0) + if (ptd->attr.stackaddr != 0) rt_free(stack); rt_free(ptd); @@ -169,10 +169,10 @@ int pthread_create(pthread_t *tid, /* initial this pthread to system */ if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd, - stack, ptd->attr.stack_size, - ptd->attr.priority, 5) != RT_EOK) + stack, ptd->attr.stacksize, + ptd->attr.schedparam.sched_priority, 5) != RT_EOK) { - if (ptd->attr.stack_base == 0) + if (ptd->attr.stackaddr == 0) rt_free(stack); if (ptd->joinable_sem != RT_NULL) rt_sem_delete(ptd->joinable_sem); @@ -195,7 +195,7 @@ int pthread_create(pthread_t *tid, /* start thread failed */ rt_thread_detach(ptd->tid); - if (ptd->attr.stack_base == 0) + if (ptd->attr.stackaddr == 0) rt_free(stack); if (ptd->joinable_sem != RT_NULL) rt_sem_delete(ptd->joinable_sem); diff --git a/components/libc/pthreads/pthread.h b/components/libc/pthreads/pthread.h index f9a0a92b68..c86ee90b0e 100644 --- a/components/libc/pthreads/pthread.h +++ b/components/libc/pthreads/pthread.h @@ -76,15 +76,21 @@ enum #define PTHREAD_SCOPE_PROCESS 0 #define PTHREAD_SCOPE_SYSTEM 1 +struct sched_param +{ + int sched_priority; +}; + struct pthread_attr { - void* stack_base; - rt_uint32_t stack_size; /* stack size of thread */ + void* stackaddr; /* stack address of thread */ + int stacksize; /* stack size of thread */ - rt_uint8_t priority; /* priority of thread */ - rt_uint8_t detachstate; /* detach state */ - rt_uint8_t policy; /* scheduler policy */ - rt_uint8_t inheritsched; /* Inherit parent prio/policy */ + int inheritsched; /* Inherit parent prio/policy */ + int schedpolicy; /* scheduler policy */ + struct sched_param schedparam; /* sched parameter */ + + int detachstate; /* detach state */ }; typedef struct pthread_attr pthread_attr_t; @@ -131,11 +137,6 @@ struct pthread_barrier }; typedef struct pthread_barrier pthread_barrier_t; -struct sched_param -{ - int sched_priority; -}; - /* pthread thread interface */ int pthread_attr_destroy(pthread_attr_t *attr); int pthread_attr_init(pthread_attr_t *attr); diff --git a/components/libc/pthreads/pthread_attr.c b/components/libc/pthreads/pthread_attr.c index 8eed35932f..2294599bf3 100644 --- a/components/libc/pthreads/pthread_attr.c +++ b/components/libc/pthreads/pthread_attr.c @@ -20,10 +20,13 @@ const pthread_attr_t pthread_default_attr = { 0, /* stack base */ DEFAULT_STACK_SIZE, /* stack size */ - DEFAULT_PRIORITY, /* priority */ - PTHREAD_CREATE_JOINABLE, /* detach state */ + + PTHREAD_INHERIT_SCHED, /* Inherit parent prio/policy */ SCHED_FIFO, /* scheduler policy */ - PTHREAD_INHERIT_SCHED /* Inherit parent prio/policy */ + { + DEFAULT_PRIORITY, /* scheduler priority */ + }, + PTHREAD_CREATE_JOINABLE, /* detach state */ }; int pthread_attr_init(pthread_attr_t *attr) @@ -73,7 +76,7 @@ int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) { RT_ASSERT(attr != RT_NULL); - attr->policy = policy; + attr->schedpolicy = policy; return 0; } @@ -83,7 +86,7 @@ int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy) { RT_ASSERT(attr != RT_NULL); - *policy = (int)attr->policy; + *policy = (int)attr->schedpolicy; return 0; } @@ -95,7 +98,7 @@ int pthread_attr_setschedparam(pthread_attr_t *attr, RT_ASSERT(attr != RT_NULL); RT_ASSERT(param != RT_NULL); - attr->priority = param->sched_priority; + attr->schedparam.sched_priority = param->sched_priority; return 0; } @@ -107,7 +110,7 @@ int pthread_attr_getschedparam(pthread_attr_t const *attr, RT_ASSERT(attr != RT_NULL); RT_ASSERT(param != RT_NULL); - param->sched_priority = attr->priority; + param->sched_priority = attr->schedparam.sched_priority; return 0; } @@ -117,7 +120,7 @@ int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size) { RT_ASSERT(attr != RT_NULL); - attr->stack_size = stack_size; + attr->stacksize = stack_size; return 0; } @@ -127,7 +130,7 @@ int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size) { RT_ASSERT(attr != RT_NULL); - *stack_size = attr->stack_size; + *stack_size = attr->stacksize; return 0; } @@ -155,8 +158,8 @@ int pthread_attr_setstack(pthread_attr_t *attr, { RT_ASSERT(attr != RT_NULL); - attr->stack_base = stack_base; - attr->stack_size = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE); + attr->stackaddr = stack_base; + attr->stacksize = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE); return 0; } @@ -168,8 +171,8 @@ int pthread_attr_getstack(pthread_attr_t const *attr, { RT_ASSERT(attr != RT_NULL); - *stack_base = attr->stack_base; - *stack_size = attr->stack_size; + *stack_base = attr->stackaddr; + *stack_size = attr->stacksize; return 0; }