Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Principles of Computers 19 th Lecture Pavel Ježek, Ph.D.

Similar presentations


Presentation on theme: "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Principles of Computers 19 th Lecture Pavel Ježek, Ph.D."— Presentation transcript:

1 CHARLES UNIVERSITY IN PRAGUE http://d3s.mff.cuni.cz/~jezek faculty of mathematics and physics Principles of Computers 19 th Lecture Pavel Ježek, Ph.D. pavel.jezek@d3s.mff.cuni.cz

2 Thread T1 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem;

3 Thread T1 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem;

4 Thread T1 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem;

5 Thread T1 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem;

6 Thread T1 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem;

7 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem;

8 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem;

9 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem;

10 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; Context Switch

11 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T2: OtherItem^.prev := AfterThis; T2: AfterThis^.next^.prev := OtherItem; T2: OtherItem^.next := AfterThis^.next; T2: AfterThis.next := OtherItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; Context Switch next prev OtherItem

12 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T2: OtherItem^.prev := AfterThis; T2: AfterThis^.next^.prev := OtherItem; T2: OtherItem^.next := AfterThis^.next; T2: AfterThis.next := OtherItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; next prev OtherItem

13 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T2: OtherItem^.prev := AfterThis; T2: AfterThis^.next^.prev := OtherItem; T2: OtherItem^.next := AfterThis^.next; T2: AfterThis.next := OtherItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; next prev OtherItem

14 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T2: OtherItem^.prev := AfterThis; T2: AfterThis^.next^.prev := OtherItem; T2: OtherItem^.next := AfterThis^.next; T2: AfterThis.next := OtherItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; next prev OtherItem

15 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T2: OtherItem^.prev := AfterThis; T2: AfterThis^.next^.prev := OtherItem; T2: OtherItem^.next := AfterThis^.next; T2: AfterThis.next := OtherItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; next prev OtherItem

16 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T2: OtherItem^.prev := AfterThis; T2: AfterThis^.next^.prev := OtherItem; T2: OtherItem^.next := AfterThis^.next; T2: AfterThis.next := OtherItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; Context Switch next prev OtherItem Still a valid doubly-linked list

17 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T2: OtherItem^.prev := AfterThis; T2: AfterThis^.next^.prev := OtherItem; T2: OtherItem^.next := AfterThis^.next; T2: AfterThis.next := OtherItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; next prev OtherItem

18 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T2: OtherItem^.prev := AfterThis; T2: AfterThis^.next^.prev := OtherItem; T2: OtherItem^.next := AfterThis^.next; T2: AfterThis.next := OtherItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; next prev OtherItem

19 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T2: OtherItem^.prev := AfterThis; T2: AfterThis^.next^.prev := OtherItem; T2: OtherItem^.next := AfterThis^.next; T2: AfterThis.next := OtherItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; next prev OtherItem

20 Threads T1 and T2 Inserting into A Doubly-Linked List next prev AfterThis next prev NewItem next prev BeforeThis T1: NewItem^.prev := AfterThis; T1: AfterThis^.next^.prev := NewItem; T2: OtherItem^.prev := AfterThis; T2: AfterThis^.next^.prev := OtherItem; T2: OtherItem^.next := AfterThis^.next; T2: AfterThis.next := OtherItem; T1: NewItem^.next := AfterThis^.next; T1: AfterThis.next := NewItem; next prev OtherItem

21 Is It Thread Safe? next prev AfterThis next prev NewItem next prev BeforeThis procedure InsertItem(AfterThis : Node; NewItem : Node) begin NewItem^.prev := AfterThis; AfterThis^.next^.prev := NewItem; NewItem^.next := AfterThis^.next; AfterThis.next := NewItem; end;

22 Is It Thread Safe? In Cooperative Multitasking? next prev AfterThis next prev NewItem next prev BeforeThis procedure InsertItem(AfterThis : Node; NewItem : Node) begin Yield; { allowed here } NewItem^.prev := AfterThis; AfterThis^.next^.prev := NewItem; no Yield here! NewItem^.next := AfterThis^.next; AfterThis.next := NewItem; Yield; { allowed here } end;

23 Is It Thread Safe? In Preemptive Multitasking? next prev AfterThis next prev NewItem next prev BeforeThis procedure InsertItem(AfterThis : Node; NewItem : Node) begin NewItem^.prev := AfterThis; AfterThis^.next^.prev := NewItem; NewItem^.next := AfterThis^.next; AfterThis.next := NewItem; end;

