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

  1. /**********************************************************************
  2.  *  
  3.  *  NAME:           semas.cpp
  4.  *  
  5.  *  DESCRIPTION:    semaphores for Task++ v. 3.00
  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.  *  11/13/91    J. Alan Eldridge    moved here from task.cpp
  14.  *  
  15.  *********************************************************************/
  16.  
  17. #include    "aedef.h"
  18. #include    "task.h"
  19.  
  20. //------------------------------------------------------------
  21. //  ********** CLASS SEMA MEMBER FUNCTIONS **********
  22. //------------------------------------------------------------
  23.  
  24. //------------------------------------------------------------
  25. //  Sema::wait()    --  wait on the semaphore, maybe blocking
  26. //  returns 1 if it got the resource, 0 if timed out 
  27. //------------------------------------------------------------
  28.  
  29. int
  30. SemaBase::wait(clock_t msec)
  31. {
  32.     int ret = 1;
  33.     
  34.     if (s_value < 1) {
  35.         s_waiting->Add(CurrTask);
  36.         if (!(ret = CurrTask->block(msec)))
  37.            s_waiting->Del(CurrTask);
  38.     } else {
  39.         --s_value;
  40. #if SUSPEND_ON_WAIT
  41.         CurrTask->suspend();
  42. #endif
  43.     }
  44.     return ret;
  45. }
  46.  
  47. //------------------------------------------------------------
  48. //  SemaBase::signal()  --  signal that task is through using a 
  49. //  a resource, possibly unblocking a task that is waiting
  50. //------------------------------------------------------------
  51.  
  52. void
  53. SemaBase::signal(int susp)
  54. {
  55.     ++s_value;
  56.     if (s_waiting->Cnt()) {
  57.         --s_value;
  58.         GetNxtTask(*s_waiting)->unblock();
  59.     }
  60.  
  61.     if (susp)
  62.         CurrTask->suspend();
  63. }
  64.  
  65.