periodic task question
Quoted 9 lines
-----Original Message-----
From: Steven Seeger
Sent: Sunday, February 03, 2008 12:01 PM
To: 'Gilles Chanteperdrix'
Subject: RE: [Xenomai-help] periodic task questionI guess I don't see the difference here. I lock the mutex first, and
then
Quoted 1 line
wait on the conditional. The only time that conditional is ever
signaled
Quoted 6 lines
is after something else sets playing to non-zero. (This code actually clears playing after locking that mutex, however I didn't put that in here.)
What may be causing the problem is the other person who is locking
sound_mutexis a non-realtime thread via write ops for a character
device.
Quoted 15 lines
Oops. Perhaps I need a new method for this. :) (
rt_mutex_acquire()is returning -1.)Steven
Quoted 10 lines
-----Original Message-----
From: Gilles Chanteperdrix [mailto:[email protected]]
Sent: Sunday, February 03, 2008 11:54 AM
To: Steven Seeger
Subject: RE: [Xenomai-help] periodic task questionQuoted 1 line · Steven Seeger
This is the relevant code. In aperiodic mode, it works fine. In
periodic
Quoted 1 line
mode (125us timebase) then when I activate this task by sending
data
Quoted 2 lines
to
Quoted 1 line
Quoted 1 line
/dev/dspthe sound plays very fast and then the watchdog kills
the
Quoted 1 line
Quoted 1 line
Quoted 1 line
thread. If I change this to set the period only after
rt_cond_wait()
Quoted 17 lines
Quoted 17 lines
Quoted 7 lines
returns, it works fine.
static void
sound_func(void *data)
{
rt_task_set_periodic(&sound_task,TM_NOW,rt_timer_ns2ticks(ST_AUDIO_PERIOD));Quoted 7 lines
for (;;) //go until suspended or thread deletion { if(!playing) { rt_mutex_acquire(&sound_mutex, TM_INFINITE); rt_cond_wait(&sound_cond, &sound_mutex, TM_INFINITE); rt_mutex_release(&sound_mutex); }This code is wrong, it should be:
Quoted 7 lines
Quoted 7 lines
rt_mutex_acquire(&sound_mutex, TM_INFINITE);
while (!playing)rt_cond_wait(&sound_cond, &sound_mutex, TM_INFINITE);
rt_mutex_release(&sound_mutex);Here is the relevant doc of
pthread_cond_wait:
A condition variable must always be associated with a mutex,
to
Quoted 2 lines
Quoted 2 lines
avoid
the race condition where a thread prepares to wait on a
condition
Quoted 2 lines
Quoted 2 lines
vari-
able and another thread signals the condition just before
the
Quoted 8 lines
Quoted 8 lines
first
thread actually waits on it.--
Gilles Chanteperdrix.
Quoted 8 lines · Steven Seeger
Quoted 8 lines
-----Original Message-----
From: Steven Seeger
Sent: Sunday, February 03, 2008 12:01 PM
To: 'Gilles Chanteperdrix'
Subject: RE: [Xenomai-help] periodic task question
Quoted 7 lines
Quoted 1 line
I guess I don't see the difference here. I lock the mutex first, and
then
Quoted 1 line
wait on the conditional. The only time that conditional is ever
signaled
Quoted 3 lines
is after something else sets playing to non-zero. (This code actually clears playing after locking that mutex, however I didn't put that in here.)
It was a general advice, not a proposed fix for the bug you observe. By locking the mutex when reading or writing the "playing" variable, you ensure that nobody else will be changing the "playing" variable while you look at it and hence you will not miss any wake up. By running rt_cond_wait in a while loop, you cause your thread to go to sleep in case of spurious wake up.
Signature · 1 line
Gilles Chanteperdrix.