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

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OThreadBase.cpp
  5. // asynchronous threads
  6.  
  7.  
  8. /*
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  15.  *    endorse or promote products derived from this software
  16.  *    without specific prior written permission.
  17.  * 3. See OCL.INF for a detailed copyright notice.
  18.  *
  19.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  20.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29.  * SUCH DAMAGE.
  30.  */
  31.  
  32. // $Header: W:/Projects/OCL/Source/rcs/OThreadBase.cpp 1.50 1996/08/11 23:49:33 B.STEIN Release $
  33.  
  34. #define __OCL_SOURCE__
  35.  
  36. #define OINCL_OSTRING
  37. #define OINCL_BASE
  38.  
  39. #include <ocl.hpp>
  40. #include <OThread.hpp>
  41.  
  42.  
  43.  
  44. OThreadBase::OThreadBase(ULONG stackSize, BOOL forPM)
  45.  : initPM(forPM),
  46.    stack(stackSize), 
  47.    tid(0)
  48.  {}
  49.  
  50.  
  51. OThreadBase::~OThreadBase()
  52. {
  53.  stop();
  54. }
  55.  
  56.  
  57. PSZ OThreadBase::isOfType() const
  58.  return("OThreadBase"); 
  59. }
  60.  
  61.  
  62. BOOL OThreadBase::run()                              // start thread
  63. {
  64.  if (isRunning())
  65.    return(FALSE);
  66. #if (defined (__BCPLUSPLUS__) || defined (__HIGHC__))
  67.  tid = _beginthread((pOThread_mbrfn)OTHREADBASE_dispatch, stack, this);
  68. #else
  69.  tid = _beginthread((pOThread_mbrfn)OTHREADBASE_dispatch, NULL, stack, this);
  70. #endif
  71.  return(tid != 0);
  72. }
  73.  
  74.  
  75. BOOL OThreadBase::isRunning()
  76. {
  77.  if (tid != 0)
  78.    return(DosWaitThread(&tid, DCWW_NOWAIT) == ERROR_THREAD_NOT_TERMINATED);
  79.  else return(FALSE);
  80. }
  81.  
  82.  
  83. BOOL OThreadBase::waitFor()
  84. {
  85.  if (tid != 0)
  86.    return(DosWaitThread(&tid, DCWW_WAIT) != ERROR_THREAD_NOT_TERMINATED);
  87.  else return(FALSE);
  88. }
  89.  
  90.  
  91. void OThreadBase::setPrty(ULONG Class, ULONG Delta)
  92. {
  93.  if (isRunning())
  94.    DosSetPriority(PRTYS_THREAD, Class, Delta, tid);
  95. }
  96.  
  97.  
  98. void OThreadBase::stop()
  99. {
  100.  if (isRunning())
  101.    DosKillThread(tid); 
  102. }
  103.  
  104.  
  105. void OThreadBase::suspend()
  106. {
  107.  if (isRunning())
  108.    DosSuspendThread(tid);
  109. }
  110.  
  111.  
  112. void OThreadBase::resume()
  113. {
  114.  if (isRunning())
  115.    DosResumeThread(tid);
  116. }
  117.  
  118.  
  119. void OThreadBase::OTHREADBASE_dispatch(PVOID pvData)
  120. {
  121.  pOThreadBase thread = (pOThreadBase) pvData;
  122.  pOPMinit     init   = NULL;
  123.  
  124.  if (!thread)
  125.    _endthread();
  126.  
  127.  if (thread->initPM)
  128.    init = new OPMinit;
  129.  
  130.  thread->async();
  131.  
  132.  if ((thread->initPM) && (init))
  133.    delete init;
  134.  
  135.  thread->tid = 0;
  136.  _endthread();
  137. }
  138.  
  139.  
  140.  
  141. // end of source
  142.