1 /* This test is designed to test thread termination on a thread suspended on a block
2    memory pool.  */
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 static unsigned long   thread_1_counter =  0;
10 static TX_THREAD       thread_1;
11 
12 
13 static TX_BLOCK_POOL   pool_0;
14 
15 
16 /* Define thread prototypes.  */
17 
18 static void    thread_0_entry(ULONG thread_input);
19 static void    thread_1_entry(ULONG thread_input);
20 
21 
22 /* Prototype for test control return.  */
23 void  test_control_return(UINT status);
24 
25 
26 /* Define what the initial system looks like.  */
27 
28 #ifdef CTEST
test_application_define(void * first_unused_memory)29 void test_application_define(void *first_unused_memory)
30 #else
31 void    threadx_block_memory_thread_terminate_application_define(void *first_unused_memory)
32 #endif
33 {
34 
35 UINT    status;
36 CHAR    *pointer;
37 
38 
39     /* Put first available memory address into a character pointer.  */
40     pointer =  (CHAR *) first_unused_memory;
41 
42     /* Put system definition stuff in here, e.g. thread creates and other assorted
43        create information.  */
44 
45     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
46             pointer, TEST_STACK_SIZE_PRINTF,
47             17, 17, 100, TX_AUTO_START);
48     pointer = pointer + TEST_STACK_SIZE_PRINTF;
49 
50     /* Check status.  */
51     if (status != TX_SUCCESS)
52     {
53 
54         printf("Running Block Memory Thread Terminate Test.......................... ERROR #1\n");
55         test_control_return(1);
56     }
57 
58     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
59             pointer, TEST_STACK_SIZE_PRINTF,
60             17, 17, 100, TX_AUTO_START);
61     pointer = pointer + TEST_STACK_SIZE_PRINTF;
62 
63     /* Check status.  */
64     if (status != TX_SUCCESS)
65     {
66 
67         printf("Running Block Memory Thread Terminate Test.......................... ERROR #2\n");
68         test_control_return(1);
69     }
70 
71     /* Create block pool.  */
72     status =  tx_block_pool_create(&pool_0, "pool 0", 100, pointer, 320);
73     pointer = pointer + 320;
74 
75     /* Check status.  */
76     if (status != TX_SUCCESS)
77     {
78 
79         printf("Running Block Memory Thread Terminate 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 CHAR    *pointer_1;
93 CHAR    *pointer_2;
94 CHAR    *pointer_3;
95 
96 
97     /* Inform user.  */
98     printf("Running Block Memory Thread Terminate Test.......................... ");
99 
100     /* Allocate all blocks from the pool.  */
101     status =   tx_block_allocate(&pool_0, (VOID **) &pointer_1, TX_NO_WAIT);
102     status +=  tx_block_allocate(&pool_0, (VOID **) &pointer_2, TX_NO_WAIT);
103     status +=  tx_block_allocate(&pool_0, (VOID **) &pointer_3, TX_NO_WAIT);
104 
105     /* Increment the run counter.  */
106     thread_0_counter++;
107 
108     /* Check status.  */
109     if (status != TX_SUCCESS)
110     {
111 
112         /* Block memory error.  */
113         printf("ERROR #4\n");
114         test_control_return(1);
115     }
116 
117     /* Set all the memory of the blocks.  */
118     TX_MEMSET(pointer_1, (CHAR) 0xEF, 100);
119     TX_MEMSET(pointer_2, (CHAR) 0xEF, 100);
120     TX_MEMSET(pointer_3, (CHAR) 0xEF, 100);
121 
122 
123     /* Let other thread suspend on block pool.  */
124     tx_thread_relinquish();
125 
126     /* Terminate the other thread.  */
127     status =  tx_thread_terminate(&thread_1);
128 
129     /* Check status.  */
130     if ((status != TX_SUCCESS) || (thread_1.tx_thread_state != TX_TERMINATED) ||
131         (thread_1_counter != 0))
132     {
133 
134         /* Block memory error.  */
135         printf("ERROR #5\n");
136         test_control_return(1);
137     }
138 
139     /* Release all the blocks.  */
140     status =   tx_block_release(pointer_1);
141     status +=  tx_block_release(pointer_2);
142     status +=  tx_block_release(pointer_3);
143 
144     /* Check status.  */
145     if ((status != TX_SUCCESS) || (thread_1.tx_thread_state != TX_TERMINATED) ||
146         (thread_1_counter != 0))
147     {
148 
149         /* Block memory error.  */
150         printf("ERROR #6\n");
151         test_control_return(1);
152     }
153 
154     /* Successful test.  */
155     printf("SUCCESS!\n");
156     test_control_return(0);
157 }
158 
159 
thread_1_entry(ULONG thread_input)160 static void    thread_1_entry(ULONG thread_input)
161 {
162 
163 UINT    status;
164 CHAR    *pointer_1;
165 
166 
167     /* Attempt to get a block from the pool.  */
168     while(1)
169     {
170 
171         /* Allocate a block from the pool.  */
172         status =  tx_block_allocate(&pool_0, (VOID **) &pointer_1, 10);
173 
174         /* Determine if the allocate was successful.  */
175         if (status != TX_NO_MEMORY)
176             return;
177 
178         /* Set all the memory of the blocks.  */
179         TX_MEMSET(pointer_1, (CHAR) 0xEF, 100);
180 
181         /* Increment the thread counter.  */
182         thread_1_counter++;
183     }
184 }
185 
186