cmsis rtos demo
Revision 1:8289e2db9449, committed 2013-01-06
- Comitter:
- cnhzcy14
- Date:
- Sun Jan 06 15:13:50 2013 +0000
- Parent:
- 0:9a66107976a4
- Commit message:
- comments
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sun Jan 06 14:34:08 2013 +0000
+++ b/main.cpp Sun Jan 06 15:13:50 2013 +0000
@@ -1,3 +1,18 @@
+/******************************************************************************
+ * DESCRIPTION:
+ * Example code for using CMSIS RTOS Signal. The main thread
+ * creates three threads. Two of those threads increment a "count" variable,
+ * while the third thread watches the value of "count". When "count"
+ * reaches a predefined limit, the waiting thread is signaled by one of the
+ * incrementing threads. The waiting thread "awakens" and then modifies
+ * count. The program continues until the incrementing threads reach
+ * TCOUNT. The main program prints the final value of count.
+ * SOURCE: Adapted from example code in "Pthreads Programming", B. Nichols
+ * et al. O'Reilly and Associates.
+ * MODIFICATOR: (Simon) CHENG Ye
+ * LAST REVISED: 06/JAN/2013
+ ******************************************************************************/
+
#include "mbed.h"
#include "cmsis_os.h"
@@ -33,10 +48,11 @@
count++;
/*
- Check the value of count and signal waiting thread when condition is
- reached. Note that this occurs while mutex is locked.
+ * Check the value of count and signal waiting thread when condition is
+ * reached. Note that this occurs while mutex is locked.
*/
- if (count == COUNT_LIMIT) {
+ if (count == COUNT_LIMIT)
+ {
printf("inc_count(): thread %ld, count = %d Threshold reached. ",
my_id, count);
osSignalSet(id, 0x1);
@@ -60,17 +76,25 @@
printf("Starting watch_count(): thread %ld\n", my_id);
- //osMutexWait(count_mutex, osWaitForever);
- while (count < COUNT_LIMIT) {
- //osMutexRelease(count_mutex);
+ /*
+ * Lock mutex and wait for signal. Note that the pthread_cond_wait routine
+ * will automatically and atomically unlock mutex while it waits.
+ * Also, note that if COUNT_LIMIT is reached before this routine is run by
+ * the waiting thread, the loop will be skipped to prevent pthread_cond_wait
+ * from never returning.
+ */
+ osMutexWait(count_mutex, osWaitForever);
+ while (count < COUNT_LIMIT)
+ {
+ osMutexRelease(count_mutex);
printf("watch_count(): thread %ld Count= %d. Going into wait...\n", my_id,count);
osSignalWait(0x1, osWaitForever);
- //osMutexWait(count_mutex, osWaitForever);
+ osMutexWait(count_mutex, osWaitForever);
printf("watch_count(): thread %ld Condition signal received. Count= %d\n", my_id,count);
printf("watch_count(): thread %ld Updating the value of count...\n", my_id);
count += 125;
printf("watch_count(): thread %ld count now = %d.\n", my_id, count);
- //osMutexRelease(count_mutex);
+ osMutexRelease(count_mutex);
}
printf("watch_count(): thread %ld Unlocking mutex.\n", my_id);
//osMutexRelease(count_mutex);
@@ -93,29 +117,17 @@
long t1=1, t2=2, t3=3;
osThreadId id;
-
- /* Initialize mutex and condition variable objects */
+ /* Initialize mutex objects */
count_mutex = osMutexCreate(osMutex(count_mutex));
- /* For portability, explicitly create threads in a joinable state */
-
id = osThreadCreate(osThread(th0), (void *)t1);
-
thread_data_array[0].id = id;
thread_data_array[0].my_id = t2;
- thread_data_array[1].id = id;
+ thread_data_array[1].id = id;
thread_data_array[1].my_id = t3;
-
osThreadCreate(osThread(th1), (void *)&thread_data_array[0]);
-
-
osThreadCreate(osThread(th2), (void *)&thread_data_array[1]);
- /* Wait for all threads to complete */
-
printf ("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n",
NUM_THREADS, count);
-
-
-
-}
\ No newline at end of file
+}
Ye Cheng