| 45 | } |
| 46 | |
| 47 | int ProgramManager::executeThread(ThreadFunction function, void *parameter, const char *name, int priority) |
| 48 | { |
| 49 | // 关中断,防止创建线程的过程被打断 |
| 50 | bool status = interruptManager.getInterruptStatus(); |
| 51 | interruptManager.disableInterrupt(); |
| 52 | |
| 53 | // 分配一页作为PCB |
| 54 | PCB *thread = allocatePCB(); |
| 55 | |
| 56 | if (!thread) |
| 57 | return -1; |
| 58 | |
| 59 | // 初始化分配的页 |
| 60 | memset(thread, 0, PCB_SIZE); |
| 61 | |
| 62 | for (int i = 0; i < MAX_PROGRAM_NAME && name[i]; ++i) |
| 63 | { |
| 64 | thread->name[i] = name[i]; |
| 65 | } |
| 66 | |
| 67 | thread->status = ProgramStatus::READY; |
| 68 | thread->priority = priority; |
| 69 | thread->ticks = priority * 10; |
| 70 | thread->ticksPassedBy = 0; |
| 71 | thread->pid = ((int)thread - (int)PCB_SET) / PCB_SIZE; |
| 72 | |
| 73 | // 线程栈 |
| 74 | thread->stack = (int *)((int)thread + PCB_SIZE - sizeof(ProcessStartStack)); |
| 75 | thread->stack -= 7; |
| 76 | thread->stack[0] = 0; |
| 77 | thread->stack[1] = 0; |
| 78 | thread->stack[2] = 0; |
| 79 | thread->stack[3] = 0; |
| 80 | thread->stack[4] = (int)function; |
| 81 | thread->stack[5] = (int)program_exit; |
| 82 | thread->stack[6] = (int)parameter; |
| 83 | |
| 84 | allPrograms.push_back(&(thread->tagInAllList)); |
| 85 | readyPrograms.push_back(&(thread->tagInGeneralList)); |
| 86 | |
| 87 | // 恢复中断 |
| 88 | interruptManager.setInterruptStatus(status); |
| 89 | |
| 90 | return thread->pid; |
| 91 | } |
| 92 | |
| 93 | void ProgramManager::schedule() |
| 94 | { |
no test coverage detected