114
Mutexを実装する Implementation of Mutex The 17/01/05 MTG of Eyes, JAPAN ito

Mutexを実装する implementation of mutex

Embed Size (px)

Citation preview

Page 1: Mutexを実装する implementation of mutex

Mutexを実装するImplementation of Mutex

The 17/01/05 MTG of Eyes, JAPANito

Page 2: Mutexを実装する implementation of mutex

自己紹介/Self-Introduction- 伊藤勇希/Yuki Ito- ito/acomagu/publmag1- おすすめのメイドカフェはシャッツ

キステ/橙幻郷(ノマドに最適)My favorite Maid-Cafe: Schatzkiste, TouGenKyo

- ももくりが終わっちゃって悲しい

I’m really sad for end of MomoKuri.

Page 3: Mutexを実装する implementation of mutex

今夜はみんなの目線をLock!Today, I gonna Lock your eyes at me!

Page 4: Mutexを実装する implementation of mutex

目次/Index1. 並行処理/並列処理とは?

What is concurrent/parallel processing?

2.

What are Atomic, Semaphore and Mutex?

3. Mutexの実装 / Implement Mutex

4. Lockのその先へ / Beyond the Lock

Page 5: Mutexを実装する implementation of mutex

“並列処理/並行処理”“Concurrent/Parallel Processing”

Page 6: Mutexを実装する implementation of mutex

is

Page 7: Mutexを実装する implementation of mutex

何what

Page 8: Mutexを実装する implementation of mutex

“並列処理/並行処理”“Concurrent/Parallel Processing”

複数の仕事を同時にするDo multiple tasks at the same time

Page 9: Mutexを実装する implementation of mutex

複数の仕事を同時にするDo multiple tasks at the same time

Page 10: Mutexを実装する implementation of mutex

複数の仕事を同時にするDo multiple tasks at the same time

誰が / Who

Page 11: Mutexを実装する implementation of mutex

複数の仕事を同時にするDo multiple tasks at the same time

僕が / I

Page 12: Mutexを実装する implementation of mutex

複数の仕事を同時にするDo multiple tasks at the same time

僕と兼沢さんが

I and Kanesawa

Page 13: Mutexを実装する implementation of mutex

僕が / I複数の仕事を同時にする

Do multiple tasks at the same time

僕と兼沢さんがI and Kanesawa複数の仕事を同時にする

Do multiple tasks at the same time

Page 14: Mutexを実装する implementation of mutex

僕が / I複数の仕事を同時にする

Do multiple tasks at the same time

僕と兼沢さんがI and Kanesawa複数の仕事を同時にする

Do multiple tasks at the same time

並行処理Concurrent Processing

並列処理Parallel Processing

諸説あります

Page 15: Mutexを実装する implementation of mutex

まとめると

並行処理 → ひとつのCPUが複数の処理を

同時に(見えるように)行う

並列処理 → 複数のCPUが別の処理を同

時に行う

諸説あります

Page 16: Mutexを実装する implementation of mutex

ITO KANESAWA

Page 17: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!

あるお昼時 / One lunchtime

Page 18: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!

名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.

あるお昼時 / One lunchtime

Page 19: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!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

Page 20: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!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

Page 21: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!

あるお昼時 / One lunchtime

Page 22: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!

名簿を見ると、今頼む予定の人数は4人かFrom the roster, 4 people will do.

あるお昼時 / One lunchtime

Page 23: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!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

Page 24: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!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

Page 25: Mutexを実装する implementation of mutex

5人分たのむよ〜I’m ordering it for 5.

Page 26: Mutexを実装する implementation of mutex

5人分たのむよ〜I’m ordering it by 5.

SUCCESS!!

Page 27: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!

またあるお昼時 / Another lunchtime

Page 28: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!I’m so hungry! Oh right, let’s order a lunch!

名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.

またあるお昼時 / Another lunchtime

Page 29: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!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

Page 30: Mutexを実装する implementation of mutex

