PSA: You can now use Maybe with the Auto helpers for Mutex and Monitor

2014-11-10 Thread Seth Fowler
In bug 1091921, we got support for using Maybe with the Auto helpers for Mutex 
and Monitor - things like MutexAutoLock and MonitorAutoEnter. This supports a 
pattern for optionally acquiring a RAII resource that I first saw used in the 
JavaScript engine, and which I’ve found very useful since. For anyone who 
hasn’t seen it, the basic pattern looks like this:

 MaybeExpensiveRAIIResource resource;
 if (resourceIsNeeded) {
   resource.emplace();
 }

This constructs an ExpensiveRAIIResource on the stack only if 
|resourceIsNeeded| is true.

So bug 1091921 lets us use this pattern with Mutexes and Monitors. I’m sure 
people will find lots of situations where this is useful, and indeed it’s 
already being used in some places.

Any time you have parallelism, though, you need to exercise caution. I 
encourage anyone who wants to use this to start by adding assertions to their 
code like Mutex’s |AssertCurrentThreadOwns| or Monitor’s 
|AssertCurrentThreadIn| anywhere they have methods that expect another method 
to do their synchronization for them. That’s good practice in any case, and 
will help ensure that you don’t make a mistake when using Maybe in this way.

Enjoy!

- Seth
___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform


Re: PSA: You can now use Maybe with the Auto helpers for Mutex and Monitor

2014-11-10 Thread Kyle Huey
On Mon, Nov 10, 2014 at 1:59 PM, Seth Fowler s...@mozilla.com wrote:
 In bug 1091921, we got support for using Maybe with the Auto helpers for 
 Mutex and Monitor - things like MutexAutoLock and MonitorAutoEnter. This 
 supports a pattern for optionally acquiring a RAII resource that I first saw 
 used in the JavaScript engine, and which I’ve found very useful since. For 
 anyone who hasn’t seen it, the basic pattern looks like this:

 MaybeExpensiveRAIIResource resource;
 if (resourceIsNeeded) {
   resource.emplace();
 }

 This constructs an ExpensiveRAIIResource on the stack only if 
 |resourceIsNeeded| is true.

 So bug 1091921 lets us use this pattern with Mutexes and Monitors. I’m sure 
 people will find lots of situations where this is useful, and indeed it’s 
 already being used in some places.

 Any time you have parallelism, though, you need to exercise caution. I 
 encourage anyone who wants to use this to start by adding assertions to their 
 code like Mutex’s |AssertCurrentThreadOwns| or Monitor’s 
 |AssertCurrentThreadIn| anywhere they have methods that expect another method 
 to do their synchronization for them. That’s good practice in any case, and 
 will help ensure that you don’t make a mistake when using Maybe in this way.

 Enjoy!

 - Seth
 ___
 dev-platform mailing list
 dev-platform@lists.mozilla.org
 https://lists.mozilla.org/listinfo/dev-platform

Since we're on the subject, I'll point out that
AssertCurrentThreadOwns is debug only (looking at you b2g, with your
almost complete lack of debug testing) and AssertNotCurrentThreadOwns
is unimplemented and for documentation purposes only.

- Kyle
___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform