events/events.c

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

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