STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Concurrency::choice< _Type > Class Template Reference

A choice messaging block is a multi-source, single-target block that represents a control-flow interaction with a set of sources. The choice block will wait for any one of multiple sources to produce a message and will propagate the index of the source that produced the message. More...

#include <agents.h>

Inheritance diagram for Concurrency::choice< _Type >:
Concurrency::ISource< size_t >

Public Types

typedef _Type type
 A type alias for _Type . More...
 
- Public Types inherited from Concurrency::ISource< size_t >
typedef size_t source_type
 A type alias for _Type . More...
 

Public Member Functions

 choice (_Type _Tuple)
 Constructs a choice messaging block. More...
 
 choice (choice &&_Choice)
 Constructs a choice messaging block. More...
 
 ~choice ()
 Destroys the choice messaging block. More...
 
bool has_value () const
 Checks whether this choice messaging block has been initialized with a value yet. More...
 
size_t index ()
 Returns an index into the tuple representing the element selected by the choice messaging block. More...
 
template<typename _Payload_type >
_Payload_type const & value ()
 Gets the message whose index was selected by the choice messaging block. More...
 
virtual void link_target (_Inout_ ITarget< size_t > *_PTarget)
 Links a target block to this choice messaging block. More...
 
virtual void unlink_target (_Inout_ ITarget< size_t > *_PTarget)
 Unlinks a target block from this choice messaging block. More...
 
virtual void unlink_targets ()
 Unlinks all targets from this choice messaging block. More...
 
virtual message< size_t > * accept (runtime_object_identity _MsgId, _Inout_ ITarget< size_t > *_PTarget)
 Accepts a message that was offered by this choice block, transferring ownership to the caller. More...
 
virtual bool reserve (runtime_object_identity _MsgId, _Inout_ ITarget< size_t > *_PTarget)
 Reserves a message previously offered by this choice messaging block. More...
 
virtual message< size_t > * consume (runtime_object_identity _MsgId, _Inout_ ITarget< size_t > *_PTarget)
 Consumes a message previously offered by this choice messaging block and successfully reserved by the target, transferring ownership to the caller. More...
 
virtual void release (runtime_object_identity _MsgId, _Inout_ ITarget< size_t > *_PTarget)
 Releases a previous successful message reservation. More...
 
virtual void acquire_ref (_Inout_ ITarget< size_t > *_PTarget)
 Acquires a reference count on this choice messaging block, to prevent deletion. More...
 
virtual void release_ref (_Inout_ ITarget< size_t > *_PTarget)
 Releases a reference count on this choice messaging block. More...
 
- Public Member Functions inherited from Concurrency::ISource< size_t >
virtual ~ISource ()
 Destroys the ISource object. More...
 

Private Member Functions

template<int _Index>
void _Initialize_choices ()
 Constructs and initializes a _Reserving_node for each tuple messaging block passed in. More...
 
template<>
void _Initialize_choices ()
 Provides a sentinel template specialization for _Initialize_choices recursive template expansion. More...
 
template<int _Index>
void _Delete_choices ()
 Deletes all _Reserving_node elements that were created in _Initialize_choices. More...
 
template<>
void _Delete_choices ()
 Provides a sentinel template specialization for _Delete_choices recursive template expansion. More...
 
choice const & operator= (choice const &)
 
 choice (choice const &)
 

Private Attributes

void_M_pSourceChoices [std::tr1::tuple_size< _Type >::value]
 
single_assignment< size_t > * _M_pSingleAssignment
 
_Type _M_sourceTuple
 
Scheduler * _M_pScheduler
 
ScheduleGroup * _M_pScheduleGroup
 

Additional Inherited Members

- Protected Member Functions inherited from Concurrency::ISource< size_t >
void _Invoke_link_source (ITarget< size_t > *_PLinkFrom)
 Links this source to a target. More...
 
void _Invoke_unlink_source (ITarget< size_t > *_PUnlinkFrom)
 Unlinks this source from a target. More...
 

