@@ -18,14 +18,13 @@ static int currently_in_critical_section = 0;
1818/* Enhanced Task A */
1919void task_a (void )
2020{
21- /* WORKAROUND: Printf not thread-safe in preemptive mode - minimize usage */
22-
2321 for (int i = 0 ; i < MAX_ITERATIONS ; i ++ ) {
2422 mo_sem_wait (binary_mutex );
2523
2624 /* === CRITICAL SECTION START === */
2725 if (currently_in_critical_section != 0 ) {
2826 critical_section_violations ++ ;
27+ printf ("Task A: VIOLATION detected at iteration %d\n" , i );
2928 }
3029 currently_in_critical_section = mo_task_id ();
3130
@@ -37,9 +36,11 @@ void task_a(void)
3736
3837 shared_counter = old_counter + 1 ;
3938 task_a_count ++ ;
39+ printf ("Task A: iteration %d, counter=%d\n" , i , shared_counter );
4040
4141 if (currently_in_critical_section != mo_task_id ()) {
4242 critical_section_violations ++ ;
43+ printf ("Task A: VIOLATION on exit at iteration %d\n" , i );
4344 }
4445 currently_in_critical_section = 0 ;
4546 /* === CRITICAL SECTION END === */
@@ -51,6 +52,8 @@ void task_a(void)
5152 mo_task_yield ();
5253 }
5354
55+ printf ("Task A completed all iterations\n" );
56+
5457 /* Keep running to prevent panic */
5558 while (1 ) {
5659 for (int i = 0 ; i < 10 ; i ++ )
@@ -61,18 +64,18 @@ void task_a(void)
6164/* Enhanced Task B */
6265void task_b (void )
6366{
64- /* WORKAROUND: Printf not thread-safe in preemptive mode - minimize usage */
65-
6667 for (int i = 0 ; i < MAX_ITERATIONS ; i ++ ) {
6768 /* Try non-blocking first */
6869 int32_t trylock_result = mo_sem_trywait (binary_mutex );
6970 if (trylock_result != ERR_OK ) {
71+ printf ("Task B: trylock failed, blocking at iteration %d\n" , i );
7072 mo_sem_wait (binary_mutex );
7173 }
7274
7375 /* === CRITICAL SECTION START === */
7476 if (currently_in_critical_section != 0 ) {
7577 critical_section_violations ++ ;
78+ printf ("Task B: VIOLATION detected at iteration %d\n" , i );
7679 }
7780 currently_in_critical_section = mo_task_id ();
7881
@@ -84,9 +87,11 @@ void task_b(void)
8487
8588 shared_counter = old_counter + 10 ;
8689 task_b_count ++ ;
90+ printf ("Task B: iteration %d, counter=%d\n" , i , shared_counter );
8791
8892 if (currently_in_critical_section != mo_task_id ()) {
8993 critical_section_violations ++ ;
94+ printf ("Task B: VIOLATION on exit at iteration %d\n" , i );
9095 }
9196 currently_in_critical_section = 0 ;
9297 /* === CRITICAL SECTION END === */
@@ -98,6 +103,8 @@ void task_b(void)
98103 mo_task_yield ();
99104 }
100105
106+ printf ("Task B completed all iterations\n" );
107+
101108 /* Keep running to prevent panic */
102109 while (1 ) {
103110 for (int i = 0 ; i < 10 ; i ++ )
@@ -108,15 +115,16 @@ void task_b(void)
108115/* Simple monitor task */
109116void monitor_task (void )
110117{
111- /* WORKAROUND: Printf not thread-safe - only print at end when tasks idle */
112-
113118 int cycles = 0 ;
114119
120+ printf ("Monitor: Starting test monitoring\n" );
121+
115122 while (cycles < 50 ) { /* Monitor for reasonable time */
116123 cycles ++ ;
117124
118125 /* Check if both tasks completed */
119126 if (task_a_count >= MAX_ITERATIONS && task_b_count >= MAX_ITERATIONS ) {
127+ printf ("Monitor: Both tasks completed, finalizing test\n" );
120128 break ;
121129 }
122130
@@ -125,11 +133,11 @@ void monitor_task(void)
125133 mo_task_yield ();
126134 }
127135
128- /* Wait a bit for tasks to fully idle */
136+ /* Wait a bit for tasks to fully complete */
129137 for (int i = 0 ; i < 50 ; i ++ )
130138 mo_task_yield ();
131139
132- /* Final report - safe to print when other tasks are idle */
140+ /* Final report */
133141 printf ("\n=== FINAL RESULTS ===\n" );
134142 printf ("Task A iterations: %d\n" , task_a_count );
135143 printf ("Task B iterations: %d\n" , task_b_count );
@@ -193,7 +201,9 @@ int32_t app_main(void)
193201 return false;
194202 }
195203
196- /* CRITICAL FIX: Printf hangs after task_spawn - remove all printf calls */
197- /* Tasks created: A=%d, B=%d, Monitor=%d, Idle=%d */
204+ printf ("Tasks created: A=%d, B=%d, Monitor=%d, Idle=%d\n" , task_a_id ,
205+ task_b_id , monitor_id , idle_id );
206+ printf ("Enabling preemptive scheduling mode\n" );
207+
198208 return true; /* Enable preemptive scheduling */
199209}
0 commit comments