38
MIL Multithreaded Intermediate Language Tiago Cogumbreiro [email protected] Joint work with Francisco Martins Vasco T. Vasconcelos Universidade de Lisboa GLUA Tech Sessions ’08

MIL in GLUA TechSessions '08

Embed Size (px)

DESCRIPTION

A presentation about MIL in GLUA TechSessions '08t

Citation preview

Page 1: MIL in GLUA TechSessions '08

MIL

Multithreaded Intermediate Language

Tiago [email protected]

Joint work withFrancisco Martins Vasco T. Vasconcelos

Universidade de Lisboa

GLUA Tech Sessions ’08

Page 2: MIL in GLUA TechSessions '08

MIL

Motivation

Outline

1 Motivation

2 A Lock Discipline

3 Data Races

4 The Language

5 Examples

Page 3: MIL in GLUA TechSessions '08

Singlecore

Page 4: MIL in GLUA TechSessions '08

Multicore

Page 5: MIL in GLUA TechSessions '08

MIL

Motivation

Context

Fine-grained multithreading

Thousand of threads

Page 6: MIL in GLUA TechSessions '08

MIL

Motivation

Goal

Concurrency primitives

Shared memory

Security properties:

typedenforce a memory usage discipline

Page 7: MIL in GLUA TechSessions '08

MIL

Motivation

Our approach

Typed assembly language

Support threads

Flannagan’s and Abadi’s locking discipline

Page 8: MIL in GLUA TechSessions '08

MIL

Motivation

MIL’s Architecture

run pool heap

CPU core 1

registers

localmemory

CPU core N

registers

localmemory

Page 9: MIL in GLUA TechSessions '08

MIL

A Lock Discipline

Outline

1 Motivation

2 A Lock Discipline

3 Data Races

4 The Language

5 Examples

Page 10: MIL in GLUA TechSessions '08

MIL

A Lock Discipline

/** Lock surface. (Don’t forget to call Unlock)*/

void nD3D9Surface::Lock(int &pitch, void** data)

The Nebula Device, a real-time 3D game/visualization engine.

Page 11: MIL in GLUA TechSessions '08

MIL

A Lock Discipline

What if someone forgets?

Page 12: MIL in GLUA TechSessions '08
Page 13: MIL in GLUA TechSessions '08

MIL

A Lock Discipline

else if(wndPtr->irefCount < 0){

/* This else if is useful to monitorthe WIN_ReleaseWndPtr function */

ERR("forgot a Lock on %p somewhere\n",wndPtr);}/* unlock all WND structures for thread safeness */USER_Unlock();

WINE 20050211

Page 14: MIL in GLUA TechSessions '08

MIL

A Lock Discipline

/* This should never happen, otherwise someoneforgot to get the HostEntry->lock! */

CODA_ASSERT(HostEntry->id == 0);

CODA 5.3.20

Page 15: MIL in GLUA TechSessions '08

MIL

A Lock Discipline

Is that a solution?

Page 16: MIL in GLUA TechSessions '08

MIL

A Lock Discipline

Rules

Close known lock → acquires permission to unlock

Obligated to unlock the lock or transfer that obligation

Page 17: MIL in GLUA TechSessions '08

MIL

Data Races

Outline

1 Motivation

2 A Lock Discipline

3 Data Races

4 The Language

5 Examples

Page 18: MIL in GLUA TechSessions '08

What if I could change yourpoker’s hand?

Page 19: MIL in GLUA TechSessions '08

MIL

Data Races

Mutual Exclusion

One thread (or process) alone handles the resource.

Page 20: MIL in GLUA TechSessions '08

MIL

Data Races

Enforcing mutual exclusion

Core 1 Core 2 Core 3

Page 21: MIL in GLUA TechSessions '08

MIL

Data Races

No race conditions!

Page 22: MIL in GLUA TechSessions '08

MIL

The Language

Outline

1 Motivation

2 A Lock Discipline

3 Data Races

4 The Language

5 Examples

Page 23: MIL in GLUA TechSessions '08

MIL

The Language

Memory and control flow

Move r := v

Alloc r := malloc [~τ ]

Load r := v [n]

Store r [n] := v

Jump jump v

Conditional Jump if r = v jump v

No call stack.

Page 24: MIL in GLUA TechSessions '08

MIL

The Language

Security properties

Unset register are not read

Loads and stores target tuples

Stores of the expected type only

Jump to code blocks only

Arithmetic only with registers holding integers

Page 25: MIL in GLUA TechSessions '08

MIL

The Language

Threads

Create fork v

Finish yield

Cooperative threads

Page 26: MIL in GLUA TechSessions '08

MIL

The Language

Security properties

Zero closed locks in order to yield

Forking code blocks only

Page 27: MIL in GLUA TechSessions '08

MIL

The Language

Test and set

Atomic operation:

int testSet(int *ptr) {int v = *ptr;*ptr = 1;return v;

}

Page 28: MIL in GLUA TechSessions '08

MIL

The Language

Implementing a lock with test and set

Case 1→ 1: was locked

Case 0→ 1: was unlocked

Page 29: MIL in GLUA TechSessions '08

MIL

The Language

Locks

Create α, r := newLock

Acquire r := testSetLock v

Release unlock v

Share share r guarded by α

Page 30: MIL in GLUA TechSessions '08

MIL

The Language

Security properties

Test and set lock open locks

Unlock closed locks, with permission

Target locks (type)

Page 31: MIL in GLUA TechSessions '08

MIL

Examples

Outline

1 Motivation

2 A Lock Discipline

3 Data Races

4 The Language

5 Examples

Page 32: MIL in GLUA TechSessions '08

MIL

Examples

Factorial

fract (r1:int) {r2 := 1if r1 = 0 jump fractFinishjump doFract

}doFract (r1:int, r2:int) {if r1 = 1 jump fractFinishr2 := r2 * r1r1 := r1 - 1jump doFract

}fractFinish (r2:int) {-- do some work

}

Page 33: MIL in GLUA TechSessions '08

MIL

Examples

Creating a tuple

prepare () {r1 := malloc [int, int]r1[0] := 10r1[1] := 20share r1 guarded by ljump compute

}compute (r1: <int, int>^l) {-- do some work

}

Page 34: MIL in GLUA TechSessions '08

MIL

Examples

Spin-lock

spinLock (r1:<lock(l)>^l, r2:<int>^l) {r3 := testSetLock r1if r3 = 0 jump criticalRegionjump spinLock

}criticalRegion (r1: <lock(l)>^l, r2:<int>^l)

requires (l) {r1[0] := 10unlock r1yield

}

Page 35: MIL in GLUA TechSessions '08

MIL

Examples

Sleep-lock

sleepLock (r1:<lock(l)>^l, r2:<int>^l) {r3 := testSetLock r1if r3 = 0 jump criticalRegionfork sleepLockyield

}criticalRegion (r1: <lock(l)>^l, r2:<int>^l)

requires (l) {r1[0] := 10unlock r1yield

}

Page 36: MIL in GLUA TechSessions '08

MIL

Examples

Omitted features

Universal quantifier

Existential quantifier

Recursive type

Read-only tuples

Page 37: MIL in GLUA TechSessions '08

MIL

Examples

Further work

Compare and swap for lock-free data structures

Dead-lock freedom

Page 38: MIL in GLUA TechSessions '08

MIL

Examples

For publications and implementation, please refer tohttp://gloss.di.fc.ul.pt/mil

Thank you.

Any questions?