events/events.c

Copyright (C) 2001-2006 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.3 2008/01/31 09:38:15 haraldkipp Added return statement in main to avoid warnings with latest GCC.

Revision 1.2 2006/07/21 09:07:19 haraldkipp Use dev/board.h to determine output device.

Revision 1.1 2005/07/23 13:54:39 haraldkipp First check in

This sample demonstrates the usage of Nut/OS event queues. It further shows how an event queue can be used as a mutual exclusion semaphore.

Two additional threads are started, one with a higher and another one with a lower priority than the main thread.

The results are printed on the debug device. Each thread prints the current action in one of three columns. The first column is used by the highest priority, the last column by the lowest priority thread.

00001 
00059 #include <cfg/os.h>
00060 
00061 #include <stdio.h>
00062 #include <io.h>
00063 
00064 #include <dev/board.h>
00065 
00066 #include <sys/thread.h>
00067 #include <sys/timer.h>
00068 #include <sys/event.h>
00069 
00070 /*
00071  * A global event queue, used as a mutex semaphore.
00072  */
00073 static HANDLE mutex;
00074 
00075 /*
00076  * High priority background thread. 
00077  */
00078 THREAD(High, arg)
00079 {
00080     NutThreadSetPriority(32);
00081     for(;;) {
00082         puts("Request");
00083         if (NutEventWait(&mutex, 2000)) {
00084             puts("Timeout");
00085         }
00086         else {
00087             puts("Acquired");
00088             NutSleep(2500);
00089             puts("Release");
00090             NutEventPost(&mutex);
00091         }
00092         NutSleep(1000);
00093     }
00094 }
00095 
00096 /*
00097  * Low priority background thread. 
00098  */
00099 THREAD(Low, arg)
00100 {
00101     NutThreadSetPriority(96);
00102     for(;;) {
00103         puts("                  Request");
00104         if (NutEventWait(&mutex, 3000)) {
00105             puts("                  Timeout");
00106         }
00107         else {
00108             puts("                  Acquired");
00109             NutSleep(3500);
00110             puts("                  Release");
00111             NutEventPost(&mutex);
00112         }
00113     }
00114 }
00115 
00116 /*
00117  * Main application routine. 
00118  */
00119 int main(void)
00120 {
00121     u_long baud = 115200;
00122 
00123     /*
00124      * Register the UART device, open it, assign stdout to it and set 
00125      * the baudrate.
00126      */
00127     NutRegisterDevice(&DEV_DEBUG, 0, 0);
00128     freopen(DEV_DEBUG_NAME, "w", stdout);
00129     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00130 
00131     /*
00132      * Print title.
00133      */
00134     puts("\nNut/OS Event Queue Demo");
00135     puts("High     Main     Low      ");
00136 
00137     /*
00138      * Post an initial event. This will put the queue into signaled 
00139      * state and immediately grant the next call to NutEventWait().
00140      */
00141     NutEventPost(&mutex);
00142 
00143     /*
00144      * Start two background threads.
00145      */
00146     NutThreadCreate("high", High, 0, 256);
00147     NutThreadCreate("low", Low, 0, 256);
00148 
00149     for(;;) {
00150         puts("         Request");
00151         if (NutEventWait(&mutex, 1000)) {
00152             puts("         Timeout");
00153         }
00154         else {
00155             puts("         Acquired");
00156             NutSleep(1500);
00157             puts("         Release");
00158             NutEventPost(&mutex);
00159         }
00160         NutSleep(1000);
00161     }
00162     return 0;
00163 }

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