static void dyn2_execute(void) { int i; tprio_t prio = chThdGetPriority(); /* Adding the WAs to the pool. */ for (i = 0; i < 4; i++) chPoolFree(&mp1, wa[i]); /* Starting threads from the memory pool. */ threads[0] = chThdCreateFromMemoryPool(&mp1, prio-1, thread, "A"); threads[1] = chThdCreateFromMemoryPool(&mp1, prio-2, thread, "B"); threads[2] = chThdCreateFromMemoryPool(&mp1, prio-3, thread, "C"); threads[3] = chThdCreateFromMemoryPool(&mp1, prio-4, thread, "D"); threads[4] = chThdCreateFromMemoryPool(&mp1, prio-5, thread, "E"); test_assert(1, (threads[0] != NULL) && (threads[1] != NULL) && (threads[2] != NULL) && (threads[3] != NULL) && (threads[4] == NULL), "thread creation failed"); /* Claiming the memory from terminated threads. */ test_wait_threads(); test_assert_sequence(2, "ABCD"); /* Now the pool must be full again. */ for (i = 0; i < 4; i++) test_assert(3, chPoolAlloc(&mp1) != NULL, "pool list empty"); test_assert(4, chPoolAlloc(&mp1) == NULL, "pool list not empty"); }
jhc_threadid_t forkOS_createThread(void *(*wrapper)(void *), void *entry, int *err) { Thread *tid; tid = chThdCreateFromMemoryPool(&pool_descriptor, NORMALPRIO, (tfunc_t) wrapper, entry); if (NULL == tid) abort(); return tid; }
/** * @brief Creates a thread allocating the stack from a memory pool. * @details Creates a new thread. The memory chunk for the stack is allocated * from a memory pool. * @warning The thread creation is suspended until there is a free slot in the * memory pool. * * @pre The stack is big enough to avoid stack overflow. * * @param[out] idp * Pointer to the @p UrosThreadId where the created thread id is * stored. * @param[in] namep * Pointer to the default thread name, valid for the whole the thread * life. Null-terminated string. * @param[in] priority * Thread priority. * @param[in] routine * Thread routine, entry point of the thread program. * @param[in] argp * Argument passed to the thread routine. Can be @p NULL. * @param[in] mempoolp Pointer to the memory pool where to allocate a stack. * @return * Error code. */ uros_err_t uros_lld_thread_createfrommempool(UrosThreadId *idp, const char *namep, uros_prio_t priority, uros_proc_f routine, void *argp, UrosMemPool *mempoolp) { urosAssert(idp != NULL); urosAssert(routine != NULL); urosAssert(mempoolp != NULL); *idp = chThdCreateFromMemoryPool(mempoolp, priority, routine, argp); if (*idp != NULL) { #if CH_USE_REGISTRY /* Set the thread name.*/ chSysLock(); (*idp)->p_name = namep; chSysUnlock(); #else (void)namep; #endif return UROS_OK; } return UROS_ERR_NOMEM; }