| 28 | } |
| 29 | |
| 30 | int ProgramManager::executeThread(ThreadFunction function, void *parameter, const char *name, int priority) |
| 31 | { |
| 32 | // 关中断,防止创建线程的过程被打断 |
| 33 | bool status = interruptManager.getInterruptStatus(); |
| 34 | interruptManager.disableInterrupt(); |
| 35 | |
| 36 | // 分配一页作为PCB |
| 37 | PCB *thread = allocatePCB(); |
| 38 | |
| 39 | if (!thread) |
| 40 | return -1; |
| 41 | |
| 42 | // 初始化分配的页 |
| 43 | memset(thread, 0, PCB_SIZE); |
| 44 | |
| 45 | for (int i = 0; i < MAX_PROGRAM_NAME && name[i]; ++i) |
| 46 | { |
| 47 | thread->name[i] = name[i]; |
| 48 | } |
| 49 | |
| 50 | thread->status = ProgramStatus::READY; |
| 51 | thread->priority = priority; |
| 52 | thread->ticks = priority * 10; |
| 53 | thread->ticksPassedBy = 0; |
| 54 | thread->pid = ((int)thread - (int)PCB_SET) / PCB_SIZE; |
| 55 | |
| 56 | // 线程栈 |
| 57 | thread->stack = (int *)((int)thread + PCB_SIZE); |
| 58 | thread->stack -= 7; |
| 59 | thread->stack[0] = 0; |
| 60 | thread->stack[1] = 0; |
| 61 | thread->stack[2] = 0; |
| 62 | thread->stack[3] = 0; |
| 63 | thread->stack[4] = (int)function; |
| 64 | thread->stack[5] = (int)program_exit; |
| 65 | thread->stack[6] = (int)parameter; |
| 66 | |
| 67 | allPrograms.push_back(&(thread->tagInAllList)); |
| 68 | readyPrograms.push_back(&(thread->tagInGeneralList)); |
| 69 | |
| 70 | // 恢复中断 |
| 71 | interruptManager.setInterruptStatus(status); |
| 72 | |
| 73 | return thread->pid; |
| 74 | } |
| 75 | |
| 76 | void ProgramManager::schedule() |
| 77 | { |
no test coverage detected