public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
From: Bart Veer <bartv@ecoscentric.com>
To: Jim Bradleigh <jim.bradleigh1@btinternet.com>
Cc: ecos-discuss@ecos.sourceware.org
Subject: Re: [ECOS] cyg_scheduler_lock and DSR's
Date: Fri, 11 Sep 2009 20:37:00 -0000	[thread overview]
Message-ID: <pnr5ud2p7i.fsf@delenn.bartv.net> (raw)
In-Reply-To: <110701.39566.qm@web87005.mail.ird.yahoo.com> (message from Jim 	Bradleigh on Mon, 7 Sep 2009 16:02:25 +0000 (GMT))

>>>>> "Jim" == Jim Bradleigh <jim.bradleigh1@btinternet.com> writes:

    Jim> Is the cyg_scheduler_lock() call not supposed to prevent
    Jim> DSR's from running?

Yes, and no.    
    
    Jim> I have a alarm, attached to the realtime clock which when
    Jim> activated posts a message to a custom queue system. This
    Jim> queue is also posted to by user land code..

    Jim> Pushes to the queue are protected by a mutex. Additionally
    Jim> around the mutex I have the cyg_scheduler_lock() and
    Jim> cyg_scheduler_unlock() to prevent the DSR posting a message
    Jim> whilst inside the mutex operation.

    Jim> However I`m seeing asserts stating the mutex is already
    Jim> locked. A quick breakpoint and I can see that the DSR is
    Jim> still being called even after the lock() function..

The purpose of cyg_scheduler_lock() is to lock the scheduler, as the
name implies. That means no other threads get to run while the current
thread is runnable (on single-processor systems anyway, it gets a bit
more complicated on SMP systems). A side effect of locking the
scheduler is that DSRs cannot be allowed to run, because a DSR must
run to completion and may manipulate the scheduler.

However, if a thread locks the scheduler and then tries to claim a
mutex that is already owned by another thread, things get more
complicated. A strict interpretation of cyg_scheduler_lock() would
mean that control cannot transfer to that or any other thread, so it
would never get a chance to unlock the mutex, so the system would be
deadlocked.

Instead the correct interpretation of cyg_scheduler_lock() is that it
locks the scheduler while, and only while, the calling thread is
runnable. If the current thread becomes no longer runnable, e.g.
because it tries to lock a mutex that is already owned or because it
tries to wait on a synchronization object, then the current state of
the scheduler lock is saved away, the thread suspends as normal, and
other threads are allowed to run again. DSRs are allowed to run again
as well - there is no reason not to and the alternative would
introduce other possibilities for deadlock. Once the other thread
releases the mutex and your thread gets to run again, the state of the
scheduler lock gets restored so that the subsequent
cyg_scheduler_unlock() does the right thing.

I do not know the full details of your custom queue system. I suspect
there is no need for the mutex lock at all - locking the scheduler
provides all the required inter-thread synchronization to prevent
concurrent access to the queue object, as well as blocking DSRs. If a
mutex is still required for some reason, you'll probably want to lock
the mutex first and then the scheduler, not the other way around.

Bart

-- 
Bart Veer                                   eCos Configuration Architect
eCosCentric Limited    The eCos experts      http://www.ecoscentric.com/
Barnwell House, Barnwell Drive, Cambridge, UK.      Tel: +44 1223 245571
Registered in England and Wales: Reg No 4422071.
 >>>> Visit us at ESC-Boston  http://www.embedded.com/esc/boston <<<<
 >>>> Sep 22-23 on Stand 226  at Hynes Convention Center, Boston <<<<

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

  reply	other threads:[~2009-09-11 20:37 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-21 14:08 [ECOS] h8300 cannot find libgcc.a m mariga
2009-07-21 15:04 ` [ECOS] " John Dallaway
2009-07-22 12:28   ` m mariga
2009-07-22 13:58     ` John Dallaway
2009-07-23 21:32       ` m mariga
2009-07-24  8:34         ` John Dallaway
2009-07-28 14:40           ` m mariga
2009-08-09 23:11             ` m mariga
2009-08-17  7:42               ` John Dallaway
2009-09-04  5:54               ` [ECOS] How to set GDB Stub Options in order to run insight ? m mariga
2009-09-04  9:38                 ` [ECOS] " John Dallaway
2009-09-04 18:33                   ` m mariga
2009-09-07  9:40                     ` m mariga
2009-09-07 11:31                       ` John Dallaway
2009-09-07 16:10                         ` [ECOS] cyg_scheduler_lock and DSR's Jim Bradleigh
2009-09-11 20:37                           ` Bart Veer [this message]
2009-09-08 10:15                         ` [ECOS] Re: How to set GDB Stub Options in order to run insight ? m mariga
2009-09-28  9:54                           ` [ECOS] About h8000 eCos GDB-Stub m mariga
2009-09-30 10:06                             ` m mariga
2009-10-16 14:34                               ` [ECOS] How to connect RedBoot via Telnet running application m mariga
2009-10-16 14:42                                 ` Gary Thomas
     [not found]                                   ` <6FC88324D6C144EFB39FFE3CBF0E5B8D@masahiro>
2009-10-16 15:40                                     ` Gary Thomas
2009-10-16 16:11                                       ` m mariga
2009-10-16 16:43                                       ` m mariga
2009-10-16 16:54                                         ` Gary Thomas
2009-09-19  1:51                         ` [ECOS] Re: How to set GDB Stub Options in order to run insight ? m mariga

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=pnr5ud2p7i.fsf@delenn.bartv.net \
    --to=bartv@ecoscentric.com \
    --cc=ecos-discuss@ecos.sourceware.org \
    --cc=jim.bradleigh1@btinternet.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 read-only IMAP folder(s) and NNTP newsgroup(s).