Detailed Description

template<class _Type>
class Concurrency::choice< _Type >

A choice messaging block is a multi-source, single-target block that represents a control-flow interaction with a set of sources. The choice block will wait for any one of multiple sources to produce a message and will propagate the index of the source that produced the message.

Template Parameters
_TypeA tuple-based type representing the payloads of the input sources.

The choice block ensures that only one of the incoming messages is consumed.

For more information, see Asynchronous Message Blocks.

See also
join Class, single_assignment Class, make_choice Function, tuple Class

Member Typedef Documentation

template<class _Type >
typedef _Type Concurrency::choice< _Type >::type

A type alias for _Type .

Constructor & Destructor Documentation

template<class _Type >
Concurrency::choice< _Type >::choice ( _Type  _Tuple)
inlineexplicit

Constructs a choice messaging block.

Parameters
_TupleA tuple of sources for the choice.

The runtime uses the default scheduler if you do not specify the _PScheduler or _PScheduleGroup parameters.

Move construction is not performed under a lock, which means that it is up to the user to make sure that there are no light-weight tasks in flight at the time of moving. Otherwise, numerous races can occur, leading to exceptions or inconsistent state.

See also
Scheduler Class, ScheduleGroup Class
9724  {
9725  _M_pSingleAssignment = new single_assignment<size_t>();
9726  _Initialize_choices<0>();
9727  }
ScheduleGroup * _M_pScheduleGroup
Definition: agents.h:10123
Scheduler * _M_pScheduler
Definition: agents.h:10120
_Type _M_sourceTuple
Definition: agents.h:10117
#define NULL
Definition: crtdbg.h:30
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
Concurrency::choice< _Type >::choice ( choice< _Type > &&  _Choice)
inline

Constructs a choice messaging block.

Parameters
_ChoiceA choice messaging block to copy from. Note that the original object is orphaned, making this a move constructor.

The runtime uses the default scheduler if you do not specify the _PScheduler or _PScheduleGroup parameters.

Move construction is not performed under a lock, which means that it is up to the user to make sure that there are no light-weight tasks in flight at the time of moving. Otherwise, numerous races can occur, leading to exceptions or inconsistent state.

See also
Scheduler Class, ScheduleGroup Class
9812  {
9813  // Copy scheduler group or scheduler to the new object.
9814  _M_pScheduleGroup = _Choice._M_pScheduleGroup;
9815  _M_pScheduler = _Choice._M_pScheduler;
9816 
9817  // Single assignment is heap allocated, so simply copy the pointer. If it already has
9818  // a value, it will be preserved.
9819  _M_pSingleAssignment = _Choice._M_pSingleAssignment;
9820  _Choice._M_pSingleAssignment = NULL;
9821 
9822  // Invoke copy assignment for tuple to copy pointers to message blocks.
9823  _M_sourceTuple = _Choice._M_sourceTuple;
9824 
9825  // Copy the pointers to order nodes to a new object and zero out in the old object.
9826  memcpy(_M_pSourceChoices, _Choice._M_pSourceChoices, sizeof(_M_pSourceChoices));
9827  memset(_Choice._M_pSourceChoices, 0, sizeof(_M_pSourceChoices));
9828  }
ScheduleGroup * _M_pScheduleGroup
Definition: agents.h:10123
Scheduler * _M_pScheduler
Definition: agents.h:10120
_Type _M_sourceTuple
Definition: agents.h:10117
#define NULL
Definition: crtdbg.h:30
void * _M_pSourceChoices[std::tr1::tuple_size< _Type >::value]
Definition: agents.h:10111
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
Concurrency::choice< _Type >::~choice ( )
inline

Destroys the choice messaging block.

9835  {
9836  delete _M_pSingleAssignment;
9837  _Delete_choices<0>();
9838  }
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
Concurrency::choice< _Type >::choice ( choice< _Type > const &  )
private

Member Function Documentation

template<class _Type >
template<int _Index>
void Concurrency::choice< _Type >::_Delete_choices ( )
inlineprivate

