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