00001
00002 #ifdef VXWORKS
00003 #include <stdioLib.h>
00004 #include <sysLib.h>
00005 #include <taskLib.h>
00006 #include <tickLib.h>
00007
00008 #if 0
00009
00010 #include <taskLib.h>
00011 #endif
00012 #else
00013 #include <stdio.h>
00014 #endif
00015
00016 #include "spinlock.h"
00017 #include "rcs_prnt.hh"
00018
00019 extern int increment_read_status (short int *);
00020 extern int decrement_read_status (short int *);
00021
00022
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
00041 while (elapsed_ticks <= timeout_ticks && (timeout >= 0.0))
00042 {
00043 taskLock ();
00044 if (!increment_read_status (sem))
00045 {
00046 return 0;
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;
00064 }
00065
00066
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;
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;
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;
00101 }