おなかへったー! そうだ、お弁当頼もう!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!

Page 31: Mutexを実装する implementation of mutex

名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.

またあるお昼時 / Another lunchtime

Page 32: Mutexを実装する implementation of mutex

名簿を見ると、今頼む予定の人数は3人かFrom the roster, 3 people will do.

一人増やせばいいんだから...ええと...I only have to increase it by one… well...

またあるお昼時 / Another lunchtime

Page 33: Mutexを実装する implementation of mutex

名簿を見ると、今頼む予定の人数は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

Page 34: Mutexを実装する implementation of mutex

名簿を見ると、今頼む予定の人数は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!

Page 35: Mutexを実装する implementation of mutex

4人分たのむよ〜I’m ordering it for 4.

Page 36: Mutexを実装する implementation of mutex

4人分たのむよ〜I’m ordering it by 4.Uh?

Page 37: Mutexを実装する implementation of mutex

整理すると / 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!

Page 38: Mutexを実装する implementation of mutex

どうすればいい!?

How

on Earth!?

Page 39: Mutexを実装する implementation of mutex

一人増やせばいいんだから...ええと...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!

Page 40: Mutexを実装する implementation of mutex

名前をつけよう

- 「どちらかが読み取った後、書き込む前にもう一方

が読み取ってしまうと、矛盾が生じてしまう!」問題

→データ競合

“It makes conflicts that one’s reading between another’s reading and writing” Problem: Data Race

- 「もう一人が書き終わるまで待つ」こと→MutexTo “wait until another writes to read”: Mutex

Page 41: Mutexを実装する implementation of mutex

名簿は... おや、伊藤さんが考え中みたいだRoster… Oh, Ito is considering deeply.

どうやってMutexする?

伊藤さんが書き終わるまで待ってあげよう!I’ll do after his writing!

Page 42: Mutexを実装する implementation of mutex

他人をブロックする?

一人増やせばいいんだから...ええと...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!

Page 43: Mutexを実装する implementation of mutex

他人をブロックする?

...フッ 残像だ。ブォン

ハァッ!!!

なにっ、お前、もしや別CPU...!

Page 44: Mutexを実装する implementation of mutex

他人をブロックする?

「割り込みの禁止」と呼ばれる行為マルチプロセッサではうまく動作しない(他CPUまではブロックできない!)

Page 45: Mutexを実装する implementation of mutex

札を置いておく?

Page 46: Mutexを実装する implementation of mutex

使用中の札をかけておこう

書き換えた! 札を外しておこう

札を置いておく?

札がないから、誰も使用中じゃないな!

・・・

Page 47: Mutexを実装する implementation of mutex

使用中の札をかけておこう

札を置いておく?

札がないから、誰も使用中じゃないな!

札がないから、誰も使用中じゃないな!

使用中の札をかけておこう

Page 48: Mutexを実装する implementation of mutex

使用中の札をかけておこう

札を置いておく?

札がないから、誰も使用中じゃないな!

札がないから、誰も使用中じゃないな!

使用中の札をかけておこう

Ha?

Page 49: Mutexを実装する implementation of mutex

使用中の札をかけておこう

札を置いておく?

札がないから、誰も使用中じゃないな!

札がないから、誰も使用中じゃないな!

使用中の札をかけておこう

読み取りReading

読み取りReading

書き込みWriting

書き込みWriting

Page 50: Mutexを実装する implementation of mutex

二の舞いやん!!!

It’s same m

istake!

Page 51: Mutexを実装する implementation of mutex

しかしそこに颯爽と現れる!

DekkerのアルゴリズムDekker’s Algorithm

Page 52: Mutexを実装する implementation of mutex

「札」の改良アルゴリズム

- デッカーのアルゴリズム

- ピーターソンのアルゴリズム

- ランポートのパン屋のアルゴリズム

- Szymanskiのアルゴリズム

- Taubenfeld の Black-White Bakery Algorithm

Page 53: Mutexを実装する implementation of mutex

