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/
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 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 }