Fix potential thread starvation issue in InstanceBits
commitb1a3162e7399205af9cc9b897f343145fffe0846
authorRick Lavoie <rlavoie@fb.com>
Fri, 7 Oct 2016 16:10:59 +0000 (7 09:10 -0700)
committerHhvm Bot <hhvm-bot-bot@fb.com>
Fri, 7 Oct 2016 16:17:54 +0000 (7 09:17 -0700)
tree767577d39f2230e3c22a7af93b81b96fd1df671c
parentac3a47548d3f742a10c3a2a396fd96de9dec59b8
Fix potential thread starvation issue in InstanceBits

Summary:
InstanceBits has a potential thread starvation issue when there's a
steady source of threads calling InstanceBits::profile(). InstanceBits uses a
ReadWriteLock to synchronize access between multiple threads adding profiling
data and a thread trying to finalize the gathered data. However, ReadWriteLock
is built on top of pthread_rwlock, and pthread_rwlock can on some
implementations (including Linux) favor readers over writers. As a result, a
thread in InstanceBits::init() can wait indefinitely trying to acquire the lock
in write mode if there's a steady enough stream of readers in
InstanceBits::profile().

Fix this by adding a second atomic flag. This flag indicates to
InstanceBits::profile() if it should return immediately or not (IE, not
profile). InstanceBits::init() sets this flag before attempting to acquire the
lock in write mode. Therefore, when a thread attempts to acquire the lock, its
known that the stream of calls to InstanceBits::profile() should stop in bounded
time.

Reviewed By: swtaarrs

Differential Revision: D3986383

fbshipit-source-id: 9b1c9a9edc7081ddd71b0b700d7ea1ebd715c089
hphp/runtime/vm/instance-bits.cpp