Mutex – working and usage in Android Frameworks Domain explained

I came across this at work today and was taken aback by the simplicity and beauty and yet very powerful and quite fool proof implementation of mutex in c++ used in Android Frameworks. There isn’t many articles on the internet about this. This could help you save some time and learn the obvious, quickly.

First of all, you can find the code at frameworks/native/include/utils/Mutex.h.

Second, coming to the implementation, the file Mutex.h has a class Mutex which does the basic implementation of mutex creation, locking and unlocking and an additional tryLock of the mutex. These apis are exposed through another inner class AutoLock which has a private refrence mLock for Mutex class. The constructor of this class does the locking on the mutex object that was passed to it by passing the object to its private refrence mLock and calling the mLock.lock() whose code is implemented in Mutex class.

inline Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); }
inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); }

The destructor of AutoLock does the exact opposite and unlocks the mutex when the AutoLock object is being destroyed. Hence the lifetime of the lock is limited to the lifetime of the AutoLock object.

inline ~Autolock() { mLock.unlock(); }

This high level explanation should suffice for most cases. But to know more on how exactly does the Mutex class implements the locking and unlocking, one can dig a lil’ further. A few more points to keep in mind [these make the code more beautiful] are:

  1. The mutex must be unlocked by the thread that locked it. They are not recursive, i.e. the same thread can’t lock it multiple times.
  2. A mutex cannot be copied. So lock and unlock operation happen upon the same object. While mutex is in locked state, no other copies of the mutex object can be used to re-lock.

Lastly, the usage of Mutex in the Android Frameworks. This probably is the reason that you are here. Since the locking and unlocking of the mutex is aligned with the lifecycle of the AutoLock object, we enclose the part of code that is depending on lock with curly braces and create the AutoLock object inside this block of code. Once the control exits this block, it destroys AutoLock object and inherently releases the lock. Simple and yet Beautiful isn’t it ???

Note: The Mutex::AutoLock is typedef’d to AutoMutex at the end of file. This is something that might slip through.

Mutex mLock;

{ // acquire lock
AutoMutex _l(mLock); // equivalent to Mutex::AutoLock _l(mLock);
...
...

} // release lock

About these ads

One thought on “Mutex – working and usage in Android Frameworks Domain explained

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s