lecture14 locking

Upload: lk

Post on 04-Feb-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/21/2019 Lecture14 Locking

    1/80

    CSE 332 Data Abstractions:

    Data Races and Memory,

    Reordering, Deadlock,Readers/Writer Locks, andCondition Variables (oh my!)

    Kate DeibelSummer 2012

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 1

  • 7/21/2019 Lecture14 Locking

    2/80

    THE FINAL EXAM

    *ominous music*

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 2

  • 7/21/2019 Lecture14 Locking

    3/80

    The FinalIt is next Wenesa!, August 1"

    It #ill ta$e u% t&e entire class %erio

    Is it com%re&ensi'e( )es an o

    Will %rimaril! call u%on onl! #&at #e co'ere sincet&e miterm +starting at sorting u% t&roug& next

    ona!-s lecture on minimum s%anning trees. Still, !ou #ill nee to unerstan algorit&mic

    anal!sis, big/&, an best#orst/case or an! atastructures #e &a'e iscusse

    )ou #ill be oing tree or &ea% mani%ulationsbut !ou ma! +i4e4, #ill. o some gra%& algorit&ms

    5ul! 11, 2012 CSE 332 Data Abstractions, Summer 2012 3

  • 7/21/2019 Lecture14 Locking

    4/80

    Specific TopicsAlt&oug& t&e inal is b! no means inalie, $no#ingt&e ollo#ing #oul be goo7 8o# to o 9ig/& +!es, again:.

    9est an #orst case or all ata structures an algorit&ms #eco'ere

    Sorting algorit&m %ro%erties +in/%lace, stable.

    ;ra%& re%resentations o%ological sorting

    Dieuctions

    =arallel =reix, =ac$, an Sorting

    ?or$5oin @ibrar! coe

    Ke! ieas &ig&/le'el notions o concurrenc!

    5ul! 11, 2012 CSE 332 Data Abstractions, Summer 2012

  • 7/21/2019 Lecture14 Locking

    5/80

    Book, Calculator, and Notes

    &e exam is close boo$

    )ou can bring a calculator i !ou #ant

    )ou can bring a limite set o notes7

    ne 3x" inex car +bot& sies.

    ust be &an#ritten +no t!%ing:.

    )ou must turn in t&e car #it& !our exam

    5ul! 11, 2012 CSE 332 Data Abstractions, Summer 2012 "

  • 7/21/2019 Lecture14 Locking

    6/80

    MORE ON RACECONDITIONS

    Some &orses li$e #et trac$s or r! trac$s or mu! trac$sB

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 6

  • 7/21/2019 Lecture14 Locking

    7/80

    RacesA race conitionoccurs #&en t&e com%utation result e%enson sc&euling +&o# t&reas are interlea'e on 1 %rocessors.

    nl! occurs i 1 an 2 are sc&eule in a %articular #a! As %rogrammers, #e cannot control t&e sc&euling o t&reas

    =rogram correctness must be ine%enent o sc&euling

    >ace conitions are bugs t&at exist onl! ue to concurrenc! o interlea'e sc&euling #it& 1 t&rea

    !%icall!, t&e %roblem is some intermediate statet&at messesu% a concurrent t&rea t&at sees t&at state

    We #ill istinguis& bet#een ata races an ba interlea'ings,bot& o #&ic& are t!%es o race conition bugs

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012

  • 7/21/2019 Lecture14 Locking

    8/80

    Data RacesA ata race is a t!%e o race conditiont&at can&a%%en in t#o #a!s7 #o t&reaspotentiall#rite a 'ariable at t&e same time

    ne t&reapotentiall#rite a 'ariable #&ile anot&er reas

    ot a race7 simultaneous reas %ro'ie no errors

    =otentiall! is im%ortant We claim t&at coe itsel &as a ata race ine%enent o an!

    %articular actual execution

    Data races are ba, but t&e! are not t&e onl! orm orace conitions We can &a'e a race, an ba be&a'ior, #it&out an! ata race

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 F

  • 7/21/2019 Lecture14 Locking

    9/80

    Stack Example

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 G

    classStack {

    privateE[] array= (E[])newObject[SIZE]; int ine!= "#;

    sync$r%ni&eb%%lean isE'pty() {

    retrnine!=="#;

    sync$r%ni&e v%ips$(E val) {

    array[**ine!] = val;

    sync$r%ni&e Ep%p() {

    i+(isE'pty()) t$r%w newStackE'ptyE!cepti%n();

    retrnarray[ine!""];

  • 7/21/2019 Lecture14 Locking

    10/80

    A Race Condition: But Not a Data Race

    In a seHuential #orl,t&is coe is o i!,ugl!, an Huestionablestle, but correct

    &e algorit&m is t&eonl! #a! to #rite a

    peek&el%er met&o it&is interace is all !ou

    &a'e to #or$ #it&

    classStack {

    ,

    sync$r%ni&eb%%lean isE'pty() {,

    sync$r%ni&e v%ips$(E val) {,

    sync$r%ni&e Ep%p(E val) {,

    Epeek() {

    E ans= p%p(); ps$(ans);

    retrnans;

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 10

    ote t&at %ee$+. t&ro#st&e Stac$Em%t! exce%tion'ia its call to %o%+.

  • 7/21/2019 Lecture14 Locking

    11/80

    peekin a Concurrent Contextpeek&as no o!eralleect on t&e s&are ata It is a reaer not a #riter

    State s&oul be t&e same ater it executes as beore

    &is im%lementation creates an inconsistentintermediate state Calls tops$anp%pare s!nc&ronie,so t&ere are no

    !ata raceson t&e unerl!ing arra!

    9ut t&ere is still a race con!ition

    &is intermeiate state

    s&oul not be ex%ose @eas to se'eral

    "ad interlea!in#s

    Epeek() { E ans= p%p();

    ps$(ans);

    retrnans;

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 11

  • 7/21/2019 Lecture14 Locking

    12/80

    Example $: peek and isEmpt

    =ro%ert! #e #ant7

    I t&ere &as been aps$+an nop%p),t&en isE'ptys&oul return +alse

    Wit&peekas #ritten, %ro%ert! can be

    'iolate &o#(

    E ans= p%p();

    ps$(ans);

    retrnans;

    ps$(!)

    b%%leanb= isE'pty()3ime

    &rea 2&rea 1 +peek.

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 12

  • 7/21/2019 Lecture14 Locking

    13/80

  • 7/21/2019 Lecture14 Locking

    14/80

    Example %: peek and push

    =ro%ert! #e #ant7

    Jalues are returne rom %o% in @I? orer

    Wit&peekas #ritten, %ro%ert! can be'iolate &o#(

    E ans= p%p();

    ps$(ans);

    retrnans;

    3ime

    &rea 2&rea 1 +peek.

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 1

    ps$(!)

    ps$(y)E e= p%p()

  • 7/21/2019 Lecture14 Locking

    15/80

    =ro%ert! #e #ant7

    Jalues are returne rom %o% in @I? orer

    Wit&peekas #ritten, %ro%ert! can be'iolate &o#(

    Example %: peek and push

    E ans= p%p();

    ps$(ans);

    retrnans;

    3ime

    &rea 2&rea 1 +peek.

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 1"

    ps$(!)

    ps$(y)E e= p%p()

    Race ca"ses error #it$: "# $ush(%) "& $o$() "# $ush(%)

    "& $ush(%)

  • 7/21/2019 Lecture14 Locking

    16/80

    Example &: peek and peek

    =ro%ert! #e #ant7

    peekoes not t&ro# an exce%tion unlesst&e stac$ is em%t!

    Wit&peekas #ritten, %ro%ert! can be

    'iolate &o#(

    E ans= p%p();

    ps$(ans);

    retrnans;

    3ime

    &rea 2&rea 1 +peek.

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 16

    E ans= p%p();

    ps$(ans);

    retrnans;

  • 7/21/2019 Lecture14 Locking

    17/80

    The Fixpeeknees s!nc&roniation to isallo# interlea'ings

    &e $e! is to ma$e a lar#er critical section &is %rotects t&e intermeiate state o peek

    se re/entrant loc$sL #ill allo# calls tops$anp%p

    Can be one in stac$ +let. or an external class +rig&t.

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 1

    classStack {

    ,

    sync$r%ni&eEpeek(){

    E ans= p%p();

    ps$(ans); retrnans;

    class-{

    E'y.eek(Stack s){

    sync$r%ni&e (s) {

    E ans= s/p%p();

    s/ps$(ans); retrnans;

  • 7/21/2019 Lecture14 Locking

    18/80

    An 'ncorrect (Fix(So ar #e &a'e ocuse on %roblems create #&en

    peek%erorms #ritest&at lea to an incorrect

    intermeiate state

    A tem%ting but incorrect %ers%ecti'e I an im%lementation opeekoes not #rite an!t&ing,

    t&en ma!be #e can s$i% t&e s!nc&roniation(

    Does not#or$ ue to data races#it&ps$anp%p Same issue a%%lies #it& ot&er reaers, suc& as isE'pty

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 1F

  • 7/21/2019 Lecture14 Locking

    19/80

    Another 'ncorrect Example

    classStack {

    privateE[] array= (E[])newObject[SIZE]; int ine!= "#;

    b%%lean isE'pty() { 00 nsync$r%ni&e1 wr%n234

    retrnine!=="#;

    sync$r%ni&e v%ips$(E val) {

    array[**ine!] = val;

    sync$r%ni&e Ep%p() {

    retrnarray[ine!""];

    Epeek() { 00 nsync$r%ni&e1 wr%n24

    retrnarray[ine!];

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 1G

  • 7/21/2019 Lecture14 Locking

    20/80

    )h )ron#*It looks likeisE'ptyanpeekcan get a#a! #it&t&is becauseps$anp%pa

  • 7/21/2019 Lecture14 Locking

    21/80

    ettin# 't Ri#ht

    A'oiing race conitions on s&are resources

    is iicult Decaes o bugs &a'e le to some con'entional

    #isom an general tec&niHues $no#n to #or$

    We #ill iscuss some $e! ieas an trae/os ore a'ailable in t&e suggeste aitional reaings

    one o t&is is specificto 5a'a or a %articular boo$

    a! be &ar to a%%reciate in beginning

    Come bac$ to t&ese guielines o'er t&e !ears

    Do not tr! to be anc!

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 21

  • 7/21/2019 Lecture14 Locking

    22/80

    %OIN% F&RTHER 'ITHEXCL&SION AND LOC(IN%

    )ale ni'ersit! is t&e best %lace to stu! loc$sB

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 22

  • 7/21/2019 Lecture14 Locking

    23/80

    Three Choices for -emor

    ?or e'er! memor! locationin !our %rogram

    +e4g4, ob

  • 7/21/2019 Lecture14 Locking

    24/80

    Thread./ocalW&ene'er %ossible, o not s&are resources: Easier or eac& t&rea to &a'e its o#n t&rea/local co%! o a

    resource instea o one #it& s&are u%ates Correct onl! i t&reas o not communicate t&roug& resource

    In ot&er #ors, multi%le co%ies are correct a%%roac&

    Exam%le7 5an%'ob

  • 7/21/2019 Lecture14 Locking

    25/80

    'mmuta"leW&ene'er %ossible, o not u%ate ob

  • 7/21/2019 Lecture14 Locking

    26/80

    E!erthin# Else: 1eep it Snchroni0ed

    Ater minimiing t&e amount o memor! t&at is bot&+1. t&rea/s&are an +2. mutable, #e nee to ollo#

    guielines or using loc$s to $ee% t&at ata consistent

    ;uieline N07 o ata races

    e'er allo# t#o t&reas to rea#rite or

    #rite#rite t&e same location at t&e same timeNecessar7

    In 5a'a or C, a %rogram #it& a ata race is almostal#a!s #rong

    But Not Sufficient7

    urpeekexam%le &a no ata races

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 26

  • 7/21/2019 Lecture14 Locking

    27/80

    Consistent /ockin#;uieline N17 Consistent @oc$ing

    ?or eac& location t&at reHuires s!nc&roniation, #es&oul &a'e a loc$ t&at is al#a!s &el #&en reaingor #riting t&e location We sa! t&e loc$ guarst&e location

    &e same loc$ can guar multi%le locations +an oten s&oul.

    Clearl! ocument t&e guar or eac& location

    In 5a'a, t&e guar is oten t&e ob

  • 7/21/2019 Lecture14 Locking

    28/80

    Consistent /ockin#&e ma%%ing rom locations to guaring loc$s is conceptual,anmust be enorce b! !ou as t&e %rogrammer

    It %artitions t&e s&are/O/mutable locations into #&ic& loc$

    Consistent loc$ing is7

    ot Suicient7It %re'ents all ata races, but still allo#s ba interlea'ings

    ur %ee$ exam%le use consistent loc$ing, but &a ex%oseintermeiate states an ba interlea'ings

    ot ecessar!7

    Can !namicall! c&ange t&e loc$ing %rotocol

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 2F

  • 7/21/2019 Lecture14 Locking

    29/80

    Beond Consistent /ockin#Consistent loc$ing is an excellent guieline

    A eault assum%tion about %rogram esign

    )ou #ill sa'e !oursel man! a &eaac&e using t&is guieline

    9ut it is not reHuire or correctness7Dierentpro#ram phasescan use ierent loc$ing tec&niHues

    =ro'ie all t&reas coorinate mo'ing to t&e next %&ase

    Exam%le rom =ro

  • 7/21/2019 Lecture14 Locking

    30/80

    LOC( %RAN&LARIT)

    W&ole/grain loc$s are better t&an o'erl! %rocesse loc$sB

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 30

    k l

  • 7/21/2019 Lecture14 Locking

    31/80

    /ock ranularitCoarserained?e#er loc$s +more ob

  • 7/21/2019 Lecture14 Locking

    32/80

    Trade.2ffsCoarse/graine a'antages Sim%ler to im%lement

    ?astereasier to im%lement o%erations t&at access multi%lelocations +because all guare b! t&e same loc$.

    Easier to im%lement moiications o ata/structure s&a%e

    ?ine/graine a'antages ore simultaneous access +im%ro'es %erormance

    #&en coarse/graine #oul lea to unnecessar! bloc$ing.

    ;uieline N27 @oc$ ;ranularit!Start #it& coarse/graine +sim%ler., mo'e to ine/graine+%erormance. onl! i contentionon coarse loc$s is an issue4Alas, oten leas to bugs4

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 32

    E l S Ch i i 3 h "l

  • 7/21/2019 Lecture14 Locking

    33/80

    Example: Separate Chainin# 3ashta"le

    Coarse/graine7 ne loc$ or entire &as&table

    ?ine/graine7 ne loc$ or eac& buc$et

    W&ic& su%%orts more concurrenc! or insertan l%%kp(

    ?ine/graineL allo#s simultaneous access to ierentbuc$ets

    W&ic& ma$es im%lementing resi&eeasier(

    Coarse/graineL

  • 7/21/2019 Lecture14 Locking

    34/80

    Critical.Section ranularitA secon, ort&ogonal granularit! issue is t&e sie ocritical/sections

    8o# muc& #or$ s&oul #e o #&ile &oling loc$+s.

    I critical sections run or too long7

    =erormance loss as ot&er t&reas are bloc$e

    I critical sections are too s&ort7

    9ugs li$el! as !ou bro$e u% somet&ing #&ere ot&ert&reas s&ouln-t be able to see intermeiate state

    ;uieline N37 ;ranularit!Do not o ex%ensi'e com%utations or I in criticalsections, but also o not introuce race conitions

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 3

    E l C iti l S ti l it

  • 7/21/2019 Lecture14 Locking

    35/80

    Example: Critical.Section ranularit

    Su%%ose #e #ant to c&ange t&e 'alue or a $e!in a &as&table #it&out remo'ing it rom t&e table

    Assume l%ckguars t&e #&ole table

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 3"

    sync$r%ni&e(l%ck) {

    v# = table/l%%kp(k);

    v6 = e!pensive(v#);

    table/re'%ve(k);

    table/insert(k7v6);

    4apa Bear5s criticalsection +as too lon#

    Ta"le is locked durin#

    the expensi!e call

    E l C iti l S ti l it

  • 7/21/2019 Lecture14 Locking

    36/80

    Example: Critical.Section ranularit

    Su%%ose #e #ant to c&ange t&e 'alue or a $e!in a &as&table #it&out remo'ing it rom t&e table

    Assume l%ckguars t&e #&ole table

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 36

    sync$r%ni&e(l%ck) {

    v# = table/l%%kp(k);

    v6 = e!pensive(v#);

    sync$r%ni&e(l%ck) { table/re'%ve(k);

    table/insert(k7v6);

    -ama Bear5s critical section+as too short

    'f another thread updated

    the entr, +e +ill lose theinter!enin# update

    E l C iti l S ti l it

  • 7/21/2019 Lecture14 Locking

    37/80

    Example: Critical.Section ranularit

    Su%%ose #e #ant to c&ange t&e 'alue or a $e!in a &as&table #it&out remo'ing it rom t&e table

    Assume l%ckguars t&e #&ole table

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 3

    %ne = +alse;

    w$ile(4%ne) {

    sync$r%ni&e(l%ck) { v# = table/l%%kp(k);

    v6 = e!pensive(v#);

    sync$r%ni&e(l%ck) {

    i+(table/l%%kp(k)==v#) {

    %ne = tre;

    table/re'%ve(k);

    table/insert(k7v6);

    Ba" Bear5s criticalsection +as 6ust ri#ht

    if another updateoccurred, +e +ill trour update a#ain

    At i it

  • 7/21/2019 Lecture14 Locking

    38/80

    AtomicitAn o%eration is atomici no ot&er t&rea can see it%artl! execute Atomic as in a%%ears ini'isible We t!%icall! #ant AD o%erations atomic, e'en to ot&er

    t&reas running o%erations on t&e same AD

    ;uieline N7 Atomicit! &in$ in terms o #&at o%erations nee to be atomic

    a$e critical sections

  • 7/21/2019 Lecture14 Locking

    39/80

    Do Not Roll 7our 2+nIn real lie, !ou rarel! #rite !our o#n ata structures Excellent im%lementations %ro'ie in stanar libraries

    =oint o CSE 332 is to unerstan t&e $e! trae/os,abstractions, an anal!sis o suc& im%lementations

    Es%eciall! true or concurrent ata structures

    ?ar too iicult to %ro'ie ine/graine s!nc&roniation#it&out race conitions

    Stanar t&rea/saelibraries li$e -%ncrrent8as$9apare#ritten b! #orl ex%erts an been extensi'el! 'ette

    ;uieline N"7 @ibraries8se "uilt.in li"raries +hene!er the meet our needs

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 3G

    -oti!atin# -emor -odel 'ssues

  • 7/21/2019 Lecture14 Locking

    40/80

    -oti!atin# -emor.-odel 'ssues

    ric$! an surprisin#l +ron#uns!nc&ronie

    concurrent coe ?irst unerstan #&! it loo$sli$e t&e assertion cannot ail7

    Eas! case7

    A call to g ens beore an! callto starts

    Eas! case7At least one call to com%letes

    beore call to g starts

    I calls to an g interlea'eB

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 0

    classC{

    privateint x = 0;

    privateint y= 0;

    void f() {

    x = 1;

    y = 1;

    }

    void g() {

    int a= y;

    int b= x;

    assert(b >= a);

    }

    'nterlea!in#s Are Not Enou#h

  • 7/21/2019 Lecture14 Locking

    41/80

    'nterlea!in#s Are Not Enou#h&ere is no interlea'ing o +an 2suc& t&at t&eassertion ails

    =roo N17Ex&austi'el! consier all %ossible orerings o accessto s&are memor! +t&ere are 6.

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 1

    'nterlea!in#s Are Not Enou#h

  • 7/21/2019 Lecture14 Locking

    42/80

    'nterlea!in#s Are Not Enou#h=roo N27Ex&austi'el! consier all %ossible orerings o access

    to s&are memor! +t&ere are 6.

    I 4(b>=a), t&en a==#anb==:49ut i a==#, t&en y=#&a%%ene beore a=y49ecause %rograms execute in orer7

    a=y&a%%ene beoreb=!an !=#&a%%ene beore y=#

    So b! transiti'it!,b==#4

    Contraiction4

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 2

    ! = #;

    y = #;

    int a= y;

    intb= !;

    assert(b >= a);

    &rea 17 f &rea 27 g

    )ron#

  • 7/21/2019 Lecture14 Locking

    43/80

    )ron#

    8o#e'er, t&e coe &as a data race ns!nc&ronie rea#rite or #rite#rite o t&e

    memor! same location

    I coe &as ata races, !ou cannot reason

    about it #it& interlea'ings &is is sim%l! t&e rules o 5a'a +an C, CPP,

    CN, ot&er languages.

    t&er#ise #e #oul slo# o#n all %rograms

  • 7/21/2019 Lecture14 Locking

    44/80

    )h?or %erormance reasons, t&e com%iler an t&e&ar#are #ill oten reorer memor! o%erations a$e a com%iler or com%uter arc&itecture course to learn

    more as to #&! t&is is goo t&ing

    course, com%ilers cannot = a);

    &rea 17 f &rea 27 g

    The rand Compromise

  • 7/21/2019 Lecture14 Locking

    45/80

    The rand Compromise&e com%iler&ar#are #ill EJE>7

    =erorm a memor! reorering t&at aects t&e result o a

    single/t&reae %rogram =erorm a memor! reorering t&at aects t&e result o a

    ata/race/reemulti/t&reae %rogram

    So7 I no interlea'ing o !our %rogram &as a ata race,

    t&en !ou can for#et a"out all this reorderin# nonsense:t&e result #ill be eHui'alent to some interlea'ing

    &e 9ig =icture7

    )our

  • 7/21/2019 Lecture14 Locking

    46/80

    Fixin# 2ur Exampleaturall!, #e can use s!nc&roniation to a'oi ataraces an t&en, inee, t&e assertion cannot ail

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 6

    class-{

    privateint ! = :;

    privateint y= :;

    v%i +() {

    sync$r%ni&e(t$is) { ! = #;

    sync$r%ni&e(t$is) { y = #;

    v%i 2() {

    int a7b; sync$r%ni&e(t$is) { a = y;

    sync$r%ni&e(t$is) { b = !;

    assert(b >= a);

    A Second Fix: Sta A+a from This

  • 7/21/2019 Lecture14 Locking

    47/80

    A Second Fix: Sta A+a from This5a'a &as v%latileiels7 accesses o not count as ata races

    9ut !ou cannot rea/u%ate/#rite

    Im%lementation Details

    Slo#er t&an regular iels but aster t&an loc$s

    >eall! or ex%erts7 a'oi t&emL use stanar libraries instea

    An #&! o !ou nee coe li$e t&is an!#a!(

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012

    class-{

    private v%latileint ! = :;

    private v%latileint y= :;

    v%i +() {

    ! = #; y = #;

    v%i 2() {

    int a= y; intb= !;

    assert(b >= a);

    Code That is )ron#

  • 7/21/2019 Lecture14 Locking

    48/80

    Code That is )ron#8ere is a more realistic exam%le o coe t&at is #rong

    o #uarantee&rea 2 #ill e!ersto% +ue to ata race.

    9ut &onestl! it #ill li$el! #or$ in %ractice

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 F

    class-{

    b%%lean st%p = +alse;

    v%i +() {

    w$ile(4st%p) {

    00 raw a '%nster

    v%i 2() {

    st%p = iserit();

    "hread & +()

    "hread # 2()

  • 7/21/2019 Lecture14 Locking

    49/80

    DEADLOC(

    ot nearl! as sill! as Deat&lo$ rom ar'el comicsB

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 G

    -oti!atin# Deadlock 'ssues

  • 7/21/2019 Lecture14 Locking

    50/80

    -oti!atin# Deadlock 'ssuesConsier t&e ollo#ing met&o or transering mone!bet#een ban$ accounts

    During call to a4e%osit, t&e t&rea &ols t#o loc$s

    @et-s in'estigate #&en t&is ma! be a %roblem

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 "0

    classank?cc%nt{

    ,

    sync$r%ni&e v%iwit$raw(int a't) {,

    sync$r%ni&e v%i ep%sit(int a't) {,

    sync$r%ni&e v%i trans+er@%(int a't7ank?cc%nta) {

    t$is/wit$raw(a't);

    a/ep%sit(a't);

    The Deadlock

  • 7/21/2019 Lecture14 Locking

    51/80

    The Deadlock

    Su%%ose !an yare iels &oling accounts

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 "1

    acquire lock for !

    do withdraw from!

    block on lock fory

    acquire lock for ydo withdraw fromy

    block on lock for !

    &rea 17x.transfero(1!y)

    3ime

    &rea 27y.transfero(1!x)

    The Dinin# 4hilosophers

  • 7/21/2019 Lecture14 Locking

    52/80

    The Dinin# 4hilosophers?i'e %&iloso%&ers go out to innertoget&er at an Italian restaurant

    &e! sit at a roun tableL oneor$ %er %late setting

    ?or etiHuette reasons, t&e%&iloso%&ers nee t#o or$s

    to eat s%ag&etti %ro%erl!

    W&en t&e s%ag&etti comes,eac& %&iloso%&er %rocees tograb t&eir rig&t or$, t&en

    t&eir let or$

    Q@oc$ing- or eac& or$ results in a !ea!loc*

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 "2

    Deadlock

  • 7/21/2019 Lecture14 Locking

    53/80

    DeadlockA ealoc$ occurs #&en t&ere are t&reas "&, B, "n

    suc& t&at7

    ?or iR1 to n/1, "iis #aiting or at least one

    resource &el b! "i+&

    "nis #aiting or a resource &el b! "&

    In ot&er #ors, t&ere is a ccleo #aiting

    ore ormall!, a gra%& o e%enencies is c!clic

    Dealoc$ a'oiance in %rogramming amounts totec&niHues to ensure a c!cle can ne'er arise

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 "3

    Back to 2ur Example

  • 7/21/2019 Lecture14 Locking

    54/80

    Back to 2ur Example%tions or ealoc$/%roo transer7

    14 a$e a smaller critical section7trans+er@%not s!nc&ronie

    Ex%oses intermeiate state aterwit$rawbeore ep%sit

    a! be o$a!, but ex%oses #rong total amount to ban$

    24 Coarsen loc$ granularit!7ne loc$ or all accounts allo#ing transers bet#een t&em

    Wor$s, but sacriices concurrent e%osits#it&ra#als

    34 ;i'e e'er! ban$/account a uniHue number an al#a!s

    acHuire loc$s in t&e same orer Entire pro#ram s&oul obe! t&is orer to a'oi c!cles

    Coe acHuiring onl! one loc$ can ignore t&e orer

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 "

    2rderin# /ocks

  • 7/21/2019 Lecture14 Locking

    55/80

    2rderin# /ocks

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 ""

    classank?cc%nt{

    ,

    privateint acctA'ber; 00 'st be niBe v%itrans+er@%(int a't7ank?cc%nta) {

    i+(t$is/acctA'ber < a/acctA'ber)

    sync$r%ni&e(t$is) {

    sync$r%ni&e(a) {

    t$is/wit$raw(a't);

    a/ep%sit(a't);

    else

    sync$r%ni&e(a) { sync$r%ni&e(t$is) {

    t$is/wit$raw(a't);

    a/ep%sit(a't);

    Strin#Buffer Example

  • 7/21/2019 Lecture14 Locking

    56/80

    Strin#Buffer Example?rom t&e 5a'a stanar librar!

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 "6

    classStrin2++er{privateint c%nt;

    privatec$ar[] vale;

    ,

    sync$r%ni&eappen(Strin2++er sb) {

    int len= sb/len2t$(); i+(t$is/c%nt * len > t$is/vale/len2t$)

    t$is/e!pan(,);

    sb/2et-$ars(:7len7t$is/vale7t$is/c%nt);

    ,

    sync$r%ni&e2et-$ars(int !7 int7 y7

    c$ar[] a7 int &) {

    "copy t$is/vale[!//y]into astarting at &"

    T+o 4ro"lems

  • 7/21/2019 Lecture14 Locking

    57/80

    T+o 4ro"lems=roblem N17

    @oc$ or sbnot &el bet#een calls to sb/len2t$an sb/2et-$ars

    So sbcoul get longer

    Woul cause appento t&ro# an?rray%nsE!cepti%n

    =roblem N27

    Dealoc$ %otential i t#o t&reas tr! to appenin o%%osite

    irections, ientical to t&e ban$/account irst exam%le

    ot eas! to ix bot& %roblems #it&out extra co%!ing7

    Do not #ant uniHue is on e'er! Strin2++er

    Do not #ant one loc$ or all Strin2++erob

  • 7/21/2019 Lecture14 Locking

    58/80

    4erspecti!eCoe li$e account/transer an string/buer a%%enare iicult to eal #it& or ealoc$

    Easier case7 ierent t!%es o ob

  • 7/21/2019 Lecture14 Locking

    59/80

    IM+RO,IN% LITERAC):READER-'RITER LOC(S

    We encourage multi%le reaersB

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 "G

    Readin# !s )ritin#

  • 7/21/2019 Lecture14 Locking

    60/80

    Readin# !s9 )ritin#>ecall7 ulti%le concurrent reas o same memor!7 Nota %roblem

    ulti%le concurrent #rites o same memor!7 =roblem ulti%le concurrent rea O #rite o same memor!7 =roblem

    So ar7 I concurrent #rite#rite or rea#rite mig&t occur, use

    s!nc&roniation to ensure one/t&rea/at/a/time

    9ut t&is is unnecessaril! conser'ati'e7

    Coul still allo# multi%le simultaneous reaers:

    Example

  • 7/21/2019 Lecture14 Locking

    61/80

    ExampleConsier a &as&table #it& one coarse/graine loc$

    nl! one t&rea can %erorm o%erations at a time

    9ut su%%ose7

    &ere are man! simultaneous l%%kpo%erations

    An inserto%erations are 'er! rare

    ote7Criticall! im%ortant t&at l%%kpoes not actuall!

    mutate s&are memor!, li$e a mo'e/to/ront list ors%la! tree o%eration #oul

    Readers)riter locks

  • 7/21/2019 Lecture14 Locking

    62/80

    Readers)riter locksA ne# s!nc&roniation AD7 t&e reaers#riter loc$

    A loc$s states all into t&ree categories7 Tnot &elU

    T&el or #ritingU b! one t&rea

    T&el or reaingU b! one or moret&reas

    %erations7

    new1 ma$e a ne# loc$, initiall! Tnot &elU acBireCwrite1 bloc$ i currentl! T&el or reaingU or i T&el

    or #ritingU, else ma$e T&el or #ritingU

    releaseCwrite1 ma$e Tnot &elU

    acBireCrea1 bloc$ i currentl! T&el or #ritingU, elsema$e$ee% T&el or reaingU an increment readers count

    releaseCrea1 ecrement reaers count, i 0, ma$e Tnot&elU

    AD In'ariants7

    0 #riters 10 reaers#riters reaersRR0

    4seudocode Example ;not

  • 7/21/2019 Lecture14 Locking

    63/80

    4seudocode Example ;not

  • 7/21/2019 Lecture14 Locking

    64/80

    Readers)riter /ock DetailsA reaers#riter loc$ im%lementation +#&ic& is Tnot our%roblemU. usuall! gi'esprioritto #riters7

    Ater a #riter bloc$s, no reaers arri!in# later#ill get t&eloc$ beore t&e #riter

    t&er#ise an insertcoul star!e

    >e/entrant +same t&rea acHuires loc$ multi%le times.(

    ostl! an ort&ogonal issue 9ut some libraries su%%ort up#radin#rom reaer to #riter

    W&! not use reaers#riter loc$s #it& more ine/graineloc$ing( @i$e on eac& buc$et(

    ot #rong, but li$el! not #ort& it ue to lo# contention

    'n

  • 7/21/2019 Lecture14 Locking

    65/80

    'n

  • 7/21/2019 Lecture14 Locking

    66/80

    CONDITION ,ARIA.LES

    &e natural successor to s&am%oo 'ariables

    August 6, 2012 CSE 332 Data Abstractions, Summer 2012 66

    -oti!atin# Condition >aria"les

  • 7/21/2019 Lecture14 Locking

    67/80

    -oti!atin# Condition >aria"les

    o moti'ate conition 'ariables, consier t&e canonical exam%le

    o a boune bueror s&aring #or$ among t&reas

    9oune buer7 A Hueue #it& a ixe sie

    nl! slig&tl! sim%ler i unboune, core nee still arises

    ?or s&aring #or$ t&in$ an assembl! line7 =roucer t&rea+s. o some #or$ an enHueue result ob

  • 7/21/2019 Lecture14 Locking

    68/80

    First Attempt

    class++er {

    E[] array= (E[])newObject[SIZE]; , 00 +r%nt7 back +iels7 isE'pty7 isHll 'et$%s

    sync$r%ni&ev%i enBee(E elt) {

    i+(isHll())

    333

    else

    , add to array and adjust back

    sync$r%ni&eE eBee()

    i+(isE'pty()) 333

    else

    , take from array and adjust front

    )aitin#

  • 7/21/2019 Lecture14 Locking

    69/80

    )aitin#enBeeto a ull buer s&oul notraise an exce%tionbut s&oul #aituntil t&ere is room

    eBeerom an em%t! buer s&oul notraise anexce%tion but s&oul #aituntil t&ere is ata

    ne baa%%roac&is to spin.+ait+#aste #or$ an

    $ee% grabbing loc$.v%i enBee(E elt) {

    w$ile(tre) {

    sync$r%ni&e(t$is) {

    i+(isHll()) c%ntine; , add to array and adjust back

    retrn;

    00 eBee si'ilar

    )hat +e )ant

  • 7/21/2019 Lecture14 Locking

    70/80

    at e a t9etter #oul be or a t&rea to sim%l!+aituntil it can %rocee

    It s&oul not s%in%rocess continuousl!

    Instea, it s&oul be notified#&en it s&oul tr! again In t&e meantime, let ot&er t&reas run

    @i$e loc$s, not somet&ing !ou can im%lement on !our o#n

    @anguage or librar! gi'es it to !ou, t!%icall! im%lemente#it& o%erating/s!stem su%%ort

    An AD t&at su%%orts t&is7 conition 'ariable

    Inorms #aiter+s. #&en t&e conditiont&at causes itt&em to

    #ait &as !aried

    erminolog! not com%letel! stanarL #ill mostl! stic$ #it& 5a'a

  • 7/21/2019 Lecture14 Locking

    71/80

    pp ? #

    class++er {

    ,

    sync$r%ni&ev%i enBee(E elt) {

    i+(isHll())

    t$is/wait(); 00 releases l%ck an waits

    add to array and adjust back

    i+(buffer was empty) t$is/n%ti+y();00 wake s%'eb%y p

    sync$r%ni&eE eBee() {

    i+(isE'pty())

    t$is/wait(); 00 releases l%ck an waits take from array and adjust front

    i+(buffer was full)

    t$is/n%ti+y();00 wake s%'eb%y p

    1e 'deas 7ou Should 1no+

  • 7/21/2019 Lecture14 Locking

    72/80

    5a'a is a bit #eir7

    E'er! ob

  • 7/21/2019 Lecture14 Locking

    73/80

    #

    9et#een t&e time a t&rea is notiie an it re/acHuires

    t&e loc$, t&e conition can become alse again:

    sync$r%ni&ev%i enBee(E elt){

    i+(isHll())

    t$is/wait();

    add to array and adjust back

    ,

    i+(isHll())

    t$is/wait();

    add to array

    3ime

    &rea 2 +eHueue.&rea 1 +enHueue.

    take from array

    i+(was full)

    t$is/n%ti+y();

    make full again

    &rea 3 +enHueue.

    Bu# Fix

  • 7/21/2019 Lecture14 Locking

    74/80

    #

    ;uieline7Al+asre/c&ec$ t&e conition ater re/gaining t&e loc$?or obscure +::. reasons, 5a'a is tec&nicall! allo#e to noti! at&rea spuriousl+i4e4, or no reason an #it&out actuall!ma$ing a call to n%ti+y.

    sync$r%ni&ev%i enBee(E elt) {

    w$ile(isHll())

    t$is/wait();

    sync$r%ni&eE eBee() {

    w$ile(isE'pty())

    t$is/wait();

    ,

    Another Bu#

  • 7/21/2019 Lecture14 Locking

    75/80

    #

    I multi%le t&reas are #aiting, #e #a$e u% onl! one

    Sure onl! one can o #or$ no+, but #e cannotorget t&e ot&ers:

    3ime

    w$ile(isHll()) t$is/wait();

    ,

    &rea 1 +enHueue.

    00 eBee #

    i+(buffer was full)

    t$is/n%ti+y();

    00 eBee 6

    i+(buffer was full)

    t$is/n%ti+y();

    &rea 3 +eHueues.&rea 2 +enHueue.

    w$ile(isHll()) t$is/wait();

    ,

    Bu# Fix

  • 7/21/2019 Lecture14 Locking

    76/80

    #

    n%ti+y?ll#a$es u% all current #aiters on t&e conition 'ariable

    ;uieline7 I in an! oubt, use n%ti+y?ll

    Wasteul #a$ing is muc& better t&an ne'er #a$ing u%+because !ou alrea! nee to re/c&ec$ conition.

    So #&! oes n%ti+yexist( Well, it is aster #&en correctB

    sync$r%ni&ev%i enBee(E elt) {

    ,

    i+(buffer was empty) t$is/n%ti+y?ll();00 wake everyb%y p

    sync$r%ni&eE eBee() {

    ,

    i+(buffer was full

    ) t$is/n%ti+y?ll();00 wake everyb%y p

    Alternate Approach

  • 7/21/2019 Lecture14 Locking

    77/80

    ppAn alternati'e is to call n%ti+y+not n%ti+y?ll. one'er! enBee eBee, not

  • 7/21/2019 Lecture14 Locking

    78/80

    ppAn alternate a%%roac& #or$s i t&e enHueuers aneHueuers #ait on differentconition 'ariables

    9ut or mutual exclusion bot& conition 'ariablesmust be associate #it& t&e same loc$

    5a'as Te'er!t&ing is a loc$ conition 'ariableU oes notsu%%ort t&is7 eac& conition 'ariable is associate #it& itsel

    Instea, 5a'a &as classes in java/til/c%ncrrent/l%cksor#&en !ou #ant multi%le conitions #it& one loc$

    class 5eentrantG%ck&as a met&o new-%niti%nt&atreturns a ne# -%niti%nobaria"le

  • 7/21/2019 Lecture14 Locking

    79/80

    n%ti+y0n%ti+y?lloten calle si2nal0br%acastorplse0plse?ll

    Conition 'ariables are subtle an &arer to use t&an loc$s

    9ut #&en !ou nee t&em, !ou nee t&em

    S%inning an ot&er #or$arouns o not #or$ #ell

    ?ortunatel!, li$e most t&ings !ou see in a ata/structurescourse, t&e common use/cases are %ro'ie in libraries #rittenb! ex%erts an &a'e been t&oroug&l! 'ette

    Exam%le7 java/til/c%ncrrent/?rrayl%ckin2ee

    All conition 'ariables &ienL

  • 7/21/2019 Lecture14 Locking

    80/80

    Access to s&are resources introuces ne# $ins o bugs

    Data races

    Critical sections too small Critical sections use #rong loc$s

    Dealoc$s

    >eHuires s!nc&roniation

    @oc$s or mutual exclusion +common, 'arious la'ors.

    Conition 'ariables or signaling ot&ers +less common.

    ;uielines or correct use &el% a'oi common %italls

    ot al#a!s clear s&are/memor! is #ort& t&e %ain 9ut ot&er moels not a %anacea +e4g4, message %assing.