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

📄 tskmsg.cpp

📁 这是一个关于C++编程开发的算法!
💻 CPP
字号:
/*
   CPPTask - A Multitasking Kernel For C++

   Version 1.0 08-12-91

   Ported by Rich Smith from:

   Public Domain Software written by
      Thomas Wagner
      Patschkauer Weg 31
      D-1000 Berlin 33
      West Germany

   TSKMSG.CPP - Message handling routines.

   Subroutines:
        mailbox::mailbox
        mailbox::~mailbox
        mailbox::send_mail
        mailbox::wait_mail
        mailbox::c_wait_mail
        mailbox::check_mailbox

*/

#include <stdio.h>

#include "task.hpp"
#include "tsklocal.hpp"


/*
   mailbox - initialises mailbox.
*/

mailbox::mailbox ()
{
   waiting = NULL;
   mail_first = mail_last = NULL;
}


/*
   ~mailbox - kills all processes waiting for mail
*/

mailbox::~mailbox ()
{
   CRITICAL;

   C_ENTER;
   tsk_kill_queue (&waiting);
   mail_first = mail_last = NULL;
   C_LEAVE;

}


/*
   wait_mail - Wait until mail arrives. If there is mail in the box on
               entry, the first mail block is assigned to the caller,
               and the task continues to run.
*/

farptr far mailbox::wait_mail (dword timeout)
{
   msgptr msg;
   CRITICAL;

   C_ENTER;
   if ((msg = mail_first) != NULL)
      {
      if ((mail_first = msg->next) == NULL)
         mail_last = NULL;
      C_LEAVE;
      return msg;
      }

   tsk_wait (&waiting, timeout);
   return tsk_current->get_retptr();
}

/*
   c_wait_mail - If there is mail in the box on entry, the first mail 
                 block is assigned to the caller, else an error is returned.
*/

farptr far mailbox::c_wait_mail (void)
{
   msgptr msg;
   CRITICAL;

   C_ENTER;
   if ((msg = mail_first) != NULL)
      if ((mail_first = msg->next) == NULL)
         mail_last = NULL;
   C_LEAVE;
   return msg;
}


/*
   send_mail - Send a mail block to a mailbox. If there are tasks waiting
               for mail, the first waiting task is assigned the block and
               is made eligible.
*/

void far mailbox::send_mail (farptr msg)
{
   tcbptr curr;
   CRITICAL;

   C_ENTER;
   if ((curr = waiting) == NULL)
      {
      if (mail_first == NULL)
         mail_first = (msgptr)msg;
      else
         mail_last->next = (msgptr)msg;
      ((msgptr)msg)->next = NULL;
      mail_last = (msgptr)msg;
      C_LEAVE;
      return;
      }
   waiting = curr->tsk_runable ();
   curr->set_retptr(msg);
   C_LEAVE;
}


/*
   check_mailbox - returns TRUE if there is mail in the box.
*/

int far mailbox::check_mailbox (void)
{
   return mail_first != NULL;
}


⌨️ 快捷键说明

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