[instance] single OpenThread instance optimizations (#1926)

This commit new configuration option `enable-multiple-instances` and
its corresponding option `OPENTHREAD_ENABLE_MULTIPLE_INSTANCES`. When
enabled OpenThread supports handling of multiple instances. By default
this is disabled.

This commit also adds two optimizations for single instance case to
simplify the code and also help reduce memory/RAM usage:

(1) OpenThread objects/classes typically keep a reference to a higher
level object (e.g., many classes keep track of owning `ThreadNetif`)
to be able to access other objects/methods within OpenThread class
hierarchy. In single instance mode, the reference member variables are
removed and instead global functions are used to access the singleton
objects from one class to the other. To implement this, a group of
`<Object>Locator` classes are defined (e.g., `ThreadNetifLocator`,
etc.) which are then used as base class of other OpenThread classes.

(2) OpenThread objects which provide a callback/handler (e.g.,
`Timer`, `Tasklet`, etc.) have `void *mContext` member variable which
is used to keep track of the owner of the object. In single instance
mode the `mConext` member variables are removed since the owner is
expected to be a singleton and can be uniquely determined from the
callback function.

To implement this, two changes are made: First the handler methods are
modified to provide a reference to the object (e.g., `Timer` handler
will provide a `Timer &aTimer` as a parameter of its handler
callback). Second, a new base class `Context` is introduced which
hides the implementation providing an arbitrary context information. A
new static method `GetOwner(aContext)` is added to classes which own a
callback providing objects. This method help convert a `Context` to
the reference of the owner class object.
This commit is contained in:
Abtin Keshavarzian
2017-06-30 11:48:26 -07:00
committed by Jonathan Hui
parent 5128602b49
commit bbab3074ed
143 changed files with 2932 additions and 1831 deletions
+13 -2
View File
@@ -1682,9 +1682,9 @@ exit:
AppendResult(error);
}
void Interpreter::s_HandlePingTimer(void *aContext)
void Interpreter::s_HandlePingTimer(Timer &aTimer)
{
static_cast<Interpreter *>(aContext)->HandlePingTimer();
GetOwner(aTimer).HandlePingTimer();
}
void Interpreter::HandlePingTimer()
@@ -3181,5 +3181,16 @@ void Interpreter::HandleDiagnosticGetResponse(Message &aMessage, const Ip6::Mess
}
#endif
Interpreter &Interpreter::GetOwner(const Context &aContext)
{
#if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES
Interpreter &interpreter = *static_cast<Interpreter *>(aContext.GetContext());
#else
Interpreter &interpreter = Uart::sUartServer->GetInterpreter();
OT_UNUSED_VARIABLE(aContext);
#endif
return interpreter;
}
} // namespace Cli
} // namespace ot