世の中には頭のいい人がいる

- デッカーのアルゴリズム

- ピーターソンのアルゴリズム

- ランポートのパン屋のアルゴリズム

- Szymanskiのアルゴリズム

- Taubenfeld の Black-White Bakery Algorithmこれを実装してみよう!

Page 54: Mutexを実装する implementation of mutex

元のプログラム

Page 55: Mutexを実装する implementation of mutex

元のプログラム

Page 56: Mutexを実装する implementation of mutex

元のプログラム

Page 57: Mutexを実装する implementation of mutex

元のプログラム

Page 58: Mutexを実装する implementation of mutex

元のプログラム

Page 59: Mutexを実装する implementation of mutex

元のプログラム

結果は!?

Page 60: Mutexを実装する implementation of mutex

元のプログラム

Race Condition!!

Page 61: Mutexを実装する implementation of mutex

Dekkerのアルゴリズムを使用/Use Dekker’s Algorithm

Page 62: Mutexを実装する implementation of mutex

Dekkerのアルゴリズムを使用/Use Dekker’s Algorithm

Page 63: Mutexを実装する implementation of mutex

Dekkerのアルゴリズムを使用/Use Dekker’s Algorithm

Page 64: Mutexを実装する implementation of mutex

Dekkerのアルゴリズムを使用/Use Dekker’s Algorithm

結果は!?

Page 65: Mutexを実装する implementation of mutex
Page 66: Mutexを実装する implementation of mutex

えーっ!

Page 67: Mutexを実装する implementation of mutex

こんだけためといて

Page 68: Mutexを実装する implementation of mutex

こんだけためといて

なんでやねん!

Page 69: Mutexを実装する implementation of mutex

うまく行かなかった理由

ハッ...! もしや... Out of Order実行!?

「「「残像だ」」」ブォォン!!

Page 70: Mutexを実装する implementation of mutex

Out-of-Order実行とはWhat’s Out-of-Order execution

Page 71: Mutexを実装する implementation of mutex

Out-of-Order実行とはWhat’s Out-of-Order execution

実行順序を守らない実行である!Not to keep execution in the decided order!

Page 72: Mutexを実装する implementation of mutex

何だって

OMG

Page 73: Mutexを実装する implementation of mutex

- プロセッサは高速化のために、先に実行

できる命令から先に実行することがある

- 現代ではほとんどのプロセッサが

Out-of-Order実行をする

- コンパイラも似たような最適化を行うこと

がある

Out-of-Order実行とはWhat’s Out-of-Order execution

Page 74: Mutexを実装する implementation of mutex
Page 75: Mutexを実装する implementation of mutex
Page 76: Mutexを実装する implementation of mutex

全てのアルゴリズムが使えない

- Dekkerのアルゴリズムなど、先ほど挙げ

た同期のためのアルゴリズムは

Out-of-Order実行されるプロセッサ上で

は正しく動作しない

Page 77: Mutexを実装する implementation of mutex

行き詰まったReached to deadlock.

Page 78: Mutexを実装する implementation of mutex

Mutexは実装できないMutex can’t be implemented.

Page 79: Mutexを実装する implementation of mutex

その声は...

ブォォン!!ブォォォン!!!

...ハズレダ

Page 80: Mutexを実装する implementation of mutex

モウイチド サイショカラ

カンガエロ

Page 81: Mutexを実装する implementation of mutex

最初?

Page 82: Mutexを実装する implementation of mutex

ソウダ

オマエナラデキル

ヒントハ...

Page 83: Mutexを実装する implementation of mutex

「待とうとしないこと」「ハードウェア」

Page 84: Mutexを実装する implementation of mutex

Good Luck!

Page 85: Mutexを実装する implementation of mutex

解決策: 読んだ後誰かが書き換えていないか確認するSolution: Check if anyone rewrite it or not

この名簿... くんくん...This roster… Sniff sniff...

