1 /* This test is designed to test termination on thread suspended on memory byte pool.  */
2 
3 #include   <stdio.h>
4 #include   "tx_api.h"
5 
6 static unsigned long   thread_0_counter =  0;
7 static TX_THREAD       thread_0;
8 static unsigned long   thread_1_counter =  0;
9 static TX_THREAD       thread_1;
10 
11 
12 static TX_BYTE_POOL   pool_0;
13 
14 
15 /* Define thread prototypes.  */
16 
17 static void    thread_0_entry(ULONG thread_input);
18 static void    thread_1_entry(ULONG thread_input);
19 
20 
21 /* Prototype for test control return.  */
22 void  test_control_return(UINT status);
23 
24 
25 /* Define what the initial system looks like.  */
26 
27 #ifdef CTEST
test_application_define(void * first_unused_memory)28 void test_application_define(void *first_unused_memory)
29 #else
30 void    threadx_byte_memory_thread_terminate_application_define(void *first_unused_memory)
31 #endif
32 {
33 
34 UINT    status;
35 CHAR    *pointer;
36 
37 
38     /* Put first available memory address into a character pointer.  */
39     pointer =  (CHAR *) first_unused_memory;
40 
41     /* Put system definition stuff in here, e.g. thread creates and other assorted
42        create information.  */
43 
44     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
45             pointer, TEST_STACK_SIZE_PRINTF,
46             17, 17, 100, TX_AUTO_START);
47     pointer = pointer + TEST_STACK_SIZE_PRINTF;
48 
49     /* Check status.  */
50     if (status != TX_SUCCESS)
51     {
52 
53         printf("Running Byte Memory Thread Terminate Test........................... ERROR #1\n");
54         test_control_return(1);
55     }
56 
57     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
58             pointer, TEST_STACK_SIZE_PRINTF,
59             17, 17, 100, TX_AUTO_START);
60     pointer = pointer + TEST_STACK_SIZE_PRINTF;
61 
62     /* Check status.  */
63     if (status != TX_SUCCESS)
64     {
65 
66         printf("Running Byte Memory Thread Terminate Test........................... ERROR #2\n");
67         test_control_return(1);
68     }
69 
70     /* Create byte pools 0 and 1.  */
71     status =  tx_byte_pool_create(&pool_0, "pool 0", pointer, 108);
72     pointer = pointer + 108;
73 
74     /* Check status.  */
75     if (status != TX_SUCCESS)
76     {
77 
78         printf("Running Byte Memory Thread Terminate Test........................... ERROR #3\n");
79         test_control_return(1);
80     }
81 }
82 
83 
84 
85 /* Define the test threads.  */
86 
thread_0_entry(ULONG thread_input)87 static void    thread_0_entry(ULONG thread_input)
88 {
89 
90 UINT    status;
91 CHAR    *pointer;
92 
93 
94     /* Inform user.  */
95     printf("Running Byte Memory Thread Terminate Test........................... ");
96 
97     /* Increment the thread counter.  */
98     thread_0_counter++;
99 
100     /* Allocate memory from the pool.  Only one block of this size will fit.   */
101     status = tx_byte_allocate(&pool_0, (VOID **) &pointer, 60, TX_NO_WAIT);
102 
103     /* Check status.  */
104     if (status != TX_SUCCESS)
105     {
106 
107         /* Byte memory error.  */
108         printf("ERROR #4\n");
109         test_control_return(1);
110     }
111 
112     /* Let other thread suspend on byte pool.  */
113     tx_thread_relinquish();
114 
115     /* Terminate the other thread.  */
116     status =  tx_thread_terminate(&thread_1);
117 
118     /* Check status.  */
119     if (status != TX_SUCCESS)
120     {
121 
122         /* Byte memory error.  */
123         printf("ERROR #5\n");
124         test_control_return(1);
125     }
126 
127     /*  Release block back.  */
128     status =  tx_byte_release(pointer);
129 
130     /* Check status.  */
131     if (status != TX_SUCCESS)
132     {
133 
134         /* Byte memory error.  */
135         printf("ERROR #6\n");
136         test_control_return(1);
137     }
138 
139     /* Allocate memory from the pool.  Only one block of this size will fit.   */
140     status = tx_byte_allocate(&pool_0, (VOID **) &pointer, 60, TX_NO_WAIT);
141 
142     /* Check status.  */
143     if ((status != TX_SUCCESS) || (thread_1_counter != 0))
144     {
145 
146         /* Byte memory error.  */
147         printf("ERROR #7\n");
148         test_control_return(1);
149     }
150 
151     /* Successful test.  */
152     printf("SUCCESS!\n");
153     test_control_return(0);
154 }
155 
156 
thread_1_entry(ULONG thread_input)157 static void    thread_1_entry(ULONG thread_input)
158 {
159 
160 UINT    status;
161 CHAR    *pointer;
162 
163     while(1)
164     {
165 
166         /* Allocate memory from the pool.  */
167         status = tx_byte_allocate(&pool_0, (VOID **) &pointer, 60, TX_WAIT_FOREVER);
168 
169         /* Should never get here!  */
170         if (status != TX_NO_MEMORY)
171             return;
172 
173         /* Increment the thread counter.  */
174         thread_1_counter++;
175     }
176 }
177 
178