Monitor
0x10. WHAT
0x11. DEF
shared state + mutual exclusion + condition synchronization + [(draft yet): encapsulation]
0x20. HOW
0x21. WHO
- shared state
- procedures (public methods)
- implicit Mutual Exclusion (monitor lock)
- CV; Condition Variables
- 이게 없을 때 발생가능한 deadlock PS????
0x22. rules
- public method는 shared state에 대한 유일한 접근 방법.
- concurrent 상황에서는 monitor lock를 통해서 entry queue에 머물게 함.
- public method (operations) 실행 중인 주체는 오직 하나만 되도록 함. 여러개가 있을 경우 entry queue에 주체가 대기
- 즉 주체(process/thread)의 접근 상태는 다음 세가지 중 하나 무조건
- running (monitor 내부 실행 중)
- entry queue (monitor 들어가려고 대기)
- wait queue (조건 안 맞아서 대기)
0x23. enter → run → exit
-
step 1: thread가 monitor method 호출
-
lock 없으면 → 바로 진입
-
lock 있으면 → entry queue에서 대기
-
-
step 2: active thread 실행
-
shared state 접근 가능
-
이 시점에는 mutual exclusion 보장됨
-
-
step 3: 종료 시
-
[(draft yet): lock] release
-
entry queue에서 하나 깨워서 실행
-
0x24. wait (condition synchronization)
-
상황
- thread가 실행 중인데 조건이 안 맞음 (ex: buffer empty)
-
동작
- wait(cv) 호출
- “조건이 맞을 때까지 monitor 밖에서 sleep”
- lock을 release
- thread는 wait queue로 이동
- 다른 thread가 들어올 수 있게 됨
- wait(cv) 호출
0x25. signal
-
다른 thread가 state 변경 후:
- signal(cv) 호출
-
효과
- wait queue에 있던 thread 하나 깨움
- BUT 바로 실행 아님
-
흐름
- 깨어난 thread → entry queue로 이동 → 다시 lock 경쟁