Skip to content

Commit f3f9aab

Browse files
committed
tmp
1 parent 6cad7e9 commit f3f9aab

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

include/nuttx/mutex.h

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <stdbool.h>
3030

3131
#include <nuttx/semaphore.h>
32+
#include <nuttx/spinlock.h>
3233

3334
/****************************************************************************
3435
* Pre-processor Definitions
@@ -43,11 +44,22 @@
4344
* Public Type Definitions
4445
****************************************************************************/
4546

47+
#ifdef _LIGHT_MUTEX_
48+
49+
static spinlock_t mutex_spinlock = SP_UNLOCKED; // trade off, it can be set per mutex
50+
struct mutex_s
51+
{
52+
dq_queue_t waitlist;
53+
FAR struct tcb_s *htcb;
54+
pid_t holder;
55+
}
56+
#else
4657
struct mutex_s
4758
{
4859
sem_t sem;
4960
pid_t holder;
5061
};
62+
#endif
5163

5264
typedef struct mutex_s mutex_t;
5365

libs/libc/misc/lib_mutex.c

+60
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ static bool nxmutex_is_reset(FAR mutex_t *mutex)
8282

8383
int nxmutex_init(FAR mutex_t *mutex)
8484
{
85+
#ifdef _LIGHT_MUTEX_
86+
dq_init(mutex->waitlist);
87+
mutex->htcb = NULL;
88+
mutex->holder = NXMUTEX_NO_HOLDER;
89+
90+
return OK;
91+
#else
8592
int ret = _SEM_INIT(&mutex->sem, 0, 1);
8693

8794
if (ret < 0)
@@ -96,6 +103,7 @@ int nxmutex_init(FAR mutex_t *mutex)
96103
_SEM_SETPROTOCOL(&mutex->sem, SEM_TYPE_MUTEX);
97104
#endif
98105
return ret;
106+
#endif
99107
}
100108

101109
/****************************************************************************
@@ -119,6 +127,14 @@ int nxmutex_init(FAR mutex_t *mutex)
119127

120128
int nxmutex_destroy(FAR mutex_t *mutex)
121129
{
130+
#ifdef _LIGHT_MUTEX_
131+
132+
dq_init(mutex->waitlist);
133+
mutex->htcb = NULL;
134+
mutex->holder = NXMUTEX_NO_HOLDER;
135+
136+
return OK;
137+
#else
122138
int ret = _SEM_DESTROY(&mutex->sem);
123139

124140
if (ret < 0)
@@ -128,6 +144,7 @@ int nxmutex_destroy(FAR mutex_t *mutex)
128144

129145
mutex->holder = NXMUTEX_NO_HOLDER;
130146
return ret;
147+
#endif
131148
}
132149

133150
/****************************************************************************
@@ -164,12 +181,17 @@ bool nxmutex_is_hold(FAR mutex_t *mutex)
164181

165182
bool nxmutex_is_locked(FAR mutex_t *mutex)
166183
{
184+
185+
#ifdef _LIGHT_MUTEX_
186+
return mutex->htcb != NULL;
187+
#else
167188
int cnt;
168189
int ret;
169190

170191
ret = _SEM_GETVALUE(&mutex->sem, &cnt);
171192

172193
return ret >= 0 && cnt < 1;
194+
#endif
173195
}
174196

175197
/****************************************************************************
@@ -194,6 +216,27 @@ bool nxmutex_is_locked(FAR mutex_t *mutex)
194216

195217
int nxmutex_lock(FAR mutex_t *mutex)
196218
{
219+
#ifdef _LIGHT_MUTEX_
220+
irqstate_t flag;
221+
222+
DEBUGASSERT(mutex != NULL && !up_interrupt_context());
223+
224+
flag = spinlock_irqsave(&mutex_spinlock);
225+
226+
if(mutex->holder == NULL)
227+
{
228+
229+
}
230+
else
231+
{
232+
233+
}
234+
235+
spinclock_irqrestore(NULL, mutex_spinlock);
236+
237+
return OK;
238+
239+
#else
197240
int ret;
198241

199242
DEBUGASSERT(!nxmutex_is_hold(mutex));
@@ -214,6 +257,7 @@ int nxmutex_lock(FAR mutex_t *mutex)
214257
}
215258

216259
return ret;
260+
#endif
217261
}
218262

219263
/****************************************************************************
@@ -329,6 +373,21 @@ int nxmutex_timedlock(FAR mutex_t *mutex, unsigned int timeout)
329373

330374
int nxmutex_unlock(FAR mutex_t *mutex)
331375
{
376+
#ifdef _LIGHT_MUTEX_
377+
int ret;
378+
379+
if (nxmutex_is_reset(mutex))
380+
{
381+
return OK;
382+
}
383+
384+
DEBUGASSERT(nxmutex_is_hold(mutex));
385+
mutex->htcb
386+
mutex->holder = NXMUTEX_NO_HOLDER;
387+
388+
389+
return OK;
390+
#else
332391
int ret;
333392

334393
if (nxmutex_is_reset(mutex))
@@ -348,6 +407,7 @@ int nxmutex_unlock(FAR mutex_t *mutex)
348407
}
349408

350409
return ret;
410+
#endif
351411
}
352412

353413
/****************************************************************************

0 commit comments

Comments
 (0)