| 43 | } |
| 44 | |
| 45 | void Semaphore::P() |
| 46 | { |
| 47 | PCB *cur = nullptr; |
| 48 | |
| 49 | while (true) |
| 50 | { |
| 51 | semLock.lock(); |
| 52 | if (counter > 0) |
| 53 | { |
| 54 | --counter; |
| 55 | semLock.unlock(); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | cur = programManager.running; |
| 60 | waiting.push_back(&(cur->tagInGeneralList)); |
| 61 | cur->status = ProgramStatus::BLOCKED; |
| 62 | |
| 63 | semLock.unlock(); |
| 64 | programManager.schedule(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void Semaphore::V() |
| 69 | { |