1 / 9

Coordination

Coordination. 6.894 Lecture 5. Why Coordinate?. Critical section: Must execute atomically, without interruption. Atomicity usually only w.r.t. other operations on the same data structures. What are sources of interruption? Hardware interrupts, UNIX signals. Thread pre-emption.

rmarcia
Télécharger la présentation

Coordination

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Coordination 6.894 Lecture 5

  2. Why Coordinate? • Critical section: • Must execute atomically, without interruption. • Atomicity usually only w.r.t. other operations on the same data structures. • What are sources of interruption? • Hardware interrupts, UNIX signals. • Thread pre-emption. • Interleaving of multiple CPUs.

  3. Tools for Atomicity (1) • If this worked, we would be done: TestAndSet(int *addr){ int old = *addr; *addr = 1; return(old); } insert(…){ while(TestAndSet(&locked) == 1) ; /* critical section goes here */ locked = 0; }

  4. Tools for Atomicity (2) • Turn off interrupts in TAS to avoid race. • Prevents pre-emption via real-time clock. • Only in the kernel, only on a uniprocessor. • Kernel emulation of TestAndSet. • Only on a uniprocessor. • Test-And-Set-Locked hardware support.

  5. Test-And-Set-Locked Instruction • Single instruction to do this: int old = *addr; *addr = 1; return(old); • The hardware locks the memory system to keep the two sub-operations atomic. • TSL lets us build more complex atomic sequences. • See Example 3 for a simple one.

  6. Problems with TSL • Operates at motherboard speeds, not CPU. • Much slower than cached load or store. • Prevents other use of the memory system. • Interferes with other CPUs and DMA. • Silly to spin in TSL on a uniprocessor. • Add a thread_yield() after every TSL.

  7. Locks Aren’t Enough • Summary of code Example 4: • Consumer: • If(input isn’t ready) sleep(); • Producer: • If(consumer is waiting) wake it up; • What’s the problem? • Can we solve the problem with locks alone?

  8. Sequence Coordination • Need to avoid races between decision to sleep and actual sleep(). • Can’t hold lock while sleeping. • Need the thread scheduler to help us release the lock. • Common abstraction: condition variables. • Code Examples 5 and 6.

  9. Condition Variable Rules • Hold the lock while calling wait(). • Hold the lock while calling signal(). • wait() acquires the lock before returning.

More Related