/* 线程1入口 */ static void thread1_entry(void* parameter) { int i; char *block; while(1) { for (i = 0; i < 48; i++) { /* 申请内存块 */ rt_kprintf("allocate No.%d\n", i); if (ptr[i] == RT_NULL) { ptr[i] = rt_mp_alloc(&mp, RT_WAITING_FOREVER); } } /* 继续申请一个内存块,因为已经没有内存块,线程应该被挂起 */ block = rt_mp_alloc(&mp, RT_WAITING_FOREVER); rt_kprintf("allocate the block mem\n"); /* 释放这个内存块 */ rt_mp_free(block); block = RT_NULL; } }
void rt_thread_free_sig(rt_thread_t tid) { rt_base_t level; struct siginfo_node *si_list; rt_sighandler_t *sig_vectors; level = rt_hw_interrupt_disable(); si_list = (struct siginfo_node*)tid->si_list; tid->si_list = RT_NULL; sig_vectors = tid->sig_vectors; tid->sig_vectors = RT_NULL; rt_hw_interrupt_enable(level); if (si_list) { struct rt_slist_node *node; struct siginfo_node *si_node; dbg_log(DBG_LOG, "free signal info list\n"); node = &(si_list->list); do { si_node = rt_slist_entry(node, struct siginfo_node, list); rt_mp_free(si_node); node = node->next; } while (node); } if (sig_vectors) { RT_KERNEL_FREE(sig_vectors); } }
/* * ISR for DMA mode Tx */ void rt_hw_serial_dma_tx_isr(rt_device_t device) { rt_uint32_t level; struct stm32_serial_data_node* data_node; struct stm32_serial_device* uart = (struct stm32_serial_device*) device->user_data; /* DMA mode receive */ RT_ASSERT(device->flag & RT_DEVICE_FLAG_DMA_TX); /* get the first data node */ data_node = uart->dma_tx->list_head; RT_ASSERT(data_node != RT_NULL); /* invoke call to notify tx complete */ if (device->tx_complete != RT_NULL) device->tx_complete(device, data_node->data_ptr); /* disable interrupt */ level = rt_hw_interrupt_disable(); /* remove list head */ uart->dma_tx->list_head = data_node->next; if (uart->dma_tx->list_head == RT_NULL) /* data link empty */ uart->dma_tx->list_tail = RT_NULL; /* enable interrupt */ rt_hw_interrupt_enable(level); /* release data node memory */ rt_mp_free(data_node); if (uart->dma_tx->list_head != RT_NULL) { /* transmit next data node */ rt_serial_enable_dma(uart->dma_tx->dma_channel, (rt_uint32_t)uart->dma_tx->list_head->data_ptr, uart->dma_tx->list_head->data_size); } else { /* no data to be transmitted, disable DMA */ DMA_Cmd(uart->dma_tx->dma_channel, DISABLE); } }
void rt_thread_handle_sig(rt_bool_t clean_state) { rt_base_t level; rt_thread_t tid = rt_thread_self(); struct siginfo_node *si_node; level = rt_hw_interrupt_disable(); while (tid->sig_pending & tid->sig_mask) { int signo, error; rt_sighandler_t handler; si_node = (struct siginfo_node *)tid->si_list; if (!si_node) break; /* remove this sig info node from list */ if (si_node->list.next == RT_NULL) tid->si_list = RT_NULL; else tid->si_list = (void*)rt_slist_entry(si_node->list.next, struct siginfo_node, list); signo = si_node->si.si_signo; handler = tid->sig_vectors[signo]; rt_hw_interrupt_enable(level); dbg_log(DBG_LOG, "handle signal: %d, handler 0x%08x\n", signo, handler); if (handler) handler(signo); level = rt_hw_interrupt_disable(); tid->sig_pending &= ~sig_mask(signo); error = si_node->si.si_errno; rt_mp_free(si_node); /* release this siginfo node */ /* set errno in thread tcb */ tid->error = error; } /* whether clean signal status */ if (clean_state == RT_TRUE) tid->stat &= ~RT_THREAD_STAT_SIGNAL; rt_hw_interrupt_enable(level); }
/* 线程2入口,线程2的优先级比线程1低,应该线程1先获得执行。*/ static void thread2_entry(void *parameter) { int i; while(1) { rt_kprintf("try to release block\n"); for (i = 0 ; i < 48; i ++) { /* 释放所有分配成功的内存块 */ if (ptr[i] != RT_NULL) { rt_kprintf("release block %d\n", i); rt_mp_free(ptr[i]); ptr[i] = RT_NULL; } } /* 休眠10个OS Tick */ rt_thread_delay(10); } }
/// Return an allocated memory block back to a specific memory pool osStatus osPoolFree(osPoolId pool_id, void *block) { rt_mp_free(block); return osOK; }
void sbuf_release(void* ptr) { rt_mp_free(ptr); }