Deletes all _Reserving_node elements that were created in _Initialize_choices.

The highest-number index of the choice's sources

10095  {
10096  delete reinterpret_cast<_Reserving_node<std::tr1::remove_pointer<std::tr1::tuple_element<_Index, _Type>::type>::type::source_type> *>(_M_pSourceChoices[_Index]);
10097  _M_pSourceChoices[_Index] = NULL;
10098  _Delete_choices<_Index + 1>();
10099  }
#define NULL
Definition: crtdbg.h:30
void * _M_pSourceChoices[std::tr1::tuple_size< _Type >::value]
Definition: agents.h:10111
template<class _Type >
template<>
void Concurrency::choice< _Type >::_Delete_choices ( )
inlineprivate

Provides a sentinel template specialization for _Delete_choices recursive template expansion.

10107  {
10108  }
template<class _Type >
template<int _Index>
void Concurrency::choice< _Type >::_Initialize_choices ( )
inlineprivate

Constructs and initializes a _Reserving_node for each tuple messaging block passed in.

The highest-number index of the choice's sources

10057  {
10058  std::tr1::tuple_element<_Index, _Type>::type _Item = std::tr1::get<_Index>(_M_sourceTuple);
10059  _Reserving_node<std::tr1::remove_pointer<std::tr1::tuple_element<_Index, _Type>::type>::type::source_type> * _Order_node_element = NULL;
10060 
10061  if (_M_pScheduleGroup != NULL)
10062  {
10063  _Order_node_element = new _Reserving_node<std::tr1::remove_pointer<std::tr1::tuple_element<_Index, _Type>::type>::type::source_type> (*_M_pScheduleGroup, _Item, _Index);
10064  }
10065  else if (_M_pScheduler != NULL)
10066  {
10067  _Order_node_element = new _Reserving_node<std::tr1::remove_pointer<std::tr1::tuple_element<_Index, _Type>::type>::type::source_type> (*_M_pScheduler, _Item, _Index);
10068  }
10069  else
10070  {
10071  _Order_node_element = new _Reserving_node<std::tr1::remove_pointer<std::tr1::tuple_element<_Index, _Type>::type>::type::source_type> (_Item, _Index);
10072  }
10073 
10074  _M_pSourceChoices[_Index] = _Order_node_element;
10075  _Order_node_element->link_target(_M_pSingleAssignment);
10076  _Initialize_choices<_Index + 1>();
10077  }
ScheduleGroup * _M_pScheduleGroup
Definition: agents.h:10123
Scheduler * _M_pScheduler
Definition: agents.h:10120
_Type _M_sourceTuple
Definition: agents.h:10117
#define NULL
Definition: crtdbg.h:30
void * _M_pSourceChoices[std::tr1::tuple_size< _Type >::value]
Definition: agents.h:10111
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
template<>
void Concurrency::choice< _Type >::_Initialize_choices ( )
inlineprivate

Provides a sentinel template specialization for _Initialize_choices recursive template expansion.

10085  {
10086  }
template<class _Type >
virtual message<size_t>* Concurrency::choice< _Type >::accept ( runtime_object_identity  _MsgId,
_Inout_ ITarget< size_t > *  _PTarget 
)
inlinevirtual

Accepts a message that was offered by this choice block, transferring ownership to the caller.

Parameters
_MsgIdThe runtime_object_identity of the offered message object.
_PTargetA pointer to the target block that is calling the accept method.
Returns
A pointer to the message that the caller now has ownership of.

Implements Concurrency::ISource< size_t >.

9950  {
9951  return _M_pSingleAssignment->accept(_MsgId, _PTarget);
9952  }
virtual message< _Target_type > * accept(runtime_object_identity _MsgId, _Inout_ ITarget< _Target_type > *_PTarget)
Accepts a message that was offered by this source_block object, transferring ownership to the caller...
Definition: agents.h:3365
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
virtual void Concurrency::choice< _Type >::acquire_ref ( _Inout_ ITarget< size_t > *  _PTarget)
inlinevirtual

