📄 mutex.lst
字号:
C51 COMPILER V6.12 MUTEX 12/07/2004 17:58:46 PAGE 1
C51 COMPILER V6.12, COMPILATION OF MODULE MUTEX
OBJECT MODULE PLACED IN .\mutex.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ..\..\..\ipc\mutex.c INCDIR(d:\rs1.12b\) DEBUG OBJECTEXTEND PRINT(.\mutex.l
-st) OBJECT(.\mutex.obj)
stmt level source
1 /*
2 ===============================================================================
3 | Copyright (C) 2004 RuanHaiShen, All rights reserved.
4 | SUMMARY:
5 | Mutex implementation.
6 |
7 | DESCRIPTION:
8 | See http://www.01s.org for documentation, latest information, license
9 | and contact details.
10 | email:ruanhaishen@01s.org
11 =============================================================================*/
12 /*===========================================================================*/
13 #include "arch/arch.h"
14 #include "inc/queue.h"
15 #include "inc/kernel.h"
16 #include "inc/memory.h"
17 #include "inc/ipc.h"
18 #include "inc/kapi.h"
19
20
21 #if CFG_MUTEX_EN > 0
err_t mutex_init(mutex_t __p_* mut, u8 prio)
{
__ASSERT(mut != NULL);
#if CFG_ARG_CHK > 1
if (prio >= TASK_IDLE_PRIO) {
return EOUTRANGE;
}
#else
__ASSERT(prio < TASK_IDLE_PRIO);
#endif
CRITICAL_ENTER;
if (_tasks[prio].state != STATE_NONE) {
CRITICAL_EXIT;
return EEXIST;
}
_tasks[prio].state = STATE_ZOMBIE;
CRITICAL_EXIT;
mut->ceiling = prio;
mut->owner = NULL_PRIO;
__queue_init(mut->waits);
return EOK;
}
#if CFG_MUTEX_EN == 1
err_t mutex_wait(mutex_t __p_* mut)
{
register u8 current;
register u8 ceiling;
register u8 owner;
C51 COMPILER V6.12 MUTEX 12/07/2004 17:58:46 PAGE 2
__ASSERT(mut != NULL);
#if CFG_ARG_CHK > 0
if (_sched_lock > 0) {
return ELOCK;
}
#else
__ASSERT(_sched_lock == 0);
#endif
while (true) {
CRITICAL_ENTER;
current = _current_prio;
ceiling = mut->ceiling;
owner = mut->owner;
if (owner == NULL_PRIO) {
mut->owner = _tasks[current].prio.normal;
__nprio_lock(current);
CRITICAL_EXIT;
return EOK;
}
if (current < owner &&
!(_tasks[owner].state | STATE_RESERVED)) {
/*change owner-priority to ceiling-priority*/
__memcpy(&_tasks[ceiling], &_tasks[owner], sizeof(task_t));
_tasks[owner].delay = 0;
_tasks[owner].state = (STATE_ZOMBIE|STATE_RESERVED);
_tasks[owner].prio.current = ceiling;
if (_tasks[ceiling].pend_q == NULL) {
__ready_que_move(owner, ceiling);
} else {
__queue_remove_f(_tasks[ceiling].pend_q, owner);
__queue_add_f(_tasks[ceiling].pend_q, ceiling);
}
#if CFG_ARG_CHK > 0
/*prevent a task waiting the same mutex again.*/
} else if (owner == _tasks[current].prio.normal) {
CRITICAL_EXIT;
return EAGAIN;
#endif
}
__ipc_block(&mut->waits, 0);
CRITICAL_EXIT;
__schedule();
}
}
#if CFG_IPC_TIMEOUT_EN > 0
err_t mutex_timewait(mutex_t __p_* mut, tick_t ticks)
{
register u8 current;
register u8 ceiling;
register u8 owner;
__ASSERT(mut != NULL);
#if CFG_ARG_CHK > 0
if (_sched_lock > 0) {
return ELOCK;
C51 COMPILER V6.12 MUTEX 12/07/2004 17:58:46 PAGE 3
}
#else
__ASSERT(_sched_lock == 0);
#endif
while (true) {
CRITICAL_ENTER;
current = _current_prio;
ceiling = mut->ceiling;
owner = mut->owner;
if (owner == NULL_PRIO) {
mut->owner = _tasks[current].prio.normal;
__nprio_lock(current);
CRITICAL_EXIT;
return EOK;
}
if (current < owner &&
!(_tasks[owner].state | STATE_RESERVED)) {
/*change owner-priority to ceiling-priority*/
__memcpy(&_tasks[ceiling], &_tasks[owner], sizeof(task_t));
_tasks[owner].delay = 0;
_tasks[owner].state = (STATE_ZOMBIE|STATE_RESERVED);
_tasks[owner].prio.current = ceiling;
if (_tasks[ceiling].pend_q == NULL) {
__ready_que_move(owner, ceiling);
} else {
__queue_remove_f(_tasks[ceiling].pend_q, owner);
__queue_add_f(_tasks[ceiling].pend_q, ceiling);
}
#if CFG_ARG_CHK > 0
/*prevent a task waiting the same mutex again.*/
} else if (owner == _tasks[current].prio.normal) {
CRITICAL_EXIT;
return EAGAIN;
#endif
}
__ipc_block(&mut->waits, ticks);
CRITICAL_EXIT;
__schedule();
CRITICAL_ENTER;
current = _current_prio;
if (_tasks[current].state & STATE_BLOCKED) {
__ipc_timeout(&mut->waits);
CRITICAL_EXIT;
return ETIMEOUT;
}
__adjust_delay(current, ticks);
CRITICAL_EXIT;
}
}
#endif
err_t mutex_trywait(mutex_t __p_* mut)
{
register u8 current;
__ASSERT(mut != NULL);
#if CFG_ARG_CHK > 0
C51 COMPILER V6.12 MUTEX 12/07/2004 17:58:46 PAGE 4
if (_sched_lock > 0) {
return ELOCK;
}
#else
__ASSERT(_sched_lock == 0);
#endif
CRITICAL_ENTER;
current = _current_prio;
if (mut->owner == NULL_PRIO) {
mut->owner = _tasks[current].prio.normal;
__nprio_lock(current);
CRITICAL_EXIT;
return EOK;
}
CRITICAL_EXIT;
return ENAVAIL;
}
#else
err_t mutex_wait(mutex_t __p_* mut)
{
register u8 current;
register u8 ceiling;
__ASSERT(mut != NULL);
#if CFG_ARG_CHK > 0
if (_sched_lock > 0) {
return ELOCK;
}
#else
__ASSERT(_sched_lock == 0);
#endif
while (true) {
CRITICAL_ENTER;
current = _current_prio;
ceiling = mut->ceiling;
if (mut->owner == NULL_PRIO) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -