Annotation of pmsdk/samples/petzold/chap17/bigjob5.c, revision 1.1.1.1

1.1       root        1: /*--------------------------------------------------
                      2:    BIGJOB5.C -- Second thread and semaphore trigger
                      3:  ---------------------------------------------------*/
                      4: 
                      5: #define INCL_WIN
                      6: #define INCL_DOS
                      7: #include <os2.h>
                      8: #include <mt\process.h>
                      9: #include <mt\stdlib.h>
                     10: #include "bigjob.h"
                     11: 
                     12: VOID _CDECL FAR CalcThread (PCALCPARAM) ;
                     13: 
                     14: HAB  hab ;
                     15: 
                     16: int main (void)
                     17:      {
                     18:      static CHAR  szClientClass [] = "BigJob5" ;
                     19:      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU  |
                     20:                                  FCF_SIZEBORDER    | FCF_MINMAX   |
                     21:                                  FCF_SHELLPOSITION | FCF_TASKLIST |
                     22:                                  FCF_MENU ;
                     23:      HMQ          hmq ;
                     24:      HWND         hwndFrame, hwndClient ;
                     25:      QMSG         qmsg ;
                     26: 
                     27:      hab = WinInitialize (0) ;
                     28:      hmq = WinCreateMsgQueue (hab, 0) ;
                     29: 
                     30:      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
                     31: 
                     32:      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
                     33:                                      &flFrameFlags, szClientClass,
                     34:                                      " - Second Thread with Semaphore",
                     35:                                      0L, NULL, ID_RESOURCE, &hwndClient) ;
                     36: 
                     37:      WinSendMsg (hwndFrame, WM_SETICON,
                     38:                  WinQuerySysPointer (HWND_DESKTOP, SPTR_APPICON, FALSE),
                     39:                  NULL) ;
                     40: 
                     41:      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
                     42:           WinDispatchMsg (hab, &qmsg) ;
                     43: 
                     44:      WinDestroyWindow (hwndFrame) ;
                     45:      WinDestroyMsgQueue (hmq) ;
                     46:      WinTerminate (hab) ;
                     47:      return 0 ;
                     48:      }
                     49:        
                     50: MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
                     51:      {
                     52:      static CALCPARAM cp ;
                     53:      static INT       iThreadStack [STACKSIZE / 2] ;
                     54:      static LONG      lRepAmts [] = { 10, 100, 1000, 10000, 100000 } ;
                     55:      static SHORT     sCurrentRep = IDM_10 ;
                     56:      static SHORT     sStatus = STATUS_READY ;
                     57:      static TID       tidCalc ;
                     58:      static ULONG     ulElapsedTime ;
                     59: 
                     60:      switch (msg)
                     61:           {
                     62:           case WM_CREATE:
                     63:                cp.hwnd = hwnd ;
                     64:                DosSemSet (&cp.ulSemTrigger) ;
                     65: 
                     66:                tidCalc = _beginthread (CalcThread, iThreadStack,
                     67:                                        STACKSIZE, &cp) ;
                     68:                return 0 ;
                     69: 
                     70:           case WM_INITMENU:
                     71:                if (tidCalc == -1 && SHORT1FROMMP (mp1) == IDM_ACTION)
                     72:                     EnableMenuItem (hwnd, IDM_START, FALSE) ;
                     73:                return 0 ;
                     74: 
                     75:           case WM_COMMAND:
                     76:                switch (COMMANDMSG(&msg)->cmd)
                     77:                     {
                     78:                     case IDM_10:
                     79:                     case IDM_100:
                     80:                     case IDM_1000:
                     81:                     case IDM_10000:
                     82:                     case IDM_100000:
                     83:                          CheckMenuItem (hwnd, sCurrentRep, FALSE) ;
                     84:                          sCurrentRep = COMMANDMSG(&msg)->cmd ;
                     85:                          CheckMenuItem (hwnd, sCurrentRep, TRUE) ;
                     86:                          return 0 ;
                     87: 
                     88:                     case IDM_START:
                     89:                          cp.lCalcRep = lRepAmts [sCurrentRep - IDM_10] ;
                     90:                          cp.fContinueCalc = TRUE ;
                     91:                          DosSemClear (&cp.ulSemTrigger) ;
                     92: 
                     93:                          sStatus = STATUS_WORKING ;
                     94:                          WinInvalidateRect (hwnd, NULL, FALSE) ;
                     95:                          EnableMenuItem (hwnd, IDM_START, FALSE) ;
                     96:                          EnableMenuItem (hwnd, IDM_ABORT, TRUE) ;
                     97:                          return 0 ;
                     98: 
                     99:                     case IDM_ABORT:
                    100:                          cp.fContinueCalc = FALSE ;
                    101:                          EnableMenuItem (hwnd, IDM_ABORT, FALSE) ;
                    102:                          return 0 ;
                    103:                     }
                    104:                break ;
                    105: 
                    106:           case WM_CALC_DONE:
                    107:                sStatus = STATUS_DONE ;
                    108:                ulElapsedTime = LONGFROMMP (mp1) ;
                    109:                WinInvalidateRect (hwnd, NULL, FALSE) ;
                    110:                EnableMenuItem (hwnd, IDM_START, TRUE) ;
                    111:                EnableMenuItem (hwnd, IDM_ABORT, FALSE) ;
                    112:                return 0 ;
                    113: 
                    114:           case WM_CALC_ABORTED:
                    115:                sStatus = STATUS_READY ;
                    116:                WinInvalidateRect (hwnd, NULL, FALSE) ;
                    117:                EnableMenuItem (hwnd, IDM_START, TRUE) ;
                    118:                return 0 ;
                    119: 
                    120:           case WM_PAINT:
                    121:                PaintWindow (hwnd, sStatus, cp.lCalcRep, ulElapsedTime) ;
                    122:                return 0 ;
                    123: 
                    124:           case WM_DESTROY:
                    125:                if (sStatus = STATUS_WORKING)
                    126:                     DosSuspendThread (tidCalc) ;
                    127:                return 0 ;
                    128:           }
                    129:      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
                    130:      }
                    131: 
                    132: VOID _CDECL FAR CalcThread (PCALCPARAM pcp)
                    133:      {
                    134:      double A ;
                    135:      LONG   lRep, lTime ;
                    136: 
                    137:      while (TRUE)
                    138:           {
                    139:           DosSemWait (&pcp->ulSemTrigger, SEM_INDEFINITE_WAIT) ;
                    140: 
                    141:           lTime = WinGetCurrentTime (hab) ;
                    142: 
                    143:           for (A = 1.0, lRep = 0 ; lRep < pcp->lCalcRep &&
                    144:                                    pcp->fContinueCalc ; lRep++)
                    145:                A = Savage (A) ;
                    146: 
                    147:           DosSemSet (&pcp->ulSemTrigger) ;
                    148: 
                    149:           if (pcp->fContinueCalc)
                    150:                {
                    151:                lTime = WinGetCurrentTime (hab) - lTime ;
                    152:                WinPostMsg (pcp->hwnd, WM_CALC_DONE, MPFROMLONG (lTime), NULL) ;
                    153:                }
                    154:           else
                    155:                WinPostMsg (pcp->hwnd, WM_CALC_ABORTED, NULL, NULL) ;
                    156:           }
                    157:      }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.