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

📄 ar_list.c

📁 Keil开发环境下ARM7内核单片机的ARTX RTOS内核源代码
💻 C
字号:
/*----------------------------------------------------------------------------
 *      A R T X  -  K e r n e l
 *----------------------------------------------------------------------------
 *      Name:    AR_LIST.C
 *      Purpose: Functions for the management of different lists
 *      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_Lib.h"

/*----------------------------------------------------------------------------
 *      Global Variables
 *---------------------------------------------------------------------------*/

/* List head of chained ready tasks */
extern struct OS_XCB  os_rdy;
/* List head of chained delay tasks */
extern struct OS_XCB  os_dly;

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

/* Queue for post service requests */
extern OS_ID os_psq[16];
extern void  *os_msg[16];
extern U16   os_psq_first;
extern U16   os_psq_last;

/* AR_Resource.c */
extern U16   os_time;


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


/*--------------------------- os_put_prio -----------------------------------*/

void os_put_prio (P_XCB p_CB, P_TCB p_task) {
   /* Put task identified with "p_task" into list ordered by priority.       */
   /* "p_CB" points to head of list; list has always an element at end with  */
   /* a priority less than "p_task->prio".                                   */
   P_TCB p_CB2;
   U32 prio;
   BOOL sem_mbx = __FALSE;

   if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
      sem_mbx = __TRUE;
      };
   prio = p_task->prio;
   p_CB2 = p_CB->p_lnk;
   /* Search for an entry in the list */
   while (p_CB2 != NULL && prio <= p_CB2->prio) {
      p_CB = (P_XCB)p_CB2;
      p_CB2 = p_CB2->p_lnk;
      };
   /* Entry found, insert the task into the list */
   p_task->p_lnk = p_CB2;
   p_CB->p_lnk = p_task;
   if (sem_mbx) {
      if (p_CB2 != NULL) {
         p_CB2->p_rlnk = p_task;
         };
      p_task->p_rlnk = (P_TCB)p_CB;
      }
   else {
      p_task->p_rlnk = NULL;
      }
} /* end of os_put_prio */


/*--------------------------- os_get_first ----------------------------------*/

P_TCB os_get_first (P_XCB p_CB) {
   /* Get task at head of list: it is the task with highest priority. */
   /* "p_CB" points to head of list. */
   P_TCB p_first;

   p_first = p_CB->p_lnk;
   p_CB->p_lnk = p_first->p_lnk;
   if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
      if (p_first->p_lnk != NULL) {
         p_first->p_lnk->p_rlnk = (P_TCB)p_CB;
         p_first->p_lnk = NULL;
         };
      p_first->p_rlnk = NULL;
      }
   else {
      p_first->p_lnk = NULL;
      }
   return (p_first);
} /* end of os_get_first */


/*--------------------------- os_put_rdy_first ------------------------------*/

void os_put_rdy_first (P_TCB p_task) {
   /* Put task identified with "p_task" at the head of the ready list. The   */
   /* task must have at least a priority equal to highest priority in list.  */
   p_task->p_lnk = os_rdy.p_lnk;
   p_task->p_rlnk = NULL;
   os_rdy.p_lnk = p_task;
} /* end of os_put_rdy_first */


/*--------------------------- os_get_same_rdy_prio --------------------------*/

P_TCB os_get_same_rdy_prio (void) {
   /* Remove a task of same priority from ready list if any exists. Other-   */
   /* wise return NULL.                                                      */
   P_TCB p_first;

   p_first = os_rdy.p_lnk;
   if (p_first->prio == os_runtask->prio) {
      os_rdy.p_lnk = os_rdy.p_lnk->p_lnk;
      return (p_first);
      }
   else {
      return (NULL);
      }
} /* end of os_get_same_rdy_prio */


/* Separate functions for delay list. */

/*--------------------------- os_put_dly ------------------------------------*/

void os_put_dly (P_TCB p_task, U16 delay) {
   /* Put a task identified with "p_task" into chained delay wait list using */
   /* a delay value of "delay".                                              */
   P_TCB p;
   U32 delta,idelay = delay;

   p = (P_TCB)&os_dly;
   if (p->p_dlnk == NULL) {
      /* Delay list empty */
      delta = 0;
      goto last;
   }
   delta = os_dly.delta_time;
   while (delta < idelay) {
      if (p->p_dlnk == NULL) {
         /* End of list found */
last:    p_task->p_dlnk = NULL;
         p->p_dlnk = p_task;
         p_task->p_blnk = p;
         p->delta_time = (U16)(idelay - delta);
         p_task->delta_time = 0;
         return;
         };
      p = p->p_dlnk;
      delta += p->delta_time;
      };
   /* Right place found */
   p_task->p_dlnk = p->p_dlnk;
   p->p_dlnk = p_task;
   p_task->p_blnk = p;
   if (p_task->p_dlnk != NULL) {
      p_task->p_dlnk->p_blnk = p_task;
   }
   p_task->delta_time = (U16)(delta - idelay);
   p->delta_time -= p_task->delta_time;
} /* end of os_put_dly */


