05.08.2014 Views

here - Stefan-Marr.de

here - Stefan-Marr.de

here - Stefan-Marr.de

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.3. Common Problems for the Implementation of Concurrency Abstractions<br />

1 object semaphore {<br />

2 class SemaphoreActor () extends Actor {<br />

3 // ...<br />

4 <strong>de</strong>f enter () {<br />

5 if ( num < MAX ) {<br />

6 // critical section<br />

7 num = num + 1; } } }<br />

8<br />

9 <strong>de</strong>f main ( args : Array [ String ]) : Unit = {<br />

10 var gate = new SemaphoreActor ()<br />

11 gate . start<br />

12 gate ! enter // executes on gate ’s thread<br />

13 gate . enter // executes on the main thread<br />

14 } }<br />

Listing 3.1: Example of incomplete State Encapsulation: This semaphore has a race<br />

condition since Scala’s actors do not enforce encapsulation and the actor as well<br />

as the main thread have access to the num field. [Karmani et al., 2009, Fig. 2]<br />

semaphore. The SemaphoreActor has a counter num, which indicates the number<br />

of activities that entered the critical section.<br />

However, Scala’s actor implementation does not guarantee encapsulation.<br />

This makes the gate object simultaneously accessible in the main thread and<br />

the thread of gate, i. e., the SemaphoreActor. Both threads can execute enter<br />

at the same time, leading to a race condition on the num variable, un<strong>de</strong>rmining<br />

one of the main benefits of the actor mo<strong>de</strong>l. If encapsulation would be guaranteed,<br />

as it is for instance in Erlang, this example would work as inten<strong>de</strong>d,<br />

because only the owning actor could access num and the data race would not<br />

be possible.<br />

While actors require state encapsulation to yield full engineering benefits,<br />

implementing this guarantee comes either at the cost of a high implementation<br />

complexity or a significant performance impact. The main reason is<br />

that VMs such as the JVM do not provi<strong>de</strong> sufficient abstraction for the notion<br />

of ownership and state encapsulation. AmbientTalk [Van Cutsem et al.,<br />

2007], JCoBox [Schäfer and Poetzsch-Heffter, 2010], and NAct 18 enforce the<br />

discussed encapsulation by construction. The compiler ensures that only socalled<br />

far references can be obtained to objects such as the SemaphoreActor.<br />

These far references enforce state encapsulation to guarantee isolation.<br />

Kilim [Srinivasan and Mycroft, 2008] is an example for another approach to<br />

the problem. Kilim employs compile-time checking based on annotations to<br />

18 http://co<strong>de</strong>.google.com/p/n-act/<br />

73

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!