Skip to content

life_assurance

Dmitry Koplyarov edited this page May 17, 2016 · 7 revisions

Back to reference

A policy that defines how the signal/listenable handlers life control is being performed. This topic is only covered at the Rationale page, exactly here and here.

namespace life_assurance
{
	struct intrusive_life_tokens;
	struct single_threaded;
	struct none;
}

Table of contents

Supplied policies

intrusive_life_tokens

This policy uses the same internals as life_tokens for signal handler validity control, but is more efficient in terms of memory and CPU usage.
It is guaranteed that the execution flow is out of the handler after the token destructor. The token destructor blocks if the handler is being executed at the moment of disconnecting.

single_threaded

This policy is good for the case where you access a signal from a single task_executor, but still use async handlers (processed in the same task_executor!).
It is guaranteed that the async handler will not start executing after the token destructor. However, the token destructor does not block if the handler is being executed in another thread at the moment of disconnecting.

none

A no-overhead policy, good for single-threaded signals.

How to implement your own policy

struct my_life_assurance
{
	using tag = wigwag::life_assurance::tag<wigwag::api_version<2, 0>>;

	class shared_data
	{ };

	struct life_assurance
	{
		void release_life_assurance(const shared_data& sd) { /*...*/ }
		bool node_should_be_released() const { /*...*/ }
		bool release_node() const { /*...*/ }
	};

	struct life_checker
	{
		life_checker(const shared_data& sd, const life_assurance& la) { /*...*/ }
	};

	struct execution_guard
	{
		execution_guard(const life_checker& lc) { /*...*/ }
		execution_guard(const shared_data& sd, const life_assurance& la) { /*...*/ }
		bool is_alive() const { /*...*/ }
	};
};

Required nested types

tag

using tag = wigwag::life_assurance::tag<wigwag::api_version<2, 0>>;

The policy tag. Must be a type alias for wigwag::life_assurance::tag<wigwag::api_version<2, 0>>.

shared_data

class shared_data
{ /*...*/ };

A shared_data object is stored inside a signal, and may hold any objects that are shared between different signal handlers.

life_assurance

struct life_assurance
{
	void release_life_assurance(const shared_data& sd) { /*...*/ }
	bool node_should_be_released() const { /*...*/ }
	bool release_node() const { /*...*/ }
};

A life_assurance object is stored inside a signal connection token, and is used to control the validity of the handler.

life_checker

struct life_checker
{
	life_checker(const shared_data& sd, const life_assurance& la) { /*...*/ }
};

A life_checker object is held inside the async handler internals, and references a life_assurance object that will be locked using the execution_guard.

execution_guard

struct execution_guard
{
	execution_guard(const life_checker& lc) { /*...*/ }
	execution_guard(const shared_data& sd, const life_assurance& la) { /*...*/ }
	bool is_alive() const { /*...*/ }
};

An execution_guard object is created locally for the handler execution time, and is used for checking if the handler is still valid and blocking the signal connection token destructor.

Required life_assurance nested class constructors

default constructor

life_assurance();

A default constructor is used to create a new life_assurance object in the signal::connect() method.

Required life_assurance nested class methods

release_life_assurance

void release_life_assurance();

This method is invoked when disconnecting from a signal.
Any subsequent execution_guard::is_alive() calls should return false, and the method should block if there are any associated execution_guards objects until these objects are destroyed.

node_should_be_released

bool node_should_be_released() const;

This method checks if the signal has the last and only reference to a signal handler node and should release it.

Return value: true if a signal should invoke release_node from the signal::operator (), false otherwise

release_node

bool release_node() const;

This method checks if there are no more references to a signal handler node, and it may be deleted.

Return value: true if the a signal handler node should be deleted, false otherwise

Required life_checker nested class constructors and destructors

shared_data and life_assurance-parametrized constructor

life_checker(const shared_data& sd, const life_assurance& la);

Constructs a life_checker object.

Arguments:

  • sd - a reference to a const shared_data object
  • la - a reference to a const life_assurance object

Required execution_guard nested class constructors and destructors

life_checker-parametrized constructor

execution_guard(const life_checker& lc);

Should lock the life_assurance object, so that its destructor would block if invoked.

Arguments:

  • lc - a reference to a const life_checker object

shared_data and life_assurance-parametrized constructor

execution_guard(const shared_data& sd, const life_assurance& la);

Should lock the life_assurance object, so that its destructor would block if invoked.

Arguments:

  • sd - a reference to a const shared_data object
  • la - a reference to a const life_assurance object

destructor

~execution_guard();

Should unlock the life_assurance object, so that its destructor would return if was blocked.

Required execution_guard nested class methods

is_alive

bool is_alive() const;

This method should return false after a life_assurance::release() call, true otherwise.

Return value: true if it is safe to invoke the signal handler, false otherwise

Clone this wiki locally