24 Critical Section Is It Thread Safe? In Preemptive Multitasking? next prev AfterThis next prev NewItem next prev BeforeThis procedure InsertItem(AfterThis : Node; NewItem : Node) begin NewItem^.prev := AfterThis; AfterThis^.next^.prev := NewItem; NewItem^.next := AfterThis^.next; AfterThis.next := NewItem; end;

25 Is It Thread Safe? In Preemptive Multitasking? next prev AfterThis next prev NewItem next prev BeforeThis procedure InsertItem(AfterThis : Node; NewItem : Node) begin asm cli; NewItem^.prev := AfterThis; AfterThis^.next^.prev := NewItem; NewItem^.next := AfterThis^.next; AfterThis.next := NewItem; asm sti; end;

26 Critical Section Is It Thread Safe? In Preemptive Multitasking? next prev AfterThis next prev NewItem next prev BeforeThis procedure InsertItem(AfterThis : Node; NewItem : Node) begin NewItem^.prev := AfterThis; AfterThis^.next^.prev := NewItem; NewItem^.next := AfterThis^.next; AfterThis.next := NewItem; end;

27 Is It Thread Safe? Using Locks? next prev AfterThis next prev NewItem next prev BeforeThis procedure InsertItem(AfterThis : Node; NewItem : Node) begin Lock(l); NewItem^.prev := AfterThis; AfterThis^.next^.prev := NewItem; NewItem^.next := AfterThis^.next; AfterThis.next := NewItem; Unlock(l); end;

28 Legend Data Thread lifetime Running thread Thread waiting for an unspecified event Thread waiting for a lock Thread ready to run Successful thread action Failed thread action Action asynchronous from the point of view of a target thread Unlocked lock Locked lock

29 Fighting for A Lock Lock A state: unlocked Lock A state: unlocked RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3

30 Fighting for A Lock Lock A state: unlocked Lock A state: unlocked RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A

31 Fighting for A Lock Lock A state: held by T1 Lock A state: held by T1 RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A

32 Fighting for A Lock Lock A state: held by T1 Lock A state: held by T1 RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A

33 Fighting for A Lock Lock A state: held by T1 Lock A state: held by T1 RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A

34 Fighting for A Lock Lock A state: held by T1 Lock A state: held by T1 RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A

35 Fighting for A Lock Lock A state: held by T1 Lock A state: held by T1 RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A lock A

36 Fighting for A Lock Lock A state: held by T1 Lock A state: held by T1 RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A lock A W. A

37 Fighting for A Lock Lock A state: held by T1 Lock A state: held by T1 RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A lock A W. A unlock A

38 Fighting for A Lock Lock A state: unlocked Lock A state: unlocked RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A lock A W. A unlock A

39 Fighting for A Lock Lock A state: unlocked Lock A state: unlocked RUNNING Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A lock A W. A unlock A wake up

40 RUNNING RTR READY-TO-RUN RTR READY-TO-RUN RTR READY-TO-RUN RTR READY-TO-RUN Fighting for A Lock Lock A state: unlocked Lock A state: unlocked Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A lock A W. A unlock A wake up

41 RUNNING RTR READY-TO-RUN RTR READY-TO-RUN RTR READY-TO-RUN RTR READY-TO-RUN Fighting for A Lock Lock A state: unlocked Lock A state: unlocked Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A lock A W. A unlock A wake up RUN lock A

42 RUNNING RTR READY-TO-RUN RTR READY-TO-RUN RTR READY-TO-RUN RTR READY-TO-RUN Fighting for A Lock Lock A state: held by T2 Lock A state: held by T2 Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A lock A W. A unlock A wake up RUN lock A

43 READY-TO-RUN RUNNING RTR READY-TO-RUN RTR READY-TO-RUN Fighting for A Lock Lock A state: held by T2 Lock A state: held by T2 Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A lock A W. A unlock A wake up lock A RUNNING RUN lock A

44 READY-TO-RUN RUNNING RTR READY-TO-RUN RTR READY-TO-RUN Fighting for A Lock Lock A state: held by T2 Lock A state: held by T2 Thread 1 RUNNING Thread 2 RUNNING Thread 3 lock A WAITING for Lock A lock A W. A unlock A wake up lock A RUNNING RUN lock A WAITING for A

