ãã¼ãããã®OSèªä½å ¥éãã Rust ã§ãã (第14ç« ï½ç¬¬16ç« )
ããã°ã®æ´æ°ã¯éã空ãã¦ãã¾ãã¾ããããOSã®ç§»æ¤ãããã¯ç´°ã ã¨ç¶ãã¦ãã¾ããã ä»åã¯ã¾ã¨ãã¦3ç« åã§ãã
- ã·ãªã¼ãºæåã®è¨äº: 「ゼロからのOS自作入門」を Rust でやる (第1章~第4章) - gifnksmの雑多なメモ
- åå: 「ゼロからのOS自作入門」を Rust でやる (第13章) - gifnksmの雑多なメモ
- 次å: 「ゼロからのOS自作入門」を Rust でやる (第17章) - gifnksmの雑多なメモ
- é¢é£è¨äºä¸è¦§: ゼロからのOS自作入門 カテゴリーの記事一覧 - gifnksmの雑多なメモ
第14ç«
第13ç« ã«å¼ãç¶ãããªã¨ã³ããã£ããã«ãã¿ã¹ã¯ãå®è£ ããç« ã§ãã 第14ç« ã§ã¯ã¿ã¹ã¯ã®ã¹ãªã¼ããåªå 度ãå®è£ ãã¾ãã
ã¿ã¹ã¯ã®ã¹ãªã¼ã (day14a)
ã¿ã¹ã¯ã®ã¹ãªã¼ããå®è£
ãã¾ããã¾ãã s
ãã¼ã w
ãã¼ã®å
¥åã§ã¿ã¹ã¯Bãã¹ãªã¼ãç¶æ
ã«ãããå®è¡ç¶æ
ã«ããããã¾ãã
å®è£
㯠C++ çã¨ããããåãã§ãã
Rust çåºæã®ä½ãè¾¼ã¿ã¨ãã¦ãã³ã³ããã¹ãã¹ã¤ããæã® Arc
ã®åãæ±ããããã¾ãã
static TASK_MANAGER: OnceCell<Mutex<TaskManager>> = OnceCell::uninit(); #[derive(Debug)] struct TaskManager { tasks: BTreeMap<TaskId, Arc<Task>>, wake_queue: VecDeque<TaskId>, } impl SwitchTask { fn switch(self) { assert!(Arc::strong_count(&self.next_task) > 1); assert!(Arc::strong_count(&self.current_task) > 1); unsafe { let next_task_ptr = Arc::as_ptr(&self.next_task); let current_task_ptr = Arc::as_ptr(&self.current_task); drop(self.next_task); drop(self.current_task); #[allow(clippy::unwrap_used)] let next_task = next_task_ptr.as_ref().unwrap(); #[allow(clippy::unwrap_used)] let current_task = current_task_ptr.as_ref().unwrap(); Task::switch(next_task, current_task) } } }
Task
æ§é ä½ã¯ TaskManager
æ§é ä½ã® tasks
ãã£ã¼ã«ãã« Arc<Task>
ã¨ããå½¢ã§æ ¼ç´ããã¦ãã¾ãã
TaskManager
æ§é ä½ã¯è¤æ°ã®ã³ã³ããã¹ãããã¢ã¯ã»ã¹ããããã« static
å¤æ° TASK_MANAGER
ã«ä¿æããã¦ãããã¢ã¯ã»ã¹ããããã«ã¯ Mutex
ããã¯ã®åå¾ãå¿
è¦ã§ãã
ãã®ãããã³ã³ããã¹ãã¹ã¤ããæã«ããã¯ãåå¾ããã¾ã¾ã ã¨æ¬¡åã®ã³ã³ããã¹ãã¹ã¤ããæã«ããã¯ãåå¾ãããã¨ãã¦ãããããã¯ã«ãªã£ã¦ãã¾ãããã以ä¸ã®ãããªé åºã§å¦çããããã«ãã¾ããã
TASK_MANAGER
ã®ããã¯ãåå¾- ç¾å¨å®è¡ä¸ã®ã¿ã¹ã¯ã¨æ¬¡ã«ã³ã³ããã¹ãã¹ã¤ããããã¿ã¹ã¯ã®
Arc<Task>
ã®ã¯ãã¼ã³ãåå¾ TASK_MANAGER
ã®ããã¯ã解é¤- ã³ã³ããã¹ãã¹ã¤ãã対象ã®
Arc<Task>
ãã*const Task
ãåå¾ããArc<Task>
㯠drop ãã - ã³ã³ããã¹ãã¹ã¤ãããå®è¡
4 㧠*const Task
ãåå¾ãã¦ããã®ã¯ã Arc<Task>
ã® drop
(åç
§ã«ã¦ã³ãã®ãã¯ãªã¡ã³ã) ãã³ã³ããã¹ãã¹ã¤ããåã«è¡ãããããã§ãã
4~5 ã®æç¹ã§ã¯ TaskManager
ã Arc<Task>
ãä¿æãã¦ããããããã¤ã³ã¿ã®æãå
ã®é åã解æ¾ããããã¨ã¯ãªãã¯ãã§ãã
ã¤ãã³ãçºçæã«ã¿ã¹ã¯ãèµ·åºããã & ã¢ã¤ãã«æã«ã¿ã¹ã¯ãã¹ãªã¼ãããã (day14b)
ã¤ãã³ãå¾ ã¡ç¶æ ã«ãªã£ãã¿ã¹ã¯ãã¹ãªã¼ãç¶æ ã«ãã & ä»ã¿ã¹ã¯ã«å¯¾ãã¦ã¤ãã³ãéç¥ãããéã«å½è©²ã¿ã¹ã¯ãèµ·åºç¶æ ã«ããã¨ããç¯ã§ãã ããã«ãããOS ã®å¿çæ§ãåä¸ãã¾ãã
C++ çã§ã¯ã¿ã¹ã¯éã§ã¤ãã³ããããã¨ãããããã®ã¤ãã³ããã¥ã¼é¢é£å¦çã§ã¿ã¹ã¯ã®èµ·åº/ã¹ãªã¼ããè¡ã£ã¦ãã¾ããã Rust çã§ã¯ async-await ã®ä»çµã¿ãæ¡ç¨ãã¦ããããã async-await ã® waker ã®ä»çµã¿ã¨é£æºã§ããã¨è¯ãããã§ãã
ã¾ãã¯ãæ°ãã«çæãããã¿ã¹ã¯ã§ async-await ã使ãããããã¿ã¹ã¯ãã¨ã«ã©ã³ã¿ã¤ã (executor) ãæã¤ããã«ãã¾ããã
å¾æ¥ã¯ Task::new
ã®å¼æ°ã«ã¿ã¹ã¯ã®ã¨ã³ããªã¼ãã¤ã³ãã¨ãªãé¢æ°ãã¤ã³ã¿ã渡ãã¦ãã¾ããã
ä¿®æ£å¾ã¯ impl Future<Output=()>
ã渡ãããã«ãã¦ãã¾ãã
ã¨ã³ããªãã¤ã³ãã¯å
¨ã¿ã¹ã¯ã§å
±éåãã第ä¸å¼æ° rax
ã«æ ¼ç´ããããã¤ã³ã¿ãã Box<EntryPointArg>
ã復å
ãããã®ä¸ã«å«ã¾ãã executor ã® run
ã¡ã½ãããå®è¡ãããã¨ã§ CoTask
ãå®è¡ã§ããããã«ãªãã¾ããã
次ã«ã async-wait ã® waker ãã¿ã¹ã¯ã® wake/sleep ã«å¯¾å¿ããã¾ãã
waker ã CoTask
ãèµ·åºãããéã«ãå½è©² CoTask
ãæå±ããã¿ã¹ã¯ãèµ·åºãããããã«ãã¾ããã
ã¾ããåã¿ã¹ã¯ã® Executor
ã§å¦çå¯è½ãª CoTask
ãåå¨ããªããªã£ããèªã¿ã¹ã¯ãã¹ãªã¼ããããããã«ãã¾ããã
ããã ãã§ã¤ãã³ãçºçæã®ã¿ã¹ã¯èµ·åº & ã¢ã¤ãã«æã®ã¿ã¹ã¯ã¹ãªã¼ããå®ç¾ã§ãã¾ãã
ç°¡åã§ããï¼
ã¿ã¹ã¯ã«åªå 度ãã¤ãã (day14c)
åã ã®ã¿ã¹ã¯ã«åªå 度 (ã¬ãã«) ãè¨ããåªå 度ã®é«ãã¿ã¹ã¯ãåªå çã«å®è£ ãããããã«ãã¾ãã
C++çã¨ã¯å°ãå®è£ æ¹æ³ãç°ãªãã¾ãããç°¡åã«å®è£ ãããã¨ãã§ãã¾ããã
ã¢ã¤ãã«ã¿ã¹ã¯ã追å (day14d)
ãã¹ã¦ã®ã¿ã¹ã¯ãã¹ãªã¼ãç¶æ
ã«ãªã£ãæã«CPUæ¶è²»ãæããããããã hlt
ãå®è¡ããã ãã®ä½åªå
度ã¿ã¹ã¯ã§ããã¢ã¤ãã«ã¿ã¹ã¯ã追å ãã¾ãã
ãã°ä¿®æ£
Rust çã®ã¿ã¹ã¯åªå 度è¨å®ã«ã¯ãã°ããããé«åªå 度ã®ã¿ã¹ã¯ãå®è¡å¯è½ç¶æ ã®å ´åã«ã¿ã¤ãã¼å²ãè¾¼ã¿å¥æ©ã®ã³ã³ããã¹ãã¹ã¤ããè¦æ±ãçºçããã¨ãä½åªå 度ã®ã¿ã¹ã¯ã«ã¹ã¤ãããã¦ãã¾ãã¨ããåé¡ãããã¾ããã®ã§ä¿®æ£ãã¾ããã
ããã²ã¨ã¤ã®ãã°ã¨ãã¦ãå²ãè¾¼ã¿ã³ã³ããã¹ãã§ã®ã³ã³ããã¹ãã¹ã¤ããæã«ã¡ã¢ãªç²å¾ãè¡ã£ã¦ãã¾ãå¯è½æ§ãããã¾ããã ã¿ã¹ã¯ã® spawn æã«å¿ è¦ãªé åç²å¾ãè¡ãããã«ãã¦ãå²ãè¾¼ã¿ã³ã³ããã¹ãã§ã¯ã¡ã¢ãªç²å¾ãè¡ãããªãããã«ãã¾ããã
第15ç«
端æ«ãå®è£ ããç« ã§ãã
ã¦ã¤ã³ãã¦æç»ãã¡ã¤ã³ã¿ã¹ã¯ã§è¡ãããã«ãã (day15a)
Rust å®è£ ã§ã¯å ã æç»å¦çã¯ã¡ã¤ã³ã¿ã¹ã¯ã§è¡ãããã«ãã¦ããããããã®ç¯ã®å¯¾å¿ã¯ä¸è¦ã§ããã
ã¢ã¯ãã£ãã¦ã¤ã³ãã¦ã®è¿½å (day15b)
ã¢ã¯ãã£ãã¦ã¤ã³ãã¦ã®æ¦å¿µãå°å ¥ããã¿ã¤ãã«ãã¼ã®è²ãå¤ãããããã¼å ¥åã¤ãã³ãã®éä¿¡å ã¿ã¹ã¯ãéå®ããããã¾ãã
ã¾ãã¯ãã¦ã¤ã³ãã¦æç»å¦çãå
±éåãã¾ãã
å
·ä½çã«ã¯ãã¿ã¤ãã«ãã¼ãæ ã®ããã¦ã¤ã³ãã¦ãæå³ãã FramedWindow
æ§é ä½ã追å ãã¾ããã
C++ çã§ã¯ ToplevelWindow
ã¨ããååã§ãããããã¾ããã£ããæ¥ãªãååã ã£ãã®ã§ FramedWindow
ã¨ãã¦ãã¾ãã
ã¾ããC++çã§ã¯ ToplevelWindow
㯠Window
ãç¶æ¿ãã¦ãã¾ããããRust çã§ã¯ç¶æ¿ã§ã¯ãªãã³ã³ãã¸ã·ã§ã³ã§ã³ã¼ãã®åå©ç¨ãå®ç¾ãã¦ãã¾ã (Rust çã§ã¯ Layer
ã Window
ãä¿æããªããã Window
åã¨ã®ãµãã¿ã¤ãã³ã°é¢ä¿ãä¸è¦ã§ããã¨ããäºæ
ãããã¾ã)ã
次ã«ã¢ã¯ãã£ãã¦ã¤ã³ãã¦ã管çãã ActiveLayer
æ§é ä½ã追å ãã¾ãã
ã ãããC++çã¨åãå®è£ ã§ãã
ç¶ãã¦ãã¦ã¤ã³ãã¦ã®ç¶æ ã«å¿ãã¦ã¿ã¤ãã«ãã¼ã®è²ãå¤æ´ãã¾ãã
ã¦ã¤ã³ãã¦ãã¨ã«ã¤ãã³ãéç¥ç¨ã®ãã¥ã¼ãä¿æããããã«ãã ActiveLayer
ãå Window
ã® active/inactive ãå¤æ´ããæã«ãå½è©²ãã¥ã¼çµç±ã§ã¤ãã³ãéç¥ããããã«ãã¾ããã
æ´ã«ãã®ãã¥ã¼ãå©ç¨ãã¦ããã¼ãã¼ãå ¥åã¤ãã³ãã¯ã¢ã¯ãã£ãã¦ã¤ã³ãã¦ã«éä¿¡ããããã«ãã¾ããã
ããã«ã¦æåã®ç¯ã¯ããã¾ãã§ãã
ã¿ã¼ããã«ã®è¿½å (day15c)
ã¿ã¼ããã«ã®ã¦ã¤ã³ãã¦ã追å ãã¾ãã
C++çã¨ã ãããåãã§ãã
æç»é度ã®é«éå (day15d)
å¾æ¥ã¯ã¦ã¤ã³ãã¦ã®ä¸é¨ã«æ´æ°ãããã¨ã¦ã¤ã³ãã¦å ¨ä½ãåæç»ãã¦ãã¾ããã åæç»æã«å¿ è¦ãªç¯å²ã ãæç»ããããæç»ç¯å²ãæå®ãããã¨ã§æç»å¦çãé«éåãã¾ãã
C++ çã§ã¯ã¿ã¼ããã«ã®ã«ã¼ã½ã«ç¹æ» ã®å¦çã®ã¿æç»ç¯å²æå®ãã¦é«éåãã¦ãã¾ããã æç»ãå¿ è¦ãªç¯å²ã¯ Window ã«æç»ããå´ã®ã³ã¼ãã§è¨ç®ããå¿ è¦ãããã¾ãã Rust çã§ã¯æç»å¦çã«ããã¦åæç»ãå¿ è¦ãªç¯å²ãè¨æ¶ããããã«ãããã¨ã§ãæç»ããå´ã®ã³ã¼ãã§ç¹æ®ãªèæ ®ãããå¿ è¦ããªããªããã«ã¼ã½ã«ã®ç¹æ» 以å¤ã®ãã¹ã¦ã®æç»å¦çã§é«éåã®æ©æµãåãããã¾ãã
ãã°ä¿®æ£
第15ç« ã¾ã§å®è£ ãããããã§ã並åå¦çé¢é£ã®ãã° (ãããããã¯ãã¯ã©ãã·ã¥) ãé«é »åº¦ã§çºçããããã«ãªã£ã¦ãã¾ã£ããããé¢é£ãã°ãä¿®æ£ãã¾ããã
ãããããã¯ãçºçããåå ã¯ãã¹ãã³ããã¯ã¨ã¿ã¹ã¯ã®åªå 度ã®å®è£ æ¹æ³ã«ããã¾ãã ã¹ãã³ããã¯ã¯ãããã¯åå¾ã«å¤±æããå ´åã¿ã¹ã¯ãã¹ãªã¼ããããã®ã§ã¯ãªããç¡éã«ã¼ãã§ä»ã®ã¿ã¹ã¯ãããã¯ã解æ¾ããã®ãå¾ ã¡ã¾ãã ã¾ããç¾æç¹ã§ã¯é«åªå 度ã®ã¿ã¹ã¯ãå®è¡å¯è½ç¶æ ã§åå¨ããéããä½åªå 度ã®ã¿ã¹ã¯ã¯å®è¡ããã¾ããã çµæã¨ãã¦ã以ä¸ã®ãããªãããããã¯ãçºçããå¯è½æ§ãããã¾ãã
- ä½åªå 度ã®ã¿ã¹ã¯ãã¹ãã³ããã¯ãåå¾ãã
- ä½åªå 度ã®ã¿ã¹ã¯ãããã¯ã解æ¾ããåã«é«åªå 度ã®ã¿ã¹ã¯ã«ã³ã³ããã¹ãã¹ã¤ãããã
- é«åªå 度ã®ã¿ã¹ã¯ãå½è©²ããã¯ãåå¾ãããã¨ããã¨ããã¤ã¾ã§çµã£ã¦ãããã¯ãåå¾ã§ããå¾ ã¡ç¶ãã¦ãã¾ã
ãã®ç¾è±¡ãåé¿ãããããããã¯åå¾å¤±ææã«ã¿ã¹ã¯ãã¹ãªã¼ãããããã㪠Mutex ãå®è£ ãã¾ããã (ã¹ã±ã¸ã¥ã¼ã©ã¼ã®ãã¸ãã¯ãå¤æ´ããã®ã§ãè¯ãã®ã§ããã Mutex ã¯ãããã«ããå¿ è¦ã«ãªãããå®è£ ãã¾ãã)
Mutex ããã¯åå¾å¤±ææãã¿ã¹ã¯IDã Mutex ã®ä¿æãããã¥ã¼ã«è¿½å ãã¾ãã Mutex ããã¯åå¾ã«æåããã¿ã¹ã¯ã¯ãã¢ã³ããã¯æã«ãã®ãã¥ã¼ã«å«ã¾ããã¿ã¹ã¯IDã®ã¿ã¹ã¯ãèµ·åºããã¾ãã (ä¸è¨ã³ãããã®ã½ã¼ã¹ã«ã¯ã¬ã¼ã¹ã³ã³ãã£ã·ã§ã³ããã£ããããå¾ã®ã³ãããã§ä¿®æ£ãã¦ãã¾ãã)
Mutex ã®å®è£
ã« SegQue
ãå©ç¨ããããã allocator ã®æä»å¶å¾¡ã« Mutex ã¯å©ç¨ã§ããªããªã£ã¦ãã¾ãã¾ããã
ãã®ãããã¡ã¢ãªå²ãå½ã¦å¦çä¸ã¯å²ãè¾¼ã¿ãææ¢ããããã«ãã¾ããã
Mutex ã®ãã¥ã¼ã«ãã¼ãã®é åã§ã¯ãªãã¹ã¿ãã¯ã®é åãå©ç¨ãããªã©ããã° allocator ã®æä»å¶å¾¡ã«ã Mutex ãå©ç¨ã§ãããããããªãã§ããã
ã¾ããè¤æ°ã¿ã¹ã¯ããããã¯ãåå¾ãããå¯è½æ§ãããç®æã«ã¤ãã¦ã¯ Mutex ãå©ç¨ããããã«ãã¾ããã
ããã¾ã§ã®ä¿®æ£ã§ãããããã¯ããããã¯ã®çºçé »åº¦ã¯ããªãä½ä¸ãã¾ããã
ArrayQueue
é¢é£å¦çã wait-free ã§ãªãããåæ§ã®ãããããã¯ãçºçããå¯è½æ§ã¯æ®ã£ã¦ããã®ã§ãããã²ã¨ã¾ãã¯ããã§ã¨ã·ã¨ãã¾ããã
ã¤ãã§ã«ãå²ãè¾¼ã¿ã³ã³ããã¹ããã allocator ãå¼ã³åºããããã¨ãæ¤ç¥ã§ããããã¢ãµã¼ã·ã§ã³ã追å ãã¦ãã¾ãã
並åå¦çãé¢é£ããã¨åé¡ã®ããã³ã¼ãããã¾ãã¾åãã¦ãã¾ããã¨ãå¤ãããã°ããã£ã¦ãåç¾æ§ããªãã¦åå 調æ»ã大å¤ã ã£ããããã®ã§ãã¢ãµã¼ã·ã§ã³ãå ¥ããã«è¶ãããã¨ã¯ãªãã§ãããã
第16ç«
ã¿ã¼ããã«ä¸ã§ã®å ¥åãã³ãã³ãå®è¡ãå®è£ ããç« ã§ãã å ¨ä½çã«C++å®è£ ã¨åãã§é£ãããã¨ã¯ãã¾ããªãã®ã§ãã³ã¡ã³ãã¯çç¥ãã¾ãã
github.com github.com github.com github.com github.com github.com
ã¾ã¨ã
ããªã¨ã³ããã£ããã«ãã¿ã¹ã¯ã¨å調çãã«ãã¿ã¹ã¯ (async-await) ããã¾ãé£æºããããã¨ãã§ãã¾ããã Rustãããå®è£ ãã§ããã®ã§ã¯ãªããã¨æãã¾ãã ã¾ãã並åå¦çã«é¢ãããã°ãä¿®æ£ã§ããåä½ã®å®å®åº¦ãä¸ãã£ã¦ãã¾ãã
æ¬¡ç« ã§ã¯ãã¡ã¤ã«ã·ã¹ãã ãåãæ±ãã¾ããã©ããªããã¨ããã楽ãã¿ã«ã
- ã·ãªã¼ãºæåã®è¨äº: 「ゼロからのOS自作入門」を Rust でやる (第1章~第4章) - gifnksmの雑多なメモ
- åå: 「ゼロからのOS自作入門」を Rust でやる (第13章) - gifnksmの雑多なメモ
- 次å: 「ゼロからのOS自作入門」を Rust でやる (第17章) - gifnksmの雑多なメモ
- é¢é£è¨äºä¸è¦§: ゼロからのOS自作入門 カテゴリーの記事一覧 - gifnksmの雑多なメモ