⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ar_mutex.c

📁 Keil开发环境下ARM7内核单片机的ARTX RTOS内核源代码
💻 C
字号:
/*----------------------------------------------------------------------------
 *      A R T X  -  K e r n e l
 *----------------------------------------------------------------------------
 *      Name:    AR_MUTEX.C
 *      Purpose: Implements mutex synchronization objects
 *      Rev.:    V2.00 / 19-oct-2005
 *----------------------------------------------------------------------------
 *      This code is part of the ARTX-ARM kernel package of Keil Software.
 *      Copyright (c) 2004-2005 Keil Software. All rights reserved. 
 *---------------------------------------------------------------------------*/

#include "Kernel\ARTX_Config.h"
#include "Kernel\AR_List.h"
#include "Kernel\AR_Task.h"

/* Ptr to TCB of running task */
extern P_TCB  os_runtask;

/* Mapping of API-type to internal type */
#define p_MCB   ((struct OS_MUCB *)mutex)


/*----------------------------------------------------------------------------
 *      Functions
 *---------------------------------------------------------------------------*/


/*--------------------------- os_mut_init -----------------------------------*/

void os_mut_init (OS_ID mutex) {
   /* Initialize a mutex object */
   tsk_lock();
   p_MCB->cb_type = MUCB;
   p_MCB->level   = 0;
   p_MCB->p_lnk   = NULL;
   p_MCB->owner   = NULL;
   tsk_unlock();
} /* end of os_mut_init */


/*--------------------------- os_mut_release --------------------------------*/

OS_RESULT os_mut_release (OS_ID mutex) {
   /* Release a mutex object */
   P_TCB p_TCB;

   tsk_lock();
   if (p_MCB->level == 0 || p_MCB->owner != os_runtask) {
      /* Unbalanced release mutex or task is not the owner */
      tsk_unlock ();
      return (OS_R_NOK);
   }
   p_MCB->level--;
   if (p_MCB->level == 0 && p_MCB->p_lnk != NULL) {
      /* A task is waiting for mutex */
      p_TCB = os_get_first ((P_XCB)p_MCB);
      p_TCB->ret_val = OS_R_MUT;
      os_rmv_dly (p_TCB);
      os_dispatch (p_TCB);
   }
   tsk_unlock();
   return (OS_R_OK);
} /* end of os_mut_release */


/*--------------------------- os_mut_wait -----------------------------------*/

OS_RESULT os_mut_wait (OS_ID mutex, U16 timeout) {
   /* Wait for a mutex, continue when mutex is free. */

   tsk_lock();
   if (p_MCB->level == 0) {
      p_MCB->owner = os_runtask;
      goto incl;
   }
   if (p_MCB->owner == os_runtask) {
      /* OK, running task is the owner of this mutex. */
incl: p_MCB->level++;
      tsk_unlock();
      return (OS_R_OK);
   }
   /* Mutex owned by another task, wait until released. */
   if (timeout == 0) {
      goto rtmo;
   }
   if (p_MCB->p_lnk != NULL) {
      os_put_prio ((P_XCB)p_MCB, os_runtask);
      }
   else {
      p_MCB->p_lnk = os_runtask;
      os_runtask->p_lnk  = NULL;
      os_runtask->p_rlnk = (P_TCB)p_MCB;
      };
   if (os_block(timeout, WAIT_MUT) == OS_R_TMO) {
rtmo: tsk_unlock();
      return (OS_R_TMO);
      }
   else {
      /* A mutex has been released, running task owns it now. */
      p_MCB->level++;
      p_MCB->owner = os_runtask;
      tsk_unlock();
      return (OS_R_MUT);
      }
} /* end of os_mut_wait */


/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -