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

spinlock.c

Go to the documentation of this file.
00001 
00002 #ifdef VXWORKS
00003 #include <stdioLib.h>           /* fprintf(), stderr */
00004 #include <sysLib.h>             /* sysClkRateGet() */
00005 #include <taskLib.h>            /* taskLock(), taskUnlock(), taskDelay() */
00006 #include <tickLib.h>            /* tickGet() */
00007 
00008 #if 0
00009 /* FIXME: can't include taskLib.h (Where is REG_SET?) */
00010 #include <taskLib.h>            /* taskLock(), taskUnlock(), taskDelay() */
00011 #endif
00012 #else
00013 #include <stdio.h>              /* fprintf(), stderr */
00014 #endif
00015 
00016 #include "spinlock.h"
00017 #include "rcs_prnt.hh"          /* rcs_print_error() */
00018 
00019 extern int increment_read_status (short int *);
00020 extern int decrement_read_status (short int *);
00021 
00022 /* acquire access to a semaphored data buffer */
00023 int
00024 acquire_access (short int *sem, double timeout)
00025 {
00026   ULONG timeout_ticks, start_ticks, current_ticks, elapsed_ticks;
00027   if (timeout == 0.0)
00028     {
00029       timeout_ticks = 1;
00030       start_ticks = tickGet ();
00031       elapsed_ticks = 0;
00032     }
00033   else if (timeout > 0.0)
00034     {
00035       timeout_ticks = (int) (timeout * sysClkRateGet ());
00036       start_ticks = tickGet ();
00037       elapsed_ticks = 0;
00038     }
00039 
00040   /* loop until timeout_count expires */
00041   while (elapsed_ticks <= timeout_ticks && (timeout >= 0.0))
00042     {
00043       taskLock ();
00044       if (!increment_read_status (sem))
00045         {
00046           return 0;             /* success */
00047         }
00048       else
00049         {
00050           taskUnlock ();
00051           taskDelay (1);
00052           if (timeout >= 0.0)
00053             {
00054               current_ticks = tickGet ();
00055               elapsed_ticks = current_ticks - start_ticks;
00056             }
00057         }
00058     }
00059 
00060   taskUnlock ();
00061   rcs_print_error ("timed out while acquiring access on semaphore 0x%x\n",
00062                    (int) sem);
00063   return -1;                    /* indicate timeout failure */
00064 }
00065 
00066 /* release semaphore when done  */
00067 int
00068 release_access (short int *sem, double timeout)
00069 {
00070   int tst;
00071   ULONG timeout_ticks, start_ticks, current_ticks, elapsed_ticks;
00072   timeout_ticks = (ULONG) (timeout * sysClkRateGet ());
00073   start_ticks = tickGet ();
00074   elapsed_ticks = 0;
00075 
00076   while (elapsed_ticks <= timeout_ticks)
00077     {
00078       tst = decrement_read_status (sem);
00079       if (tst == 0)
00080         {
00081           taskUnlock ();
00082           return 0;             /* success */
00083         }
00084       else
00085         {
00086           if (tst < 0)
00087             {
00088               taskUnlock ();
00089               rcs_print_error ("invalid semaphore on 0x%x", (int) sem);
00090               return -1;        /* invalid semaphore  */
00091             }
00092         }
00093       current_ticks = tickGet ();
00094       elapsed_ticks = current_ticks - start_ticks;
00095     }
00096 
00097   taskUnlock ();
00098   rcs_print_error ("timed out while releasing access on semphore 0x%x",
00099                    (int) sem);
00100   return -1;                    /* timeout failure */
00101 }

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