Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | List of all members
net_instaweb::ThreadSynchronizer Class Reference

#include "thread_synchronizer.h"

Public Member Functions

 ThreadSynchronizer (ThreadSystem *thread_system)
 
void EnableForPrefix (StringPiece prefix)
 
void Wait (const char *key)
 Waits for a thread to signal the specified key.
 
void TimedWait (const char *key, int64 timeout_ms)
 
void Signal (const char *key)
 
void AllowSloppyTermination (const char *key)
 

Detailed Description

Helps create deterministic multi-threaded tests targeting programmer-identified race conditions.

Note that the goal of this class is not to provoke, or test for potential race conditions. That is a noble goal, left for another day.

The goal of this class is to help programmers that suspect they have found a race condition reproduce it robustly in a way that can be checked in as a unit-test and run instantly.

This methodology does not rely on sleep(). It uses condition variables so it will run as fast as possible. The sync-points can be left in all but the most nano-second-optimized production code as they are no-ops (single inlined if-statement) when turned off.

This class is disabled by default, so that calls to it can be left in production code, but enabled for targeted tests.

Member Function Documentation

void net_instaweb::ThreadSynchronizer::AllowSloppyTermination ( const char *  key)

Signals that are delivered in a timing-dependent fashion may not be totally balanced at the end of a test. Such signals should be the exception rather than the rule, but they can be declared with this method. This method is intended to be called from tests only, thus it does not have an inline shunt.

void net_instaweb::ThreadSynchronizer::EnableForPrefix ( StringPiece  prefix)
inline

By default, the synchronizer is a no-op so we can inject sync-points in production code at sensitive points with minimal overhead. To enable in a test, call EnableForPrefix("Prefix"), which will enable any synchronization key beginning with "Prefix".

EnableForPrefix should be called prior to any threading.

void net_instaweb::ThreadSynchronizer::Signal ( const char *  key)
inline

Signals any thread waiting for a key that it can continue. Signals delivered in advance of a wait are remembered. It is an error to destruct the ThreadSynchronizer with pending signals, unless AllowSloppyTermination is called for the key.

void net_instaweb::ThreadSynchronizer::TimedWait ( const char *  key,
int64  timeout_ms 
)
inline

Waits for a thread to signal the specified key, or the specified timeout in milliseconds, whichever comes first. Note that even if the timer expires, the program should eventually Signal the key unless AllowSloppyTermination is called.


The documentation for this class was generated from the following file: