Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09.

Similar presentations


Presentation on theme: "Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09."— Presentation transcript:

1 Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09

2 Introduction to the Introduction Some problems with locks: –Deadlock: The condition where there are at least two processes A and B such that A holds a lock on a resource required by B to complete and B holds a lock on a resource required by A to complete and they wait indefinitely. –In general Deadlock avoidance is hard if not NP-Complete.

3 (Some) Problems with Locks Priority Inversion: A low-priority process holds a lock on a resource desired by a high priority process. Not very granular, a small percentage of the operations performed in the critical section may actually modify shared memory, hurting performance

4 If not locks than what? Without hardware support Lock-free algorithms are possible, but not really practical (exceptions do exist: see Lamport’s Bakery Algorithm) Hardware support can be generalized as atomic operations.

5 What is Lock-free? (Definitions) Atomic: From the Greek ἄτομος (atomos) meaning indivisible or uncuttable. Atomic Operations: A set of operations that execute as if they were a single simple operation.

6 What is Lock-free? (Definitions) Lock-free: Algorithms/Data Structures that can be invoked or accessed in a parallel context accessing shared memory without a mechanism to protect their critical section (such as a mutex) Critical Section: A set of instructions necessary to complete a single complete operation on a shared memory resource. (Ex: Pop operation on a stack)

7 Compare and Swap (CAS) For integers n and n' and a memory location a CAS( n, a, n' ) if the value at address a is n write the value of n' to address a return true otherwise return false

8 Compare and Swap (CAS) Executes atomically, often the values of n and n' represent memory addresses Supported in some form as far back as the IBM 370 and available on almost all modern general purpose microprocessors (Pentium, Power PC, etc)

9 Compare and Swap (CAS) Often used to implement 'busy waiting’... success <- false Do success <- CAS( n, a, n') Until success...

10 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Let Top be address of the top of the stack Let Next(I) where I is a valid address of a stack item be the address where the address of the next item after I is stored Let New be the address of the item for the stack

11 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Push ( New )Success <- false Do T' <- Top Next(New) <- T’ Success <- CAS( T', Top, New ) Until Success

12 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Push ( New )Success <- false Do T' <- Top Next(New) <- T’ Success <- CAS( T', Top, New ) Until Success 53 Head

13 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Push ( New )Success <- false Do T' <- Top Next(New) <- T’ Success <- CAS( T', Top, New ) Until Success 53 Top We create a new item 7

14 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Push ( New )Success <- false Do T' <- Top Next(New) <- T’ Success <- CAS( T', Top, New ) Until Success 53 Top Read the value of Top 7 T’

15 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Push ( New )Success <- false Do T' <- Top Next(New) <- T’ Success <- CAS( T', Top, New ) Until Success 53 Top Set the new item’s Next to T’ 7 T’

16 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Push ( New )Success <- false Do T' <- Top Next(New) <- T’ Success <- CAS( T', Top, New ) Until Success 53 Top Suppose another thread adds an item before the assignment and the CAS 7 T’ 3

17 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Push ( New )Success <- false Do T' <- Top Next(New) <- T’ Success <- CAS( T', Top, New ) Until Success 53 Top T’ != Top so the CAS will fail – we must start again 7 T’ 3

18 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Push ( New )Success <- false Do T' <- Top Next(New) <- T’ Success <- CAS( T', Top, New ) Until Success 53 Top 7 T’ 3

19 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Push ( New )Success <- false Do T' <- Top Next(New) <- T’ Success <- CAS( T', Top, New ) Until Success 53 Top 7 T’ 3

20 Compare and Swap (CAS) Example: Pushing an item onto a lock-free stack Push ( New )Success <- false Do T' <- Top Next(New) <- T’ Success <- CAS( T', Top, New ) Until Success 53 Top 7 T’ 3 This time the CAS succeeds and we are done

21 ABA problem Where the value read(and stored) by a process (A) is changed by another process (B) to the same value as read by A, but causes some other state change that causes A to produce an incorrect result

