Skip to content

Commit b7cc4e9

Browse files
authored
Merge pull request #2678 from BernardXiong/pthreads
[pthreads] The fields definition are more like those of newlib/glibc.
2 parents 3778758 + 3638e51 commit b7cc4e9

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

components/libc/pthreads/pthread.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void _pthread_destroy(_pthread_data_t *ptd)
3434
rt_sem_delete(ptd->joinable_sem);
3535

3636
/* release thread resource */
37-
if (ptd->attr.stack_base == RT_NULL)
37+
if (ptd->attr.stackaddr == RT_NULL)
3838
{
3939
/* release thread allocated stack */
4040
rt_free(ptd->tid->stack_addr);
@@ -119,13 +119,13 @@ int pthread_create(pthread_t *tid,
119119
}
120120

121121
rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
122-
if (ptd->attr.stack_base == 0)
122+
if (ptd->attr.stackaddr == 0)
123123
{
124-
stack = (void *)rt_malloc(ptd->attr.stack_size);
124+
stack = (void *)rt_malloc(ptd->attr.stacksize);
125125
}
126126
else
127127
{
128-
stack = (void *)(ptd->attr.stack_base);
128+
stack = (void *)(ptd->attr.stackaddr);
129129
}
130130

131131
if (stack == RT_NULL)
@@ -139,7 +139,7 @@ int pthread_create(pthread_t *tid,
139139
ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
140140
if (ptd->tid == RT_NULL)
141141
{
142-
if (ptd->attr.stack_base == 0)
142+
if (ptd->attr.stackaddr == 0)
143143
rt_free(stack);
144144
rt_free(ptd);
145145

@@ -151,7 +151,7 @@ int pthread_create(pthread_t *tid,
151151
ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
152152
if (ptd->joinable_sem == RT_NULL)
153153
{
154-
if (ptd->attr.stack_base != 0)
154+
if (ptd->attr.stackaddr != 0)
155155
rt_free(stack);
156156
rt_free(ptd);
157157

@@ -169,10 +169,10 @@ int pthread_create(pthread_t *tid,
169169

170170
/* initial this pthread to system */
171171
if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
172-
stack, ptd->attr.stack_size,
173-
ptd->attr.priority, 5) != RT_EOK)
172+
stack, ptd->attr.stacksize,
173+
ptd->attr.schedparam.sched_priority, 5) != RT_EOK)
174174
{
175-
if (ptd->attr.stack_base == 0)
175+
if (ptd->attr.stackaddr == 0)
176176
rt_free(stack);
177177
if (ptd->joinable_sem != RT_NULL)
178178
rt_sem_delete(ptd->joinable_sem);
@@ -195,7 +195,7 @@ int pthread_create(pthread_t *tid,
195195

196196
/* start thread failed */
197197
rt_thread_detach(ptd->tid);
198-
if (ptd->attr.stack_base == 0)
198+
if (ptd->attr.stackaddr == 0)
199199
rt_free(stack);
200200
if (ptd->joinable_sem != RT_NULL)
201201
rt_sem_delete(ptd->joinable_sem);

components/libc/pthreads/pthread.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,21 @@ enum
7676
#define PTHREAD_SCOPE_PROCESS 0
7777
#define PTHREAD_SCOPE_SYSTEM 1
7878

79+
struct sched_param
80+
{
81+
int sched_priority;
82+
};
83+
7984
struct pthread_attr
8085
{
81-
void* stack_base;
82-
rt_uint32_t stack_size; /* stack size of thread */
86+
void* stackaddr; /* stack address of thread */
87+
int stacksize; /* stack size of thread */
88+
89+
int inheritsched; /* Inherit parent prio/policy */
90+
int schedpolicy; /* scheduler policy */
91+
struct sched_param schedparam; /* sched parameter */
8392

84-
rt_uint8_t priority; /* priority of thread */
85-
rt_uint8_t detachstate; /* detach state */
86-
rt_uint8_t policy; /* scheduler policy */
87-
rt_uint8_t inheritsched; /* Inherit parent prio/policy */
93+
int detachstate; /* detach state */
8894
};
8995
typedef struct pthread_attr pthread_attr_t;
9096

@@ -131,11 +137,6 @@ struct pthread_barrier
131137
};
132138
typedef struct pthread_barrier pthread_barrier_t;
133139

134-
struct sched_param
135-
{
136-
int sched_priority;
137-
};
138-
139140
/* pthread thread interface */
140141
int pthread_attr_destroy(pthread_attr_t *attr);
141142
int pthread_attr_init(pthread_attr_t *attr);

components/libc/pthreads/pthread_attr.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ const pthread_attr_t pthread_default_attr =
2020
{
2121
0, /* stack base */
2222
DEFAULT_STACK_SIZE, /* stack size */
23-
DEFAULT_PRIORITY, /* priority */
24-
PTHREAD_CREATE_JOINABLE, /* detach state */
23+
24+
PTHREAD_INHERIT_SCHED, /* Inherit parent prio/policy */
2525
SCHED_FIFO, /* scheduler policy */
26-
PTHREAD_INHERIT_SCHED /* Inherit parent prio/policy */
26+
{
27+
DEFAULT_PRIORITY, /* scheduler priority */
28+
},
29+
PTHREAD_CREATE_JOINABLE, /* detach state */
2730
};
2831

2932
int pthread_attr_init(pthread_attr_t *attr)
@@ -73,7 +76,7 @@ int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
7376
{
7477
RT_ASSERT(attr != RT_NULL);
7578

76-
attr->policy = policy;
79+
attr->schedpolicy = policy;
7780

7881
return 0;
7982
}
@@ -83,7 +86,7 @@ int pthread_attr_getschedpolicy(pthread_attr_t const *attr, int *policy)
8386
{
8487
RT_ASSERT(attr != RT_NULL);
8588

86-
*policy = (int)attr->policy;
89+
*policy = (int)attr->schedpolicy;
8790

8891
return 0;
8992
}
@@ -95,7 +98,7 @@ int pthread_attr_setschedparam(pthread_attr_t *attr,
9598
RT_ASSERT(attr != RT_NULL);
9699
RT_ASSERT(param != RT_NULL);
97100

98-
attr->priority = param->sched_priority;
101+
attr->schedparam.sched_priority = param->sched_priority;
99102

100103
return 0;
101104
}
@@ -107,7 +110,7 @@ int pthread_attr_getschedparam(pthread_attr_t const *attr,
107110
RT_ASSERT(attr != RT_NULL);
108111
RT_ASSERT(param != RT_NULL);
109112

110-
param->sched_priority = attr->priority;
113+
param->sched_priority = attr->schedparam.sched_priority;
111114

112115
return 0;
113116
}
@@ -117,7 +120,7 @@ int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stack_size)
117120
{
118121
RT_ASSERT(attr != RT_NULL);
119122

120-
attr->stack_size = stack_size;
123+
attr->stacksize = stack_size;
121124

122125
return 0;
123126
}
@@ -127,7 +130,7 @@ int pthread_attr_getstacksize(pthread_attr_t const *attr, size_t *stack_size)
127130
{
128131
RT_ASSERT(attr != RT_NULL);
129132

130-
*stack_size = attr->stack_size;
133+
*stack_size = attr->stacksize;
131134

132135
return 0;
133136
}
@@ -155,8 +158,8 @@ int pthread_attr_setstack(pthread_attr_t *attr,
155158
{
156159
RT_ASSERT(attr != RT_NULL);
157160

158-
attr->stack_base = stack_base;
159-
attr->stack_size = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
161+
attr->stackaddr = stack_base;
162+
attr->stacksize = RT_ALIGN_DOWN(stack_size, RT_ALIGN_SIZE);
160163

161164
return 0;
162165
}
@@ -168,8 +171,8 @@ int pthread_attr_getstack(pthread_attr_t const *attr,
168171
{
169172
RT_ASSERT(attr != RT_NULL);
170173

171-
*stack_base = attr->stack_base;
172-
*stack_size = attr->stack_size;
174+
*stack_base = attr->stackaddr;
175+
*stack_size = attr->stacksize;
173176

174177
return 0;
175178
}

0 commit comments

Comments
 (0)