Upload
yuki-ito
View
142
Download
5
Embed Size (px)
Citation preview
Mutexを実装するImplementation of Mutex
The 17/01/05 MTG of Eyes, JAPANito
自己紹介/Self-Introduction- 伊藤勇希/Yuki Ito- ito/acomagu/publmag1- おすすめのメイドカフェはシャッツ
キステ/橙幻郷(ノマドに最適)My favorite Maid-Cafe: Schatzkiste, TouGenKyo
- ももくりが終わっちゃって悲しい
I’m really sad for end of MomoKuri.
今夜はみんなの目線をLock!Today, I gonna Lock your eyes at me!
目次/Index1. 並行処理/並列処理とは?
What is concurrent/parallel processing?
2.
What are Atomic, Semaphore and Mutex?
3. Mutexの実装 / Implement Mutex
4. Lockのその先へ / Beyond the Lock
“並列処理/並行処理”“Concurrent/Parallel Processing”
is
何what
“並列処理/並行処理”“Concurrent/Parallel Processing”
複数の仕事を同時にするDo multiple tasks at the same time
複数の仕事を同時にするDo multiple tasks at the same time
複数の仕事を同時にするDo multiple tasks at the same time
誰が / Who
複数の仕事を同時にするDo multiple tasks at the same time
僕が / I
複数の仕事を同時にするDo multiple tasks at the same time
僕と兼沢さんが
I and Kanesawa
僕が / I複数の仕事を同時にする
Do multiple tasks at the same time
僕と兼沢さんがI and Kanesawa複数の仕事を同時にする
Do multiple tasks at the same time
僕が / I複数の仕事を同時にする
Do multiple tasks at the same time
僕と兼沢さんがI and Kanesawa複数の仕事を同時にする
Do multiple tasks at the same time
並行処理Concurrent Processing
並列処理Parallel Processing
諸説あります
まとめると
並行処理 → ひとつのCPUが複数の処理を
同時に(見えるように)行う
並列処理 → 複数のCPUが別の処理を同
時に行う
諸説あります
ITO KANESAWA
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
あるお昼時 / One lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.
あるお昼時 / One lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
あるお昼時 / One lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
4人にすればいいんだな! 書き換えよう!It’s 4! Let’s rewrite!
あるお昼時 / One lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
あるお昼時 / One lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
名簿を見ると、今頼む予定の人数は4人かFrom the roster, 4 people will do.
あるお昼時 / One lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
名簿を見ると、今頼む予定の人数は4人かFrom the roster, 4 people will do.
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
あるお昼時 / One lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
名簿を見ると、今頼む予定の人数は4人かFrom the roster, 4 people will do.
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
5人にすればいいんだな! 書き換えよう!It’s 5! Let’s rewrite!
あるお昼時 / One lunchtime
5人分たのむよ〜I’m ordering it for 5.
5人分たのむよ〜I’m ordering it by 5.
SUCCESS!!
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
またあるお昼時 / Another lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.
またあるお昼時 / Another lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
またあるお昼時 / Another lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
またあるお昼時 / Another lunchtime
おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!
名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.
またあるお昼時 / Another lunchtime
名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
またあるお昼時 / Another lunchtime
名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
4人にすればいいんだな! 書き換えよう!It’s 4! Let’s rewrite!
またあるお昼時 / Another lunchtime
名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
4人にすればいいんだな! 書き換えよう!It’s 4! Let’s rewrite!
またあるお昼時 / Another lunchtime
4人にすればいいんだな! 書き換えよう!It’s 4! Let’s rewrite!
4人分たのむよ〜I’m ordering it for 4.
4人分たのむよ〜I’m ordering it by 4.Uh?
整理すると / In summary,● 伊藤が人数を読み取り、計算し、結果を書き込む
Ito read the number of people, calculate and write the result.
● 兼沢が人数を読み取り、計算し、結果を書き込む
Kanesawa read the number of people, calculate and write the result.
どちらかが読み取った後、書き込む前にもう一方が読み取ってしま
うと、矛盾が生じてしまう!It makes conflicts that one’s reading between another’s reading and writing!
どうすればいい!?
How
on Earth!?
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
解決策: もう一人が書き込むまで待つSolution: Wait until another writes to read
名簿は... おや、伊藤くんが考え中みたいだRoster… Oh, Ito is considering deeply.
伊藤くんが書き終わるまで待ってあげよう!I’ll do after his writing!
名前をつけよう
- 「どちらかが読み取った後、書き込む前にもう一方
が読み取ってしまうと、矛盾が生じてしまう!」問題
→データ競合
“It makes conflicts that one’s reading between another’s reading and writing” Problem: Data Race
- 「もう一人が書き終わるまで待つ」こと→MutexTo “wait until another writes to read”: Mutex
名簿は... おや、伊藤さんが考え中みたいだRoster… Oh, Ito is considering deeply.
どうやってMutexする?
伊藤さんが書き終わるまで待ってあげよう!I’ll do after his writing!
他人をブロックする?
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
一人増やせばいいんだから...ええと...I only have to increase it by one… well...
父さんはここを絶対に通さん!I never make way for you!
...なにっ!
...Shit!
他人をブロックする?
...フッ 残像だ。ブォン
ハァッ!!!
なにっ、お前、もしや別CPU...!
他人をブロックする?
「割り込みの禁止」と呼ばれる行為マルチプロセッサではうまく動作しない(他CPUまではブロックできない!)
札を置いておく?
使用中の札をかけておこう
書き換えた! 札を外しておこう
札を置いておく?
札がないから、誰も使用中じゃないな!
・・・
使用中の札をかけておこう
札を置いておく?
札がないから、誰も使用中じゃないな!
札がないから、誰も使用中じゃないな!
使用中の札をかけておこう
使用中の札をかけておこう
札を置いておく?
札がないから、誰も使用中じゃないな!
札がないから、誰も使用中じゃないな!
使用中の札をかけておこう
Ha?
使用中の札をかけておこう
札を置いておく?
札がないから、誰も使用中じゃないな!
札がないから、誰も使用中じゃないな!
使用中の札をかけておこう
読み取りReading
読み取りReading
書き込みWriting
書き込みWriting
二の舞いやん!!!
It’s same m
istake!
しかしそこに颯爽と現れる!
DekkerのアルゴリズムDekker’s Algorithm
「札」の改良アルゴリズム
- デッカーのアルゴリズム
- ピーターソンのアルゴリズム
- ランポートのパン屋のアルゴリズム
- Szymanskiのアルゴリズム
- Taubenfeld の Black-White Bakery Algorithm
世の中には頭のいい人がいる
- デッカーのアルゴリズム
- ピーターソンのアルゴリズム
- ランポートのパン屋のアルゴリズム
- Szymanskiのアルゴリズム
- Taubenfeld の Black-White Bakery Algorithmこれを実装してみよう!
元のプログラム
元のプログラム
元のプログラム
元のプログラム
元のプログラム
元のプログラム
結果は!?
元のプログラム
Race Condition!!
Dekkerのアルゴリズムを使用/Use Dekker’s Algorithm
Dekkerのアルゴリズムを使用/Use Dekker’s Algorithm
Dekkerのアルゴリズムを使用/Use Dekker’s Algorithm
Dekkerのアルゴリズムを使用/Use Dekker’s Algorithm
結果は!?
えーっ!
こんだけためといて
こんだけためといて
なんでやねん!
うまく行かなかった理由
ハッ...! もしや... Out of Order実行!?
「「「残像だ」」」ブォォン!!
Out-of-Order実行とはWhat’s Out-of-Order execution
Out-of-Order実行とはWhat’s Out-of-Order execution
実行順序を守らない実行である!Not to keep execution in the decided order!
何だって
OMG
- プロセッサは高速化のために、先に実行
できる命令から先に実行することがある
- 現代ではほとんどのプロセッサが
Out-of-Order実行をする
- コンパイラも似たような最適化を行うこと
がある
Out-of-Order実行とはWhat’s Out-of-Order execution
全てのアルゴリズムが使えない
- Dekkerのアルゴリズムなど、先ほど挙げ
た同期のためのアルゴリズムは
Out-of-Order実行されるプロセッサ上で
は正しく動作しない
行き詰まったReached to deadlock.
Mutexは実装できないMutex can’t be implemented.
その声は...
ブォォン!!ブォォォン!!!
...ハズレダ
モウイチド サイショカラ
カンガエロ
最初?
ソウダ
オマエナラデキル
ヒントハ...
「待とうとしないこと」「ハードウェア」
Good Luck!
解決策: 読んだ後誰かが書き換えていないか確認するSolution: Check if anyone rewrite it or not
この名簿... くんくん...This roster… Sniff sniff...
なんだか伊藤さんが書き換えたにおいがするぞIt’s like the smells of Ito’s rewriting.
最初から計算し直そうStart over again.
解決策: 書き込むときに変わっていないか確認するSolution: Check if it changed or not when write
4人にすればいいんだな! 書き換えよう!It’s 4! Let’s rewrite!
あれ、既に3人から4人に書き換わってる...?Hmm, it was changed from 3 to 4 already…?
なにか変だから最初から計算し直すかSomething is wrong so start over again.
LL&SCとはWhat’s LL&SC?
- Load-Link and Store-Conditional- Alpha、PowerPC、MIPS、ARM等に定
義される命令
CASとはWhat’s CAS?
- Compare-And-Swap- Intel, AMD and so on
LL&SCとはWhat’s LL&SC?
● LL命令→読み取り
LL Instruction→Reading● SC命令→書き込み。しかしもし前回のLL命令の
後誰かが書き換えた形跡があれば、命令は失敗
する
SC Instruction→Writing. But if updates have occurred since last LL instruction, it fails.
Increment関数の全体and it can be called in Golang as function.
LL命令で読み込みRead with LL instruction.
1を足し、Add 1.
SC命令で書き込む。Write the value with SC instruction.
もしSC命令が失敗したら、戻ってもう一度If SC instruction fails, go back and try again.
こんな感じでアセンブリを書くとWrite assembly such like,
Go言語で関数として呼び出せるand it can be called in Golang as function.
動け!Works!
よかった〜I’m relieved.
ん?Hmm?
LL&SC使えばWith LL&SC,
使用中の札をかけておこう
札を置いておく?
札がないから、誰も使用中じゃないな!
札がないから、誰も使用中じゃないな!
使用中の札をかけておこう
読み取りReading
読み取りReading
書き込みWriting
書き込みWriting
使用中の札をかけておこう
札を置いておく?
札がないから、誰も使用中じゃないな!
札がないから、誰も使用中じゃないな!
使用中の札をかけておこう
読み取りReading
読み取りReading
書き込みWriting
書き込みWriting
これもできんじゃね?I can also this, possibly?
LL命令でフラグの内容を読み取り。Read the value of flag with SC instruction.
SC命令で”1”を書き込むWrite “1” to it with SC instruction.
もしSC命令が失敗したら、戻ってもう一度If SC instruction fails, go back and try again.
もし既にフラグが”1”だったら、戻ってもう一回(スピンロック)If the value of the flag is “1”, go back and try again.(Spin Lock)
The whole code.
lock()では、lockedフラグ(札)を1に変更(さっきのアセンブリ)In lock(), change the value of locked flag to 1(the assembly)
unlock()では、lockedフラグを0に変更In lock(), change it to 0
あとはlock()とunlock()で囲めば...And, put it between lock() and unlock()...
できた!It worked!
まとめ
MutexはLL&SCやCAS等の
アセンブリ命令を使用すると作れる!(それ以外だと厳しい)
Mutex can implemented byassembly instructions like LL&SC or CAS.
(The other ways will be difficult)