threads/threads.c

Copyright (C) 2001-2004 by egnite Software GmbH. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

For additional information see http://www.ethernut.de/

$Log$ Revision 1.6 2008/01/31 09:38:15 haraldkipp Added return statement in main to avoid warnings with latest GCC.

Revision 1.5 2005/11/22 09:17:31 haraldkipp Replaced specific device names by generalized macros. Thread stack size increased to get it running on ARM CPUs.

Revision 1.4 2005/04/05 18:00:29 haraldkipp Make it work on the Gameboy Advance.

Revision 1.3 2004/09/10 10:31:06 haraldkipp Use newer AVR UART device driver

Revision 1.2 2004/09/08 10:18:23 haraldkipp For EB40A

Revision 1.1 2003/08/05 18:59:05 haraldkipp Release 3.3 update

Revision 1.7 2003/02/04 18:19:40 harald Version 3 released

Revision 1.6 2003/02/04 16:24:36 harald Adapted to version 3

Revision 1.5 2002/06/26 17:29:06 harald First pre-release with 2.4 stack

Revision 1.4 2002/06/04 19:13:06 harald *** empty log message ***

Revision 1.3 2002/05/08 16:02:33 harald First Imagecraft compilation

Revision 1.2 2001/12/04 16:45:40 harald Stack space increased

Revision 1.1 2001/06/28 18:43:13 harald Preview release

This sample demonstrates Nut/OS multithreading.

Each thread is started with 192 bytes of stack. This is very low and doesn't provide much space for local variables.

00001 
00086 #include <stdio.h>
00087 #include <io.h>
00088 
00089 #include <cfg/arch.h>
00090 #include <dev/board.h>
00091 
00092 #include <sys/thread.h>
00093 #include <sys/timer.h>
00094 
00095 /*
00096  * High priority thread.
00097  */
00098 THREAD(Thread1, arg)
00099 {
00100     /*
00101      * Endless loop in high priority thread.
00102      */
00103     NutThreadSetPriority(16);
00104     for (;;) {
00105         putchar('H');
00106         NutSleep(125);
00107     }
00108 }
00109 
00110 /*
00111  * Low priority thread.
00112  */
00113 THREAD(Thread2, arg)
00114 {
00115     /*
00116      * Endless loop in low priority thread.
00117      */
00118     NutThreadSetPriority(128);
00119     for (;;) {
00120         putchar('L');
00121         NutSleep(125);
00122     }
00123 }
00124 
00125 /*
00126  * Main application thread. 
00127  */
00128 int main(void)
00129 {
00130     u_long baud = 115200;
00131 
00132     /*
00133      * Register the UART device, open it, assign stdout to it and set 
00134      * the baudrate.
00135      */
00136     NutRegisterDevice(&DEV_UART, 0, 0);
00137     freopen(DEV_UART_NAME, "w", stdout);
00138     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00139 
00140     puts("\nThread Test");
00141 
00142     /*
00143      * Start two additional threads. All threads are started with 
00144      * priority 64.
00145      */
00146     NutThreadCreate("t1", Thread1, 0, 512);
00147     NutThreadCreate("t2", Thread2, 0, 512);
00148 
00149     /*
00150      * Endless loop in main thread.
00151      */
00152     for (;;) {
00153         putchar('M');
00154         NutSleep(125);
00155     }
00156     return 0;
00157 }

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/