home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MSJV5-6.ZIP / CPUMETER.ZIP / CPUMETE.C < prev    next >
C/C++ Source or Header  |  1990-11-01  |  5KB  |  163 lines

  1. //
  2. // cpumete.c - (c) 1990 Chiverton Graphics, Inc.
  3. //
  4.  
  5. #define INCL_WIN
  6. #define INCL_DOS
  7. #include <os2.h>
  8. #include <process.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include "cpumete.h"
  12.  
  13. #define THREADSTACKSIZE   4096
  14.  
  15. VOID TimerThread (void) ;
  16. VOID CounterThread (void) ;
  17. VOID CalibrationThread (void) ;
  18. INT  nearest_10_percent (LONG lAmount);
  19.  
  20. TID   tidTimer ,
  21.       tidCounter,
  22.       tidCalibration;
  23.  
  24. HWND  hwndClient,
  25.       hwndFrame ;
  26.  
  27. HSYSSEM hSem ;
  28.  
  29. LONG  lCount     = 0L,
  30.       lCountMax  = 0L,
  31.       lTenth     = 0L,
  32.       lTwentieth = 0L;
  33.  
  34. INT   iTimerThreadStack       [THREADSTACKSIZE / 2] ,
  35.       iCalibrationThreadStack [THREADSTACKSIZE / 2] ,
  36.       iCounterThreadStack     [THREADSTACKSIZE / 2] ;
  37.  
  38. HPOINTER hIcon [11];
  39.  
  40. int main (void)
  41.      {
  42.      static CHAR  szClientClass [] = "CPUMETE";
  43.      static ULONG flFrameFlags = FCF_TITLEBAR  |  FCF_SYSMENU  |
  44.                                  FCF_MINBUTTON |  FCF_TASKLIST |
  45.                                  FCF_ICON ;
  46.      HAB   hab ;
  47.      HMQ   hmq ;
  48.      QMSG  qmsg ;
  49.  
  50.      if ( DosCreateSem(CSEM_PUBLIC, &hSem, "\\sem\\cpumeter.sem") )
  51.           DosExit (EXIT_PROCESS, 0);
  52.  
  53.      hab = WinInitialize (0) ;
  54.      hmq = WinCreateMsgQueue (hab, 0) ;
  55.  
  56.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
  57.  
  58.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  59.                                      &flFrameFlags, szClientClass,
  60.                                      NULL, 0L, NULL, ID_ICON00, &hwndClient) ;
  61.  
  62.      WinSetWindowPos (hwndFrame, HWND_TOP, 0, 0,
  63.              WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN)/3,
  64.              WinQuerySysValue (HWND_DESKTOP, SV_CYTITLEBAR),
  65.              SWP_SHOW | SWP_SIZE | SWP_MOVE);
  66.  
  67.      DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, 31, 1);
  68.      tidCalibration = _beginthread (CalibrationThread, iCalibrationThreadStack, THREADSTACKSIZE, NULL) ;
  69.      DosSleep (1000L);
  70.      DosSuspendThread (tidCalibration);
  71.      DosSetPrty (PRTYS_THREAD, PRTYC_REGULAR, 0, 1);
  72.  
  73.      lTenth     = lCountMax / 10;
  74.      lTwentieth = lCountMax / 20;
  75.  
  76.      tidTimer   = _beginthread (TimerThread,   iTimerThreadStack,   THREADSTACKSIZE, NULL) ;
  77.      tidCounter = _beginthread (CounterThread, iCounterThreadStack, THREADSTACKSIZE, NULL) ;
  78.  
  79.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  80.           WinDispatchMsg (hab, &qmsg) ;
  81.  
  82.      WinDestroyWindow (hwndFrame) ;
  83.      WinDestroyMsgQueue (hmq) ;
  84.      WinTerminate (hab) ;
  85.      return 0 ;
  86.      }
  87.  
  88. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  89.      {
  90.      int i;
  91.      switch (msg)
  92.           {
  93.           case WM_CREATE:
  94.                {
  95.                for (i=0; i<=10; i++)  hIcon [i] = WinLoadPointer (HWND_DESKTOP, NULL, (100 + i));
  96.                return 0 ;
  97.                }
  98.  
  99.           case WM_SEM1:
  100.                {
  101.                static char szBuffer [60];
  102.                static int iOld = -1;
  103.  
  104.                i = nearest_10_percent (LONGFROMMP (mp1));
  105.                if (i != iOld)
  106.                     {
  107.                     iOld = i;
  108.                     sprintf (szBuffer, "%ld/%ld -> %d0%%", LONGFROMMP (mp1), lCountMax, i);
  109.                     WinSetWindowText (hwndFrame, szBuffer) ;
  110.  
  111.                     WinSendMsg (hwndFrame, WM_SETICON, hIcon [i], NULL) ;
  112.                     WinInvalidateRect (hwndFrame, NULL, FALSE);
  113.                     WinUpdateWindow (hwndFrame);
  114.                     }
  115.                return 0 ;
  116.                }
  117.  
  118.           case WM_DESTROY:
  119.                {
  120.                for (i=0; i<=10; i++)  WinDestroyPointer (hIcon [i]);
  121.                DosCloseSem (hSem);
  122.                return 0 ;
  123.                }
  124.           }
  125.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  126.      }
  127.  
  128. VOID TimerThread ()
  129.      {
  130.      DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, PRTYD_MAXIMUM, tidTimer);
  131.      while (TRUE)
  132.           {
  133.           DosSleep (1000L);
  134.  
  135.           WinPostMsg (hwndClient, WM_SEM1, MPFROMLONG (lCount), NULL) ;
  136.           lCount = 0L;
  137.           }
  138.      }
  139.  
  140. VOID CounterThread ()
  141.      {
  142.      DosSetPrty (PRTYS_THREAD, PRTYC_IDLETIME, 0, tidCounter);
  143.      while (TRUE) lCount++;
  144.      }
  145.  
  146. VOID CalibrationThread ()
  147.      {
  148.      DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, 30, tidCalibration);
  149.      while (TRUE) lCountMax++;
  150.      }
  151.  
  152. INT nearest_10_percent (LONG lAmount)
  153.      {
  154.      ldiv_t struct_ldiv;
  155.  
  156.      if (lAmount == 0) return 10;
  157.      if (lAmount >= lCountMax) return 0;
  158.  
  159.      struct_ldiv = ldiv (lAmount, lTenth);
  160.      if (struct_ldiv.rem >= lTwentieth) struct_ldiv.quot++;
  161.      return (int) (10 - struct_ldiv.quot);
  162.      }
  163.