Implementing timeout for system calls
Hi,
I wonder whether I can implement timeout for VxWorks system calls as
following. Also, I want to know whether this implementation could
corrupt VxWorks semaphore data structures.
I cannot use the original VxWorks semaphore with timeout in my
program.
Thanks,
Aung
/* I adapted this example from Richard Stevens's Advanced Programming
in the UNIX Environment book */
/* timer interrupt handler will send a signal when timeout */
void sig_timeout(int signo) {
longjmp(env_timeout, 1);
}
int rr_semTakeTimeout(SEM_ID sem_id, int timeout) {
STATUS volatile status = ERROR;
signal(SIGUSR1, sig_timeout) == SIG_ERR;
if (setjmp(env_timeout) == 0) {
setHardwareTimer(timeout);
status = semTake(sem_id, WAIT_FOREVER);
deleteHardwareTimer(0);
}
return status;
}
Re: Implementing timeout for system calls
Aung wrote:[color=blue]
> Hi,
> I wonder whether I can implement timeout for VxWorks system calls as
> following. Also, I want to know whether this implementation could
> corrupt VxWorks semaphore data structures.
>
> I cannot use the original VxWorks semaphore with timeout in my
> program.
>
> Thanks,
> Aung
>
> /* I adapted this example from Richard Stevens's Advanced Programming
> in the UNIX Environment book */
>
> /* timer interrupt handler will send a signal when timeout */
> void sig_timeout(int signo) {
> longjmp(env_timeout, 1);
> }
>
> int rr_semTakeTimeout(SEM_ID sem_id, int timeout) {
> STATUS volatile status = ERROR;
>
> signal(SIGUSR1, sig_timeout) == SIG_ERR;
>
> if (setjmp(env_timeout) == 0) {
> setHardwareTimer(timeout);
> status = semTake(sem_id, WAIT_FOREVER);
> deleteHardwareTimer(0);
> }
> return status;
> }[/color]
Re: Implementing timeout for system calls
That last reply didn't go well, did it? ;-)
It comes down to whether you have the ability to clean up the semaphore
and the blocked task from within the interrupt context. If that's not
possible, then try a blocking option, if that's acceptable.
Do the semTake() with NO_WAIT and spin through a while() loop during
the timeout period, returning ERROR and setting errno to
S_objlib_Timeout if you can't get the semaphore. That way, it is
functionally equivalent to the semTake behavior, though with one
glaring deficiency; task is blocked during the timeout period.
There's a second option that would at least stay VxWorks portable and
that would be to use the watchdog library. check the docs for the exact
call, but it would be something along the lines of:
WD_ID wdog = wdCreate();
wdStart(wdog, timeoutTicks, timeoutFunc)
return semTake(sem_id, WAIT_FOREVER);
Same deal. You'll have to cleanup semaphore and task from timeoutFunc()
in an interrupt context.