Docstoc

nclab.kaist.ac.krlecturecs230_2003_Springshared

Document Sample
nclab.kaist.ac.krlecturecs230_2003_Springshared Powered By Docstoc
					Coordinating references to
 shared writable variables
             Problem: Shared Writable
                    Variables
•       Yield() {
           threadtable[me] = SP;
           next_thread = (next_thread + 1)%7
           SP = threadtable[next_thread] ;
           return ;
    }

Temp = next_thread + 1 ; /* 1 */
Temp = temp % 7 ;         /* 2 */
Next_thread = temp ;     /* 3 */

•       Two thread running yield()
         –   two different execution scenarios
               •   A1, A2, A3, B1, B2, B3
               •   A1, B1, A2, A3, B2, B3
•       Race Condition
• Race condition
  – Timing dependent
  – Difficult to debug
    • Difficult to reproduce a bug
• What can we do?
                         Atomic Actions
•   Make two threads cannot execute a sequence of instructions
    simultaneously
     –   Atomic action: a unit of instructions guaranteed to execute indivisibly
           •   We shouldn’t be able to discover that an atomic action is composed of multiple
               instructions
•   Use locks to make an atomic action
     –   Acquire() / release()
     –   Single-acquire rule

     Lock ThreadLock;

     acquire(ThreadLock) ;
           Temp = next_thread + 1 ;   /* 1 */
           Temp = temp % 7 ;           /* 2 */
           Next_thread = temp ;       /* 3 */
     release(ThreadLock) ;

•   Use “locks” when you need an atomic action. But all programs should honor this
    convention
•   A new problem: deadlock
      Implementing Locks
• We should impose the single-acquire
  rule
  – Multiple threads may try, but only one
    should succeed
  – Acquire itself should be an atomic
    operation
Broken-aquire(L) {
   /* 1 */ while ( L == TRUE)   ; // spin until L is false
   /* 2 */ L = TRUE;
}
Release (L) {
   L = FALSE ;
}

What if threads A and B do the following:

A1   (discover L == FALSE)
B1   (discover L == FALSE)
A2   (set L to TRUE)
A2   (set L to TRUE)

Violates single-acquire rule
• How to make the “acquire()” atomic?
  – From an atomic lock, we can build a
    larger atomic actions. But, we need
    something smaller that is atomic.
  – What can we do?
    • HW? SW?
    Try to Set Lock / Test and Set
                 Lock
• TSL (try to set lock, test and set lock)

 TSL mem, R:      /* TSL memory location mem;
                         result in register R; */
    R  [mem];  /* Load value stored at mem into R */
   [mem] TRUE; /* store TRUE into memory location
                                              mem */


• Note that R is a register, not a variable.
• TSL is executed in a single memory cycle
• By a bus arbiter, mutual exclusion is guaranteed
                            TSL
Acquire (L) {
   Do {
        TSL L, R0
   } while (R0 == TRUE) ;
}
Release(L) {
     L = FALSE ;
}

• Weak point: spin loop
   – Should be implemented using wait() especially when
     critical section is long
                TSL
• Can you make a software version?
• Homework for an extra-credit.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:5/19/2013
language:Unknown
pages:10