home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mandlcpp.zip / process.cpp < prev    next >
C/C++ Source or Header  |  1993-06-24  |  3KB  |  135 lines

  1. #include "process.h"
  2. #ifdef INCLUDESOURCE
  3. #include "tree.cc"
  4. #endif
  5.  
  6.  
  7. typedef struct helpStruct
  8. {    HEV hev;
  9.     thread *pThread;
  10. };
  11.  
  12.  
  13. static void _syscall helpFct(struct helpStruct *pHelp)
  14. {    PIB *pPIB;
  15.     thread *pThread = pHelp->pThread;
  16.     chain *pParent;
  17.  
  18.     DosGetInfoBlocks(&pThread->pTIB, &pPIB);
  19.     DosPostEventSem(pHelp->hev);
  20.     DosWaitEventSem(pThread->hEventConstructed, (unsigned int)SEM_INDEFINITE_WAIT);
  21.     DosCloseEventSem(pThread->hEventConstructed);
  22.     pThread->run();
  23.     (pParent = pThread->pParent)->getMutualExclusiveAccess();
  24.     delete pThread;
  25.     pParent->freeMutualExclusiveAccess();
  26.  
  27. }
  28.  
  29.  
  30. thread::thread(process *pProcessNew, chain *pParent)
  31.     :chainElement(pParent)
  32. {
  33.     pProcess = pProcessNew;
  34.     createThread();
  35. }
  36.  
  37.  
  38. void thread::createThread(void)
  39. {    struct helpStruct help;
  40.     TID tid;
  41.  
  42.     DosCreateEventSem((unsigned char*)0,
  43.         &hEventConstructed,
  44.         (unsigned long)0,
  45.         (BOOL32)0);
  46.     help.pThread = this;
  47.     DosCreateEventSem((unsigned char*)0,
  48.         &help.hev,
  49.         (unsigned long)0,
  50.         (BOOL32)0);
  51.     DosCreateThread(&tid,
  52.        (void (_syscall *)(unsigned long))helpFct,
  53.        (unsigned long)&help,
  54.        (unsigned long)0,
  55.        (unsigned long)1024*1024);
  56.     DosWaitEventSem(help.hev, (unsigned int)SEM_INDEFINITE_WAIT);
  57.     DosCloseEventSem(help.hev);
  58. }
  59.  
  60.  
  61. thread::thread(Boolean bCreate, process *pProcessNew)
  62.     :chainElement(pProcessNew)
  63. {    PIB *pPIB;
  64.  
  65.     pProcess = pProcessNew;
  66.     if (bCreate)
  67.         createThread();
  68.     else
  69.     {    pProcess->pProcessThread = this;
  70.         DosGetInfoBlocks(&pTIB, &pPIB);
  71.     }
  72. }
  73.  
  74.  
  75. /*
  76. I'm not so shure that this works,
  77. but until now I did'nt get any crash.
  78. The destructor of a thread can be called by more then one thread:
  79. The thread with tid == 1 or the thread itself in helpFct().
  80. To avoid that a thread object will be freed twice,
  81. you should always call pParent->getMutualExclusiveAccess() and
  82. check then wether the thread is not already freed before calling
  83. the destructor.
  84. */
  85. thread::~thread(void)
  86. {    PIB *pPIBHelp;
  87.     TIB *pTIBHelp;
  88.  
  89.     DosGetInfoBlocks(&pTIBHelp, &pPIBHelp);
  90.     if (pTIBHelp->tib_ptib2->tib2_ultid != pTIB->tib_ptib2->tib2_ultid)
  91.         DosKillThread(pTIB->tib_ptib2->tib2_ultid);
  92.     if (pProcess->pProcessThread == this)
  93.         pProcess->pProcessThread = (thread*)0;
  94. }
  95.  
  96.  
  97. process::process(int argcNew, char **argvNew):construct(), chain()
  98. {    TIB *pTIB;
  99.  
  100.     DosGetInfoBlocks(&pTIB, &pPIB);
  101.     argc = argcNew;
  102.     argv = argvNew;
  103.     pProcessThread = (thread*)0;
  104.     pPIB = (PPIB)0;
  105. }
  106.  
  107.  
  108. Boolean process::create(void)
  109. {    if (!new thread(FALSE, this))
  110.         return FALSE;
  111.     else
  112.         if (pProcessThread->isSuccessfull())
  113.             return TRUE;
  114.         else
  115.         {    delete pProcessThread;
  116.             return FALSE;
  117.         }
  118. }
  119.  
  120.  
  121. void process::destruct(void)
  122. {       if (pProcessThread)
  123.         delete pProcessThread;
  124. }
  125.  
  126.  
  127. void process::doSomething(void)
  128. {    pProcessThread->run();
  129. }
  130.  
  131.  
  132. process::~process(void)
  133. {
  134. }
  135.