Merge pull request #2678 from BernardXiong/pthreads

[pthreads] The fields definition are more like those of newlib/glibc.
This commit is contained in:
Bernard Xiong 2019-05-12 17:01:26 +08:00 committed by GitHub
commit b7cc4e9c8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 34 deletions

View File

@ -34,7 +34,7 @@ static void _pthread_destroy(_pthread_data_t *ptd)
rt_sem_delete(ptd->joinable_sem); rt_sem_delete(ptd->joinable_sem);
/* release thread resource */ /* release thread resource */
if (ptd->attr.stack_base == RT_NULL) if (ptd->attr.stackaddr == RT_NULL)
{ {
/* release thread allocated stack */ /* release thread allocated stack */
rt_free(ptd->tid->stack_addr); 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 ++); 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 else
{ {
stack = (void *)(ptd->attr.stack_base); stack = (void *)(ptd->attr.stackaddr);
} }
if (stack == RT_NULL) 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)); ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
if (ptd->tid == RT_NULL) if (ptd->tid == RT_NULL)
{ {
if (ptd->attr.stack_base == 0) if (ptd->attr.stackaddr == 0)
rt_free(stack); rt_free(stack);
rt_free(ptd); 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); ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
if (ptd->joinable_sem == RT_NULL) if (ptd->joinable_sem == RT_NULL)
{ {
if (ptd->attr.stack_base != 0) if (ptd->attr.stackaddr != 0)
rt_free(stack); rt_free(stack);
rt_free(ptd); rt_free(ptd);
@ -169,10 +169,10 @@ int pthread_create(pthread_t *tid,
/* initial this pthread to system */ /* initial this pthread to system */
if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd, if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
stack, ptd->attr.stack_size, stack, ptd->attr.stacksize,
ptd->attr.priority, 5) != RT_EOK) ptd->attr.schedparam.sched_priority, 5) != RT_EOK)
{ {
if (ptd->attr.stack_base == 0) if (ptd->attr.stackaddr == 0)
rt_free(stack); rt_free(stack);
if (ptd->joinable_sem != RT_NULL) if (ptd->joinable_sem != RT_NULL)
rt_sem_delete(ptd->joinable_sem); rt_sem_delete(ptd->joinable_sem);
@ -195,7 +195,7 @@ int pthread_create(pthread_t *tid,
/* start thread failed */ /* start thread failed */
rt_thread_detach(ptd->tid); rt_thread_detach(ptd->tid);
if (ptd->attr.stack_base == 0) if (ptd->attr.stackaddr == 0)
rt_free(stack); rt_free(stack);
if (ptd->joinable_sem != RT_NULL) if (ptd->joinable_sem != RT_NULL)
rt_sem_delete(ptd->joinable_sem); rt_sem_delete(ptd->joinable_sem);

View File

@ -76,15 +76,21 @@ enum
#define PTHREAD_SCOPE_PROCESS 0 #define PTHREAD_SCOPE_PROCESS 0
#define PTHREAD_SCOPE_SYSTEM 1 #define PTHREAD_SCOPE_SYSTEM 1
struct sched_param
{
int sched_priority;
};
struct pthread_attr struct pthread_attr
{ {
void* stack_base; void* stackaddr; /* stack address of thread */
rt_uint32_t stack_size; /* stack size of thread */ int stacksize; /* stack size of thread */
rt_uint8_t priority; /* priority of thread */ int inheritsched; /* Inherit parent prio/policy */
rt_uint8_t detachstate; /* detach state */ int schedpolicy; /* scheduler policy */
rt_uint8_t policy; /* scheduler policy */ struct sched_param schedparam; /* sched parameter */
rt_uint8_t inheritsched; /* Inherit parent prio/policy */
int detachstate; /* detach state */
}; };
typedef struct pthread_attr pthread_attr_t; typedef struct pthread_attr pthread_attr_t;
@ -131,11 +137,6 @@ struct pthread_barrier
}; };
typedef struct pthread_barrier pthread_barrier_t; typedef struct pthread_barrier pthread_barrier_t;
struct sched_param
{
int sched_priority;
};
/* pthread thread interface */ /* pthread thread interface */
int pthread_attr_destroy(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *attr);
int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_init(pthread_attr_t *attr);

View File

@ -20,10 +20,13 @@ const pthread_attr_t pthread_default_attr =
{ {
0, /* stack base */ 0, /* stack base */
DEFAULT_STACK_SIZE, /* stack size */ DEFAULT_STACK_SIZE, /* stack size */
DEFAULT_PRIORITY, /* priority */
PTHREAD_CREATE_JOINABLE, /* detach state */ PTHREAD_INHERIT_SCHED, /* Inherit parent prio/policy */
SCHED_FIFO, /* scheduler 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) 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); RT_ASSERT(attr != RT_NULL);
attr->policy = policy; attr->schedpolicy = policy;
return 0; return 0;
} }
@ -83,7 +86,7 @@ int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy)
{ {
RT_ASSERT(attr != RT_NULL); RT_ASSERT(attr != RT_NULL);
*policy = (int)attr->policy; *policy = (int)attr->schedpolicy;
return 0; return 0;
} }
@ -95,7 +98,7 @@ int pthread_attr_setschedparam(pthread_attr_t *attr,
RT_ASSERT(attr != RT_NULL); RT_ASSERT(attr != RT_NULL);
RT_ASSERT(param != RT_NULL); RT_ASSERT(param != RT_NULL);
attr->priority = param->sched_priority; attr->schedparam.sched_priority = param->sched_priority;
return 0; return 0;
} }
@ -107,7 +110,7 @@ int pthread_attr_getschedparam(pthread_attr_t const *attr,
RT_ASSERT(attr != RT_NULL); RT_ASSERT(attr != RT_NULL);
RT_ASSERT(param != RT_NULL); RT_ASSERT(param != RT_NULL);
param->sched_priority = attr->priority; param->sched_priority = attr->schedparam.sched_priority;
return 0; return 0;
} }
@ -117,7 +120,7 @@ int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size)
{ {
RT_ASSERT(attr != RT_NULL); RT_ASSERT(attr != RT_NULL);
attr->stack_size = stack_size; attr->stacksize = stack_size;
return 0; return 0;
} }
@ -127,7 +130,7 @@ int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size)
{ {
RT_ASSERT(attr != RT_NULL); RT_ASSERT(attr != RT_NULL);
*stack_size = attr->stack_size; *stack_size = attr->stacksize;
return 0; return 0;
} }
@ -155,8 +158,8 @@ int pthread_attr_setstack(pthread_attr_t *attr,
{ {
RT_ASSERT(attr != RT_NULL); RT_ASSERT(attr != RT_NULL);
attr->stack_base = stack_base; attr->stackaddr = stack_base;
attr->stack_size = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE); attr->stacksize = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
return 0; return 0;
} }
@ -168,8 +171,8 @@ int pthread_attr_getstack(pthread_attr_t const *attr,
{ {
RT_ASSERT(attr != RT_NULL); RT_ASSERT(attr != RT_NULL);
*stack_base = attr->stack_base; *stack_base = attr->stackaddr;
*stack_size = attr->stack_size; *stack_size = attr->stacksize;
return 0; return 0;
} }