Acquires a reference count on this choice messaging block, to prevent deletion.

Parameters
_PTargetA pointer to the target block that is calling this method.

This method is called by an ITarget object that is being linked to this source during the link_target method.

Implements Concurrency::ISource< size_t >.

10028  {
10029  _M_pSingleAssignment->acquire_ref(_PTarget);
10030  }
virtual void acquire_ref(_Inout_ ITarget< _Target_type > *)
Acquires a reference count on this source_block object, to prevent deletion.
Definition: agents.h:3541
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
virtual message<size_t>* Concurrency::choice< _Type >::consume ( runtime_object_identity  _MsgId,
_Inout_ ITarget< size_t > *  _PTarget 
)
inlinevirtual

Consumes a message previously offered by this choice messaging block and successfully reserved by the target, transferring ownership to the caller.

Parameters
_MsgIdThe runtime_object_identity of the reserved message object.
_PTargetA pointer to the target block that is calling the consume method.
Returns
A pointer to the message object that the caller now has ownership of.

The consume method is similar to accept, but must always be preceded by a call to reserve that returned true.

Implements Concurrency::ISource< size_t >.

9997  {
9998  return _M_pSingleAssignment->consume(_MsgId, _PTarget);
9999  }
virtual message< _Target_type > * consume(runtime_object_identity _MsgId, _Inout_ ITarget< _Target_type > *_PTarget)
Consumes a message previously offered by this source_block object and successfully reserved by the ta...
Definition: agents.h:3457
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
bool Concurrency::choice< _Type >::has_value ( ) const
inline

Checks whether this choice messaging block has been initialized with a value yet.

Returns
true if the block has received a value, false otherwise.
9854  {
9855  return _M_pSingleAssignment->has_value();
9856  }
bool has_value() const
Checks whether this single_assignment messaging block has been initialized with a value yet...
Definition: agents.h:7069
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
size_t Concurrency::choice< _Type >::index ( )
inline

Returns an index into the tuple representing the element selected by the choice messaging block.

Returns
The message index.

The message payload can be extracted using the get method.

9870  {
9871  return _M_pSingleAssignment->value();
9872  }
_Type const & value()
Gets a reference to the current payload of the message being stored in the single_assignment messagin...
Definition: agents.h:7085
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
virtual void Concurrency::choice< _Type >::link_target ( _Inout_ ITarget< size_t > *  _PTarget)
inlinevirtual

Links a target block to this choice messaging block.

Parameters
_PTargetA pointer to an ITarget block to link to this choice messaging block.

Implements Concurrency::ISource< size_t >.

9907  {
9908  _M_pSingleAssignment->link_target(_PTarget);
9909  }
virtual void link_target(_Inout_ ITarget< _Target_type > *_PTarget)
Links a target block to this source_block object.
Definition: agents.h:3281
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
choice const& Concurrency::choice< _Type >::operator= ( choice< _Type > const &  )
private
template<class _Type >
virtual void Concurrency::choice< _Type >::release ( runtime_object_identity  _MsgId,
_Inout_ ITarget< size_t > *  _PTarget 
)
inlinevirtual

Releases a previous successful message reservation.

Parameters
_MsgIdThe runtime_object_identity of the message object being released.
_PTargetA pointer to the target block that is calling the release method.

Implements Concurrency::ISource< size_t >.

10012  {
10013  _M_pSingleAssignment->release(_MsgId, _PTarget);
10014  }
virtual void release(runtime_object_identity _MsgId, _Inout_ ITarget< _Target_type > *_PTarget)
Releases a previous successful message reservation.
Definition: agents.h:3506
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
virtual void Concurrency::choice< _Type >::release_ref ( _Inout_ ITarget< size_t > *  _PTarget)
inlinevirtual

Releases a reference count on this choice messaging block.

Parameters
_PTargetA pointer to the target block that is calling this method.

