SXM: Software Transactional Memory in C# Maurice Herlihy.

21
SXM: Software Transactional Memory in C# Maurice Herlihy

Transcript of SXM: Software Transactional Memory in C# Maurice Herlihy.

Page 1: SXM: Software Transactional Memory in C# Maurice Herlihy.

SXM: Software Transactional Memory in C#

Maurice Herlihy

Page 2: SXM: Software Transactional Memory in C# Maurice Herlihy.

2

Prior STM Work

• Awkward user interface– Long-lived transactional “wrappers” vs– Short-lived “versions”

• Programmer conventions– List element points to wrapper which

points to list ….– Don’t use short-lived objects beyond

lifetime ….

Page 3: SXM: Software Transactional Memory in C# Maurice Herlihy.

3

public class List { public int item; public TMObject<List> next;}

Old-School Atomic Classes

Next field is explicit wrapper

Page 4: SXM: Software Transactional Memory in C# Maurice Herlihy.

4

List next = list.next.OpenRead();

Old-School Atomic Classes

Explicit open(specify read or write)

Page 5: SXM: Software Transactional Memory in C# Maurice Herlihy.

5

List next = list.next.OpenRead();

Old-School Atomic Classes

Must discard after transaction, don’t modify,

etc…

Page 6: SXM: Software Transactional Memory in C# Maurice Herlihy.

6

List rVersion = list.next.OpenRead();

Old-School Atomic Classes

Read version unchangedRead

version changed

List wVersion = list.next.OpenWrite();wVersion.item++;

List wVersion = list.next.OpenWrite();

List rVersion = list.next.OpenRead();

Page 7: SXM: Software Transactional Memory in C# Maurice Herlihy.

7Software Transactional Memory

Current Work

• Rewriting STM from scratch in C#• Use Reflection.Emit to provide

reasonable user interface• Ability to generate code at run-

time opens up new possibilities …

Page 8: SXM: Software Transactional Memory in C# Maurice Herlihy.

8

[Atomic]public class List { private int item; private List next;}

Atomic Classes in SXM

Declare “Atomic” property

Page 9: SXM: Software Transactional Memory in C# Maurice Herlihy.

9

[Atomic]public class List { private int item; private List next;}

Atomic Classes in SXM

Declare fields private

No explicit wrapper!

Page 10: SXM: Software Transactional Memory in C# Maurice Herlihy.

10

public class List { private int item; private List next;

public virtual int Item { get { return this.item; } set { this.item = value; } }}

C# Properties

Page 11: SXM: Software Transactional Memory in C# Maurice Herlihy.

11

public class List { private int item; private List next;

public virtual int Item { get { return this.item; } set { this.item = value; } }}

C# Properties

Wrap each field in a public virtual property

Page 12: SXM: Software Transactional Memory in C# Maurice Herlihy.

12

XObjectFactory listFactory = new XObjectFactory(typeof(List));

Transactional List

Generates code to intercept property get & set methods

for List type

Page 13: SXM: Software Transactional Memory in C# Maurice Herlihy.

13

XObjectFactory listFactory = new XObjectFactory(typeof(List));

Transactional List

Uses reflection to “walk” over type and generate

synchronization wrappers for Properties

Page 14: SXM: Software Transactional Memory in C# Maurice Herlihy.

14

List list = (List)ListFactory.Create();

Transactional List

Anonymous subtype of List with transparent

transactional machinery

Page 15: SXM: Software Transactional Memory in C# Maurice Herlihy.

15

public override object Insert(int v) { Node prevNode = Find(v); if (prevNode.Next.Value == v) { return false; } else { Node newNode = (Node)factory.Create(v); newNode.Next = prevNode.Next; prevNode.Next = newNode; return true; } }

Transaction Code

Page 16: SXM: Software Transactional Memory in C# Maurice Herlihy.

16

public bool Insert(int v) { Node prevNode = Find(v); if (prevNode.Next.Value == v) { return false; } else { Node newNode = (Node)factory.Create(v); newNode.Next = prevNode.Next; prevNode.Next = newNode; return true; } }

Transaction Code

Look like field references …actually property calls

Page 17: SXM: Software Transactional Memory in C# Maurice Herlihy.

17

public override object Insert(int v) { Node prevNode = Find(v); if (prevNode.Next.Value == v) { return false; } else { Node newNode = (Node)factory.Create(v); newNode.Next = prevNode.Next; prevNode.Next = newNode; return true; } }

Transaction Code

Create a new node(same args as constructor)

Page 18: SXM: Software Transactional Memory in C# Maurice Herlihy.

18

Transactions: Simple C# Interface

XStart insertXStart = new XStart(Insert);

Create a delegate (function ptr)

Page 19: SXM: Software Transactional Memory in C# Maurice Herlihy.

19

Transactions: Simple C# Interface

XStart insertXStart = new XStart(Insert);Xaction.Run(insertXStart);

Run the transaction

Page 20: SXM: Software Transactional Memory in C# Maurice Herlihy.

20

Transactions in SXM

• No explicit retry loop– Contention manager decides when to

retry failed transactions

• Transaction == method– Reduces scoping errors

Page 21: SXM: Software Transactional Memory in C# Maurice Herlihy.

21

In Progress

• Modular Retry– As in [HPJMH 05]– Requires 1st class nested transactions

• Polymorphic contention managers– Easier on-the-fly switching– Communication among

heterogeneous CMs