Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

testsem.cc

Go to the documentation of this file.
00001 
00002 #ifdef VXWORKS
00003 extern "C"
00004 {
00005 #include <vxWorks.h>
00006 #include <stdio.h>              /* feof(), stdin, fputs(), etc. */
00007 #include <errno.h>              /* errno */
00008 #include <taskLib.h>            /* taskSpawn */
00009 }
00010 #else
00011 extern "C"
00012 {
00013 #include <stdio.h>              /* feof(), stdin, fputs(), etc. */
00014 #include <errno.h>              /* perror() */
00015 }
00016 #endif
00017 #include "semaphore.hh"
00018 #include "timer.hh"             /* esleep() */
00019 
00020 #ifdef VXWORKS
00021 static int semtaker_done = 0;
00022 static void
00023 semtaker ()
00024 {
00025   RCS_SEMAPHORE sem (0x100, RCS_SEMAPHORE_NOCREATE);
00026 
00027   semtaker_done = 0;
00028   while (!semtaker_done)
00029     {
00030       sem.wait ();
00031       printf ("semtaker got semaphore\n");
00032       esleep (2.0);             /* wait 2 seconds */
00033       sem.post ();
00034       printf ("semtaker gave semaphore\n");
00035       esleep (2.0);
00036     }
00037 
00038   printf ("semtaker quit\n");
00039 }
00040 #endif
00041 
00042 #ifdef VXWORKS
00043 extern "C" int testsem ();      /* make it easily shell-accessible */
00044 int
00045 testsem ()                      /* run this in the foreground */
00046 #else
00047 int
00048 main (int argc, char **argv)
00049 #endif
00050 {
00051   RCS_SEMAPHORE *sem;
00052 
00053 #if defined (VXWORKS)
00054   sem = new RCS_SEMAPHORE (0x100, RCS_SEMAPHORE_CREATE, 0664, 0);
00055   taskSpawn ("semtaker", 100, VX_FP_TASK, 8000, (FUNCPTR) semtaker);
00056 #else
00057   if (argc > 1)
00058     {
00059       /* any cmd line args mean this is the semaphore creator */
00060       sem = new RCS_SEMAPHORE (0x100, RCS_SEMAPHORE_CREATE, 0664, 0);
00061     }
00062   else
00063     {
00064       sem = new RCS_SEMAPHORE (0x100, RCS_SEMAPHORE_NOCREATE);
00065     }
00066 #endif
00067 
00068   /* run give and take, interactively */
00069   while (!feof (stdin))
00070     {
00071 #define BUFFERSIZE 80
00072       char buf[BUFFERSIZE];
00073       fputs ("give, take, or quit (g/t/q)? ", stdout);
00074       fgets (buf, BUFFERSIZE, stdin);
00075 
00076       if (buf[0] == 'q')
00077         break;
00078       if (buf[0] == 'g')
00079         {
00080           /* give it */
00081           if (sem->post () == -1)
00082             {
00083               fputs ("can't give it\n", stdout);
00084             }
00085           else
00086             {
00087               fputs ("gave it\n", stdout);
00088               printf ("val = %d\n", sem->getvalue ());
00089             }
00090         }
00091       if (buf[0] == 't')
00092         {
00093           /* take it */
00094           if (sem->wait () == -1)
00095             {
00096               fputs ("can't give it\n", stdout);
00097             }
00098           else
00099             {
00100               fputs ("took it\n", stdout);
00101               printf ("val = %d\n", sem->getvalue ());
00102             }
00103         }
00104     }
00105 
00106 #if defined (VXWORKS)
00107   semtaker_done = 1;
00108 #endif
00109 
00110   delete sem;
00111 }

Generated on Sun Dec 2 15:56:53 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001