1 /* This test is designed to test the semaphore suspension and another thread resuming the
2    higher priority thread by doing a semaphore 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_SEMAPHORE    semaphore_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 
25 void  test_control_return(UINT status);
26 
27 
put_notify(TX_SEMAPHORE * semaphore_ptr)28 static void   put_notify(TX_SEMAPHORE *semaphore_ptr)
29 {
30 
31     /* Don't need to do anything in here...  */
32 }
33 
34 
35 /* Define what the initial system looks like.  */
36 
37 #ifdef CTEST
test_application_define(void * first_unused_memory)38 void test_application_define(void *first_unused_memory)
39 #else
40 void    threadx_semaphore_preemption_application_define(void *first_unused_memory)
41 #endif
42 {
43 
44 UINT    status;
45 CHAR    *pointer;
46 
47 
48     /* Put first available memory address into a character pointer.  */
49     pointer =  (CHAR *) first_unused_memory;
50 
51     /* Put system definition stuff in here, e.g. thread creates and other assorted
52        create information.  */
53 
54     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
55             pointer, TEST_STACK_SIZE_PRINTF,
56             16, 16, 100, TX_AUTO_START);
57     pointer = pointer + TEST_STACK_SIZE_PRINTF;
58 
59     /* Check for status.  */
60     if (status != TX_SUCCESS)
61     {
62 
63         printf("Running Semaphore Preemption Test................................... ERROR #1\n");
64         test_control_return(1);
65     }
66 
67     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
68             pointer, TEST_STACK_SIZE_PRINTF,
69             15, 15, 100, TX_AUTO_START);
70     pointer = pointer + TEST_STACK_SIZE_PRINTF;
71 
72     /* Check for status.  */
73     if (status != TX_SUCCESS)
74     {
75 
76         printf("Running Semaphore Preemption Test................................... ERROR #2\n");
77         test_control_return(1);
78     }
79 
80     /* Create a semaphore with an initial count of 0.  */
81     status =  tx_semaphore_create(&semaphore_0, "semaphore 0", 0);
82 
83     /* Check for status.  */
84     if (status != TX_SUCCESS)
85     {
86 
87         printf("Running Semaphore Preemption Test................................... ERROR #3\n");
88         test_control_return(1);
89     }
90 
91     /* Setup the semaphore notify callback.  */
92     status =  tx_semaphore_put_notify(&semaphore_0, put_notify);
93 
94 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
95 
96     /* Check for status.  */
97     if (status != TX_SUCCESS)
98     {
99 
100         printf("Running Semaphore Preemption Test................................... ERROR #4\n");
101         test_control_return(1);
102     }
103 #else
104 
105     /* Check for status.  */
106     if (status != TX_FEATURE_NOT_ENABLED)
107     {
108 
109         printf("Running Semaphore Preemption Test................................... ERROR #5\n");
110         test_control_return(1);
111     }
112 
113 #endif
114 
115 }
116 
117 
118 
119 /* Define the test threads.  */
120 
thread_0_entry(ULONG thread_input)121 static void    thread_0_entry(ULONG thread_input)
122 {
123 
124 UINT    status;
125 
126 
127     /* Inform user.  */
128     printf("Running Semaphore Preemption Test................................... ");
129 
130     /* Increment thread 0 counter.  */
131     thread_0_counter++;
132 
133     /* The other thread should now be suspended on the semaphore.  */
134     if (thread_1_counter != 1)
135     {
136 
137         /* Semaphore error.  */
138         printf("ERROR #6\n");
139         test_control_return(1);
140     }
141 
142     /* Place an instance on the semaphore, this should cause the other thread
143        to preempt.  */
144     status =  tx_semaphore_put(&semaphore_0);
145 
146     /* Check status and run counter of other thread.  */
147     if ((status != TX_SUCCESS) || (thread_1_counter != 2))
148     {
149 
150         /* Semaphore error.  */
151         printf("ERROR #7\n");
152         test_control_return(1);
153     }
154     else
155     {
156 
157         /* Successful test.  */
158         printf("SUCCESS!\n");
159         test_control_return(0);
160     }
161 }
162 
163 
thread_1_entry(ULONG thread_input)164 static void    thread_1_entry(ULONG thread_input)
165 {
166 
167 UINT    status;
168 
169     /* Increment thread run counter.  */
170     thread_1_counter++;
171 
172     /* Suspend on the semaphore. */
173     status =  tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
174 
175     /* Did we get the right status?  */
176     if (status == TX_SUCCESS)
177         thread_1_counter++;
178 }
179 
180