éåæI/O APIã®è¨è¨ããªããªãé£ãã
yuniã§å®ç¨çãªããã°ã©ã ãæ¸ãããã«ã¯ãã©ããã¦ãéåæI/Oã©ã¤ãã©ãªãå¿ è¦ã«ãªããã¨ããããã§é»ã ã¨è¨è¨ãã¦ããããã©ãããããªããªãé£ããã
éåæI/Oã©ã¤ãã©ãªã®é£ãã
- ããããOS/å¦çç³»æ¯ã«å¥ç©ãå¿ è¦
"éåæI/Oã©ã¤ãã©ãªãªãã¦libuvä¸æã ã"ã¨ããæè¦ãæããããããªãããå®éãNode.jsã¯libuvã®ãã¶ã¤ã³ã®å®ç¨æ§ã証æãã¤ã¥ãã¦ããã¨è¨ãã(å®éã«ã¯éã§ãNode.jsã®OSæ½è±¡åã¬ã¤ã¤ã¨ãã¦libuvãå®è£
ããã¦ãã)ãããlibuvã¯ã«ã¼ãã«æ©è½ã®æ½è±¡åã§ãããªããåããã¶ã¤ã³ãyuniã«é©ç¨ã§ããã¨ã¯éããªããyuniã¯æ¢ã«Kawa(Javaä¸ã®Schemeå®è£
)ãIronScheme(.netä¸ã®Schemeå®è£
)ãã¿ã¼ã²ãããã¦ããã®ã§ããããã§ãåä½ãããããªé
æ
®ãå¿
è¦ã«ãªãã
ãããyuniã®éåæI/Oã©ã¤ãã©ãªãåãªãlibuvã®ãã¤ã³ãã£ã³ã°ã¨ãããªãã°ãI/Oã使ã£ãã¹ã¯ãªãããå®è¡ããã ãã§FFIãå¿
é ã«ãªã£ã¦ãã¾ããlibuvã¯å¤§ä½ã®OSã§åãããlibtuvã®ãããªãã¤ã³ã³åãã®æ´¾ç( https://github.com/Samsung/libtuv )ã¾ã§æããããããªãã®ãµã¤ãºã®ã©ã¤ãã©ãªãhard dependencyã«ãã¦ãã¾ãã®ã¯æ°ãå¼ããã¨ããåé¡ãããã
yuniã§ã¯ãããã¯ã¨ã³ãã¨ãªãéåæI/Oå¦çç³»ã"ã«ã«ãã£"ã¨å¼ã³ãåä¸ã®ããã°ã©ã å
ã§è¤æ°ã®ã«ã«ãã£ãåæã«ä½¿ç¨ã§ããããã«é
æ
®ãããã¨ã«ããã
- "ã«ã«ãã£"æ¯ã«ä½æ³ãç°ãªã
ãã®åé¡ã¯libuvã§æ¢ã«å¤§ä½åãçµã¾ãã¦ããããOSãã©ã¤ãã©ãªã«ãã£ã¦ã¤ãã³ãã®ããªã¬ãå¦çé ã大ããç°ãªãããé©å½ãªæ½è±¡åãå¿
è¦ã«ãªãã
ä¾ãã°ãWindowsã®IOCP(I/O Completion Port)ã§ã¯ã¦ã¼ã¶ã®çºè¡ããèªã¿åããªã¯ã¨ã¹ãã®å®äºãéç¥ããã¦ããããBSD/macOSã®kqueueã§ã¯ã"èªã¿åãå¯è½ã«ãªã£ã"ã¨ããã¤ãã³ããé£ãã§ããã ãã§ãå®éã®èªã¿åãè¦æ±ã¯ãã®å¾ã«çºè¡ããå¿
è¦ãããã
ç°ãªãã«ã«ãã£éã§å
±éã®ããã°ã©ã ã使ãåãããã«ã¯ãæ
éã«APIãè¨è¨ãããããªãã
éåæçãç¡ãå ´åã®é æ ®
éè¦ãªãã¤ã³ãã¯ãéåæI/Oã¸ã®é
æ
®ãä¸åãªããã©ãããã©ã¼ã ã§ãåãã¹ã¯ãªãããåããå¿
è¦ãããç¹ã¨è¨ããã
ä¾ãã°ãSDLã¯ã¹ã¬ããããã¡ã¤ã«I/Oãæ½è±¡åãã¦ãããããã®ãã¡ã¤ã«I/Oæ©è½ã«ã¯ä¸åã®éåæI/Oã¤ã³ã¿ã¼ãã§ã¼ã¹ãç¨æããã¦ããªãã(ãã®åé¡ã¯C++æ¨æºã©ã¤ãã©ãªããããããè¨èªæ¨æºã«ã¹ã¬ããã®æ¦å¿µãç¡ãR6RS/R7RSã§ãåããã¨ãè¨ããã)
ãã®ãããªã±ã¼ã¹ã§ã¯ã¯ã¼ã«ã¼ã¹ã¬ããã¨ãã¥ã¼ã使ãããããåç´ã«ä½ãã¨1ã¹ããªã¼ã 1ã¹ã¬ãããç¨æãããã¨ã«ãªã£ã¦ãã¾ããå®éã«ã¯ãåãããã¤ã¹ã§è¤æ°ã®I/Oã併走ããã¦ãç¡é§ãªãã¨ãå¤ããããç¡é§ã«ã¹ã¬ãããæ¶è²»ãããã¨ã«ãªãã
ä¸é解ã¨ãã¦ã¯ãI/Oã®æ§è³ªã«åããã¦ã¹ã¬ãããåå²ãããã¤ã¾ã:
- èªã¿åãã®å¿çæ§ãéè¦ãªãªã¼ãã£ãªã¯å¥ã®ã¹ã¬ããã«åé¢ãã
- èªã¿åãã®å¿çæ§ãæ¯è¼çã©ãã§ãè¯ãç»åãã¼ã¿ã¯ã¹ã¬ããã1ã¤ã«éç´ãã
ãã¨ã§ããªã¼ãã£ãªãã¼ã¿ã®èªã¿åãã¯ç´ãã§ããããã«ãã¤ã¤ãå¿
è¦æä½éã®ã¹ã¬ããæ°ã§å¯¾å¿ãããã¨ãã§ããã(ãã ããå®éã®ã²ã¼ã ã§ã¯ãã£ã¨çé¢ç®ã«ãªã¯ã¨ã¹ããã¥ã¼ãå®è£
ãã¦ãããã¨ãå¤ã)
... ãã®ãããªé
æ
®ãã©ããã£ã¦APIã«ããã°ãä»ã®éåæI/Oãå®è£
ããã«ã«ãã£ã¨å
±éã®ããã°ã©ã ã«ãããã¨ãã§ããã ããããã
SDL2ã®ããã«ä¸åéåæI/Oãåå¨ããªãç°å¢ãæãããä¾ãã°DNSåç
§ã¯æ®ã©ã®OSã§éåæã¤ã³ã¿ã¼ãã§ã¼ã¹ãåå¨ããªããããè¦æ¨¡ã®å¤§å°ã¯å¥ã«ãã¦ã©ããã§ã¯çµ¶å¯¾ã«å¿
è¦ã«ãªããã¡ãªã¿ã«libuvã§ã¯ã©ã¤ãã©ãªèªä½ã«DNSåç
§æ©è½ãæããã¦ãã®åé¡ãåé¿ãã¦ãããã¾ããlibuvã«ã¯ã¹ã¬ãããã¼ã«æ©è½ãããããã¤ã¶ã¨ãªãã°ãã¡ãã«éãããã¨ã¯ã§ããã
ãã¤ãã¼ãªæ©è½ãã©ããã£ã¦æ½è±¡åããã®ãåé¡
libuvã«ã¯ãã¡ã¤ã«ã³ãã¼æ©è½ãAPIã¨ãã¦åå¨ãã( https://github.com/libuv/libuv/issues/925 )ãå®ã¯åOSã¯ãã¡ã¤ã«ã³ãã¼ãè¡ãAPIãOSã¬ãã«ã§æã£ã¦ãããã¨ããããå°ç¨ã®æé©åãæ³å®ããã¦ãã(ä¾ãã°ããããã¯ã¼ã¯ãã¡ã¤ã«ã·ã¹ãã ã§ããã°ãµã¼ãä¸ã§ã³ãã¼ãè¡ãç)ãå½ç¶ãããããAPIã¨ãã¦ç¨æãã¦ãããªãã¨OSã®æé©åã®æ©æµã¯åããããªãã
- http://docs.libuv.org/en/v1.x/fs.html#c.uv_fs_type
- UV_FS_COPYFILEãç¾æç¹ã§ã¯æå¾ã®è¿½å ã¨ãªã£ã¦ãã
ä¾ãã°ã·ãªã¢ã«ãã¼ãã®å ¥åºåçã¯libuvã«ã¯ç¡ããã·ãªã¢ã«ãã¼ãã¯ãã¶ãæã£ã¦ãè¯ãã¨æããã©ãã§ã¯é»æºã¤ãã³ãã¨ãMIDIã¯ã©ãããã
SRFI-18ãportå ¥åºåã§ãããã¯ããåé¡
ã¾ãä¸æ¦Schemeã«ãããã¹ã¬ããå®è£
ã®æ大å
¬ç´æ°ã¨è¨ããSRFI-18( https://srfi.schemers.org/srfi-18/srfi-18.html )ã§è©¦ãã«å®è£
ãããã¨æã£ã¦ããããã©ããSRFI-18ç°å¢ã§Schemeã®portã¨ã¹ã¬ãããå
±åãããæ¹æ³ãç¡ããããã©ãããããããã
ä¾ãã°ãchickenã§ã¯æ示çã«"I/Oã«ãããããã¯ã¯å
¨ã¹ã¬ããããããã¯ããã"ã¨ãã¦ãã:
Blocking I/O will block all threads, except for some socket operations (see the section about the tcp unit). An exception is the read-eval-print loop on UNIX platforms: waiting for input will not block other threads, provided the current input port reads input from a console.
軽ã調ã¹ã¦ã¿ãã¨ãããSRFI-18ãå®è£ ãã¦ããå¦çç³»ã¯ãã¤ãã£ãã¹ã¬ããã使ç¨ããå®è£ ã¨ãããããã°ãªã¼ã³ã¹ã¬ããå®è£ ã®2種é¡ã«åãããã:
- ã°ãªã¼ã³ã¹ã¬ããå®è£
- ãã¤ãã£ãå®è£
- Gauche: http://practical-scheme.net/gauche/man/gauche-refe/Threads.html
- Guile: https://www.gnu.org/software/guile/docs/master/guile.html/SRFI_002d18.html (ããªããã£ã: https://www.gnu.org/software/guile/docs/master/guile.html/Threads.html )
- Sagittarius: http://ktakashi.github.io/sagittarius-online-ref/section613.html
portã§ãããã¯ãã¦ãä»ã®ã¹ã¬ããã®å®è¡ãç¶ããããã®ã¯Gambitã¨åãã¤ãã£ãå®è£
ã ã£ãããã®ãããä»®ã«åå¦çç³»ã®ã°ãªã¼ã³ã¹ã¬ããå®è£
ãæ´»ç¨ããã«ãã¦ããScheme portã§ã¯ãªãå¦çç³»ã«åºæã®multiplexææ³ã使ãå¿
è¦ãããã
ã¡ãªã¿ã«ãä»ã®å¦çç³»ã¯å®è£
ææ³ãã¾ã¡ã¾ã¡ã§ã
- promise/futureãã¼ã¹ã®å®è£
- Kawa: https://www.gnu.org/software/kawa/Threads.html
- Racket: https://docs.racket-lang.org/guide/parallelism.html (ã°ãªã¼ã³ã¹ã¬ãããæã: https://docs.racket-lang.org/guide/concurrency.html )
- Share nothing(ãªãã¸ã§ã¯ãã®ããã¨ãã¯æ示çã«channelã使ç¨ãã)
- nmosh
- ä¸æ(åå¨ããã¨ããå)
- ããããã¹ã¬ããããµãã¼ããã¦ããªã
- Larceny
- Vicare
ãããã«ãããã¹ã¬ããã®ç¡ãå¦çç³»ããµãã¼ãããããã«ã¯FFIçµç±ã§ã®å®è£ ãã©ããã®ã¿ã¤ãã³ã°ã§ã¯å¿ è¦ã«ãªããã°ãªã¼ã³ã¹ã¬ããå®è£ ã§FFIå¿ é ã«ãããã¯å¾®å¦ãªãã¤ã³ãã¨è¨ããã
次ã®ä¸æ
ã¨ããããããã«ããã¬ã¼ã¹( http://d.hatena.ne.jp/mjt/20171204/p1 )ã§ä½¿ç¨ãã¦ããI/Oããªããã£ããã¤ã¾ãã
- ããã»ã¹ã¨pipe
- ãã¡ã¤ã«
- Socket
ãåºæ¬ã»ããã¨ãã¦ããããã¯å¯è½ãªéãå¤ãã®å¦çç³»ã§Schemeãªã³ãªã¼ã§å®è£
ã§ããããã«é
æ
®ãããã¨ã«ãããæ®å¿µãªããããã»ã¹ã®èµ·åå¦çã¨DNS lookupã¯ã©ããã£ã¦ãblockingã«ãªãããä»åã¯blockã®æéãä¸çãããã¨ãããã¤ã³ãã§å¦¥åãããã¨ã«ãããã(DNSã§ã¿ã¤ã ã¢ã¦ããèµ·ãããããªã±ã¼ã¹ã§ã¯é·ããªã£ã¦ãã¾ãããã)
ãããã«ç¨éãçµãã°ãã¹ã¬ããããµãã¼ããã¦ããªãå¦çç³»ã§ããããããselect()ã¨non-blocking I/Oãããµãã¼ããã¦ããã°è¯ããã¨ã«ãªãã