This method is called by an ITarget object that is being unlinked from this source. The source block is allowed to release any resources reserved for the target block.

Implements Concurrency::ISource< size_t >.

10044  {
10045  _M_pSingleAssignment->release_ref(_PTarget);
10046  }
virtual void release_ref(_Inout_ ITarget< _Target_type > *_PTarget)
Releases a reference count on this source_block object.
Definition: agents.h:3557
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
virtual bool Concurrency::choice< _Type >::reserve ( runtime_object_identity  _MsgId,
_Inout_ ITarget< size_t > *  _PTarget 
)
inlinevirtual

Reserves a message previously offered by this choice messaging block.

Parameters
_MsgIdThe runtime_object_identity of the message object being reserved.
_PTargetA pointer to the target block that is calling the reserve method.
Returns
true if the message was successfully reserved, false otherwise. Reservations can fail for many reasons, including: the message was already reserved or accepted by another target, the source could deny reservations, and so forth.

After you call reserve, if it succeeds, you must call either consume or release in order to take or give up possession of the message, respectively.

Implements Concurrency::ISource< size_t >.

9974  {
9975  return _M_pSingleAssignment->reserve(_MsgId, _PTarget);
9976  }
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
virtual bool reserve(runtime_object_identity _MsgId, _Inout_ ITarget< _Target_type > *_PTarget)
Reserves a message previously offered by this source_block object.
Definition: agents.h:3401
template<class _Type >
virtual void Concurrency::choice< _Type >::unlink_target ( _Inout_ ITarget< size_t > *  _PTarget)
inlinevirtual

Unlinks a target block from this choice messaging block.

Parameters
_PTargetA pointer to an ITarget block to unlink from this choice messaging block.

Implements Concurrency::ISource< size_t >.

9919  {
9921  }
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
virtual void unlink_target(_Inout_ ITarget< _Target_type > *_PTarget)
Unlinks a target block from this source_block object.
Definition: agents.h:3306
template<class _Type >
virtual void Concurrency::choice< _Type >::unlink_targets ( )
inlinevirtual

Unlinks all targets from this choice messaging block.

This method does not need to be called from the destructor because destructor for the internal single_assignment block will unlink properly.

Implements Concurrency::ISource< size_t >.

9932  {
9934  }
virtual void unlink_targets()
Unlinks all target blocks from this source_block object.
Definition: agents.h:3327
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114
template<class _Type >
template<typename _Payload_type >
_Payload_type const& Concurrency::choice< _Type >::value ( )
inline

Gets the message whose index was selected by the choice messaging block.

Template Parameters
_Payload_typeThe type of the message payload.
Returns
The payload of the message.

Because a choice messaging block can take inputs with different payload types, you must specify the type of the payload at the point of retrieval. You can determine the type based on the result of the index method.

9891  {
9892  return reinterpret_cast<_Reserving_node<_Payload_type> *>(_M_pSourceChoices[_M_pSingleAssignment->value()])->value();
9893  }
_Payload_type const & value()
Gets the message whose index was selected by the choice messaging block.
Definition: agents.h:9890
void * _M_pSourceChoices[std::tr1::tuple_size< _Type >::value]
Definition: agents.h:10111
_Type const & value()
Gets a reference to the current payload of the message being stored in the single_assignment messagin...
Definition: agents.h:7085
single_assignment< size_t > * _M_pSingleAssignment
Definition: agents.h:10114

Member Data Documentation

template<class _Type >
ScheduleGroup* Concurrency::choice< _Type >::_M_pScheduleGroup
private
template<class _Type >
Scheduler* Concurrency::choice< _Type >::_M_pScheduler
private
template<class _Type >
single_assignment<size_t>* Concurrency::choice< _Type >::_M_pSingleAssignment
private
template<class _Type >
void* Concurrency::choice< _Type >::_M_pSourceChoices[std::tr1::tuple_size< _Type >::value]
private
template<class _Type >
_Type Concurrency::choice< _Type >::_M_sourceTuple
private

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