Difference between revisions of "TOSThreads Tutorial"

From TinyOS Wiki
Jump to: navigation, search
(New page: This lesson demonstrates how to use the TOSThreads library. You will learn how to do the following: * Use the nesC API to create and manipulate both static and dynamic threads. * Use the C...)
 
Line 8: Line 8:
 
=Introduction=
 
=Introduction=
  
''TOSThreads'' is an attempt to combine the ease of a threaded programming model with the efficiency of a fully event-based OS. Unlike earlier threads packages designed for TinyOS, ''TOSThreads'' offers the following benefits:
+
TOSThreads is an attempt to combine the ease of a threaded programming model with the efficiency of a fully event-based OS. Unlike earlier threads packages designed for TinyOS, TOSThreads offers the following benefits:
 
# It supports fully-preemptive application-level threads.
 
# It supports fully-preemptive application-level threads.
 
# It does not need explicit continuation management, such as state variables between corresponding commands and events.
 
# It does not need explicit continuation management, such as state variables between corresponding commands and events.
 
# It does not violate TinyOS's concurrency model.
 
# It does not violate TinyOS's concurrency model.
# It requires minimal changes to the existing TinyOS code base. In addition, adding ''TOSThreads'' support to a new platform is a fairly easy process.
+
# It requires minimal changes to the existing TinyOS code base. In addition, adding TOSThreads support to a new platform is a fairly easy process.
 
# It offers both nesC and C APIs.
 
# It offers both nesC and C APIs.
  
In ''TOSThreads'', TinyOS runs inside a single high-priority kernel thread, while the application logic is implemented in user-level threads. User-level threads execute whenever the TinyOS core becomes idle. This approach is a natural extension to the existing TinyOS concurrency model: adding support for long-running computations while preserving the timing-sensitive nature of TinyOS itself.
+
In TOSThreads, TinyOS runs inside a single high-priority kernel thread, while the application logic is implemented in user-level threads. User-level threads execute whenever the TinyOS core becomes idle. This approach is a natural extension to the existing TinyOS concurrency model: adding support for long-running computations while preserving the timing-sensitive nature of TinyOS itself.
  
In this model, application threads access underlying TinyOS services using a kernel API of blocking system calls. The kernel API defines the set of TinyOS services provided to applications, such as radio, collection [TEP119], and so on. Each system call in the API is comprised of a thin blocking wrapper built on top of one of these services. The blocking wrapper is responsible for maintaining states across the non-blocking split-phase operations. ''TOSThreads'' allows system developers to re-define the kernel API by appropriately selecting an existing set or implementing their own blocking system call wrappers.
+
In this model, application threads access underlying TinyOS services using a kernel API of blocking system calls. The kernel API defines the set of TinyOS services provided to applications, such as radio, collection [TEP119], and so on. Each system call in the API is comprised of a thin blocking wrapper built on top of one of these services. The blocking wrapper is responsible for maintaining states across the non-blocking split-phase operations. TOSThreads allows system developers to re-define the kernel API by appropriately selecting an existing set or implementing their own blocking system call wrappers.
  
Please refer to TEP134 for more details on the ''TOSThreads'' implementation.
+
Please refer to TEP134 for more details on the TOSThreads implementation.
  
 
=The TOSThreads library=
 
=The TOSThreads library=
  
At the time of writing, ''TOSThreads'' supports the following platforms: telosb, micaZ, and mica2. And, it supports various generic split-phase operations, such as the Read interface and the SplitControl interface, and system services, such as radio, serial, external flash (both Block and Log abstractions), CTP (Collection Tree Protocol), and so on. You can find the code in [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/tos/lib/tosthreads tinyos-2.x/tos/lib/tosthreads] as described below.
+
At the time of writing, TOSThreads supports the following platforms: telosb, micaZ, and mica2. And, it supports various generic split-phase operations, such as the Read interface and the SplitControl interface, and system services, such as radio, serial, external flash (both Block and Log abstractions), CTP (Collection Tree Protocol), and so on. You can find the code in [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/tos/lib/tosthreads tinyos-2.x/tos/lib/tosthreads] as described below.
  
''TOSThreads'' system files are located in several subdirectories under [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/tos/lib/tosthreads tinyos-2.x/tos/lib/tosthreads].
+
TOSThreads system files are located in several subdirectories under [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/tos/lib/tosthreads tinyos-2.x/tos/lib/tosthreads].
 
