Threads for the Programmer, Events for the Machine

Alexander Bernauer, Kay Uwe Römer, Silvia Santini


Major motes operating systems like TinyOS or Contiki [1, 2] rely on an event-driven programming paradigm. While the use of events allows for limiting memory usage on resource-constrained motes, it may also hamper the development and debugging of applications, especially as their complexity increases [3]. Several authors also investigated the possibility of introducing threads to mote programming [4–6]. However, the proposed solutions all induce runtime overhead which is inherent to the thread paradigm. In contrast, protothreads[3] combine the benefits of both paradigms by providing thread semantics to the programmer while using events at runtime. This is achieved by an automatic code generation step performed by the C preprocessor. However, while using the C preprocessor guarantees portability across C compilers, it also introduces some limitations. For instance, certain C language constructs such as switch statements may not be used and values of local variables are not retained across context switches [3]. Furthermore, thread functions are not reentrant, blocking calls may only occur in the top-level thread functions, and debugging is performed in the generated code. To overcome these limitations, we propose to extend the protothreads abstraction by providing cooperative threads with blocking I/O, reentrant functions, and arbitrary nesting of function calls. This is achieved by a comprehensive compiler which translates thread-based code into efficient event-based code. In order to guarantee the efficiency of the generated code there are still some limitations, though. First, the exact number of threads must be known at compile time. Second, recursive functions must not invoke blocking functions. And third, function pointers must not be used to invoke functions that directly or indirectly invoke blocking functions. We argue, though, that these limitations do not severely affect programming of motes as these constructs are rarely used. Furthermore, the compiler can reliably detect any violations of those restrictions.
Original languageEnglish
Number of pages3
Publication statusPublished - 2010
Event7th European Conference on Wireless Sensor Networks - Coimbra, Portugal
Duration: 17.02.201019.02.2010
Conference number: 79931


Conference7th European Conference on Wireless Sensor Networks
Abbreviated titleEWSN 2010


Dive into the research topics of 'Threads for the Programmer, Events for the Machine'. Together they form a unique fingerprint.

Cite this