130 likes | 441 Views
Computer Organization CS224. Fall 2012 Lesson 12. Synchronization. Two processors or threads sharing an area of memory P1 writes, then P2 reads Data race if P1 and P2 don’t synchronize Result depends of order of accesses !! Software synchronization routines needed
E N D
Computer OrganizationCS224 Fall 2012 Lesson 12
Synchronization • Two processors or threads sharing an area of memory • P1 writes, then P2 reads • Data race if P1 and P2 don’t synchronize • Result depends of order of accesses !! • Software synchronization routines needed • Lock and unlock, etc. • To implement mutual exclusion to shared memory • Hardware support required • Atomic read-and-modify memory operation • No other access to the location allowed between the read and write • Used for e.g. atomic swap of register ↔ memory • Could be a single instruction (as in some processors) • Or an atomic pair of instructions (as in MIPS and others) §2.11 Parallelism and Instructions: Synchronization
Atomic Exchange Support Atomic exchange (atomic swap) – interchanges a value in a register for a value in memory atomically, i.e., as one operation (instruction) Implementing an atomic exchange would require both a memory read and a memory write in a single, uninterruptable instruction. An alternative is to have a pair of specially configured instructions ll $t1, 0($s1) #load linked sc $t0, 0($s1) #store conditional
Atomic Exchange with ll and sc If the contents of the memory location specified by the ll are changed before the sc to the same address occurs, the sc fails (returns a zero) try: add $t0, $zero, $s4 #$t0=$s4 (exchange value) ll $t1, 0($s1) #load memory value to $t1 sc $t0, 0($s1) #try to store exchange #value to memory, if fail #$t0 will be 0 beq $t0, $zero, try #try again on failure add $s4, $zero, $t1 #load value in $s4 • If the value in memory between thelland the sc instructions changes, then sc returns a 0 in $t0 causing the code sequence to try again.
The C Code Translation Hierarchy compiler assembly code assembler object code library routines linker executable loader memory C program §2.12 Translating and Starting a Program machine code
Assembler Pseudoinstructions • Most assembler instructions represent machine instructions one-to-one • Pseudoinstructions: figments of the assembler’s imagination move $t0, $t1→add $t0, $zero, $t1 blt $t0, $t1, L→slt $at, $t0, $t1bne $at, $zero, L • $at (register 1): assembler temporary
Producing an Object Code Module • Assembler (or compiler) translates program into machine instructions, makes an object module • Provides information for building a complete program from the pieces • Header: described contents of object module • Text segment: translated instructions • Static data segment: data allocated for the life of the program • Relocation info: for contents that depend on absolute location of loaded program • Symbol table: global definitions and external refs • Debug info: for associating with source code
Linking Object Modules • Produces an executable image (.exe file) 1. Merges segments 2. Resolve labels (determine their addresses) 3. Patch location-dependent and external refs • Could leave location dependencies for fixing later with a relocating loader • But with virtual memory, no need to do this • Program can be loaded into absolute location in virtual memory space
Loading a Program • Load from image file on disk into memory 1. Read header to determine segment sizes for text and data 2. Create virtual address space (large enough for text and data) 3. Copy text and initialized data into memory 4. Copy parameters for main program (if any) onto stack 5. Initialize registers (including $sp, $fp, $gp) 6. Jump to startup routine • Copies arguments to $a0, … and calls main • When main returns, do exit syscall
Dynamic Linking • Only link/load library procedure when it is called • Requires procedure code to be relocatable • Avoids image bloat caused by static linking of all (transitively) referenced libraries • Automatically picks up new library versions
Lazy Linkage Indirection table Stub: loads routine ID,jumps to linker/loader Linker/loader code Dynamicallymapped code
Starting Java Applications Simple portable instruction set for the JVM Compiles bytecodes of “hot” methods into native code for host machine Interprets bytecodes