# '''chips''': Some chip-specific files that shadow ''tinyos-2.x/tos/chips'' to add code such as the interrupt postamble.
 
# '''chips''': Some chip-specific files that shadow ''tinyos-2.x/tos/chips'' to add code such as the interrupt postamble.
 
# '''csystem''': Contain C API system files and the header file for different system services.
 
# '''csystem''': Contain C API system files and the header file for different system services.
Line 33: Line 33:
 
# '''sensorboards''': Contain blocking wrappers for telosb's onboard SHT11 sensors, and an universal sensor that generates a sine wave.
 
# '''sensorboards''': Contain blocking wrappers for telosb's onboard SHT11 sensors, and an universal sensor that generates a sine wave.
 
# '''system''': Contain nesC API system files and the blocking wrappers for different system services.
 
# '''system''': Contain nesC API system files and the blocking wrappers for different system services.
# '''types''': Define the structs used by ''TOSThreads'' system files.
+
# '''types''': Define the structs used by TOSThreads system files.
  
You can find example ''TOSThreads'' applications are in [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/apps/tosthreads tinyos-2.x/apps/tosthreads].
+
You can find example TOSThreads applications are in [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/apps/tosthreads tinyos-2.x/apps/tosthreads].
 +
 
 +
=Dynamic threads=
 +
 
 +
Other than running static threads as the example shows, nesC API can also create dynamic threads at run time. The nesC interface for creating and manipulating dynamic threads is ''DynamicThread'':
 +
 
 +
