home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MSJV3-3.ZIP / SEMCODE.ALL < prev    next >
Text File  |  1988-04-28  |  4KB  |  349 lines

  1. Microsoft Systems Journal
  2. Volume 3; Issue 3; May, 1988
  3.  
  4. Code Listings For:
  5.  
  6.     OS/2 Semaphore Examples
  7.     pp. 19-26
  8.  
  9. Author(s): Kevin Ruddell
  10. Title:     Using OS/2 Semaphores To Coordinate Concurrent THreads of Execution
  11.  
  12.  
  13.  
  14.  
  15. Figure 1
  16. ========
  17.  
  18.  
  19. if( rc=DosCreateSem( NoExclusive, &sysSem, &semName))
  20.     if( rc == ERROR_ALREADY_EXISTS)
  21.        DosOpenSem( &sysSem, &semName) ;
  22.     else
  23.         ... error ...
  24.  
  25. ... use the semaphore ...
  26.  
  27.     DosCloseSem( sysSem) ;
  28.  
  29.  
  30.  
  31.  
  32.  
  33. Figure 2
  34. ========
  35.  
  36.  
  37. long resourceSem = 0;
  38.  
  39. thread1()
  40. {
  41.     .
  42.     .
  43.     .
  44.     DosSemRequest( &resourceSem, -1L);
  45.     
  46.     ... read/modify resource ...
  47.  
  48.     DosSemClear( &resourceSem);
  49.     .
  50.     .
  51.     .
  52. }
  53.  
  54. thread2()
  55. {    .
  56.     .
  57.     .
  58.     DosSemRequest( &resourceSem, -1L);
  59.     
  60.     ... read/modify resource...
  61.  
  62.     DosSemClear( &resourceSem);
  63.     .
  64.     .
  65.     .
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72. Figure 3
  73. ========
  74.  
  75.  
  76. main()
  77. {
  78.     .
  79.     .
  80.     .
  81.     DosExitList( 1, &Cleanup);    /* add to exit list */
  82.     .
  83.     .
  84.     .
  85. }
  86.  
  87. Cleanup()
  88. {
  89.      if( DosFSRamSemRequest( &sem, 0L) != ERR_TIMEOUT)
  90.      {
  91.           ... put in a consistent state ...
  92.           DosFSRamSemClear( &sem);
  93.      }
  94.      DosExitList( 3, 0);  /* goto next ExitList routine*/
  95. }
  96.  
  97.  
  98.  
  99.  
  100. Figure 4
  101. ========
  102.  
  103. DosSemSet( sigSem);
  104.  
  105. thread1()
  106. {    .
  107.     .
  108.     .
  109.     F1
  110.     DosSemClear( sigSem);
  111.     .
  112.     .
  113.     .
  114. }
  115.  
  116. thread2()
  117. {    .
  118.     .
  119.     .
  120.     DosSemWait( sigSem, -1L);
  121.     F2
  122.     .
  123.     .
  124.     .
  125. }
  126.  
  127.  
  128.  
  129.  
  130. Figure 5
  131. ========
  132.  
  133.  
  134. thread1()
  135. {    .
  136.     .
  137.     .
  138.     while(1)
  139.     {
  140.         F1
  141.         DosSemClear( sigSem) ;
  142.     }
  143.     .
  144.     .
  145.     .
  146. }
  147.  
  148. thread2()
  149. {    .
  150.     .
  151.     .
  152.     while(1)
  153.     {
  154.         DosSemSetWait( segSem, -1L) ;
  155.         F2
  156.     }
  157.     .
  158.     .
  159.     .
  160. }
  161.  
  162.  
  163.  
  164.  
  165. Figure 6
  166. ========
  167.  
  168.  
  169. struct {
  170.      int numSem;
  171.      int res1;
  172.      unsigned long semHandle1;
  173.      int res2;
  174.      unsigned long semHandle2;
  175.      int res3;
  176.      unsigned long semHandle3;
  177. } muxSemList ;
  178.  
  179. int muxIndex;
  180.  
  181. thread1()
  182. {    .
  183.     .
  184.     .
  185.     F1
  186.     DosSemClear( sigSem1) ;
  187.     .
  188.     .
  189.     .
  190. }
  191.  
  192. thread2()
  193. {    .
  194.     .
  195.     .
  196.     F2
  197.     DosSemClear( sigSem2) ;
  198.     .
  199.     .
  200.     .
  201. }
  202.  
  203. thread3()
  204. {    .
  205.     .
  206.     .
  207.     F3
  208.     DosSemClear( sigSem3) ;
  209.     .
  210.     .
  211.     .
  212. }
  213.  
  214. thread4()
  215. {    .
  216.     .
  217.     .
  218.     muxSemList.numSem = 3 ;
  219.     muxSemList.res1 = 0;
  220.     muxSemList.semHandle1 = sigSem1 ;
  221.     muxSemList.res2 = 0 ;
  222.     muxSemList.semHandle2 = sigSem2 ;
  223.     muxSemList.res3 = 0 ;
  224.     muxSemList.semHandle3 = sigSem3 ;
  225.     .
  226.     .
  227.     .
  228.     DosMuxSemWait( &muxIndex, &muxSemList, -1L) ;
  229.     switch( muxIndex) {
  230.     case 1:
  231.         .
  232.         .    /* respond to F1 */
  233.         .
  234.     case 2: 
  235.         .
  236.         .    /* respond to F2 */
  237.         .
  238.     case 3: 
  239.         .
  240.         .    /* respond to F3 */
  241.         .
  242.     }
  243. }
  244.  
  245.  
  246.  
  247.  
  248. Figure 7
  249. ========
  250.  
  251.  
  252. thread1()
  253. {    .
  254.     .
  255.     .
  256.  
  257.     ... get item c ...
  258.     DosSemWait( &fullSem, -1L) ;
  259.  
  260.     DosSemRequest( &mutexSem, -1L);
  261.     Buffer.head = c;     /* store c in Buffer */
  262.     head++;         /* advance head of Buffer */
  263.     head %= bufSize;     /* wrap around to beginning */
  264.     if((head==tail)||((tail==0)&&(head==bufSize-1)))
  265.         DosSemSet( &fullSem);    /* set if full */
  266.     DosSemClear( &emptySem);       /* not empty */
  267.     DosSemClear( &mutexSem);
  268.     .
  269.     .
  270.     .
  271. }
  272.  
  273. thread2()
  274. {    .
  275.     .
  276.     .
  277.     DosSemWait( &emptySem, -1L);
  278.  
  279.     DosSemRequest( &mutexSem, -1L);
  280.     c = Buffer.tail;    /* get c from Buffer */
  281.     tail++;            /* advance tail of Buffer */
  282.     tail %= bufSize;    /* wrap around to beginning */
  283.     if( head==tail)
  284.         DosSemSet( &emptySem);    /* set if empty */
  285.     DosSemClear( &fullSem);        /* not full */
  286.     DosSemClear( &mutexSem);
  287.  
  288.     ... use item c ...
  289.  
  290.     .
  291.     .
  292.     .
  293. }
  294.  
  295.  
  296.  
  297.  
  298.  
  299. Figure 8
  300. ========
  301.  
  302.  
  303.  
  304. P()
  305. {
  306.     ... wait until classicCountSem > 0 ...
  307.     classicCountSem--;
  308. }
  309.  
  310. V()
  311. {
  312.     classicCountSem++;
  313. }
  314.  
  315.  
  316.  
  317.  
  318.  
  319. Figure 9
  320. ========
  321.  
  322.  
  323. P()
  324. {
  325.     int blocked=1;
  326.  
  327.     while( blocked == 1)
  328.     {
  329.         DosSemWait( &countSem);        /* wait til maybe ok */
  330.  
  331.         DosSemRequest( &mutexSem, -1L); /* mutual excl */
  332.         if( count == 0)        /* not ready yet */
  333.         DosSemSet( &countSem);    /* set up block */
  334.     else    {
  335.             count--;    /* decrement count */
  336.             blocked--;    /* set up loop exit */
  337.     }
  338.     DosSemClear( &mutexSem);    /* mutual excl */
  339.     }
  340. }
  341.  
  342. V()
  343. {
  344.     DosSemRequest( &mutexSem, -1L);    /* mutual excl */
  345.     count++;            /* increment count */
  346.     DosSemClear( &countSem);    /* free waiters */
  347.     DosSemClear( &mutexSem);    /* mutual excl */
  348. }
  349.