Beispiel #1
0
/* Store the hent away in the per-thread data. */
static void
store_hent(struct hostent *hent)
{
    // Prevent memory leaks by setting a destructor to be
    // called on thread exit to free per-thread data.
    cyg_thread_add_destructor( &thread_destructor, 0 );
    cyg_thread_set_data(ptdindex, (CYG_ADDRWORD)hent);
}
Beispiel #2
0
/* If there is an answer to an old query, free the memory it uses. */
static void
free_stored_hent(void)
{
    struct hostent *hent;
    hent = (struct hostent *)cyg_thread_get_data(ptdindex);
    if (hent) {
        free_hent(hent);
        cyg_thread_set_data(ptdindex, (CYG_ADDRWORD)NULL);
        cyg_thread_rem_destructor( &thread_destructor, 0 );
    }
}
/*
 *  eCos doesn't clean the memory blocks of stack and thread info required to create
 *  a thread, if they are dynamically allocated. Here is how we clean them:
 *      1) when a thread is created, we allocate a memory block for both stack and thread info.
 *      2) The memory block pointer is stored into thread data, index 0
 *      3) When a thread exits, it retrieves the memory block pointer from thread data, index 0
 *          and put it into a pool
 *      4) Next time when another thread exits, it first cleans up the memory block pool.
 *      5) In step 3) and 4), a spin lock is used to protect the memory block pool.
 */
KERNEL_HANDLE
KernelCreateTask
    (
        cyg_thread_entry_t*         startEntry,
        ULONG                       stackSize,
        ULONG                       priority,
        KERNEL_HANDLE               hContext,
        char*                       pName
    )
{
    ULONG                           ulSize      = 0;
    PKERNEL_TASK_MEM_BLOCK          pMem        = NULL;
    PUCHAR                          pStack      = NULL;
    cyg_thread*                     pThreadInfo = NULL;
    cyg_handle_t                    hThread     = 0;

    ulSize  = (sizeof(KERNEL_TASK_MEM_BLOCK) + CYGNUM_HAL_STACK_FRAME_SIZE) / CYGNUM_HAL_STACK_FRAME_SIZE * CYGNUM_HAL_STACK_FRAME_SIZE;

    pMem    = (PKERNEL_TASK_MEM_BLOCK)KernelAllocateMemory(ulSize + stackSize);

    if ( pMem )
    {
        pStack      = pMem + ulSize;
        pThreadInfo = &pMem->ThreadInfo;

        cyg_thread_create
            (
                priority,
                startEntry,
                hContext,
                pName,
                pStack,
                stackSize,
                &hThread,
                pThreadInfo
            );

        if ( hThread )
        {
            KernelTrace
                (
                    "KernelCreateTask -- memory block pointer for task %s is 0x%lX.\n",
                    pName,
                    pMem
                );

            cyg_thread_set_data(0, pMem);

            cyg_thread_resume(hThread);

            return  hThread;
        }
    }

    if ( pMem )
    {
        KernelFreeMemory(pMem);
    }

    return  (KERNEL_HANDLE)NULL;
}