45 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: unlocked Lock A state: unlocked WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3

46 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: unlocked Lock A state: unlocked WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3 lock A

47 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3 lock A

48 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3 lock A wake up

49 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3 lock A wake up RTR READY-TO-RUN RTR READY-TO-RUN preemption (forced context switch)

50 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3 lock A wake up RTR READY-TO-RUN RTR READY-TO-RUN RUNNING

51 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3 lock A wake up RTR READY-TO-RUN RTR READY-TO-RUN RUNNING wake up

52 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3 lock A wake up RTR READY-TO-RUN RTR READY-TO-RUN RUNNING wake up RTR READY-TO-RUN RTR READY-TO-RUN preemption (forced context switch)

53 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3 lock A wake up RTR READY-TO-RUN RTR READY-TO-RUN RUNNING wake up RTR READY-TO-RUN RTR READY-TO-RUN RUNNING RTR READY-TO-RUN RTR READY-TO-RUN

54 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3 lock A wake up RTR READY-TO-RUN RTR READY-TO-RUN RUNNING wake up RTR READY-TO-RUN RTR READY-TO-RUN RUNNING RTR READY-TO-RUN RTR READY-TO-RUN lock A

55 Priority Inversion Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 WAITING Thread 1 WAITING Thread 2 RUNNING Thread 3 lock A wake up RTR READY-TO-RUN RTR READY-TO-RUN RUNNING wake up RTR READY-TO-RUN RTR READY-TO-RUN RUNNING RTR READY-TO-RUN RTR READY-TO-RUN lock A WAITING for Lock A RUNNING context switch

56 Priority Inversion: Solution? Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN

57 Solution? Priority Boost Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN WAIT priority boost RTR READY-TO-RUN RTR READY-TO-RUN

58 Solution? Priority Boost Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN WAITING for Lock A RTR READY-TO-RUN RTR READY-TO-RUN RUNNING

59 Solution? Priority Boost Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T3 Lock A state: held by T3 Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN WAITING for Lock A RTR READY-TO-RUN RTR READY-TO-RUN RUNNING unlock A

60 Solution? Priority Boost Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: unlocked Lock A state: unlocked Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN WAITING for Lock A RTR READY-TO-RUN RTR READY-TO-RUN RUNNING unlock A

61 Solution? Priority Boost Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: unlocked Lock A state: unlocked Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN WAITING for Lock A RTR READY-TO-RUN RTR READY-TO-RUN RUNNING unlock A wake up

62 Solution? Priority Boost Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: unlocked Lock A state: unlocked Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN WAITING for Lock A RTR READY-TO-RUN RTR READY-TO-RUN RUNNING unlock A RUN restore old priority RTR READY-TO-RUN RTR READY-TO-RUN

63 Solution? Priority Boost Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: unlocked Lock A state: unlocked Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN WAITING for Lock A RTR READY-TO-RUN RTR READY-TO-RUN RUNNING unlock A RUN RTR READY-TO-RUN RTR READY-TO-RUN preemption (forced context switch) RUN RTR READY-TO-RUN RTR READY-TO-RUN

64 Solution? Priority Boost Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: unlocked Lock A state: unlocked Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN WAITING for Lock A RTR READY-TO-RUN RTR READY-TO-RUN RUNNING unlock A RUN RTR READY-TO-RUN RTR READY-TO-RUN RUN RTR READY-TO-RUN RTR READY-TO-RUN lock A

65 Solution? Priority Boost Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Lock A state: held by T1 Lock A state: held by T1 Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN WAITING for Lock A RTR READY-TO-RUN RTR READY-TO-RUN RUNNING unlock A RUN RTR READY-TO-RUN RTR READY-TO-RUN RUN RTR READY-TO-RUN RTR READY-TO-RUN lock A

66 Solution? Priority Boost Priority Level 0 (highest) Priority Level 1 Priority Level 2 Priority Level 3 Priority Level 4 (lowest) Thread 1 Thread 2 Thread 3 READY-TO-RUN RUNNING lock A READY-TO-RUN WAITING for Lock A RTR READY-TO-RUN RTR READY-TO-RUN RUNNING unlock A RUN RTR READY-TO-RUN RTR READY-TO-RUN RUNNING READY-TO-RUN Lock A state: held by T1 Lock A state: held by T1 lock A


Download ppt "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Principles of Computers 19 th Lecture Pavel Ježek, Ph.D."

Similar presentations


Ads by Google