Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /****************************************************************************** 00002 * DESCRIPTION: 00003 * Example code for using CMSIS RTOS Signal. The main thread 00004 * creates three threads. Two of those threads increment a "count" variable, 00005 * while the third thread watches the value of "count". When "count" 00006 * reaches a predefined limit, the waiting thread is signaled by one of the 00007 * incrementing threads. The waiting thread "awakens" and then modifies 00008 * count. The program continues until the incrementing threads reach 00009 * TCOUNT. The main program prints the final value of count. 00010 * SOURCE: Adapted from example code in "Pthreads Programming", B. Nichols 00011 * et al. O'Reilly and Associates. 00012 * MODIFICATOR: (Simon) CHENG Ye 00013 * LAST REVISED: 06/JAN/2013 00014 ******************************************************************************/ 00015 00016 #include "mbed.h" 00017 #include "cmsis_os.h" 00018 00019 #define NUM_THREADS 3 00020 #define TCOUNT 10 00021 #define COUNT_LIMIT 12 00022 00023 int count = 0; 00024 osMutexId count_mutex; 00025 osMutexDef(count_mutex); 00026 00027 struct thread_data 00028 { 00029 osThreadId id; 00030 long my_id; 00031 }; 00032 00033 struct thread_data thread_data_array[NUM_THREADS-1]; 00034 Serial debug(USBTX, USBRX); 00035 00036 void inc_count(void const *threadarg) 00037 { 00038 int i; 00039 long my_id; 00040 osThreadId id; 00041 struct thread_data *my_data; 00042 my_data = (struct thread_data *) threadarg; 00043 id = my_data->id; 00044 my_id = my_data->my_id; 00045 00046 for (i=0; i < TCOUNT; i++) { 00047 osMutexWait(count_mutex, osWaitForever); 00048 count++; 00049 00050 /* 00051 * Check the value of count and signal waiting thread when condition is 00052 * reached. Note that this occurs while mutex is locked. 00053 */ 00054 if (count == COUNT_LIMIT) 00055 { 00056 printf("inc_count(): thread %ld, count = %d Threshold reached. ", 00057 my_id, count); 00058 osSignalSet(id, 0x1); 00059 printf("Just sent signal.\n"); 00060 } 00061 printf("inc_count(): thread %ld, count = %d, unlocking mutex\n", 00062 my_id, count); 00063 osMutexRelease(count_mutex); 00064 00065 /* Do some work so threads can alternate on mutex lock */ 00066 osDelay(1000); 00067 } 00068 id = osThreadGetId(); 00069 osThreadTerminate(id); 00070 } 00071 00072 void watch_count(void const *t) 00073 { 00074 osThreadId id; 00075 long my_id = (long)t; 00076 00077 printf("Starting watch_count(): thread %ld\n", my_id); 00078 00079 /* 00080 * Lock mutex and wait for signal. Note that the pthread_cond_wait routine 00081 * will automatically and atomically unlock mutex while it waits. 00082 * Also, note that if COUNT_LIMIT is reached before this routine is run by 00083 * the waiting thread, the loop will be skipped to prevent pthread_cond_wait 00084 * from never returning. 00085 */ 00086 osMutexWait(count_mutex, osWaitForever); 00087 while (count < COUNT_LIMIT) 00088 { 00089 osMutexRelease(count_mutex); 00090 printf("watch_count(): thread %ld Count= %d. Going into wait...\n", my_id,count); 00091 osSignalWait(0x1, osWaitForever); 00092 osMutexWait(count_mutex, osWaitForever); 00093 printf("watch_count(): thread %ld Condition signal received. Count= %d\n", my_id,count); 00094 printf("watch_count(): thread %ld Updating the value of count...\n", my_id); 00095 count += 125; 00096 printf("watch_count(): thread %ld count now = %d.\n", my_id, count); 00097 osMutexRelease(count_mutex); 00098 } 00099 printf("watch_count(): thread %ld Unlocking mutex.\n", my_id); 00100 //osMutexRelease(count_mutex); 00101 id = osThreadGetId(); 00102 osThreadTerminate(id); 00103 } 00104 00105 void th0(void const *argument) {watch_count(argument);} 00106 osThreadDef(th0, osPriorityNormal, DEFAULT_STACK_SIZE); 00107 00108 void th1(void const *argument) {inc_count(argument);} 00109 osThreadDef(th1, osPriorityNormal, DEFAULT_STACK_SIZE); 00110 00111 void th2(void const *argument) {inc_count(argument);} 00112 osThreadDef(th2, osPriorityNormal, DEFAULT_STACK_SIZE); 00113 00114 int main(int argc, char *argv[]) 00115 { 00116 debug.baud(57600); 00117 long t1=1, t2=2, t3=3; 00118 osThreadId id; 00119 00120 /* Initialize mutex objects */ 00121 count_mutex = osMutexCreate(osMutex(count_mutex)); 00122 00123 id = osThreadCreate(osThread(th0), (void *)t1); 00124 thread_data_array[0].id = id; 00125 thread_data_array[0].my_id = t2; 00126 thread_data_array[1].id = id; 00127 thread_data_array[1].my_id = t3; 00128 osThreadCreate(osThread(th1), (void *)&thread_data_array[0]); 00129 osThreadCreate(osThread(th2), (void *)&thread_data_array[1]); 00130 00131 printf ("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n", 00132 NUM_THREADS, count); 00133 }
Generated on Thu Jul 28 2022 17:00:25 by
1.7.2