public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] How to know if scheduler has started?
@ 2013-10-31 16:25 Grant Edwards
  2013-11-01 17:04 ` Nick Garnett
  0 siblings, 1 reply; 4+ messages in thread
From: Grant Edwards @ 2013-10-31 16:25 UTC (permalink / raw)
  To: ecos-discuss

This question has come up many times in the past 10-12 years, but I've
never seen a real answer:

How do you tell if the scheduler has been started?

Specifically, how do you know when it's safe to attempt to lock a mutex?

Several people have suggested this snippet:

 scheduler_running = cyg_thread_self() != cyg_thread_idle_thread();

That seems to work when called from cyg_user_start() and from normal
threads, but it doesn't work when called from initialization code in 
a device driver.

So, I'll ask again:

how does one write code that knows to lock/unlock a mutex when called
from a normal user thread but not attempt the lock/unlock whcn called
from initialization code in cyg_user_start() or a device driver?

The function does not get called from ISR/DSR contexts: it is only
called during initialization (before the scheduler is started) and
from normal user threads.

OTOH, are you are allowed to lock/unlock mutexes from initialization
code?  Maybe I'm misremembering, but I thought that was forbidden?

-- 
Grant Edwards               grant.b.edwards        Yow! I was born in a
                                  at               Hostess Cupcake factory
                              gmail.com            before the sexual
                                                   revolution!


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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [ECOS] How to know if scheduler has started?
  2013-10-31 16:25 [ECOS] How to know if scheduler has started? Grant Edwards
@ 2013-11-01 17:04 ` Nick Garnett
  2013-11-02 16:26   ` [ECOS] " Grant Edwards
  0 siblings, 1 reply; 4+ messages in thread
From: Nick Garnett @ 2013-11-01 17:04 UTC (permalink / raw)
  To: Grant Edwards, ecos-discuss



On 31/10/13 16:24, Grant Edwards wrote:
> This question has come up many times in the past 10-12 years, but I've
> never seen a real answer:
> 
> How do you tell if the scheduler has been started?
> 
> Specifically, how do you know when it's safe to attempt to lock a mutex?
> 
> Several people have suggested this snippet:
> 
>  scheduler_running = cyg_thread_self() != cyg_thread_idle_thread();
> 
> That seems to work when called from cyg_user_start() and from normal
> threads, but it doesn't work when called from initialization code in 
> a device driver.
> 
> So, I'll ask again:
> 
> how does one write code that knows to lock/unlock a mutex when called
> from a normal user thread but not attempt the lock/unlock whcn called
> from initialization code in cyg_user_start() or a device driver?
> 
> The function does not get called from ISR/DSR contexts: it is only
> called during initialization (before the scheduler is started) and
> from normal user threads.
> 
> OTOH, are you are allowed to lock/unlock mutexes from initialization
> code?  Maybe I'm misremembering, but I thought that was forbidden?
> 

You are allowed to lock/unlock a mutex during initialization. That is
why the idle thread is set current throughout the init code, and why the
trick you mention above will work.

Obviously you need to avoid anything that might cause a thread to sleep,
and to leave any mutexes unlocked when you return.



-- 
Nick Garnett                                         Kernel Architect
eCosCentric Limited    http://www.eCosCentric.com    The eCos experts
Barnwell House, Barnwell Drive, Cambridge, UK.   Tel: +44 1223 245571
Registered in England and Wales:                      Reg No: 4422071

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ECOS] Re: How to know if scheduler has started?
  2013-11-01 17:04 ` Nick Garnett
@ 2013-11-02 16:26   ` Grant Edwards
  0 siblings, 0 replies; 4+ messages in thread
From: Grant Edwards @ 2013-11-02 16:26 UTC (permalink / raw)
  To: ecos-discuss

On 2013-11-01, Nick Garnett <nickg@calivar.com> wrote:
>
>> OTOH, are you are allowed to lock/unlock mutexes from initialization
>> code?  Maybe I'm misremembering, but I thought that was forbidden?
>
> You are allowed to lock/unlock a mutex during initialization. That is
> why the idle thread is set current throughout the init code, and why the
> trick you mention above will work.

Except the trick I mentioned above does _not_ work when called from
device driver intialization code.  I haven't tracked down which of the
two calls fails in that case, but one of them causes a segfault when
called from driver init code (but works fine when called from
cyg_user_start()).

-- 
Grant


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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ECOS] How to know if scheduler has started?
@ 2003-09-17 14:49 Grant Edwards
  0 siblings, 0 replies; 4+ messages in thread
From: Grant Edwards @ 2003-09-17 14:49 UTC (permalink / raw)
  To: ecos-discuss


How does driver or application code know if the scheduler has
been started?

I've been asked that by several customers who need to be able
to write functions that can be called from either the
cyg_user_start() context or from a thread contex, and I've
never been able to come up with an answer.

It looks like (Scheduler::current_thread != NULL) may be one
way in older versions of eCos -- but I can't find the
equivalent in 2.0 code.  Anyway, I was hoping there'd be
something kapi.h that could be called.

Can cyg_thread_self() be used to determine if the scheduler has
been started?  I've been looking at the eCos sources, but
haven't been able to figure out how cyg_thread_self() works,
since I can't find the implimentation of the self() method for
Cyg_Thread.  Where is it?

-- 
Grant Edwards
grante@visi.com

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-11-02 16:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-31 16:25 [ECOS] How to know if scheduler has started? Grant Edwards
2013-11-01 17:04 ` Nick Garnett
2013-11-02 16:26   ` [ECOS] " Grant Edwards
  -- strict thread matches above, loose matches on Subject: below --
2003-09-17 14:49 [ECOS] " Grant Edwards

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).