From: Kalle Pokki <kalle.pokki@iki.fi>
To: Rune Torgersen <runet@innovsys.com>, linuxppc-embedded@ozlabs.org
Subject: Re: wait_event and interrupts
Date: Wed, 21 Sep 2005 08:16:15 +0200 [thread overview]
Message-ID: <4330FAAF.1060704@iki.fi> (raw)
In-Reply-To: <DCEAAC0833DD314AB0B58112AD99B93B859463@ismail.innsys.innovsys.com>
Rune Torgersen wrote:
>Hi
>I have a driver that roughly does something like:
>
>int driver_read(int cs, int addr, void *buf, int len)
>{
> hw_done = 0;
> /* init_hw */
>
> if (!hw_done)
> {
> ret = wait_event_interruptible_timeout(inq, hw_done, TIMEOUT);
> if (ret == 0)
> {
> if (hw_done)
> goto hw_finished;
>
> return -EIO;
> }
> }
>hw_finished:
> return len;
>}
>
>static irqreturn_t myinterrupt(int irq, void * dev_id, struct pt_regs *
>regs)
>{
> hw_done = 1;
> schedule_work(&tqueue);
>
> return IRQ_HANDLED;
>}
>
>static void do_softint(void *private_)
>{
> wake_up_interruptible(&inq);
>
>}
>
>I have a problem however with this, because in about 10% of my cases,
>the interrupt triggers very fast, and ends up being served between the
>check for hw_done and the wait_event call. This cause the wait to
>timeout instead of getting waked up.
>
>Is there a better way of doing this?
>I do not want to do a busy wait, because the hardware can take up to
>several 100's of ms to return, but most often returns within 20us.
>
>
You seem to create an unnecessary race condition by checking the hw_done
variable in the read function. You can avoid this by changing it to
int driver_read(int cs, int addr, void *buf, int len)
{
hw_done = 0;
/* init_hw */
if (wait_event_interruptible_timeout(inq, hw_done, TIMEOUT))
return -ERESTARTSYS;
hw_done = 0;
/* copy the data to user space here */
return len;
}
The wait_event_interruptible_timeout() function checks the variable for
you. You still must make sure the read function does the right thing if
it is called simultaneously by more than one application.
next prev parent reply other threads:[~2005-09-21 6:27 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-09-20 17:56 wait_event and interrupts Rune Torgersen
2005-09-20 18:40 ` Jeff Angielski
2005-09-21 6:16 ` Kalle Pokki [this message]
-- strict thread matches above, loose matches on Subject: below --
2005-09-20 19:14 Rune Torgersen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4330FAAF.1060704@iki.fi \
--to=kalle.pokki@iki.fi \
--cc=linuxppc-embedded@ozlabs.org \
--cc=runet@innovsys.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).