home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 463.lha / SemLibrary / test.c < prev   
C/C++ Source or Header  |  1991-01-04  |  2KB  |  104 lines

  1.  
  2. /*
  3.  *  TEST.C
  4.  *
  5.  *  TEST the semaphore library.  If DICE_AUTO_TEST is defined then also
  6.  *  tests DICE's auto-library openning/closing feature.  If not set then
  7.  *  this source will compile under any compiler, including DICE.
  8.  */
  9.  
  10. #include "defs.h"
  11. #include <stdio.h>
  12.  
  13. long Sem;
  14.  
  15. #ifndef DICE_AUTO_TEST
  16. long SemBase;
  17. #endif
  18.  
  19. void
  20. myexit(void)
  21. {
  22.     if (Sem) {
  23.     DeleteSem(Sem);
  24.     Sem = 0;
  25.     }
  26. #ifndef DICE_AUTO_TEST
  27.     if (SemBase) {
  28.     CloseLibrary(SemBase);
  29.     SemBase = 0;
  30.     }
  31. #endif
  32. }
  33.  
  34. main(ac, av)
  35. char *av[];
  36. {
  37.     char buf[256];
  38.  
  39.     if (ac == 1) {
  40.     fputs("TEST sem [r/x/rw/xw/s/u [n]]\n", stderr);
  41.     exit(1);
  42.     }
  43.     atexit(myexit);
  44.  
  45. #ifndef DICE_AUTO_TEST
  46.     if (SemBase = OpenLibrary(SEMNAME, 0)) {
  47. #else
  48.     {
  49. #endif
  50.     Sem = CreateSem(av[1]);
  51.  
  52.     if (ac >= 3) {
  53.         switch(av[2][0]) {
  54.         case 'r':
  55.         printf("read: ");
  56.         fflush(stdout);
  57.         SharSem(Sem);
  58.         puts("ok");
  59.         break;
  60.         case 'x':
  61.         printf("eXcl: ");
  62.         fflush(stdout);
  63.         ExclSem(Sem);
  64.         puts("ok");
  65.         break;
  66.         case 's':
  67.         printf("Sign: ");
  68.         fflush(stdout);
  69.         SigsSem(Sem, (av[3] ? atoi(av[3]) : 1));
  70.         puts("ok");
  71.         break;
  72.         case 'u':
  73.         printf("unlk: ");
  74.         fflush(stdout);
  75.         RelsSem(Sem);
  76.         puts("ok");
  77.         break;
  78.         default:
  79.         puts("huh?");
  80.         break;
  81.         }
  82.         if (av[2][1] == 'w') {
  83.         printf("(wait) ");
  84.         fflush(stdout);
  85.         WaitSem(Sem);
  86.         puts("ok");
  87.         }
  88.     }
  89.     {
  90.         GlobSem *gs = (GlobSem *)Sem;
  91.         printf("sem %08lx W=%08lx S=%08lx Refs=%d Lck=%d Task=%08lx\n",
  92.         gs,
  93.         GetHead(&gs->gs_WaitList),
  94.         GetHead(&gs->gs_SigsList),
  95.         gs->gs_RefCnt,
  96.         gs->gs_Locked,
  97.         gs->gs_Task
  98.         );
  99.     }
  100.     }
  101.     return(0);
  102. }
  103.  
  104.