/*--------------------------- os_dec_dly ------------------------------------*/

void os_dec_dly (void) {
   /* Decrement delta time of list head: remove tasks having a value of zero.*/
   P_TCB p_rdy;

   if (os_dly.p_dlnk == NULL) {
      return;
      }
   os_dly.delta_time--;
   while ((os_dly.delta_time == 0) && (os_dly.p_dlnk != NULL)) {
      p_rdy = os_dly.p_dlnk;
      if (p_rdy->p_rlnk != NULL) {
         /* Task is really enqueued, remove task from semaphore/mailbox */
         /* timeout waiting list. */
         p_rdy->p_rlnk->p_lnk = p_rdy->p_lnk;
         if (p_rdy->p_lnk != NULL) {
            p_rdy->p_lnk->p_rlnk = p_rdy->p_rlnk;
            p_rdy->p_lnk = NULL;
            }
         p_rdy->p_rlnk = NULL;
      }
      os_put_prio (&os_rdy, p_rdy);
      os_dly.delta_time = p_rdy->delta_time;
      if (p_rdy->state == WAIT_ITV) {
         /* Calculate the next time for interval wait. */
         p_rdy->delta_time = p_rdy->interval_time + os_time;
      }
      p_rdy->state = READY;
      p_rdy->ret_val = OS_R_TMO;
      os_dly.p_dlnk = p_rdy->p_dlnk;
      if (p_rdy->p_dlnk != NULL) {
         p_rdy->p_dlnk->p_blnk =  (P_TCB)&os_dly;
         p_rdy->p_dlnk = NULL;
      }
      p_rdy->p_blnk = NULL;
      };
} /* end of os_dec_dly */


/*--------------------------- os_rmv_list -----------------------------------*/

void os_rmv_list (P_TCB p_task) {
   /* Remove task identified with "p_task" from ready, semaphore or mailbox  */
   /* waiting list if enqueued.                                              */
   P_TCB p_b;

   if (p_task->p_rlnk != NULL) {
      /* A task is enqueued in semaphore / mailbox waiting list. */
      p_task->p_rlnk->p_lnk = p_task->p_lnk;
      if (p_task->p_lnk != NULL) {
         p_task->p_lnk->p_rlnk = p_task->p_rlnk;
         }
      return;
      }

   p_b = (P_TCB)&os_rdy;
   while (p_b != NULL) {
      /* Search the ready list for task "p_task" */
      if (p_b->p_lnk == p_task) {
         p_b->p_lnk = p_task->p_lnk;
         return;
         }
      p_b = p_b->p_lnk;
      }
} /* end of os_rmv_list */


/*--------------------------- os_rmv_dly ------------------------------------*/

void os_rmv_dly (P_TCB p_task) {
   /* Remove task identified with "p_task" from delay list if enqueued.      */
   P_TCB p_b;

   p_b = p_task->p_blnk;
   if (p_b != NULL) {
      /* Task is really enqueued */
      p_b->p_dlnk = p_task->p_dlnk;
      if (p_task->p_dlnk != NULL) {
         /* 'p_task' is in the middle of list */
         p_b->delta_time += p_task->delta_time;
         p_task->p_dlnk->p_blnk = p_b;
         p_task->p_dlnk = NULL;
         }
      else {
         /* 'p_task' is at the end of list */
         p_b->delta_time = 0;
         }
      p_task->p_blnk = NULL;
      }
} /* end of os_rmv_dly */


/*--------------------------- os_psq_enq ------------------------------------*/

void os_psq_enq (OS_ID entry, void *p_msg) {
   /* Insert post service request "entry" into ps-queue. */
   U32 idx;

   idx = os_psq_first;
   _inc16 (&os_psq_first);
   os_psq[idx] = entry;
   os_msg[idx] = p_msg;
} /* end of os_psq_enq */


/*--------------------------- os_psq_deq ------------------------------------*/

OS_ID os_psq_deq (void **msg) {
   /* Remove an entry from ps-queue. */
   P_TCB req;

   req = os_psq[os_psq_last];
   *msg = os_msg[os_psq_last];
   _inc16 (&os_psq_last);
   return (req);
} /* end of os_psq_deq */

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

⌨️ 快捷键说明

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