Skip to content

Commit bb506e8

Browse files
committed
[pthreads] Add spinlock declare and fix code issue.
1 parent 5d36fa7 commit bb506e8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

components/libc/pthreads/pthread.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ _pthread_data_t *pth_table[PTHREAD_NUM_MAX] = {NULL};
2020

2121
_pthread_data_t *_pthread_get_data(pthread_t thread)
2222
{
23+
RT_DECLARE_SPINLOCK(pth_lock);
2324
_pthread_data_t *ptd;
2425

2526
if (thread >= PTHREAD_NUM_MAX) return NULL;
@@ -36,6 +37,7 @@ _pthread_data_t *_pthread_get_data(pthread_t thread)
3637
pthread_t _pthread_data_get_pth(_pthread_data_t *ptd)
3738
{
3839
int index;
40+
RT_DECLARE_SPINLOCK(pth_lock);
3941

4042
rt_hw_spin_lock(&pth_lock);
4143
for (index = 0; index < PTHREAD_NUM_MAX; index ++)
@@ -51,11 +53,12 @@ pthread_t _pthread_data_create(void)
5153
{
5254
int index;
5355
_pthread_data_t *ptd = NULL;
56+
RT_DECLARE_SPINLOCK(pth_lock);
5457

5558
ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
5659
if (!ptd) return PTHREAD_NUM_MAX;
5760

58-
memset(ptd, 0x0, sizeof(sizeof(_pthread_data_t)));
61+
memset(ptd, 0x0, sizeof(_pthread_data_t));
5962
ptd->canceled = 0;
6063
ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
6164
ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
@@ -67,6 +70,7 @@ pthread_t _pthread_data_create(void)
6770
if (pth_table[index] == NULL)
6871
{
6972
pth_table[index] = ptd;
73+
break;
7074
}
7175
}
7276
rt_hw_spin_unlock(&pth_lock);
@@ -76,6 +80,8 @@ pthread_t _pthread_data_create(void)
7680

7781
void _pthread_data_destroy(pthread_t pth)
7882
{
83+
RT_DECLARE_SPINLOCK(pth_lock);
84+
7985
_pthread_data_t *ptd = _pthread_get_data(pth);
8086
if (ptd)
8187
{

include/rthw.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ extern rt_hw_spinlock_t _rt_critical_lock;
157157
(rt_hw_spinlock_t) __RT_HW_SPIN_LOCK_INITIALIZER(lockname)
158158

159159
#define RT_DEFINE_SPINLOCK(x) rt_hw_spinlock_t x = __RT_HW_SPIN_LOCK_UNLOCKED(x)
160+
#define RT_DECLARE_SPINLOCK(x)
160161

161162
/**
162163
* ipi function
@@ -172,6 +173,13 @@ void rt_hw_secondary_cpu_up(void);
172173
* secondary cpu idle function
173174
*/
174175
void rt_hw_secondary_cpu_idle_exec(void);
176+
#else
177+
178+
#define RT_DEFINE_SPINLOCK(x)
179+
#define RT_DECLARE_SPINLOCK(x) rt_ubase_t x
180+
181+
#define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable()
182+
#define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock))
175183

176184
#endif
177185

0 commit comments

Comments
 (0)