22 ABA problem Consider the stack and two threads A and B and the following (incorrect) pop operation: Let Value(I), where I is a valid address of a stack item, be the value associated with I Pop () Do T' <- Top’ N' <- Next(T') V <- Value(T') Success <- CAS( T', Top, N' ) Until Success Return V

23 ABA problem AB 53 Top 72

24 ABA problem A Do T' <- Top N' <- Next(T’) V <- Value(T') B 53 Top 72 V = 7 T’ N’

25 ABA problem A Do T' <- Top’ N' <- Next(T’) V <- Value(T') B Pop() 53 2 V = 7 T’ N’ 7 is returned Top

26 ABA problem A Do T' <- Top’ N' <- Next(T’) V <- Value(T') B Pop() Push( 42 ) 53 2 V = 7 T’ 42 The new item is created in the old memory location N’ Top

27 ABA problem A Do T' <- Top’ N' <- Next(T’) V <- Value(T') Success <- CAS( T', Top, N’ ) B Pop() Push( 42 ) 53 2 V = 7 T’ N’ Top 42 The CAS will Succeed

28 ABA problem A Do T' <- Top’ N' <- Next(T’) V <- Value(T') Success <- CAS( T', Top, N’ ) Until Success Return V B Pop() Push( 42 ) 53 2 V = 7 T’ N’ Top 42 7 will be returned

29 ABA problem A Do T' <- Top’ N' <- Next(T’) V <- Value(T') Success <- CAS( T', Top, N’ ) Until Success Return V B Pop() Push( 42 ) 53 2 V = 7 T’ N’ Top 42 7 will be returned - again.

30 What is Wait-free? Wait free: Algorithms/Data Structures that can be invoked or accessed in a parallel context accessing shared memory such that execution time is guaranteed and predictable. Almost always lock-free. Not all lock-free constructions are wait-free (most use some form of 'busy waiting' which is inherently unpredictable)

31 Multi-Producer/Consumer Circular Queues (The Problem) Circular queue: An array of fixed size, to be accessed in a FIFO manner Multi-producer: More than one process may write to the queue at once Multi-consumer: More than one process may read from the queue at once

32 Multi-Producer/Consumer Circular Queues (The Problem) The advantages of lock free: –After a cell in the array has been 'reserved' for a particular process - writing or reading will cause no contention. –Only maintenance of queue parameters need made safe

33 Multi-Producer/Consumer Circular Queues (The Algorithm) The advantages of lock free: –After a cell in the array has been 'reserved' for a particular process - writing or reading will cause no contention. –Only maintenance of queue parameters need made safe

34 Multi-Producer/Consumer Circular Queues (The Algorithm) Sequential Version: Let A(i) be the element of the array at position i (starting at 0) Let Size be the number of cells in the array Let Head be the index of the first free cell for writing Let Tail be the index of the first occupied cell for writing Let Write(i) represent the work to write the data in question to array position i

35 Multi-Producer/Consumer Circular Queues (The Algorithm) Sequential Version: Enqueue() If not head = tail Write( Head ) Head <- ( Head + 1 ) mod Size else Fail

36 Multi-Producer/Consumer Circular Queues (The Algorithm) Obvious not correct in a parallel context Consider: A B

37 Multi-Producer/Consumer Circular Queues (The Algorithm) Obvious not correct in a parallel context Consider: A B Enqueue()

38 Multi-Producer/Consumer Circular Queues (The Algorithm) Obvious not correct in a parallel context Consider: A B Enqueue()

39 Multi-Producer/Consumer Circular Queues (The Algorithm) Obvious not correct in a parallel context Consider: A B Enqueue() Write( Head )

40 Multi-Producer/Consumer Circular Queues (The Algorithm) Obvious not correct in a parallel context Consider: A B Enqueue() Write( Head )

41 Multi-Producer/Consumer Circular Queues (The Algorithm) Obvious not correct in a parallel context Consider: A B Enqueue() Write( Head ) Head<-( Head + 1 ) mod Size

42 Multi-Producer/Consumer Circular Queues (The Algorithm) Obvious not correct in a parallel context Consider: A B Enqueue() Write( Head ) Head<-( Head + 1 ) mod Size Values will be written to the same place

43 Multi-Producer/Consumer Circular Queues (The Algorithm) Need additional information: Let NumWriters and NumReaders be the maximum amount of writers and readers allowed in the queue respectively Initially NumWriters = Size and NumReaders = 0

44 Multi-Producer/Consumer Circular Queues (The Algorithm) First solution: Advance the head pointer atomically Let *Head be the memory address of the value Head Let *NR be the address of NumReaders Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Increase NumReaders

45 Multi-Producer/Consumer Circular Queues (The Algorithm) Just when you thought is was (thread) safe: Consider the following series of events: Queue is Empty Head NumReaders = 0

46 Multi-Producer/Consumer Circular Queues (The Algorithm) Just when you thought is was (thread) safe: Consider the following series of events: Queue is Empty Thread A succeeds in advancing Head pointer Head NumReaders = 0

47 Multi-Producer/Consumer Circular Queues (The Algorithm) Just when you thought is was (thread) safe: Consider the following series of events: Queue is Empty Thread A succeeds in advancing Head pointer Thread A begins writing Head NumReaders = 0 23423 234

48 Multi-Producer/Consumer Circular Queues (The Algorithm) Just when you thought is was (thread) safe: Consider the following series of events: Queue is Empty Thread A succeeds in advancing Head pointer Thread A begins writing Thread B succeeds in advancing Head pointer Head NumReaders = 0 23423 2343

49 Multi-Producer/Consumer Circular Queues (The Algorithm) Just when you thought is was (thread) safe: Consider the following series of events: Queue is Empty Thread A succeeds in advancing Head pointer Thread A begins writing Thread B succeeds in advancing Head pointer Thread B begins writing Head NumReaders = 0 23458 334 23423 23431

50 Multi-Producer/Consumer Circular Queues (The Algorithm) Just when you thought is was (thread) safe: Consider the following series of events: Queue is Empty Thread A succeeds in advancing Head pointer Thread A begins writing Thread B succeeds in advancing Head pointer Thread B begins writing Thread B finishes writing Head NumReaders = 0 23423 23431 9 23458 33498 39274

51 Multi-Producer/Consumer Circular Queues (The Algorithm) Just when you thought is was (thread) safe: Consider the following series of events: Queue is Empty Thread A succeeds in advancing Head pointer Thread A begins writing Thread B succeeds in advancing Head pointer Thread B begins writing Thread B finishes writing Thread B succeeds in increasing NumReaders Head NumReaders = 1 23423 23431 95 23458 33498 39274

52 Multi-Producer/Consumer Circular Queues (The Algorithm) Just when you thought is was (thread) safe: Consider the following series of events: Queue is Empty Thread A succeeds in advancing Head pointer Thread A begins writing Thread B succeeds in advancing Head pointer Thread B begins writing Thread B finishes writing Thread B succeeds in increasing NumReaders Head NumReaders = 1 23423 23431 95 23458 33498 39274 This can now be read – before writing has finished!

53 Multi-Producer/Consumer Circular Queues (The Algorithm) A 'busy waiting' solution Let WriteMarker be equal to Head when Queue is created Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders

54 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 23 We begin as another thread is already writing to the queue H’ WriteMarker

55 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 234 Copy the head index H’ WriteMarker

56 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 2348 Advance head (assume we succeed) H’ WriteMarker

57 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 23485 Write to the buffer H’ WriteMarker 97234

58 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 23485 8 Write to the buffer H’ WriteMarker 97234 98734

59 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 23485 86 Write to the buffer H’ WriteMarker 97234 98734 02393

60 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 23485 867 Keeping looping until the other thread advances WriteMarker H’ WriteMarker 97234 98734 02393

61 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 23485 8678 Keeping looping until the other thread advances WriteMarker H’ WriteMarker 97234 98734 02393

62 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 23485 86784 Keeping looping until the other thread advances WriteMarker H’ WriteMarker 97234 98734 02393

63 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 23485 86784 Keeping looping until the other thread advances WriteMarker H’ WriteMarker 97234 98734 02393

64 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 23485 86784 Now we advance WriteMarker ourselves. (Why don’t we need to CAS this?) H’ WriteMarker 97234 98734 02393

65 Multi-Producer/Consumer Circular Queues (The Algorithm) Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *Head, (H' + 1) mod Size ) Until Success Write( H' ) Do W <- WriteMarker Until W = H’ WriteMarker <- ( WriteMarker + 1 ) mod Size Increase NumReaders Head 24234 23423 42343 24234 23485 86784 Finally we increase NumReaders now that the data is safe to be read H’ WriteMarker 97234 98734 02393

66 Multi-Producer/Consumer Circular Queues (The Algorithm) Everything works better when we all cooperate Let Done(i) be a boolean variable associated with each cell in the Array, Initially false Let *WM be the address of WriteMarkers Enqueue() ( NumWriters is decremented ) Success <- false Do H' <- Head Success <- CAS( H', *H, (H' + 1) mod Size ) Until Success Write( H' )

67 Multi-Producer/Consumer Circular Queues (The Algorithm) Everything works better when we all cooperate (con’t) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success

68 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23 86538 69 WriteMarker 97234 98734 02393 Done: false truefalse The story so far:

69 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23 86538 69 WriteMarker 97234 98734 02393 Done: false truefalse The story so far: There are two cells currently being written

70 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23 86538 69 WriteMarker 97234 98734 02393 Done: false truefalse The story so far: This is ‘us’

71 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23 86538 69 WriteMarker 97234 98734 02393 Done: false truefalse Writing continues

72 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 237 86538 694 WriteMarker 97234 98734 02393 Done: false truefalse Writing continues

73 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 86538 69445 WriteMarker 97234 98734 02393 Done: false truefalse Writing continues

74 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 8 86538 69445 4 WriteMarker 97234 98734 02393 Done: false truefalse Writing continues

75 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 893 86538 69445 489 WriteMarker 97234 98734 02393 Done: false truefalse Writing continues

76 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false truefalse Writing finishes

77 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false true Both threads update Done

78 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false true Both threads try to move the write pointer

79 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false true We succeed. This means that the other thread has failed. It will finish knowing that we will ‘clean things up’

80 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false true Update the Done flag

81 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false true Let another reader in – now that we know it’s safe.

82 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false true Repeat until all everything is in a proper state or somebody else takes over.

83 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false true Repeat until all everything is in a proper state or somebody else takes over.

84 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false Repeat until all everything is in a proper state or somebody else takes over.

85 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false Repeat until all everything is in a proper state or somebody else takes over.

86 Multi-Producer/Consumer Circular Queues (The Algorithm) Done(H') <- true Do Success <- false If not WriteMarker = Head and Done( WriteMarker ) W <- WriteMarker Success <- CAS( W, *WM, (W+1) mod Size ) if Success Done( W ) <- false Increase NumReaders While Success Head 24234 23745 89334 86538 69445 48973 WriteMarker 97234 98734 02393 Done: false Exit with the knowledge of a job well done

87 Multi-Producer/Consumer Circular Queues (Proof - Sketch) Everything works better when we all cooperate Criteria for correctness: –1) At all times: ( WriteMarker - Tail ) mod Size >= NumReaders –2) Enqueue always terminates –3) At any point in execution if no Enqueue operation is in progress then WriteMarker = Head –4) Once an item is enqueued in cell i no other enqueue operation will write to i until it is dequeued (follows from 1 and case analysis)

88 Some Performance Results


Download ppt "Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09."

Similar presentations


Ads by Google