[asterisk-dev] [Code Review] Scoped locks

Mark Michelson reviewboard at asterisk.org
Mon Jul 23 16:52:14 CDT 2012


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviewboard.asterisk.org/r/2060/
-----------------------------------------------------------

Review request for Asterisk Developers.


Summary
-------

This is inspired by the RAII_VAR addition in Asterisk 11.

The idea is that using the cleanup attribute means that we can automatically unlock locks when the go out of scope. I have created a SCOPED_LOCK macro for general purpose RAII locking and unlocking. There are also specializations for common locking operations (SCOPED_MUTEX, SCOPED_RDLOCK, SCOPED_WRLOCK, SCOPED_AO2LOCK).

I have created two unit tests. The first simply demonstrates that the SCOPED_LOCK macro actually works as expected (i.e. locks when declared, unlocks when the lock goes out of scope). The second tests that cleanup attributes run in an expected order. This proves that SCOPED_LOCKs and ao2_objects can work in harmony if applied carefully.

As a final measure, I have changed the mutexes in config.c to use SCOPED_MUTEXes as a proof of concept. You can see in a few spots that it is possible to remove some calls to ast_mutex_unlock() since the unlock occurs automatically when the lock goes out of scope.

The actual SCOPED_LOCK macro and its friends are hideously underdocumented right now mainly because I have no idea if this is the sort of change that will be welcomed with open arms or not. Let me know if this idea is good and I'll provide some doxygen for the SCOPED_LOCK macro.


Diffs
-----

  /trunk/include/asterisk/lock.h 370381 
  /trunk/main/config.c 370381 
  /trunk/tests/test_scoped_lock.c PRE-CREATION 

Diff: https://reviewboard.asterisk.org/r/2060/diff


Testing
-------

The unit tests, yo!


Thanks,

Mark

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-dev/attachments/20120723/eabd8ea9/attachment.htm>


More information about the asterisk-dev mailing list