Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
threading.Barrier Class Reference
Inheritance diagram for threading.Barrier:
Inheritance graph
[legend]
Collaboration diagram for threading.Barrier:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def wait
def reset
def abort
def parties
def n_waiting
def broken

Public Attributes

_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Private Member Functions

def _enter
def _release
def _wait
def _exit
def _break

Private Attributes

 _cond
 _action
 _timeout
 _parties
 _state
 _count

Detailed Description

Barrier.  Useful for synchronizing a fixed number of threads
at known synchronization points.  Threads block on 'wait()' and are
simultaneously once they have all made that call.

Definition at line 439 of file threading.py.


Constructor & Destructor Documentation

def threading.Barrier.__init__ (   self,
  parties,
  action = None,
  timeout = None,
  verbose = None 
)
Create a barrier, initialised to 'parties' threads.
'action' is a callable which, when supplied, will be called
by one of the threads after they have all entered the
barrier and just prior to releasing them all.
If a 'timeout' is provided, it is uses as the default for
all subsequent 'wait()' calls.

Definition at line 445 of file threading.py.

00445 
00446     def __init__(self, parties, action=None, timeout=None, verbose=None):
00447         """
00448         Create a barrier, initialised to 'parties' threads.
00449         'action' is a callable which, when supplied, will be called
00450         by one of the threads after they have all entered the
00451         barrier and just prior to releasing them all.
00452         If a 'timeout' is provided, it is uses as the default for
00453         all subsequent 'wait()' calls.
00454         """
00455         _Verbose.__init__(self, verbose)
00456         self._cond = Condition(Lock())
00457         self._action = action
00458         self._timeout = timeout
00459         self._parties = parties
00460         self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
00461         self._count = 0

Here is the caller graph for this function:


Member Function Documentation

def threading.Barrier._break (   self) [private]

Definition at line 563 of file threading.py.

00563 
00564     def _break(self):
00565         # An internal error was detected.  The barrier is set to
00566         # a broken state all parties awakened.
00567         self._state = -2
00568         self._cond.notify_all()

Here is the caller graph for this function:

def threading.Barrier._enter (   self) [private]

Definition at line 491 of file threading.py.

00491 
00492     def _enter(self):
00493         while self._state in (-1, 1):
00494             # It is draining or resetting, wait until done
00495             self._cond.wait()
00496         #see if the barrier is in a broken state
00497         if self._state < 0:
00498             raise BrokenBarrierError
00499         assert self._state == 0

Here is the caller graph for this function:

def threading.Barrier._exit (   self) [private]

Definition at line 527 of file threading.py.

00527 
00528     def _exit(self):
00529         if self._count == 0:
00530             if self._state in (-1, 1):
00531                 #resetting or draining
00532                 self._state = 0
00533                 self._cond.notify_all()

Here is the caller graph for this function:

def threading.Barrier._release (   self) [private]

Definition at line 502 of file threading.py.

00502 
00503     def _release(self):
00504         try:
00505             if self._action:
00506                 self._action()
00507             # enter draining state
00508             self._state = 1
00509             self._cond.notify_all()
00510         except:
00511             #an exception during the _action handler.  Break and reraise
00512             self._break()
00513             raise

Here is the call graph for this function:

Here is the caller graph for this function:

def threading.Barrier._wait (   self,
  timeout 
) [private]

Definition at line 516 of file threading.py.

00516 
00517     def _wait(self, timeout):
00518         if not self._cond.wait_for(lambda : self._state != 0, timeout):
00519             #timed out.  Break the barrier
00520             self._break()
00521             raise BrokenBarrierError
00522         if self._state < 0:
00523             raise BrokenBarrierError
00524         assert self._state == 1

Here is the call graph for this function:

Here is the caller graph for this function:

def threading.Barrier.abort (   self)
Place the barrier into a 'broken' state.
Useful in case of error.  Any currently waiting threads and
threads attempting to 'wait()' will have BrokenBarrierError
raised.

Definition at line 553 of file threading.py.

00553 
00554     def abort(self):
00555         """
00556         Place the barrier into a 'broken' state.
00557         Useful in case of error.  Any currently waiting threads and
00558         threads attempting to 'wait()' will have BrokenBarrierError
00559         raised.
00560         """
00561         with self._cond:
00562             self._break()

Here is the call graph for this function:

def threading.Barrier.broken (   self)
Return True if the barrier is in a broken state

Definition at line 588 of file threading.py.

00588 
00589     def broken(self):
00590         """
00591         Return True if the barrier is in a broken state
00592         """
00593         return self._state == -2
00594 
#exception raised by the Barrier class
Return the number of threads that are currently waiting at the barrier.

Definition at line 577 of file threading.py.

00577 
00578     def n_waiting(self):
00579         """
00580         Return the number of threads that are currently waiting at the barrier.
00581         """
00582         # We don't need synchronization here since this is an ephemeral result
00583         # anyway.  It returns the correct value in the steady state.
00584         if self._state == 0:
00585             return self._count
00586         return 0

def threading.Barrier.parties (   self)
Return the number of threads required to trip the barrier.

Definition at line 570 of file threading.py.

00570 
00571     def parties(self):
00572         """
00573         Return the number of threads required to trip the barrier.
00574         """
00575         return self._parties

def threading.Barrier.reset (   self)
Reset the barrier to the initial state.
Any threads currently waiting will get the BrokenBarrier exception
raised.

Definition at line 534 of file threading.py.

00534 
00535     def reset(self):
00536         """
00537         Reset the barrier to the initial state.
00538         Any threads currently waiting will get the BrokenBarrier exception
00539         raised.
00540         """
00541         with self._cond:
00542             if self._count > 0:
00543                 if self._state == 0:
00544                     #reset the barrier, waking up threads
00545                     self._state = -1
00546                 elif self._state == -2:
00547                     #was broken, set it to reset state
00548                     #which clears when the last thread exits
00549                     self._state = -1
00550             else:
00551                 self._state = 0
00552             self._cond.notify_all()

Here is the caller graph for this function:

def threading.Barrier.wait (   self,
  timeout = None 
)
Wait for the barrier.  When the specified number of threads have
started waiting, they are all simultaneously awoken. If an 'action'
was provided for the barrier, one of the threads will have executed
that callback prior to returning.
Returns an individual index number from 0 to 'parties-1'.

Definition at line 462 of file threading.py.

00462 
00463     def wait(self, timeout=None):
00464         """
00465         Wait for the barrier.  When the specified number of threads have
00466         started waiting, they are all simultaneously awoken. If an 'action'
00467         was provided for the barrier, one of the threads will have executed
00468         that callback prior to returning.
00469         Returns an individual index number from 0 to 'parties-1'.
00470         """
00471         if timeout is None:
00472             timeout = self._timeout
00473         with self._cond:
00474             self._enter() # Block while the barrier drains.
00475             index = self._count
00476             self._count += 1
00477             try:
00478                 if index + 1 == self._parties:
00479                     # We release the barrier
00480                     self._release()
00481                 else:
00482                     # We wait until someone releases us
00483                     self._wait(timeout)
00484                 return index
00485             finally:
00486                 self._count -= 1
00487                 # Wake up any threads waiting for barrier to drain.
00488                 self._exit()

Here is the call graph for this function:


Member Data Documentation

Definition at line 456 of file threading.py.

Definition at line 455 of file threading.py.

Definition at line 460 of file threading.py.

Definition at line 458 of file threading.py.

Definition at line 459 of file threading.py.

Definition at line 457 of file threading.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.


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