Documente Academic
Documente Profesional
Documente Cultură
S. Wallentowitz
You have a processor at hand that does not contain any hardware support for locking or atomar operations. Extend Peterson's Lock to support three threads. How can this be generalized?
volatile bool flag[2]; volatile int victim; void lock() { int me=get_thread_id(); int other=1-me;
Initially: flag={false,false}
memory order
victim=1 victim=0 victim=1 victim=0
flag[me]=true; victim=me; while(flag[other] && victim==me){ do nothing } } void unlock() { int me=get_thread_id(); flag[me]=false; }
Section
Section
volatile bool flag[3]; volatile int victim; void lock() { int me=get_thread_id(); int other=1-me; flag[me]=true; victim=me; while(flag[other] && victim==me){ do nothing } } void unlock() { int me=get_thread_id(); flag[me]=false; }
volatile bool flag[3]; volatile int victim; int lefts[3]={2,0,1}; int rights[3]={1,2,0}; void lock() { int me=get_thread_id(); int left=lefts[me]; int right=rights[me]; flag[me]=true; victim=me; while(flag[other] && victim==me){ do nothing } } void unlock() { int me=get_thread_id(); flag[me]=false; }
volatile bool flag[3]; volatile int victim; int lefts[3]={2,0,1}; int rights[3]={1,2,0}; void lock() { int me=get_thread_id(); int left=lefts[me]; int right=rights[me]; flag[me]=true; victim=me; while((flag[left] || flag[right]) && victim==me){ do nothing } } void unlock() { int me=get_thread_id(); flag[me]=false; }
T0 T1 T2
Memory
volatile bool flag[3]; volatile int victim; int lefts[3]={2,0,1}; int rights[3]={1,2,0}; void lock() { int me=get_thread_id(); int left=lefts[me]; int right=rights[me]; flag[me]=true; victim=me; while((flag[left] || flag[right]) && victim==me){ do nothing } } void unlock() { int me=get_thread_id(); flag[me]=false; }
volatile bool flag[3]; volatile int victim[2]; int lefts[3]={2,0,1}; int rights[3]={1,2,0}; void lock() { int me=get_thread_id(); int left=lefts[me]; int right=rights[me]; flag[me]=true; victim[0]=me; victim[1]=me; while((flag[left] || flag[right]) && victim[0]==me && victim[1]==me) { do nothing } } void unlock() { int me=get_thread_id(); flag[me]=false; }
T0 T1 T2
Memory
volatile bool flag[3]; volatile int victim[2]; int lefts[3]={2,0,1}; int rights[3]={1,2,0}; void lock() { int me=get_thread_id(); int left=lefts[me]; int right=rights[me]; flag[me]=true; victim[0]=me; victim[1]=me; while((flag[left] || flag[right]) && victim[0]==me && victim[1]==me) { do nothing } } void unlock() { int me=get_thread_id(); flag[me]=false; }
...
T0
T1
T2
Memory
volatile bool flag[3]; volatile int victim[2]; int lefts[3]={2,0,1}; int rights[3]={1,2,0}; void lock() { int me=get_thread_id(); int left=lefts[me]; int right=rights[me]; flag[me]=true; victim[0]=me; while((flag[left] || flag[right]) && victim[0]==me) { do nothing } victim[1]=me; while((flag[left] || flag[right]) && victim[1]==me) { do nothing } } void unlock() { int me=get_thread_id(); flag[me]=false; }
Chip Multicore Processors Tutorial 3 11 S. Wallentowitz Institute for Integrated Systems
volatile int level[3]; volatile int victim[2]; int lefts[3]={2,0,1}; int rights[3]={1,2,0};
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; }
In the context of mutexes, explain what the difference between spinlocks and blocking locks is.