Sunteți pe pagina 1din 21

Technische Universitt Mnchen

Chip Multicore Processors


Tutorial 3

S. Wallentowitz

Institute for Integrated Systems Theresienstr. 90 Building N1 www.lis.ei.tum.de

Technische Universitt Mnchen

Task 3.1: 3-Thread Lock

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?

Chip Multicore Processors Tutorial 3 2 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

Remember: Petersons Lock


Add shared variable victim
Interleaved execution: the last write to victim gives access to the other thread
Thread 0 Thread 1

volatile bool flag[2]; volatile int victim; void lock() { int me=get_thread_id(); int other=1-me;

Initially: flag={false,false}

flag[0]=true victim=0 while (..) while (..) Critical

flag[1]=true victim=1 while (..) while (..) Critical

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; }

I am interested let the other go first

Section

Section

Works for two threads Extend for 3 threads

Chip Multicore Processors Tutorial 3 3 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

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; }

Chip Multicore Processors Tutorial 3 4 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

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; }

Chip Multicore Processors Tutorial 3 5 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

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

Chip Multicore Processors Tutorial 3 6 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

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; }

Chip Multicore Processors Tutorial 3 7 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

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

Chip Multicore Processors Tutorial 3 8 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

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; }

Chip Multicore Processors Tutorial 3 9 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

...

T0

T1

T2

Memory

void lock() { ...


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 10 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

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

Technische Universitt Mnchen

volatile int level[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];


level[me]=1; victim[0]=me; while((level[left]>=1 || level[right]>=1) && victim[0]==me) { do nothing }

level[me]=2; victim[1]=me; while((level[left]==2 || level[right]==2) && victim[1]==me) { do nothing }


} void unlock() { int me=get_thread_id(); flag[me]=false; }

Chip Multicore Processors Tutorial 3 12 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

Task 3.2: Locking

#define N 100 int buf[N]; int c=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

Chip Multicore Processors Tutorial 3 13 S. Wallentowitz

Technische Universitt Mnchen

void write(int i) { int tmp tmp=c; while(tmp==N) { tmp=c; } buf[c]=i; c=c+1; }

Chip Multicore Processors Tutorial 3 14 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

int read() { int r, tmp tmp = c; while(tmp==0) { tmp=c; } r=buf[c]; c=c-1; return r; }

Chip Multicore Processors Tutorial 3 15 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

void write(int i) { int tmp

int read() { int r, tmp tmp = c;

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; }

Chip Multicore Processors Tutorial 3 16 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

void write(int i) { int tmp

int read() { int r, tmp tmp = c;

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; }

Chip Multicore Processors Tutorial 3 17 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

void write(int i) { int tmp

int read() { int r, tmp tmp = c;

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; }

Chip Multicore Processors Tutorial 3 18 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

void write(int i) { int tmp

int read() { int r, tmp tmp = c;

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; }

Chip Multicore Processors Tutorial 3 19 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

Task 3.3: Spinlocks vs. Blocking locks

In the context of mutexes, explain what the difference between spinlocks and blocking locks is.

Chip Multicore Processors Tutorial 3 20 S. Wallentowitz

Institute for Integrated Systems

Technische Universitt Mnchen

Chip Multicore Processors Tutorial 3 21 S. Wallentowitz

Institute for Integrated Systems

S-ar putea să vă placă și