home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / AETSK101 / MAILBOX.CC < prev    next >
C/C++ Source or Header  |  1991-11-17  |  2KB  |  62 lines

  1. /**********************************************************************
  2.  *  
  3.  *  NAME:           mailbox.cpp
  4.  *  
  5.  *  DESCRIPTION:    class MBox (mailbox) implementation
  6.  *  
  7.  *  copyright (c) 1991 J. Alan Eldridge
  8.  * 
  9.  *  M O D I F I C A T I O N   H I S T O R Y
  10.  *
  11.  *  when        who                 what
  12.  *  -------------------------------------------------------------------
  13.  *  05/09/91    J. Alan Eldridge    broke out of task.cpp
  14.  *  
  15.  *  11/14/91    jae                 now class MBoxBase member functions
  16.  *                                  are here, and the constructors for
  17.  *                                  MBox & MBoxPri are inline in task.h
  18.  *
  19.  *********************************************************************/
  20.  
  21. #include    "aedef.h"
  22. #include    "task.h"
  23.  
  24. //------------------------------------------------------------
  25. //  ********** CLASS MBOX MEMBER FUNCTIONS **********
  26. //------------------------------------------------------------
  27.  
  28. //------------------------------------------------------------
  29. //  MBoxBase::send()    --  wait for box to be free, then put msg
  30. //  NOTE: sender doesn't return until message is picked up
  31. //------------------------------------------------------------
  32.  
  33. int
  34. MBoxBase::send(void *msg, int n, clock_t msec)
  35. {
  36.     if (!s_send->wait(msec))
  37.         return 0;
  38.     m_len = n;
  39.     m_msg = msg;
  40.     s_recv->signal(0);
  41.     return s_pickup.wait();
  42. }
  43.  
  44. //------------------------------------------------------------
  45. //  MBoxBase::recv()    --  wait for a message, then retrieve it
  46. //  and wake up the sender... return -1 if timed out, otherwise
  47. //  return length of data
  48. //------------------------------------------------------------
  49.  
  50. int
  51. MBoxBase::recv(void *msg, clock_t msec)
  52. {
  53.     int len;
  54.     
  55.     if (!s_recv->wait(msec))
  56.         return 0;
  57.     memcpy(msg, m_msg, len = m_len);
  58.     s_send->signal(0);
  59.     s_pickup.signal();
  60.     return len;
  61. }
  62.