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:

> Maybe<ExpensiveRAIIResource> 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

Reply via email to