なんだか伊藤さんが書き換えたにおいがするぞIt’s like the smells of Ito’s rewriting.

最初から計算し直そうStart over again.

Page 86: Mutexを実装する implementation of mutex

解決策: 書き込むときに変わっていないか確認する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.

Page 87: Mutexを実装する implementation of mutex

LL&SCとはWhat’s LL&SC?

- Load-Link and Store-Conditional- Alpha、PowerPC、MIPS、ARM等に定

義される命令

Page 88: Mutexを実装する implementation of mutex

CASとはWhat’s CAS?

- Compare-And-Swap- Intel, AMD and so on

Page 89: Mutexを実装する implementation of mutex

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.

Page 90: Mutexを実装する implementation of mutex

Increment関数の全体and it can be called in Golang as function.

Page 91: Mutexを実装する implementation of mutex

LL命令で読み込みRead with LL instruction.

Page 92: Mutexを実装する implementation of mutex

1を足し、Add 1.

Page 93: Mutexを実装する implementation of mutex

SC命令で書き込む。Write the value with SC instruction.

Page 94: Mutexを実装する implementation of mutex

もしSC命令が失敗したら、戻ってもう一度If SC instruction fails, go back and try again.

Page 95: Mutexを実装する implementation of mutex

こんな感じでアセンブリを書くとWrite assembly such like,

Page 96: Mutexを実装する implementation of mutex

Go言語で関数として呼び出せるand it can be called in Golang as function.

Page 97: Mutexを実装する implementation of mutex

動け!Works!

Page 98: Mutexを実装する implementation of mutex
Page 99: Mutexを実装する implementation of mutex

よかった〜I’m relieved.

Page 100: Mutexを実装する implementation of mutex

ん?Hmm?

Page 101: Mutexを実装する implementation of mutex

LL&SC使えばWith LL&SC,

Page 102: Mutexを実装する implementation of mutex

使用中の札をかけておこう

札を置いておく?

札がないから、誰も使用中じゃないな!

札がないから、誰も使用中じゃないな!

使用中の札をかけておこう

読み取りReading

読み取りReading

書き込みWriting

書き込みWriting

Page 103: Mutexを実装する implementation of mutex

使用中の札をかけておこう

札を置いておく?

札がないから、誰も使用中じゃないな!

札がないから、誰も使用中じゃないな!

使用中の札をかけておこう

読み取りReading

読み取りReading

書き込みWriting

書き込みWriting

これもできんじゃね?I can also this, possibly?

Page 104: Mutexを実装する implementation of mutex

LL命令でフラグの内容を読み取り。Read the value of flag with SC instruction.

Page 105: Mutexを実装する implementation of mutex

SC命令で”1”を書き込むWrite “1” to it with SC instruction.

Page 106: Mutexを実装する implementation of mutex

もしSC命令が失敗したら、戻ってもう一度If SC instruction fails, go back and try again.

Page 107: Mutexを実装する implementation of mutex

もし既にフラグが”1”だったら、戻ってもう一回(スピンロック)If the value of the flag is “1”, go back and try again.(Spin Lock)

Page 108: Mutexを実装する implementation of mutex

The whole code.

Page 109: Mutexを実装する implementation of mutex

lock()では、lockedフラグ(札)を1に変更(さっきのアセンブリ)In lock(), change the value of locked flag to 1(the assembly)

Page 110: Mutexを実装する implementation of mutex

unlock()では、lockedフラグを0に変更In lock(), change it to 0

Page 111: Mutexを実装する implementation of mutex

あとはlock()とunlock()で囲めば...And, put it between lock() and unlock()...

Page 112: Mutexを実装する implementation of mutex
Page 113: Mutexを実装する implementation of mutex

できた!It worked!

Page 114: Mutexを実装する implementation of mutex

まとめ

MutexはLL&SCやCAS等の

アセンブリ命令を使用すると作れる!(それ以外だと厳しい)

Mutex can implemented byassembly instructions like LL&SC or CAS.

(The other ways will be difficult)