home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
CPUMET.ZIP
/
CPUMETER.C
next >
Wrap
C/C++ Source or Header
|
1990-12-21
|
5KB
|
202 lines
#define INCL_BASE
#define INCL_WIN
#include <os2.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include "cpumeter.h"
#define THREADSTACKSIZE 4096
void TimingThread(void);
void CountingThread(void);
void CalibrationThread(void);
int nearest_10_percent(LONG lAmount);
TID tidTiming;
TID tidCounting;
TID tidCalibration;
HWND hwndClient;
HWND hwndFrame;
HSYSSEM hSem;
LONG lCount = 0L;
LONG lCountMax = 0L;
LONG lTenth = 0L;
LONG lTwentieth = 0L;
UCHAR iTimingThreadStack [THREADSTACKSIZE];
UCHAR iCalibrationThreadStack [THREADSTACKSIZE];
UCHAR iCountingThreadStack [THREADSTACKSIZE];
HPOINTER hIcon[11];
//
// Main function of programm
//
int main (int argc, char *argv[])
{
static CHAR szClientClass [] = "CPUMETER";
static ULONG flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU |
FCF_MINBUTTON | FCF_TASKLIST |
FCF_ICON;
HAB hab;
HMQ hmq;
QMSG qmsg ;
//
// only one instance
//
if (DosCreateSem(CSEM_PUBLIC, &hSem, "\\sem\\cpumeter.sem"))
DosExit(EXIT_PROCESS, 0);
//
// Initialize PM
//
hab = WinInitialize (0) ;
hmq = WinCreateMsgQueue (hab, 0) ;
WinRegisterClass (
hab, // Anchor block handle
szClientClass, // Name of class being registered
ClientWndProc, // Window procedure for class
CS_SIZEREDRAW, // Class style
0) ; // Extra bytes to reserve
hwndFrame = WinCreateStdWindow (
HWND_DESKTOP, // Parent window handle
WS_VISIBLE, // Style of frame window
&flFrameFlags, // Pointer to control data
szClientClass, // Client window class name
NULL, // Title bar text
0L, // Style of client window
(HMODULE)NULL, // Module handle for resources
ID_ICON00, // ID of resources
&hwndClient) ; // Pointer to client window handle
WinSetWindowPos (hwndFrame, HWND_TOP,
0, 0, WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN)/3,
WinQuerySysValue (HWND_DESKTOP, SV_CYTITLEBAR),
SWP_SHOW|SWP_MOVE|SWP_SIZE);
WinSetWindowPos (hwndFrame, HWND_TOP,
0, 0, 0, 0,
SWP_SHOW|SWP_MINIMIZE);
DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, 31, 1);
tidCalibration =_beginthread (CalibrationThread, iCalibrationThreadStack,
THREADSTACKSIZE, NULL);
DosSleep (1000L);
DosSuspendThread (tidCalibration);
DosSetPrty (PRTYS_THREAD, PRTYC_REGULAR, 0, 1);
lTenth = lCountMax / 10;
lTwentieth = lCountMax / 20;
tidTiming = _beginthread (TimingThread, iTimingThreadStack,
THREADSTACKSIZE, NULL);
tidCounting = _beginthread (CountingThread, iCountingThreadStack,
THREADSTACKSIZE, NULL);
//
// Main message loop
//
while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
WinDispatchMsg (hab, &qmsg) ;
//
// End the thread & term PM
//
WinDestroyWindow (hwndFrame) ;
WinDestroyMsgQueue (hmq) ;
WinTerminate (hab) ;
return 0 ;
}
//
// Window Procedure
//
MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
{
int i;
switch (msg)
{
case WM_CREATE:
for (i = 0; i < 11; i++)
hIcon[i] = WinLoadPointer (HWND_DESKTOP, (HMODULE)NULL,
100+i);
return 0;
case WM_SEM1:
{
static char szBuffer[60];
static int iOld = -1;
i = nearest_10_percent (LONGFROMMP(mp1));
if (i != iOld)
{
iOld = i;
// sprintf(szBuffer, "%ld/%ld -> %d0%%",
// LONGFROMMP(mp1), lCountMax, i);
sprintf(szBuffer, "%d0%%", i);
WinSetWindowText (hwndFrame, szBuffer);
WinSendMsg (hwndFrame, WM_SETICON, hIcon[i], NULL);
WinInvalidateRect (hwndFrame, NULL, FALSE);
WinUpdateWindow (hwndFrame);
}
return 0;
}
case WM_DESTROY:
{
for (i = 0; i < 11; i++)
WinDestroyPointer (hIcon[i]);
DosCloseSem (hSem);
return 0;
}
}
return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
}
void TimingThread ()
{
DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, PRTYD_MAXIMUM, tidTiming);
while (TRUE)
{
DosSleep (1000L);
WinPostMsg (hwndClient, WM_SEM1, MPFROMLONG(lCount), NULL);
lCount = 0L;
}
}
void CountingThread ()
{
DosSetPrty (PRTYS_THREAD, PRTYC_IDLETIME, 0, tidCounting);
while (TRUE) lCount++;
}
void CalibrationThread ()
{
DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, 30, tidCalibration);
while (TRUE) lCountMax++;
}
INT nearest_10_percent (LONG lAmount)
{
ldiv_t struct_ldiv;
if (lAmount == 0)
return 10;
if (lAmount >= lCountMax)
return 0;
struct_ldiv = ldiv (lAmount, lTenth);
if (struct_ldiv.rem >= lTwentieth)
struct_ldiv.quot++;
return (int)(10-struct_ldiv.quot);
}