home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / os2 / timer0 / timer0.c next >
Text File  |  1999-05-11  |  3KB  |  111 lines

  1. #define INCL_DOSPROCESS
  2. #define INCL_DOSDEVICES
  3. #define INCL_DOSDEVIOCTL
  4. #include <os2.h>
  5. #include <conio.h>
  6. #include <stdio.h>
  7.  
  8. #include "tmr0_ioc.h"
  9.  
  10. #define NUM_THREADS     4
  11.  
  12. HFILE hfile;
  13. volatile int fActive = TRUE;
  14. volatile unsigned uNumThreads = 0;
  15. ULONG *pulTimer;
  16.  
  17. ULONG ulCounter[NUM_THREADS];
  18.  
  19.  
  20. VOID APIENTRY ThreadFunction(ULONG ulIndex)
  21. {
  22.    APIRET rc;
  23.    ULONG ulDelay = (ulIndex + 1) * 500;
  24.    ULONG ulSize = sizeof(ulDelay);
  25.  
  26.    rc = DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
  27.    if (rc)
  28.       return;
  29.  
  30.    uNumThreads++;    // This is an atomic operation.
  31.  
  32.    while (fActive) {
  33.       rc = DosDevIOCtl(hfile, HRT_IOCTL_CATEGORY, HRT_BLOCKUNTIL, &ulDelay, ulSize, &ulSize, NULL, 0, NULL);
  34.       if (rc)
  35.          break;
  36.       ulCounter[ulIndex]++;
  37.    }
  38.  
  39.    uNumThreads--;    // This is an atomic operation.
  40. }
  41.  
  42. int main(void)
  43. {
  44.    APIRET rc;
  45.    TID tid;
  46.    ULONG ulAction;
  47.    ULONG ulOpenFlag = OPEN_ACTION_OPEN_IF_EXISTS;
  48.    ULONG ulOpenMode = OPEN_FLAGS_FAIL_ON_ERROR | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE;
  49.  
  50.    ULONG ulResolution = 1;
  51.    ULONG ulSize1 = sizeof(ulResolution);
  52.  
  53.    ULONG * _Seg16 pulTimer16;                   // defines a 16:16 pointer
  54.    ULONG ulSize2 = sizeof(pulTimer16);
  55.  
  56.    unsigned i;
  57.  
  58.    rc = DosOpen("TIMER0$  ", &hfile, &ulAction, 0, 0, ulOpenFlag, ulOpenMode, NULL);
  59.    if (rc) {
  60.       printf("Couldn't open TIMER0$.\n");
  61.       return 1;
  62.    }
  63.  
  64.    printf("TIMER0$ opened.  File Handle is %lu\n", hfile);
  65.  
  66.    rc = DosDevIOCtl(hfile, HRT_IOCTL_CATEGORY, HRT_SET_RESOLUTION,  &ulResolution, ulSize1, &ulSize1, NULL, 0, NULL);
  67.    if (rc) {
  68.       printf("Couldn't set resolution.\n");
  69.       DosClose(hfile);
  70.       return 2;
  71.    }
  72.  
  73.    rc = DosDevIOCtl(hfile, HRT_IOCTL_CATEGORY, HRT_GET_POINTER, NULL, 0, NULL, (void *) &pulTimer16, ulSize2, &ulSize2);
  74.    if (rc) {
  75.       printf("Couldn't get pointer.\n");
  76.       DosClose(hfile);
  77.       return 3;
  78.    }
  79.  
  80.    pulTimer = pulTimer16;    // converts a 16:16 pointer to a 0:32 pointer
  81.    if (!pulTimer) {
  82.       printf("NULL pointer.\n");
  83.       DosClose(hfile);
  84.       return 4;
  85.    }
  86.  
  87.    for (i=0; i<NUM_THREADS; i++) {
  88.       rc = DosCreateThread(&tid, ThreadFunction, i, 0, 8192);
  89.       if (rc) {
  90.          printf("Can't create thread %i, rc=%lu\n", i+1, rc);
  91.          fActive = FALSE;
  92.          while (uNumThreads);     // Wait until all threads have ended
  93.          return 5;
  94.       }
  95.    }
  96.  
  97.    while (!kbhit()) {
  98.       printf("Counter: %08lu  ", *pulTimer);
  99.       for (i=0; i<NUM_THREADS; i++)
  100.          printf("%i:%08lu ", i+1, ulCounter[i]);
  101.       printf("\r");
  102.       DosSleep(0);
  103.    }
  104.    fActive=FALSE;
  105.    getch();
  106.    while (uNumThreads);     // Wait until all threads have ended
  107.  
  108.    return 0;
  109. }
  110.  
  111.