⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread.c

📁 KaOS is a real-time operating system that has been implemented with the basic real-time constraints
💻 C
字号:
os_thread* CreateThread(void (*entrypoint)(void), UCHAR priVal, UINT swStackSize, UINT hwStackSize)
{
    os_thread* temp_thread;
    register char i;

    temp_thread = (os_thread*)g_freethreads;

    if (temp_thread == NULL) return -1;

    // pop a free thread from the list
    g_freethreads = temp_thread->next;
    g_freethreads->prev = NULL;

    // push the free thread on the used threads list
    temp_thread->next = g_usedthreads;
    g_usedthreads->prev = temp_thread;
    g_usedthreads = temp_thread;

    // apply thread settings
    g_usedthreads->priority = priVal;
    g_usedthreads->state = THREADSTATE_RUNNING;
    g_usedthreads->sleep_duration = 0;

    // Allocate stacks for this thread
    g_usedthreads->hwstack = os_alloc_stack(hwStackSize);
    g_usedthreads->swstack = os_alloc_stack(swStackSize);

    // we need to do some stack stuff, like put in 0's for registers and the stack address
    *((UCHAR*)g_usedthreads->hwstack)-- = (UCHAR)(entrypoint >> 0);
    *((UCHAR*)g_usedthreads->hwstack)-- = (UCHAR)(entrypoint >> 8);
    for (i = 0; i < 30; i++)
    {
        *((UCHAR*)g_usedthreads->hwstack)-- = 0;
    }
    // put on SREG with interrupts enabled
    *((UCHAR*)g_usedthreads->hwstack)-- = 0x80;

    g_threadcount++;

    return g_usedthreads;
}
void SleepThread(os_thread* thread, UINT sleepLength)
{
    #asm("cli");
    if (thread != NULL)
    {
        thread->sleep_duration = sleepLength;
        // re-run the scheduler
        os_schedule();
    }
    #asm("sei");
}
void SuspendThread(os_thread* thread)
{
    #asm("cli");
    if (thread != NULL)
    {
        thread->state = THREADSTATE_SUSPENDED;
        // re-run the scheduler
        //os_schedule(0);
    }
    #asm("sei");
}
void ResumeThread(os_thread* thread)
{
    #asm("cli");
    if (thread != NULL)
    {
        thread->state = THREADSTATE_RUNNING;
        // re-run the scheduler
        //os_schedule(1);
    }
    #asm("sei");
}
void TerminateThread(os_thread* thread)
{
    #asm("cli");
    if (thread != NULL)
    {
        thread->state = THREADSTATE_VOID;
        // remove this thread from the list
        if (thread->prev == NULL)
        {
            g_usedthreads = thread->next;
            g_freethreads->prev = thread;
            thread->next = g_freethreads;
            g_freethreads = thread;
        }
        else
        {
            if (thread->prev != NULL)
                    thread->prev->next = thread->next;

            if (thread->next != NULL)
                    thread->next->prev = thread->prev;

            thread->prev = NULL;
            thread->next = g_freethreads;
            g_freethreads = thread;
        }
        g_threadcount--;
        // re-run the scheduler
        os_schedule();
    }
    #asm("sei");
}

os_thread* FindThread(UCHAR threadID)
{
    os_thread *temp_thread, *found_thread;

    temp_thread = g_usedthreads;
    found_thread = NULL;
    while (temp_thread != NULL)
    {
        if (temp_thread->threadID == threadID)
        {
            found_thread = temp_thread;
            break;
        }
        temp_thread = g_usedthreads->next;
    }
    return found_thread;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -