1 /* This test is designed to test the mutex suspension and another thread resuming the
2 higher priority thread by doing a mutex put. Higher-priority thread should preempt. */
3
4 #include <stdio.h>
5 #include "tx_api.h"
6
7 static unsigned long thread_0_counter = 0;
8 static TX_THREAD thread_0;
9
10 static unsigned long thread_1_counter = 0;
11 static TX_THREAD thread_1;
12
13
14 static TX_MUTEX mutex_0;
15
16
17 /* Define thread prototypes. */
18
19 static void thread_0_entry(ULONG thread_input);
20 static void thread_1_entry(ULONG thread_input);
21
22
23 /* Prototype for test control return. */
24 void test_control_return(UINT status);
25
26
27 /* Define what the initial system looks like. */
28
29 #ifdef CTEST
test_application_define(void * first_unused_memory)30 void test_application_define(void *first_unused_memory)
31 #else
32 void threadx_mutex_preemption_application_define(void *first_unused_memory)
33 #endif
34 {
35
36 UINT status;
37 CHAR *pointer;
38
39
40 /* Put first available memory address into a character pointer. */
41 pointer = (CHAR *) first_unused_memory;
42
43 /* Put system definition stuff in here, e.g. thread creates and other assorted
44 create information. */
45
46 status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
47 pointer, TEST_STACK_SIZE_PRINTF,
48 16, 16, 100, TX_AUTO_START);
49 pointer = pointer + TEST_STACK_SIZE_PRINTF;
50
51 /* Check for status. */
52 if (status != TX_SUCCESS)
53 {
54
55 printf("Running Mutex Put with Preemption Test.............................. ERROR #1\n");
56 test_control_return(1);
57 }
58
59 status = tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
60 pointer, TEST_STACK_SIZE_PRINTF,
61 15, 15, 100, TX_DONT_START);
62 pointer = pointer + TEST_STACK_SIZE_PRINTF;
63
64 /* Check for status. */
65 if (status != TX_SUCCESS)
66 {
67
68 printf("Running Mutex Put with Preemption Test.............................. ERROR #2\n");
69 test_control_return(1);
70 }
71
72 /* Create a mutex. */
73 status = tx_mutex_create(&mutex_0, "mutex 0", TX_NO_INHERIT);
74
75 /* Check for status. */
76 if (status != TX_SUCCESS)
77 {
78
79 printf("Running Mutex Put with Preemption Test.............................. ERROR #3\n");
80 test_control_return(1);
81 }
82 }
83
84
85
86 /* Define the test threads. */
87
thread_0_entry(ULONG thread_input)88 static void thread_0_entry(ULONG thread_input)
89 {
90
91 UINT status;
92
93
94 /* Inform user. */
95 printf("Running Mutex Put with Preemption Test.............................. ");
96
97 /* Increment thread 0 counter. */
98 thread_0_counter++;
99
100 /* Get the mutex. */
101 status = tx_mutex_get(&mutex_0, TX_NO_WAIT);
102
103 /* Should be successful. */
104 if (status != TX_SUCCESS)
105 {
106
107 /* Mutex error. */
108 printf("ERROR #4\n");
109 test_control_return(1);
110 }
111
112 /* Now resume the higher priority thread to cause suspension. */
113 tx_thread_resume(&thread_1);
114
115 /* The other thread should now be suspended on the mutex. */
116 if (thread_1_counter != 1)
117 {
118
119 /* Mutex error. */
120 printf("ERROR #5\n");
121 test_control_return(1);
122 }
123
124 /* Release the mutex, this should cause the other thread
125 to preempt. */
126 status = tx_mutex_put(&mutex_0);
127
128 /* Check status and run counter of other thread. */
129 if ((status != TX_SUCCESS) || (thread_1_counter != 2))
130 {
131
132 /* Mutex error. */
133 printf("ERROR #6\n");
134 test_control_return(1);
135 }
136 else
137 {
138
139 /* Successful test. */
140 printf("SUCCESS!\n");
141 test_control_return(0);
142 }
143 }
144
145
thread_1_entry(ULONG thread_input)146 static void thread_1_entry(ULONG thread_input)
147 {
148
149 UINT status;
150
151 /* Increment thread run counter. */
152 thread_1_counter++;
153
154 /* Suspend on the mutex. */
155 status = tx_mutex_get(&mutex_0, TX_WAIT_FOREVER);
156
157 /* Did we get the right status? */
158 if (status == TX_SUCCESS)
159 thread_1_counter++;
160 }
161
162