This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Proposal for helping out with concurrency

4 replies
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
People of the Scala language,

To prevent from unknowingly closing over non-threadsafe mutable state, I propose the following:

Adding @noAsync and @async, so you can tag that a closure/function parameter is @async, and then the compiler will barf if it closes over something annotated with @noAsync.
You may also declare a reference type as @async, which makes it eligible for closing over even if the parent is marked as @noAsync

@noAsync class Actor {
  val self = ActorRef @async = ... //Means that it's OK to close over self
  val foo = 2 //Since owning class is @noAsync, this is not legal to close over when closure is @async
}

class MyActor extends Actor {
  def receive = {
    case "foo" => Future { foo } //Illegal, since Future[T](thunk: T @async) requires what is closed over to be @async, or not have any parent that is @noAsync
  }
}

This would also weed out accidental issues with ParallelCollections.

Ideas? Thoughts? Rants?

Cheers,


--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
Patrik Andersson
Joined: 2009-11-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Proposal for helping out with concurrency
Wouldn't it be even better to come up with someting generic such as a meta annotation that indicates whether annotation type A may be enclosed within type B or not. That way you could specify that @noAsync must never be called from something that is annotated with @async.

On Mon, Dec 5, 2011 at 2:34 PM, √iktor Ҡlang <viktor.klang@gmail.com> wrote:
People of the Scala language,

To prevent from unknowingly closing over non-threadsafe mutable state, I propose the following:

Adding @noAsync and @async, so you can tag that a closure/function parameter is @async, and then the compiler will barf if it closes over something annotated with @noAsync.
You may also declare a reference type as @async, which makes it eligible for closing over even if the parent is marked as @noAsync

@noAsync class Actor {
  val self = ActorRef @async = ... //Means that it's OK to close over self
  val foo = 2 //Since owning class is @noAsync, this is not legal to close over when closure is @async
}

class MyActor extends Actor {
  def receive = {
    case "foo" => Future { foo } //Illegal, since Future[T](thunk: T @async) requires what is closed over to be @async, or not have any parent that is @noAsync
  }
}

This would also weed out accidental issues with ParallelCollections.

Ideas? Thoughts? Rants?

Cheers,


--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Proposal for helping out with concurrency

2011/12/5 √iktor Ҡlang :
> Ideas? Thoughts? Rants?

As described, it should be extremely straightforward to implement as a
compiler plugin. Give it a try.

I'd be interested to see how feasible it would be to fully and
consistently annotate a large codebase such as Akka like this. I
strongly suspect that you'd very rapidly find yourself wanting "Async
polymorphism" (ie. the ability to specific @async/noAsync
type-parametrically), but either way I'd love to see how it worked
out.

Cheers,

Miles

Patrik Andersson
Joined: 2009-11-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Proposal for helping out with concurrency
(premature click)
That way something like @pure/ @unpure could also be introduced.

On Mon, Dec 5, 2011 at 2:42 PM, Patrik Andersson <pandersson@gmail.com> wrote:
Wouldn't it be even better to come up with someting generic such as a meta annotation that indicates whether annotation type A may be enclosed within type B or not. That way you could specify that @noAsync must never be called from something that is annotated with @async.

On Mon, Dec 5, 2011 at 2:34 PM, √iktor Ҡlang <viktor.klang@gmail.com> wrote:
People of the Scala language,

To prevent from unknowingly closing over non-threadsafe mutable state, I propose the following:

Adding @noAsync and @async, so you can tag that a closure/function parameter is @async, and then the compiler will barf if it closes over something annotated with @noAsync.
You may also declare a reference type as @async, which makes it eligible for closing over even if the parent is marked as @noAsync

@noAsync class Actor {
  val self = ActorRef @async = ... //Means that it's OK to close over self
  val foo = 2 //Since owning class is @noAsync, this is not legal to close over when closure is @async
}

class MyActor extends Actor {
  def receive = {
    case "foo" => Future { foo } //Illegal, since Future[T](thunk: T @async) requires what is closed over to be @async, or not have any parent that is @noAsync
  }
}

This would also weed out accidental issues with ParallelCollections.

Ideas? Thoughts? Rants?

Cheers,


--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang


Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Proposal for helping out with concurrency
2011/12/5 √iktor Ҡlang <viktor.klang@gmail.com>
People of the Scala language,

To prevent from unknowingly closing over non-threadsafe mutable state ...

I've often wished for a "mutable state escapes into closure" warning that I could leave on by default and suppress on a per-method or per-file level.
Either way, doesn't this sound like a job for the* effects system?
-0xe1a
* for some values of "the", https://github.com/lrytz/effects

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland