[Contiki-developers] preemptive multitasking

121 views
Skip to first unread message

Michal Ulianko

unread,
Nov 12, 2011, 8:24:42 PM11/12/11
to contiki-d...@lists.sourceforge.net
Hi,

I'm porting Contiki to energymicro efm32g (cortex-m3) platform and I have few questions about preemptive multitasking.
There is mention about mtarch_pstart() and mtarch_pstop() functions in the documentation, but they aren't called anywhere. What they should do?
Is it really a good idea to allocate stack statically in mtarch_thread?
How should I treat stack overflow? (Detecting it with cortex-m3's MPU should not be a problem.)

Anyway, how is preemptive multitasking supposed to work with Contiki's protothread-process system? From the example 'multi-threading.c' from the documentation I guess it is intended to put this whole "multitasking" to one protothread-process. And how to implement preemption? My idea is that every time this protothread is run it could run some sort of scheduler, run one task until it yield or some preemption-timer expires, then the protothread itself would yield to let other contiki protothread-processes process some events.
Actually I'm really a newbie to Contiki...

Regards,
Michal Ulianko

Muhammad Omer Farooq

unread,
Nov 13, 2011, 1:22:04 AM11/13/11
to Contiki developer mailing list
Contiki does not support preemptive multitasking in a true sense. Actually, it is a event based OS and events are fired to the concerned processes in a FIFO manner. The event handlers run to completion i.e., once scheduled they are not preemptive by the OS. In Contiki, multithreading is implemented on top of event driven kernel, as events run to completion, therefore OS does not preemptive a thread once scheduled. To give an illusion of preemptive multitasking threads must explicitly yield or wait for some event and you need to write a scheduler to schedule your threads.
 
 
Best regards,
Omer 

--- On Sun, 11/13/11, Michal Ulianko <michal....@gmail.com> wrote:
-----Inline Attachment Follows-----

------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1

-----Inline Attachment Follows-----

_______________________________________________
Contiki-developers mailing list
Contiki-d...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/contiki-developers

David Kopf

unread,
Nov 13, 2011, 7:51:33 AM11/13/11
to contiki-d...@lists.sourceforge.net
Have you seen http://dak664.github.com/contiki-doxygen/a01625.html? I have
not used it and most micros do not have enough RAM for more than one stack.
Rtimer interrupts might perhaps give a similar function; look at how
contikimac interrupts at 8Hz into a protothread. One must then make certain
that any routines called are reentrant if the interrupt could have occurred
in them.

Hi,

Regards,
Michal Ulianko


------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1

Michal Ulianko

unread,
Nov 13, 2011, 10:20:58 AM11/13/11
to Contiki developer mailing list
Yes, I've read that. And I now acknowledge the Contiki is protothread, FIFO, event based OS.

But a question remain: What are functions mtarch_pstart() and mtarch_pstop() supposed to mean? They are mentioned in http://dak664.github.com/contiki-doxygen/a01626.html and they are declared in mt.h.
They look like either remains of some idea or inception of some idea.

2011/11/13 David Kopf <dak...@embarqmail.com>

Kasun Hewage

unread,
Nov 16, 2011, 10:52:47 PM11/16/11
to Contiki developer mailing list
Hi,

As I know preemptive multitasking is supported by Contiki.But I have never used it. Have look at a platform specific implementation ("mtarch.h" "mtarch,c") in http://contiki.git.sourceforge.net/git/gitweb.cgi?p=contiki/contiki;a=tree;f=cpu/msp430;hb=HEAD
--
Best Regards,
Kasun Hewage


Oliver Schmidt

unread,
Nov 17, 2011, 5:28:28 AM11/17/11
to Contiki developer mailing list
Hi,

Trying to clarify the obviously not so obvious ;-)

1. Contiki itself is built and uses protothreads. As they share a
single stack they for sure are non-preemptive.

2. Totally unrelated to the rest of Contiki there's the
multi-threading library. Note that the name says nothing about
preemtion! That library defines an interface for threads with
individual stacks (in contrast to the shared stack protothreads).
Those threads can either be scheduled in an non-preemptive fashion
(relying on the threads calling mt_yield() _OR_ in a preemptive way.
The docs referenced below say:

"Additionally, two functions for controlling the preemption (if any)
must be implemented: mtarch_pstart() and mtarch_pstop(). If no
preemption is used, these functions can be implemented as empty
functions."

So with the background above it should be clear that the two functions
are supposed to start / stop the preemptive scheduler. Usually that
would be some timer interrupt handler doing thread context switching.

According to my knowledge there's no "architecture support for
multi-threading" implementation that supports preemption. Therefore
the multi-threading example program I wrote presumes non-preemption:
http://contiki.git.sourceforge.net/git/gitweb.cgi?p=contiki/contiki;a=blob_plain;f=examples/multi-threading/multi-threading.c;hb=HEAD

One might ask what the usecases of non-preemptive scheduled threads
with individual stacks are. The example program shows one: Recursion.

Regards,
Oliver

> ------------------------------------------------------------------------------
> All the data continuously generated in your IT infrastructure
> contains a definitive record of customers, application performance,
> security threats, fraudulent activity, and more. Splunk takes this
> data and makes sense of it. IT sense. And common sense.
> http://p.sf.net/sfu/splunk-novd2d


> _______________________________________________
> Contiki-developers mailing list
> Contiki-d...@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/contiki-developers
>
>

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d

Reply all
Reply to author
Forward
0 new messages