home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / ONSem.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  4KB  |  167 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // ONSem.cpp
  5. // Named Semaphores
  6.  
  7. // class member functions
  8.  
  9. /*
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  16.  *    endorse or promote products derived from this software
  17.  *    without specific prior written permission.
  18.  * 3. See OCL.INF for a detailed copyright notice.
  19.  *
  20.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  */
  32.  
  33.  
  34. // $Header: W:/Projects/OCL/Source/rcs/ONSem.cpp 1.50 1996/08/11 23:49:25 B.STEIN Release $
  35.  
  36. #define __OCL_SOURCE__
  37.  
  38. #define OINCL_OSTRING
  39. #define OINCL_BASE
  40.  
  41. #include <ocl.hpp>
  42. #include <ONSem.hpp>
  43. #include <OMessage.hpp>
  44.  
  45.  
  46. ONSem::ONSem(HEV handle) throw (OVioException&)
  47.   : sema(handle),
  48.     semState(0)
  49. {
  50.  if (!openSem(sema))
  51.    throw OVioException(OCL::apierror(125, semState), semState);
  52. }
  53.  
  54.  
  55. ONSem::ONSem(PCSZ name, ULONG action, BOOL initialState) throw (OVioException&)
  56.   : sema((HEV)NULL), 
  57.     semState(0),
  58.     semName((PSZ)name)
  59. {
  60.  switch(action)
  61.   {
  62.    case ONSem::create: {
  63.     if((semState = DosCreateEventSem(semName, &sema, 0, initialState)) != 0)
  64.       throw OVioException(OCL::apierror(126, semState), semState);
  65.     break; }
  66.  
  67.    case ONSem::open:
  68.     if (!openSem(name))
  69.       throw OVioException(OCL::apierror(127, semState), semState);
  70.     if (initialState)
  71.       postSem();
  72.     break;
  73.   }
  74. }
  75.  
  76.  
  77.  
  78. ONSem::~ONSem()
  79. {
  80.  if (sema) closeSem();
  81. }
  82.  
  83.  
  84. PSZ ONSem::isOfType() const
  85.  return("ONSem"); 
  86. }
  87.  
  88.  
  89. void ONSem::setSemHandle(HEV hev) 
  90. {
  91.  sema = hev;
  92. }
  93.  
  94.  
  95. HEV  ONSem::getSemHandle()
  96.  return(sema); 
  97. }
  98.  
  99.  
  100. BOOL ONSem::waitSem(const ULONG timeout)
  101. {
  102.  semState = DosWaitEventSem(sema, timeout);
  103.  return(semState == 0);
  104. }
  105.  
  106.  
  107.  
  108. BOOL ONSem::postSem()
  109. {
  110.  semState = DosPostEventSem(sema);
  111.  return(semState == 0);
  112. }
  113.  
  114.  
  115. BOOL ONSem::closeSem()
  116. {
  117.  semState = DosCloseEventSem(sema);
  118.  sema = (HEV)NULL;
  119.  return(semState == 0);
  120. }
  121.  
  122.  
  123. BOOL ONSem::openSem(PCSZ name)
  124. {
  125.  
  126.  if (!name) {
  127.    semState = ERROR_INVALID_NAME;
  128.    return(FALSE); }
  129.  else {
  130.    sema = (HEV) NULL;
  131.    semName << (PSZ) name;
  132.    semState = DosOpenEventSem(semName, &sema); }
  133.  return(semState == 0);
  134. }
  135.  
  136.  
  137. BOOL ONSem::openSem(const HEV aSem)
  138. {
  139.  sema = aSem;
  140.  semName << (PSZ) NULL;
  141.  semState = DosOpenEventSem(semName, &sema);
  142.  return(semState == 0);
  143. }
  144.  
  145.  
  146. ULONG ONSem::resetSem()
  147. {
  148.  ULONG  postCount = 0;
  149.  
  150.  semState = DosResetEventSem(sema, &postCount);
  151.  return(postCount);
  152. }
  153.  
  154.  
  155. ULONG ONSem::querySem()
  156. {
  157.  ULONG  postCount = 0;
  158.  
  159.  semState = DosQueryEventSem(sema, &postCount);
  160.  return(postCount);
  161. }
  162.  
  163.  
  164. // end of source
  165.