- ''DynamicThread.nc '' -
 +
{| border="0" cellpadding="0"
 +
|interface DynamicThread { ||
 +
|-
 +
|  command error_t create(tosthread_t* t, void (*start_routine)(void*), void* arg, ||
 +
|-
 +
|                                   void* arg, uint16_t stack_size); ||
 +
|-
 +
|  command error_t destroy(tosthread_t* t); ||
 +
|-
 +
|  command error_t pause(tosthread_t* t); ||
 +
|-
 +
|  command error_t resume(tosthread_t* t); ||
 +
|-
 +
|  command error_t sleep(uint32_t milli); ||
 +
|}
 +
 
 +
[http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/apps/tosthreads/apps/Blink_DynamicThreads Blink_DynamicThreads] is an example application that demostrates how to use dynamic threads.
 +
 
 +
=Synchronization primitives=
 +
 
 +
TOSThreads supports the following synchronization primitives:
 +
# Mutex: The interface file is [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/tos/lib/tosthreads/interfaces/Mutex.nc?view=markup tinyos-2.x/tos/lib/tosthreads/interfaces/Mutex.nc].
 +
# Semaphore: This is an implementation of counting semaphore. The interface file is [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/tos/lib/tosthreads/interfaces/Semaphore.nc?view=markup tinyos-2.x/tos/lib/tosthreads/interfaces/Semaphore.nc].
 +
# Barrier: All threads that call Barrier.block() are paused until ''v'' threads have made the block call. The interface file is [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/tos/lib/tosthreads/interfaces/Barrier.nc?view=markup tinyos-2.x/tos/lib/tosthreads/interfaces/Barrier.nc].
 +
# Condition variable: The interface file is The interface file is [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/tos/lib/tosthreads/interfaces/ConditionVariable.nc?view=markup tinyos-2.x/tos/lib/tosthreads/interfaces/ConditionVariable.nc].
 +
# Blocking reference counter: A thread can wait for the maintained counter to reach ''count''. The interface provides a way for other threads to increment and decrement the maintained counter. The interface is [http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/tos/lib/tosthreads/interfaces/ReferenceCounter.nc?view=markup tinyos-2.x/tos/lib/tosthreads/interfaces/ReferenceCounter.nc].
 +
 
 +
[http://tinyos.cvs.sourceforge.net/viewvc/tinyos/tinyos-2.x/apps/tosthreads/apps/Bounce Bounce] is an example application that demostrates how to use the barrier synchronization primitive.
  
 
=Related Documentation=
 
=Related Documentation=
  
 
* [http://www.tinyos.net/tinyos-2.1.0/doc/html/tep134.html TEP 134: The TOSThreads Thread Library]
 
* [http://www.tinyos.net/tinyos-2.1.0/doc/html/tep134.html TEP 134: The TOSThreads Thread Library]

Revision as of 00:36, 2 December 2008

This lesson demonstrates how to use the TOSThreads library. You will learn how to do the following:

  • Use the nesC API to create and manipulate both static and dynamic threads.
  • Use the C API to create and manipulate threads.
  • Add TOSThreads support to new system services.

Note: TOSThreads is part of TinyOS 2.1.

Introduction

TOSThreads is an attempt to combine the ease of a threaded programming model with the efficiency of a fully event-based OS. Unlike earlier threads packages designed for TinyOS, TOSThreads offers the following benefits:

  1. It supports fully-preemptive application-level threads.
  2. It does not need explicit continuation management, such as state variables between corresponding commands and events.
  3. It does not violate TinyOS's concurrency model.
  4. It requires minimal changes to the existing TinyOS code base. In addition, adding TOSThreads support to a new platform is a fairly easy process.
  5. It offers both nesC and C APIs.

In TOSThreads, TinyOS runs inside a single high-priority kernel thread, while the application logic is implemented in user-level threads. User-level threads execute whenever the TinyOS core becomes idle. This approach is a natural extension to the existing TinyOS concurrency model: adding support for long-running computations while preserving the timing-sensitive nature of TinyOS itself.

In this model, application threads access underlying TinyOS services using a kernel API of blocking system calls. The kernel API defines the set of TinyOS services provided to applications, such as radio, collection [TEP119], and so on. Each system call in the API is comprised of a thin blocking wrapper built on top of one of these services. The blocking wrapper is responsible for maintaining states across the non-blocking split-phase operations. TOSThreads allows system developers to re-define the kernel API by appropriately selecting an existing set or implementing their own blocking system call wrappers.

Please refer to TEP134 for more details on the TOSThreads implementation.

The TOSThreads library

At the time of writing, TOSThreads supports the following platforms: telosb, micaZ, and mica2. And, it supports various generic split-phase operations, such as the Read interface and the SplitControl interface, and system services, such as radio, serial, external flash (both Block and Log abstractions), CTP (Collection Tree Protocol), and so on. You can find the code in tinyos-2.x/tos/lib/tosthreads as described below.

TOSThreads system files are located in several subdirectories under tinyos-2.x/tos/lib/tosthreads.

  1. chips: Some chip-specific files that shadow tinyos-2.x/tos/chips to add code such as the interrupt postamble.
  2. csystem: Contain C API system files and the header file for different system services.
  3. interfaces: Contain nesC API interfaces.
  4. lib: Shadow some files in tinyos-2.x/tos/lib, and contain the blocking wrapper for CTP.
  5. platforms: Shadow some files in tinyos-2.x/tos/platforms.
  6. sensorboards: Contain blocking wrappers for telosb's onboard SHT11 sensors, and an universal sensor that generates a sine wave.
  7. system: Contain nesC API system files and the blocking wrappers for different system services.
  8. types: Define the structs used by TOSThreads system files.

You can find example TOSThreads applications are in tinyos-2.x/apps/tosthreads.

Dynamic threads

Other than running static threads as the example shows, nesC API can also create dynamic threads at run time. The nesC interface for creating and manipulating dynamic threads is DynamicThread:

- DynamicThread.nc -

interface DynamicThread {
  command error_t create(tosthread_t* t, void (*start_routine)(void*), void* arg,
                                   void* arg, uint16_t stack_size);
  command error_t destroy(tosthread_t* t);
  command error_t pause(tosthread_t* t);
  command error_t resume(tosthread_t* t);
  command error_t sleep(uint32_t milli);

Blink_DynamicThreads is an example application that demostrates how to use dynamic threads.

Synchronization primitives

TOSThreads supports the following synchronization primitives:

  1. Mutex: The interface file is tinyos-2.x/tos/lib/tosthreads/interfaces/Mutex.nc.
  2. Semaphore: This is an implementation of counting semaphore. The interface file is tinyos-2.x/tos/lib/tosthreads/interfaces/Semaphore.nc.
  3. Barrier: All threads that call Barrier.block() are paused until v threads have made the block call. The interface file is tinyos-2.x/tos/lib/tosthreads/interfaces/Barrier.nc.
  4. Condition variable: The interface file is The interface file is tinyos-2.x/tos/lib/tosthreads/interfaces/ConditionVariable.nc.
  5. Blocking reference counter: A thread can wait for the maintained counter to reach count. The interface provides a way for other threads to increment and decrement the maintained counter. The interface is tinyos-2.x/tos/lib/tosthreads/interfaces/ReferenceCounter.nc.

Bounce is an example application that demostrates how to use the barrier synchronization primitive.

Related Documentation