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

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