Chip Multicore Processors: Tutorial 4
Chip Multicore Processors: Tutorial 4
S. Wallentowitz
mutex lock;
On the right you find a piece of source code to implement a stack. In a stack, the last written element is read as first (LIFO).
Use the functions mutex_lock(mutex*) and mutex_unlock(mutex*) to make the code thread-safe.
void write(int i) { int tmp=c; while(tmp==N) { tmp=c; } buf[c]=i; c=c+1; } int read() { int r, tmp=c; while(tmp==0) { tmp=c; } r=buf[c]; c=c-1; return r; }
Institute for Integrated Systems
int read() { int r, tmp tmp = c; while(tmp==0) { tmp=c; } r=buf[c]; c=c-1; return r; }
tmp=c;
while(tmp==N) { tmp=c;
while(tmp==0) {
tmp=c; } r=buf[c]; c=c-1; return r; }
}
buf[c]=i; c=c+1; }
tmp=c;
while(tmp==N) { tmp=c;
while(tmp==0) {
tmp=c; } r=buf[c]; c=c-1; return r; }
}
buf[c]=i; c=c+1; }
tmp=c;
while(tmp==N) { tmp=c;
while(tmp==N) {
tmp=c; } r=buf[c]; c=c-1; return r; }
}
buf[c]=i; c=c+1; }
tmp=c;
while(tmp==N) { tmp=c;
while(tmp==N) {
tmp=c; } r=buf[c]; c=c-1; return r; }
}
buf[c]=i; c=c+1; }
Condition Variables
Functions
wait() signal() or broadcast()
wait()
Thread 0
Thread 1
Thread 2
wait() signal()
wait()
broadcast()
Signal event
Pop thread from queue Re-activate thread
Broadcast
Re-activate all threads in queue
Signal
Signal to one waiting thread that the condition holds
Broadcast
Signal to all waiting threads that the condition holds Useful for example when implementing barriers
#define N 100 int buf[N]; int c=0; mutex lock; void write(int i) { int tmp tmp=c; int read() { int r, tmp tmp=c;
Implement the counter with locks, compare-and-swap and loadlinked/store-conditional. How is each of the implementations characterized?
volatile int count; void countup() { int tmp; tmp = count; tmp = tmp+1; count = tmp; }
countup: ... R3 <- @(count); R4 <- LD(R3); R4 <- R4 + 1; R4 -> ST(R3) ...
Lock Implementation
volatile int count; void countup() { int tmp; tmp = count; tmp = tmp+1; count = tmp; }
Compare-and-Swap
Similar to Test-and-Set Test for data value and conditionally replace
Read value If value matches compared value: swap If value does not match: leave old value Return previous value
compare swap address
int cas(int* addr, int compare, int swap) { int tmp=*addr; if (tmp==compare) { *addr=swap; } return tmp; }
value
==
Modify
(Bus) Interconnect
Read
data Memory
Write
Compare-and-Swap Implementation
int CAS(int* addr,int compare,int swap);
volatile int count; void countup() { int tmp; tmp = count; tmp = tmp+1; count = tmp; }
Load-Linked/Store-Conditional
Alternative approach: Hardware tracks changes Load-Linked (LL):
Load value and set linked bit Store value when data is unchanged Returns success of store Can also incorrectly fail (implementation dependent): Exceptions, other LL, cache line replacement
LL
value addr
SC
success addr value
Linked bit(s)
Store-Conditional (SC):
set
==1
LL/SC Implementation
int LL(int* addr); bool SC(int* addr,int value);
tmp = tmp+1;
count = tmp; }
FiFo Implementation
int data[N]; unsigned int start; unsigned int end; void enqueue(int d) { while(((end+1)%N)==start)) {} data[end] = d; end = (end+1)%N; } int dequeue() { int d; while(end==start) {} d=data[start]; start=(start+1)%N; return d; }