Skip to content

Commit d25d73e

Browse files
committed
[feat](pthread): Add pthread_mutex_timedlock
[Descriptions]: 1. Support POSIX pthread extension function pthread_mutex_timedlock 2. Format the files
1 parent c3125df commit d25d73e

File tree

3 files changed

+178
-114
lines changed

3 files changed

+178
-114
lines changed

components/libc/posix/pthreads/pthread.c

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
RT_DEFINE_HW_SPINLOCK(pth_lock);
2323
_pthread_data_t *pth_table[PTHREAD_NUM_MAX] = {NULL};
24-
static int concurrency_level;
24+
static int concurrency_level;
2525

2626
_pthread_data_t *_pthread_get_data(pthread_t thread)
2727
{
@@ -43,7 +43,7 @@ pthread_t _pthread_data_get_pth(_pthread_data_t *ptd)
4343
int index;
4444

4545
rt_hw_spin_lock(&pth_lock);
46-
for (index = 0; index < PTHREAD_NUM_MAX; index ++)
46+
for (index = 0; index < PTHREAD_NUM_MAX; index++)
4747
{
4848
if (pth_table[index] == ptd) break;
4949
}
@@ -54,20 +54,20 @@ pthread_t _pthread_data_get_pth(_pthread_data_t *ptd)
5454

5555
pthread_t _pthread_data_create(void)
5656
{
57-
int index;
57+
int index;
5858
_pthread_data_t *ptd = NULL;
5959

60-
ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
60+
ptd = (_pthread_data_t *)rt_malloc(sizeof(_pthread_data_t));
6161
if (!ptd) return PTHREAD_NUM_MAX;
6262

6363
memset(ptd, 0x0, sizeof(_pthread_data_t));
64-
ptd->canceled = 0;
64+
ptd->canceled = 0;
6565
ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
66-
ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
67-
ptd->magic = PTHREAD_MAGIC;
66+
ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
67+
ptd->magic = PTHREAD_MAGIC;
6868

6969
rt_hw_spin_lock(&pth_lock);
70-
for (index = 0; index < PTHREAD_NUM_MAX; index ++)
70+
for (index = 0; index < PTHREAD_NUM_MAX; index++)
7171
{
7272
if (pth_table[index] == NULL)
7373
{
@@ -90,7 +90,7 @@ pthread_t _pthread_data_create(void)
9090
static inline void _destroy_item(int index, _pthread_data_t *ptd)
9191
{
9292
extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
93-
void *data;
93+
void *data;
9494

9595
if (_thread_keys[index].is_used)
9696
{
@@ -103,7 +103,7 @@ static inline void _destroy_item(int index, _pthread_data_t *ptd)
103103
}
104104

105105
#ifdef RT_USING_CPLUSPLUS11
106-
#define NOT_USE_CXX_TLS -1
106+
#define NOT_USE_CXX_TLS -1
107107
#endif
108108

109109
void _pthread_data_destroy(_pthread_data_t *ptd)
@@ -123,16 +123,16 @@ void _pthread_data_destroy(_pthread_data_t *ptd)
123123
* destructors of C++ object must be called safely.
124124
*/
125125
extern pthread_key_t emutls_get_pthread_key(void);
126-
pthread_key_t emutls_pthread_key = emutls_get_pthread_key();
126+
pthread_key_t emutls_pthread_key = emutls_get_pthread_key();
127127

128128
if (emutls_pthread_key != NOT_USE_CXX_TLS)
129129
{
130130
/* If execution reaches here, C++ 'thread_local' may be used.
131131
* Destructors of c++ class object must be called before emutls_key_destructor.
132132
*/
133133
int start = ((emutls_pthread_key - 1 + PTHREAD_KEY_MAX) % PTHREAD_KEY_MAX);
134-
int i = 0;
135-
for (index = start; i < PTHREAD_KEY_MAX; index = (index - 1 + PTHREAD_KEY_MAX) % PTHREAD_KEY_MAX, i ++)
134+
int i = 0;
135+
for (index = start; i < PTHREAD_KEY_MAX; index = (index - 1 + PTHREAD_KEY_MAX) % PTHREAD_KEY_MAX, i++)
136136
{
137137
_destroy_item(index, ptd);
138138
}
@@ -143,7 +143,7 @@ void _pthread_data_destroy(_pthread_data_t *ptd)
143143
/* If only C TLS is used, that is, POSIX TLS or __Thread_local,
144144
* just iterate the _thread_keys from index 0.
145145
*/
146-
for (index = 0; index < PTHREAD_KEY_MAX; index ++)
146+
for (index = 0; index < PTHREAD_KEY_MAX; index++)
147147
{
148148
_destroy_item(index, ptd);
149149
}
@@ -153,7 +153,7 @@ void _pthread_data_destroy(_pthread_data_t *ptd)
153153
ptd->tls = RT_NULL;
154154
}
155155

156-
pth = _pthread_data_get_pth(ptd);
156+
pth = _pthread_data_get_pth(ptd);
157157
/* remove from pthread table */
158158
rt_hw_spin_lock(&pth_lock);
159159
pth_table[pth] = NULL;
@@ -191,7 +191,7 @@ static void _pthread_cleanup(rt_thread_t tid)
191191

192192
static void pthread_entry_stub(void *parameter)
193193
{
194-
void *value;
194+
void *value;
195195
_pthread_data_t *ptd;
196196

197197
ptd = (_pthread_data_t *)parameter;
@@ -253,12 +253,12 @@ int pthread_create(pthread_t *pid,
253253
const pthread_attr_t *attr,
254254
void *(*start)(void *), void *parameter)
255255
{
256-
int ret = 0;
257-
void *stack;
258-
char name[RT_NAME_MAX];
256+
int ret = 0;
257+
void *stack;
258+
char name[RT_NAME_MAX];
259259
static rt_uint16_t pthread_number = 0;
260260

261-
pthread_t pth_id;
261+
pthread_t pth_id;
262262
_pthread_data_t *ptd;
263263

264264
/* pid shall be provided */
@@ -292,10 +292,10 @@ int pthread_create(pthread_t *pid,
292292
goto __exit;
293293
}
294294

295-
rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
295+
rt_snprintf(name, sizeof(name), "pth%02d", pthread_number++);
296296

297297
/* pthread is a static thread object */
298-
ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
298+
ptd->tid = (rt_thread_t)rt_malloc(sizeof(struct rt_thread));
299299
if (ptd->tid == RT_NULL)
300300
{
301301
ret = ENOMEM;
@@ -318,7 +318,7 @@ int pthread_create(pthread_t *pid,
318318
}
319319

320320
/* set parameter */
321-
ptd->thread_entry = start;
321+
ptd->thread_entry = start;
322322
ptd->thread_parameter = parameter;
323323

324324
/* stack */
@@ -340,7 +340,8 @@ int pthread_create(pthread_t *pid,
340340
/* initial this pthread to system */
341341
if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
342342
stack, ptd->attr.stacksize,
343-
ptd->attr.schedparam.sched_priority, 20) != RT_EOK)
343+
ptd->attr.schedparam.sched_priority, 20)
344+
!= RT_EOK)
344345
{
345346
ret = EINVAL;
346347
goto __exit;
@@ -350,7 +351,7 @@ int pthread_create(pthread_t *pid,
350351
*pid = pth_id;
351352

352353
/* set pthread cleanup function and ptd data */
353-
ptd->tid->cleanup = _pthread_cleanup;
354+
ptd->tid->cleanup = _pthread_cleanup;
354355
ptd->tid->pthread_data = (void *)ptd;
355356

356357
/* start thread */
@@ -394,7 +395,7 @@ RTM_EXPORT(pthread_create);
394395
*/
395396
int pthread_detach(pthread_t thread)
396397
{
397-
int ret = 0;
398+
int ret = 0;
398399
_pthread_data_t *ptd = _pthread_get_data(thread);
399400
if (ptd == RT_NULL)
400401
{
@@ -466,7 +467,7 @@ RTM_EXPORT(pthread_detach);
466467
int pthread_join(pthread_t thread, void **value_ptr)
467468
{
468469
_pthread_data_t *ptd;
469-
rt_err_t result;
470+
rt_err_t result;
470471

471472
ptd = _pthread_get_data(thread);
472473

@@ -524,9 +525,9 @@ RTM_EXPORT(pthread_join);
524525
*
525526
* @see pthread_create, pthread_equal, pthread_join
526527
*/
527-
pthread_t pthread_self (void)
528+
pthread_t pthread_self(void)
528529
{
529-
rt_thread_t tid;
530+
rt_thread_t tid;
530531
_pthread_data_t *ptd;
531532

532533
tid = rt_thread_self();
@@ -568,7 +569,7 @@ RTM_EXPORT(pthread_self);
568569
*/
569570
int pthread_getcpuclockid(pthread_t thread, clockid_t *clock_id)
570571
{
571-
if(_pthread_get_data(thread) == NULL)
572+
if (_pthread_get_data(thread) == NULL)
572573
{
573574
return EINVAL;
574575
}
@@ -777,10 +778,10 @@ RTM_EXPORT(pthread_setschedparam);
777778
*/
778779
int pthread_setschedprio(pthread_t thread, int prio)
779780
{
780-
_pthread_data_t *ptd;
781+
_pthread_data_t *ptd;
781782
struct sched_param param;
782783

783-
ptd = _pthread_get_data(thread);
784+
ptd = _pthread_get_data(thread);
784785
param.sched_priority = prio;
785786
pthread_attr_setschedparam(&ptd->attr, &param);
786787

@@ -808,9 +809,9 @@ RTM_EXPORT(pthread_setschedprio);
808809
*/
809810
void pthread_exit(void *value)
810811
{
811-
_pthread_data_t *ptd;
812+
_pthread_data_t *ptd;
812813
_pthread_cleanup_t *cleanup;
813-
rt_thread_t tid;
814+
rt_thread_t tid;
814815

815816
if (rt_thread_self() == RT_NULL)
816817
{
@@ -833,7 +834,7 @@ void pthread_exit(void *value)
833834
*/
834835
while (ptd->cleanup != RT_NULL)
835836
{
836-
cleanup = ptd->cleanup;
837+
cleanup = ptd->cleanup;
837838
ptd->cleanup = cleanup->next;
838839

839840
cleanup->cleanup_func(cleanup->parameter);
@@ -956,7 +957,7 @@ int pthread_kill(pthread_t thread, int sig)
956957
{
957958
#ifdef RT_USING_SIGNALS
958959
_pthread_data_t *ptd;
959-
int ret;
960+
int ret;
960961

961962
ptd = _pthread_get_data(thread);
962963
if (ptd)
@@ -1036,7 +1037,7 @@ int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
10361037
*/
10371038
void pthread_cleanup_pop(int execute)
10381039
{
1039-
_pthread_data_t *ptd;
1040+
_pthread_data_t *ptd;
10401041
_pthread_cleanup_t *cleanup;
10411042

10421043
if (rt_thread_self() == NULL) return;
@@ -1092,7 +1093,7 @@ RTM_EXPORT(pthread_cleanup_pop);
10921093
*/
10931094
void pthread_cleanup_push(void (*routine)(void *), void *arg)
10941095
{
1095-
_pthread_data_t *ptd;
1096+
_pthread_data_t *ptd;
10961097
_pthread_cleanup_t *cleanup;
10971098

10981099
if (rt_thread_self() == NULL) return;
@@ -1105,11 +1106,11 @@ void pthread_cleanup_push(void (*routine)(void *), void *arg)
11051106
if (cleanup != RT_NULL)
11061107
{
11071108
cleanup->cleanup_func = routine;
1108-
cleanup->parameter = arg;
1109+
cleanup->parameter = arg;
11091110

11101111
rt_enter_critical();
11111112
cleanup->next = ptd->cleanup;
1112-
ptd->cleanup = cleanup;
1113+
ptd->cleanup = cleanup;
11131114
rt_exit_critical();
11141115
}
11151116
}
@@ -1261,7 +1262,7 @@ RTM_EXPORT(pthread_setcanceltype);
12611262
*/
12621263
void pthread_testcancel(void)
12631264
{
1264-
int cancel = 0;
1265+
int cancel = 0;
12651266
_pthread_data_t *ptd;
12661267

12671268
if (rt_thread_self() == NULL) return;
@@ -1303,9 +1304,9 @@ RTM_EXPORT(pthread_testcancel);
13031304
*/
13041305
int pthread_cancel(pthread_t thread)
13051306
{
1306-
_pthread_data_t *ptd;
1307+
_pthread_data_t *ptd;
13071308
_pthread_cleanup_t *cleanup;
1308-
rt_thread_t tid;
1309+
rt_thread_t tid;
13091310

13101311
/* get posix thread data */
13111312
ptd = _pthread_get_data(thread);
@@ -1331,7 +1332,7 @@ int pthread_cancel(pthread_t thread)
13311332
*/
13321333
while (ptd->cleanup != RT_NULL)
13331334
{
1334-
cleanup = ptd->cleanup;
1335+
cleanup = ptd->cleanup;
13351336
ptd->cleanup = cleanup->next;
13361337

13371338
cleanup->cleanup_func(cleanup->parameter);

0 commit comments

Comments
 (0)