88#include <irq_offload.h>
99#include "test_kheap.h"
1010
11+ #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
12+ K_THREAD_STACK_DEFINE (tstack , STACK_SIZE );
13+ struct k_thread tdata ;
14+
1115K_HEAP_DEFINE (k_heap_test , HEAP_SIZE );
1216
1317#define ALLOC_SIZE_1 1024
@@ -24,6 +28,16 @@ static void tIsr_kheap_alloc_nowait(void *data)
2428 k_heap_free (& k_heap_test , p );
2529}
2630
31+ static void thread_alloc_heap (void * p1 , void * p2 , void * p3 )
32+ {
33+ k_timeout_t timeout = Z_TIMEOUT_MS (200 );
34+
35+ char * p = (char * )k_heap_alloc (& k_heap_test , ALLOC_SIZE_2 , timeout );
36+
37+ zassert_not_null (p , "k_heap_alloc operation failed" );
38+ k_heap_free (& k_heap_test , p );
39+ }
40+
2741/*test cases*/
2842
2943/**
@@ -105,9 +119,38 @@ void test_k_heap_free(void)
105119/**
106120 * @brief Validate allocation and free heap memory in isr context.
107121 *
122+ * @details The test validates k_heap_alloc() in isr context, the timeout
123+ * param should be K_NO_WAIT, because this situation isn't allow to wait.
124+ *
108125 * @ingroup kernel_heap_tests
109126 */
110127void test_kheap_alloc_in_isr_nowait (void )
111128{
112129 irq_offload ((irq_offload_routine_t )tIsr_kheap_alloc_nowait , NULL );
113130}
131+
132+ /**
133+ * @brief Validate the k_heap support wait between different threads.
134+ *
135+ * @details In main thread alloc a buffer from the heap, then run the
136+ * child thread. If there isn't enough space in the heap, the child thread
137+ * will wait timeout long until main thread free the buffer to heap.
138+ *
139+ * @ingroup kernel_heap_tests
140+ */
141+ void test_k_heap_alloc_pending (void )
142+ {
143+ k_tid_t tid = k_thread_create (& tdata , tstack , STACK_SIZE ,
144+ thread_alloc_heap , NULL , NULL , NULL ,
145+ K_PRIO_PREEMPT (5 ), 0 , K_NO_WAIT );
146+
147+ char * p = (char * )k_heap_alloc (& k_heap_test , ALLOC_SIZE_2 , K_NO_WAIT );
148+
149+ zassert_not_null (p , "k_heap_alloc operation failed" );
150+
151+ /* make the child thread run */
152+ k_msleep (1 );
153+ k_heap_free (& k_heap_test , p );
154+
155+ k_thread_join (tid , K_FOREVER );
156+ }
0 commit comments