カリカリなMutexを作る / "Crispifying" Go's Mutex

Room 1 14:30 - 14:50

(English follows Japanese) Go言語といえば並列処理、並列処理といえばMutexですよね。今回はそんなGo言語のMutexについてです。 案外簡単なのでは?と思いきや、実際にコードを読んでみると複雑な実装になっていて、いかに「カリカリ」に最適化されているかがわかります。 もはや職人芸とも言えるGo言語のMutex。とても真似できないと思われるかもしれませんが、決してそんなことはありません。今回は、簡単なスピンロックの実装からスターベーションモードの実装まで、皆さんと一緒に「オレオレMutex」をカリカリにしていこうと思います。 実際に標準ライブラリ内にオレオレMutexを実装して、テストを通し、またおまけとしてSPINというツールで安全性 (+活性性?) のモデル検査を行います。もしかしたらバグがあるかもしれませんが、そこはご愛嬌ということで... ぜひお楽しみに! --- When you think of Go, you think of concurrency. And when you think of concurrency, you think of mutex. So today, let's talk about Go's mutex. At first glance, it might seem straightforward, depending on who you ask. But once you dive into the source code, you quickly realise it's far more intricate than expected - packed with finely tuned optimisations. You might be tempted to think, "There's no way I could ever pull this off myself". But actually, that’s not the case at all! Starting from a simple spinlock, we'll work step by step towards a mutex that even handles starvation mode, gradually building our own "DIY mutex" until it's nice and"crispy". We’ll implement it right into the standard library, run it through the test suite, and, as a bonus, try out SPIN to check its safety property (and maybe even its liveness). Of course, there might be a few bugs here and there... but that’s all part of the fun. So let’s dive in and enjoy!