-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresentation-output.html
988 lines (800 loc) · 24.5 KB
/
presentation-output.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
<!--
Google IO 2012 HTML5 Slide Template
Authors: Eric Bidelman <[email protected]>
Luke Mahe <[email protected]>
URL: https://code.google.com/p/io-2012-slides
-->
<!DOCTYPE html>
<html>
<head>
<title>Vancouver's Haskell Meetup</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">-->
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0">-->
<!--This one seems to work all the time, but really small on ipad-->
<!--<meta name="viewport" content="initial-scale=0.4">-->
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" media="all" href="theme/css/default.css">
<link rel="stylesheet" media="all" href="theme/css/app.css">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="theme/css/phone.css">
<base target="_blank"> <!-- This amazingness opens all links in a new tab. -->
<script data-main="js/slides" src="js/require-1.0.8.min.js"></script>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<!-- <slide class="logoslide nobackground"> -->
<!-- <article class="flexbox vcenter"> -->
<!-- <span><img src="images/google_developers_logo.png"></span> -->
<!-- </article> -->
<!-- </slide> -->
<slide class="fill nobackground" style="background-image: url(images/screen-slideshow-BirdseyeSimple.jpg);">
</slide>
<slide class="title-slide segue nobackground">
<aside class="gdbar"><img src="images/haskell_logo.png"></aside>
<!-- <aside class="gdbar"><img src="images/Github-BirdsEyeAvatar2-transp.png"></aside> -->
<!-- The content of this hgroup is replaced programmatically through the slide_config.json. -->
<hgroup class="auto-fadein">
<h1 data-config-title><!-- populated from slide_config.json --></h1>
<h2 data-config-subtitle><!-- populated from slide_config.json --></h2>
<p data-config-presenter><!-- populated from slide_config.json --></p>
</hgroup>
</slide>
<slide class="big" >
<hgroup>
<h2>Disclaimer</h2>
<h3></h3>
</hgroup>
<article ><p>About concurrency, not parallelism</p>
<p>Parallelism:</p>
<ul>
<li>Program does same thing regardless of the amount of cores</li>
<li>Used to speed up pure (non-IO monad) Haskell code</li>
</ul>
<p>Concurrency:</p>
<ul>
<li>IO Monad everywhere!</li>
<li>Effects from multiple threads are interleaved (non-deterministic)</li>
<li>Necessary to deal with multiple sources of input/output</li>
</ul>
<aside class="note">
<ul>
<li>Independently of the # of cores, it will do the same thing</li>
<li>pure</li>
<li>IO Monad everywhere, at least the construct parts</li>
<li>All actions are hapenning at the same time, results may vary in different executions</li>
<li>User Input / Server Background input, etc</li>
</ul>
</aside></article>
</slide>
<slide class="big image" >
<hgroup>
<h2></h2>
<h3></h3>
</hgroup>
<article ><article class="flexbox vcenter">
<img src="images/con_and_par.jpg" width="500px" height="400px" alt="Desktop Pic" title="Desktop Pic">
<footer class="source">Courtesy of Joe Armstrong</footer>
</article></article>
</slide>
<slide class="big" >
<hgroup>
<h2>Agenda</h2>
<h3></h3>
</hgroup>
<article ><p>Things we'll cover:</p>
<ul class="build">
<li>
<p>Basic Concurrency:</p>
<ul class="build">
<li>forkIO</li>
<li>MVars</li>
</ul>
</li>
<li>
<p>Async Exceptions:</p>
<ul class="build">
<li>cancellation</li>
<li>timeout</li>
</ul>
</li>
<li>
<p>Software Transaction Memory (STM)</p>
</li>
</ul></article>
</slide>
<slide class="segue nobackground dark" >
<aside class="gdbar"><img src="images/haskell_logo.png"></aside>
<hgroup class="auto-fadein">
<h2>Threading</h2>
<h3>using forkIO</h3>
</hgroup>
</slide>
<slide >
<hgroup>
<h2>Forking threads</h2>
<h3></h3>
</hgroup>
<article ><pre class="prettyprint lang-hs bigger" data-lang="HASKELL">
forkIO :: IO () -> IO ThreadId
</pre>
<ul>
<li>Creates a new thread to execute the given IO action</li>
<li>new thread runs "at the same time" as the current thread.</li>
</ul>
<aside class="note">
<ul>
<li>What can you do with a ThreadId?</li>
<li> Compare threads </li>
<li> Send messages to threads (more on that later)</li>
</ul>
</aside></article>
</slide>
<slide >
<hgroup>
<h2>Interleaving example</h2>
<h3></h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
import Control.Concurrent
import Control.Monad
import System.IO
main = do
hSetBuffering stdout NoBuffering
<b>-- forkIO :: IO () -> IO ThreadId</b>
forkIO $ forever (putChar 'A')
forkIO $ forever (putChar 'B')
threadDelay (10 ^ 6)
</pre>
<pre class="" data-lang="BASH">
$ ghc fork.hs
[1 of 1] Compiling Main ( fork.hs, fork.o ) Linking fork ...
$ ./fork | tail -c 159
AAAAAAAAABABABABABABABABABABABABABABABABABABABABABABAB
ABABABABABABABABABABABABABABABABABABABABABABABABABABAB
ABABABABABABABABABABABABABABABABABABABABABABABABABABAB
</pre></article>
</slide>
<slide >
<hgroup>
<h2>A note about performance</h2>
<h3>Green threads are freaking cheap!</h3>
</hgroup>
<article ><p>1 MILLION green threads require around 1.5 GB of memory space (approx. 1.5k per thread)</p>
<p><center>
<img src="/images/use_the_forkio.jpeg" class="reflect" alt="Use the forkIO!">
</center></p>
<aside class="note">
They are not system threads, this threads are managed by an Internal IO scheduler</br>
Feel free to use as much of them as you need
</aside></article>
</slide>
<slide class="segue nobackground dark" >
<aside class="gdbar"><img src="images/haskell_logo.png"></aside>
<hgroup class="auto-fadein">
<h2>Communication</h2>
<h3>using MVars</h3>
</hgroup>
</slide>
<slide >
<hgroup>
<h2>MVar</h2>
<h3>The most basic concurrency tool in Haskell</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
data MVar a -- Abstract Constructor
newEmptyMVar :: IO (MVar a)
takeMVar :: MVar a -> IO a
putMVar :: MVar a -> a -> IO ()
</pre>
<ul>
<li>Wraps a value (Image a wrapper class in OO land)</li>
<li>It has 2 states: <b>empty</b> or <b>full</b></li>
<li>When full and want to put a value, <b>blocks</b></li>
<li>When empty and want to take a value, <b>blocks</b></li>
<li>Doesn't block otherwise</li>
</ul>
<aside class="note">
<ul>
<li>Like to think of an MVar as a Pint of Beer, Beer => Content; Glass => MVar</li>
<li>Glass can have two states, filled with drink or empty</li>
<li>Can't drink from an empty glass, wait/block until is full</li>
<li>Can't fill up a full glass, wait/block until it is consumed</li>
</ul>
</aside></article>
</slide>
<slide >
<hgroup>
<h2>Downloading URLs concurrently</h2>
<h3>First attempt</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
getURL :: String -> IO String
downloadPages = do
m1 <- newEmptyMVar
m2 <- newEmptyMVar
forkIO $ getURL "http://www.wikipedia.org/wiki/haskell" >>= putMVar m1
forkIO $ getURL "http://www.haskell.org/" >>= putMVar m2
<b>r1 <- takeMVar m1
r2 <- takeMVar m2</b>
return (r1, r2)
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Downloading URLs concurrently</h2>
<h3>Abstracting the pattern</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
newtype Async a = Async (MVar a)
async :: IO a -> IO (Async a)
async action = do
m <- newEmptyMVar
forkIO $ do { result <- action; putMVar m result; }
return (Async m)
wait :: Async a -> IO a
wait (Async m) = <b>readMVar m</b>
-- ^ read != take, doesn't take out the value from
-- the MVar box
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Downloading URLs concurrently</h2>
<h3>Second Attempt</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
downloadPages = do
a1 <- async $ getURL "http://www.wikipedia.org/wiki/haskell"
a2 <- async $ getURL "http://www.haskell.org/"
r1 <- wait a1
r2 <- wait a2
return (r1, r2)
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Downloading URLs concurrently</h2>
<h3>Third Attempt</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
sites = [ "http://www.google.com"
, "http://www.bing.com"
...
]
downloadPages = mapM <b>(async . http)</b> sites >>= <b>mapM wait</b>
where
http url = do
(result, time) <- timeit $ getURL url
printf "downloaded: %s (%d bytes, %.2fs)\n" url (length result) time
</pre>
<pre class="" data-lang="OUTPUT">
downloaded: http://www.google.com (14524 bytes, 0.17s)
downloaded: http://www.bing.com (24740 bytes, 0.18s)
downloaded: http://www.yahoo.com (153065 bytes, 1.11s)
</pre></article>
</slide>
<slide >
<hgroup>
<h2>An MVar could be:</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>
<p><b>lock</b></p>
<ul>
<li>`<code>MVar ()</code>` behaves like a lock: full is unlocked, empty is locked</li>
<li>Can be used as a mutex to protect some other shared state</li>
</ul>
</li>
<li>
<p><b>one-spot-channel</b></p>
<ul>
<li><code>MVars</code> holds at most one value => async channel of buffer-size one</li>
</ul>
</li>
<li>
<p><b>building block</b></p>
<ul>
<li><code>MVars</code> can be used to build other concurrent data
structures / abstractions (e.g <code>Async</code>)</li>
</ul>
</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>A note on fairness in MVars</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>Blocks in FIFO fashion</li>
<li><code>putMVar</code> wakes exactly <b>one</b> thread and is atomic</li>
<li><code>MVar</code> contention may be expensive.</li>
<li>Fairness can lead to alternation when two threads compete for an MVar<ul>
<li>thread A: takeMVar (succeeds)</li>
<li>thread B: takeMVar (blocks)</li>
<li>thread A: putMVar (succeeds, and wakes thread B)</li>
<li>thread A: takeMVar again (blocks)</li>
<li>Cannot break cycle until a thread is pre-empted (re-scheduled)
while MVar is full</li>
</ul>
</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Unbound Channels</h2>
<h3></h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
data Chan a
newChan :: IO (Chan a)
writeChan :: Chan a -> a -> IO ()
readChan :: Chan a -> IO a
</pre>
<ul>
<li>Writes to the Chan don't block</li>
<li>Reads to Chan don't block until Chan is empty</li>
<li>Great to use as a task queue on a producer/consumer scenario</li>
<li>Implemented using a list-like of MVars internally</li>
</ul></article>
</slide>
<slide class="segue nobackground dark" >
<aside class="gdbar"><img src="images/haskell_logo.png"></aside>
<hgroup class="auto-fadein">
<h2>Interruption / Cancelation</h2>
<h3>asynchrounous exceptions</h3>
</hgroup>
</slide>
<slide >
<hgroup>
<h2>Motivations</h2>
<h3></h3>
</hgroup>
<article ><ul>
<li>There are many cases when we want to interrupt a thread:<ul>
<li>Web browser stop button</li>
<li>Timeout on slow connections</li>
<li>Cancel an operation that is pending, but not needed any more</li>
<li>etc.</li>
</ul>
</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Interrupting a thread</h2>
<h3>introducing `throwTo`</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
throwTo :: Exception e => ThreadId -> e -> IO ()
</pre>
<ul>
<li>Throws an async exception <code>e</code> on the thread pointed by the <code>ThreadId</code></li>
<li>
<p>Interruption appears as an exception</p>
<ul>
<li>Good: We need exception handling to clean up possible errors, the
same handlers could be used for interruptions too.</li>
<li>Exception safe code will be fine with an interruption.</li>
</ul>
<p><pre class="prettyprint lang-hs" data-lang="HASKELL">
bracket (newTempFile "temp") -- open
(\file -> removeFile file) -- clenaup
(\file -> ...) -- do
</pre></p>
</li>
</ul>
<aside class="note">
Remember the ThreadId from forkIO?
</aside></article>
</slide>
<slide >
<hgroup>
<h2>Example</h2>
<h3>Extending Async to handle interruptions</h3>
</hgroup>
<article ><p>Let's add a cancel function to the Async type</p>
<pre class="prettyprint lang-hs" data-lang="HASKELL">
newtype Async a = Async (MVar a)
async :: IO a -> IO (Async a)
async io = do
m <- newEmptyMVar
forkIO $ do r <- io; putMVar m r
return (Async m)
wait :: Async a -> IO a
wait (Async m) = readMVar m
-- we want to add
<b>cancel :: Async a -> IO ()</b>
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Example</h2>
<h3>Modify Async definition</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
data Async a = Async <b>ThreadId</b> (MVar a)
async :: IO a -> IO (Async a)
async io = do
m <- newEmptyMVar
<b>tid</b> <- forkIO $ do r <- io; putMVar m r
return (Async <b>tid</b> m)
<b>
cancel :: Async a -> IO ()
cancel (Async tid _) = throwTo tid ThreadKilled
</b>
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Example</h2>
<h3>Modify Async definition</h3>
</hgroup>
<article ><p>But what about <b>wait</b>? Previously it had type</p>
<pre class="prettyprint lang-hs" data-lang="HASKELL">
wait :: Async a -> IO ()
</pre>
<p>Should it return if the <code>Async</code> was cancelled?</p>
<p>Cancellation is an exception, so wait should return the Exception
that was thrown...</p>
<p><b>Extra WIN</b>: safe handling of other errors as well</p>
<aside class="note">
Not just that, what about errors? we naively designed
this thinking that IO actions won't have errors on them.
</aside></article>
</slide>
<slide >
<hgroup>
<h2>Example</h2>
<h3>Async with exception support</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
newtype Async a = Async ThreadId <b>(MVar (Either SomeException a))</b>
async :: IO a -> IO (Async a)
async io = do
m <- newEmptyMVar
tid <- forkIO $ <b>returnRight `catch` returnLeft</b>
where
<b>returnRight = do
r <- io
putMVar m (Right r)
returnLeft (e :: SomeException) =
putMVar m (Left e)</b>
<b>wait :: Async a -> IO (Either SomeException a)</b>
wait (Async _ var) = readMVar var
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Example</h2>
<h3>Using Async with cancellation</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
main = do
asyncs <- mapM (async . http) sites
forkIO $ do
hSetBuffering stdin NoBuffering
forever $ do
c <- getChar
-- hit q stops downloads
<b>when (c == 'q') $ mapM_ cancel asyncs</b>
rs <- mapM wait asyncs
-- ^ blocks until all async actions are done / cancelled
printf "%d/%d finished\n" (length (rights rs)) (length rs)
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Example</h2>
<h3>Output</h3>
</hgroup>
<article ><pre class="" data-lang="BASH">
./geturlscancel
downloaded: http://www.google.com (14538 bytes, 0.17s)
downloaded: http://www.bing.com (24740 bytes, 0.22s)
q2/5 Finished
</pre>
<p>Points to note:</p>
<ul class="build">
<li>
<p>We are using a complicated HTTP library underneath, yet it
supports interruption automatically</p>
</li>
<li>
<p>Having async interruption be the default is powerful</p>
</li>
<li>
<p>Not a silver bullet: With truly mutable state, interruptions
can be difficult.</p>
</li>
<li>
<p>STATE PROPAGATED EVERYWHERE == COMPLEXITY</p>
</li>
</ul></article>
</slide>
<slide class="segue nobackground dark" >
<aside class="gdbar"><img src="images/haskell_logo.png"></aside>
<hgroup class="auto-fadein">
<h2>Software Transactional Memory</h2>
<h3></h3>
</hgroup>
</slide>
<slide >
<hgroup>
<h2>STM</h2>
<h3>What is it?</h3>
</hgroup>
<article ><ul>
<li>
<p>An alternative to MVar for managing</p>
<ul>
<li>shared state</li>
<li>communication</li>
</ul>
</li>
<li>
<p>STM has several advantages:</p>
<ul>
<li>compositional (Monads FTW)</li>
<li>much easier to get right (no deadlocks)</li>
<li>much easier to manage error conditions (async exceptions included)</li>
</ul>
</li>
</ul>
<aside class="note">
<ul>
<li>Imagine Database Transactions happening in Memory</li>
<li>Same approach, have an execution log, sync at the very end,
were any vars modified while doing this? YAY try again, Nay, good to go</li>
</ul>
</aside></article>
</slide>
<slide >
<hgroup>
<h2>Example</h2>
<h3>A Window Manager</h3>
</hgroup>
<article ><article class="flexbox vcenter">
<img src="images/Desktop.png" width="500px" height="400px" alt="Desktop Pic" title="Desktop Pic">
</article>
<aside class="note">
No crazy UI code here, just logic state, how can we represent this?
</aside></article>
</slide>
<slide >
<hgroup>
<h2>Window Manager</h2>
<h3>Implementation details</h3>
</hgroup>
<article ><p>Suppose we want to have one thread for each input/output stream:</p>
<ul>
<li>On thread to listen to the user</li>
<li>One thread for each client application</li>
<li>One thread to render the display</li>
</ul>
<p>All threads share the state of the desktops at the same time.</p>
<p>How should we represent this using Haskell's toolbelt?</p></article>
</slide>
<slide >
<hgroup>
<h2>Window Manager</h2>
<h3>Option 1: a single MVar for the whole state</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
type Display = MVar (Map Desktop (Set Window))
</pre>
<p>Advantages:</p>
<ul>
<li>Simple</li>
</ul>
<p>Disadvantages:</p>
<ul>
<li>Single point of contention.<ul>
<li>Missbehaving thread can block everyone else</li>
<li>Performance penalties</li>
</ul>
</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Window Manager</h2>
<h3>Option 2: an MVar per Desktop</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
<strike>type Display = MVar (Map Desktop (Set Window))</strike>
type Display = Map Desktop (MVar (Set Window))
</pre>
<p>Avoids single point of contention, but new problem emerges:</p>
<pre class="prettyprint lang-hs" data-lang="HASKELL">
moveWindow :: Display -> Window -> Desktop -> Desktop -> IO ()
moveWindow disp win desktopA desktopB = do
<b>windowSetA <- takeMVar mWindowSetA
-- other threads say hello to inconsistent intermmidate state
windowSetB <- takeMVar mWindowSetB</b>
putMVar mWindowSetA (Set.delete win windowSetA)
putMVar mWindowSetB (Set.insert win windowSetB)
where
mWindowSetA = fromJust (Map.lookup desktopA disp)
mWindowSetB = fromJust (Map.lookup desktopB disp)
</pre></article>
</slide>
<slide >
<hgroup>
<h2>Window Manager</h2>
<h3>Dinning Philosophers</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
moveWindow disp win desktopA desktopB = do
<b>windowSetA <- takeMVar mWindowSetA
windowSetB <- takeMVar mWindowSetB</b>
...
</pre>
<ul class="build">
<li>Thread 1 (T1): calls <code>moveWindow disp w1 a b</code></li>
<li>Thread 2 (T2): calls <code>moveWindow disp w2 b a</code></li>
<li>T1 takes <code>MVar</code> of <code>Desktop a</code></li>
<li>T2 takes <code>MVar</code> of <code>Desktop b</code></li>
<li>T1 tries to take <code>MVar</code> for <code>Desktop b</code>, blocks...</li>
<li>T2 tries to take <code>MVar</code> for <code>Desktop a</code>, blocks...</li>
<li><b>DEADLOCK</b></li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Window Manager</h2>
<h3>Can we solve this with MVars?</h3>
</hgroup>
<article ><p>We could, but requires a high price:</p>
<ul class="build">
<li>Impose fixed ordering on <code>MVars</code>, make <code>takeMVar</code> calls in the same order in <b>every</b> thread.<ul class="build">
<li>Libraries must obey this rules</li>
<li>Error-Checking can be done at runtime, complicated...</li>
<li><img src="images/fuckthat.jpg"></img></li>
</ul>
</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>Window Manager</h2>
<h3>STM to the rescue</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
type Display = Map Desktop <b>(TVar (Set Window))</b>
moveWindow :: Display -> Window -> Desktop -> Desktop -> IO ()
moveWindow disp win a b = <b>atomically</b> $ do
wa <- readTVar ma
wb <- readTVar mb
writeTVar ma (Set.delete win wa)
writeTVar mb (Set.insert win wb)
where
ma = fromJust (Map.lookup a disp)
mb = fromJust (Map.lookup a disp)
</pre>
<ul>
<li>Operations inside <code>atomically</code> happen indivisibly to the rest of the program (transaction)</li>
<li>Ordering is irrelevant - we can interleave read/write actions</li>
</ul></article>
</slide>
<slide >
<hgroup>
<h2>STM</h2>
<h3>Basic API</h3>
</hgroup>
<article ><pre class="prettyprint lang-hs" data-lang="HASKELL">
data STM a -- abstract
instance Monad STM -- amongst other things
atomically :: STM a -> IO a
data TVar a -- abstract
newTVar :: STM (TVar a)
readTVar :: TVar a -> STM a
writeTVar :: TVar a -> a -> STM ()
data TChan a -- abstract
newTChan :: STM (TChan a)
readTChan :: TChan a -> STM a
writeTChan :: TChan a -> a -> STM ()
</pre>
<p>Implementation doesn't use a global lock, two transactions operating on
disjoint sets of TVars can work simultaneously.</p></article>
</slide>
<slide >
<hgroup>
<h2>STM</h2>
<h3>Composable Transactions</h3>
</hgroup>
<article ><p>Write an operation to swap to Windows</p>
<pre class="prettyprint lang-hs" data-lang="HASKELL">
swapWindows :: Display
-> Window -> Desktop
-> Window -> Desktop
-> IO ()
</pre>
<p>With <code>MVars</code> we would have to write a special purpose routine, on STM on the other hand</p>
<pre class="prettyprint lang-hs" data-lang="HASKELL">
swapWindows disp w a v b = <b>atomically</b> $ do
moveWindowsSTM disp w a b
moveWindowsSTM disp v b a
-- moveWindows fn seen previously on the STM monad
</pre>
<p>STM allows composition of stateful operations into larger transactions</p></article>
</slide>
<slide >
<hgroup>
<h2>STM</h2>
<h3>Blocking</h3>
</hgroup>
<article ><p>Concurrent algorithms often times need a way to block execution
to wait for some condition to comply</p>
<pre class="prettyprint lang-hs" data-lang="HASKELL">
retry :: STM a
</pre>
<p>Semantics of retry is "try current transaction again", when a TVar in the transaction
changes (no busy loop)</p>
<pre class="prettyprint lang-hs" data-lang="HASKELL">
atomically $ do
x <- readTVar v
if x == 0
then retry
else return x
</pre>
<p>This thread will resume when some other thread puts in the <code>TVar v</code>
a value that is not 0.</p></article>
</slide>
<slide >
<hgroup>
<h2>STM</h2>
<h3>Benefits and Woes</h3>
</hgroup>
<article ><ul>
<li>Composable atomicity</li>
<li>Composable blocking</li>
<li>Robustness: easy error handling</li>
<li>Slow on very large transactions</li>
<li>Why would you use MVar?<ul>
<li>fairness</li>
<li>single wakeup</li>
<li>performance</li>
</ul>
</li>
</ul></article>
</slide>
<slide class="thank-you-slide segue nobackground">
<aside class="gdbar right"><img src="images/haskell_logo.png"></aside>
<article class="flexbox vleft auto-fadein">
<h2><Thank You!></h2>
</article>
<p class="auto-fadein" data-config-contact>
<!-- populated from slide_config.json -->
<br>
<a href="http://twitter.com/birdseye_sw">@birdseye_sw</a><br>
<a href="http://twitter.com/romanandreg">@romanandreg</a><br>
<br>
<a href="http://birdseye-software.com">http://birdseye-software.com</a>
</p>
</slide>
<!-- <slide class="logoslide dark nobackground"> -->
<!-- <article class="flexbox vcenter"> -->
<!-- <span><img src="images/google_developers_logo_white.png"></span> -->
<!-- </article> -->
<!-- </slide> -->
<slide class="backdrop"></slide>
</slides>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!--[if IE]>
<script src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js"></script>
<script>CFInstall.check({mode: 'overlay'});</script>
<![endif]-->
</body>
</html>