Libspinscale: Initial top-level SMO port to coroutine framework

We haven't ported everything. Just the top-level methods. We'll
dig in to the leaf stuff later. Surprisingly, this all went without
any real difficulties.

Runs like a charm on first try.
This commit is contained in:
2026-05-24 16:12:29 -04:00
parent c539e6e924
commit cde2737876
44 changed files with 1296 additions and 1530 deletions
+10 -10
View File
@@ -3,8 +3,8 @@
#include <functional>
#include <optional>
#include <opts.h>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/callback.h>
#include <spinscale/cps/asynchronousContinuation.h>
#include <spinscale/cps/callback.h>
#include <user/senseApiDesc.h>
#include "protocol.h"
#include "core.h"
@@ -74,7 +74,7 @@ std::optional<std::shared_ptr<Device>> DeviceManager::getDevice(
// GetOrCreateDeviceReq nested class implementation
class DeviceManager::GetOrCreateDeviceReq
: public sscl::NonPostedAsynchronousContinuation<
: public sscl::cps::NonPostedAsynchronousContinuation<
livoxProto1_getOrCreateDeviceReqCbFn>
{
public:
@@ -86,8 +86,8 @@ public:
GetOrCreateDeviceReq(
DeviceManager& mgr,
std::shared_ptr<Device> device,
sscl::Callback<livoxProto1_getOrCreateDeviceReqCbFn> cb)
: sscl::NonPostedAsynchronousContinuation<
sscl::cps::Callback<livoxProto1_getOrCreateDeviceReqCbFn> cb)
: sscl::cps::NonPostedAsynchronousContinuation<
livoxProto1_getOrCreateDeviceReqCbFn>(std::move(cb)),
deviceManager(mgr), pendingDevice(device)
{}
@@ -132,7 +132,7 @@ void DeviceManager::getOrCreateDeviceReq(
int commandTimeoutMs, int retryDelayMs,
const std::string& smoIp, uint8_t smoSubnetNbits,
uint16_t dataPort, uint16_t cmdPort, uint16_t imuPort,
sscl::Callback<livoxProto1_getOrCreateDeviceReqCbFn> callback)
sscl::cps::Callback<livoxProto1_getOrCreateDeviceReqCbFn> callback)
{
// Validate smoIp format using Boost.Asio IPv4 validation
if (!smoIp.empty() && !comms::isValidIPv4(smoIp))
@@ -179,7 +179,7 @@ void DeviceManager::getOrCreateDeviceReq(
}
class DeviceManager::DestroyDeviceReq
: public sscl::NonPostedAsynchronousContinuation<
: public sscl::cps::NonPostedAsynchronousContinuation<
livoxProto1_destroyDeviceReqCbFn>
{
public:
@@ -190,8 +190,8 @@ public:
DestroyDeviceReq(
DeviceManager& mgr,
std::shared_ptr<Device> device,
sscl::Callback<livoxProto1_destroyDeviceReqCbFn> cb)
: sscl::NonPostedAsynchronousContinuation<
sscl::cps::Callback<livoxProto1_destroyDeviceReqCbFn> cb)
: sscl::cps::NonPostedAsynchronousContinuation<
livoxProto1_destroyDeviceReqCbFn>(std::move(cb)),
deviceManager(mgr), pendingDevice(device)
{}
@@ -220,7 +220,7 @@ public:
void DeviceManager::destroyDeviceReq(
std::shared_ptr<Device> dev,
sscl::Callback<livoxProto1_destroyDeviceReqCbFn> callback
sscl::cps::Callback<livoxProto1_destroyDeviceReqCbFn> callback
)
{
/** EXPLANATION:
+3 -3
View File
@@ -11,7 +11,7 @@
#include "broadcastListener.h"
#include "udpCommandDemuxer.h"
#include "livoxProto1.h"
#include <spinscale/callback.h>
#include <spinscale/cps/callback.h>
namespace livoxProto1 {
@@ -29,11 +29,11 @@ public:
int commandTimeoutMs, int retryDelayMs,
const std::string& smoIp, uint8_t smoSubnetNbits,
uint16_t dataPort, uint16_t cmdPort, uint16_t imuPort,
sscl::Callback<livoxProto1_getOrCreateDeviceReqCbFn> callback);
sscl::cps::Callback<livoxProto1_getOrCreateDeviceReqCbFn> callback);
void destroyDeviceReq(
std::shared_ptr<Device> device,
sscl::Callback<livoxProto1_destroyDeviceReqCbFn> callback);
sscl::cps::Callback<livoxProto1_destroyDeviceReqCbFn> callback);
std::optional<std::shared_ptr<Device>> getDevice(
const std::string &deviceIdentifier);
+39 -39
View File
@@ -17,8 +17,8 @@
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/posix/stream_descriptor.hpp>
#include <opts.h>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/callback.h>
#include <spinscale/cps/asynchronousContinuation.h>
#include <spinscale/cps/callback.h>
#include "device.h"
#include "protocol.h"
#include "core.h"
@@ -124,15 +124,15 @@ Device::~Device()
* This class manages the overall device connection process including handshake and heartbeat setup
*/
class Device::ConnectReq
: public sscl::NonPostedAsynchronousContinuation<Device::connectReqCbFn>
: public sscl::cps::NonPostedAsynchronousContinuation<Device::connectReqCbFn>
{
private:
Device& device;
boost::asio::deadline_timer delayTimer;
public:
ConnectReq(Device& dev, sscl::Callback<Device::connectReqCbFn> cb)
: sscl::NonPostedAsynchronousContinuation<Device::connectReqCbFn>(
ConnectReq(Device& dev, sscl::cps::Callback<Device::connectReqCbFn> cb)
: sscl::cps::NonPostedAsynchronousContinuation<Device::connectReqCbFn>(
std::move(cb)), device(dev),
delayTimer(dev.componentThread->getIoService())
{}
@@ -216,7 +216,7 @@ public:
{ callOriginalCallback(false); }
};
void Device::connectReq(sscl::Callback<Device::connectReqCbFn> callback)
void Device::connectReq(sscl::cps::Callback<Device::connectReqCbFn> callback)
{
// Create the connection request object to hold state and callbacks
auto request = std::make_shared<ConnectReq>(*this, std::move(callback));
@@ -233,7 +233,7 @@ void Device::connectReq(sscl::Callback<Device::connectReqCbFn> callback)
}
class Device::ConnectToKnownDeviceReq
: public sscl::NonPostedAsynchronousContinuation<
: public sscl::cps::NonPostedAsynchronousContinuation<
Device::connectToKnownDeviceReqCbFn>
{
public:
@@ -241,8 +241,8 @@ public:
std::string deviceIP;
std::shared_ptr<livoxProto1::comms::DiscoveredDevice> deviceInfo;
ConnectToKnownDeviceReq(Device& dev, sscl::Callback<Device::connectToKnownDeviceReqCbFn> cb)
: sscl::NonPostedAsynchronousContinuation<
ConnectToKnownDeviceReq(Device& dev, sscl::cps::Callback<Device::connectToKnownDeviceReqCbFn> cb)
: sscl::cps::NonPostedAsynchronousContinuation<
Device::connectToKnownDeviceReqCbFn>(std::move(cb)), device(dev)
{}
@@ -269,7 +269,7 @@ public:
* broadcastListener.
*/
void Device::connectToKnownDeviceReq(
sscl::Callback<Device::connectToKnownDeviceReqCbFn> callback
sscl::cps::Callback<Device::connectToKnownDeviceReqCbFn> callback
)
{
// Create the connection request object to hold state and callbacks
@@ -336,7 +336,7 @@ void Device::connectToKnownDeviceReq(
}
class Device::ConnectByDeviceIdentifierReq
: public sscl::NonPostedAsynchronousContinuation<
: public sscl::cps::NonPostedAsynchronousContinuation<
Device::connectByDeviceIdentifierReqCbFn>
{
public:
@@ -344,8 +344,8 @@ public:
std::string deviceIP;
ConnectByDeviceIdentifierReq(
Device& dev, sscl::Callback<Device::connectByDeviceIdentifierReqCbFn> cb)
: sscl::NonPostedAsynchronousContinuation<
Device& dev, sscl::cps::Callback<Device::connectByDeviceIdentifierReqCbFn> cb)
: sscl::cps::NonPostedAsynchronousContinuation<
Device::connectByDeviceIdentifierReqCbFn>(
std::move(cb)), device(dev)
{}
@@ -370,7 +370,7 @@ public:
};
void Device::connectByDeviceIdentifierReq(
sscl::Callback<Device::connectByDeviceIdentifierReqCbFn> callback
sscl::cps::Callback<Device::connectByDeviceIdentifierReqCbFn> callback
)
{
/** EXPLANATION:
@@ -419,13 +419,13 @@ void Device::connectByDeviceIdentifierReq(
}
class Device::ExecuteHandshakeReq
: public sscl::NonPostedAsynchronousContinuation<
: public sscl::cps::NonPostedAsynchronousContinuation<
Device::executeHandshakeReqCbFn>
{
public:
friend void Device::executeHandshakeReq(
const std::string& deviceIP,
sscl::Callback<Device::executeHandshakeReqCbFn> callback);
sscl::cps::Callback<Device::executeHandshakeReqCbFn> callback);
enum class SocketState
{
@@ -459,8 +459,8 @@ public:
Device& dev, const std::string& deviceIP,
std::shared_ptr<boost::asio::posix::stream_descriptor>
&cmdEndpointFdDesc,
sscl::Callback<Device::executeHandshakeReqCbFn> cb)
: sscl::NonPostedAsynchronousContinuation<Device::executeHandshakeReqCbFn>(
sscl::cps::Callback<Device::executeHandshakeReqCbFn> cb)
: sscl::cps::NonPostedAsynchronousContinuation<Device::executeHandshakeReqCbFn>(
std::move(cb)),
device(dev), deviceIP(deviceIP),
cmdEndpointFdDesc(cmdEndpointFdDesc),
@@ -753,7 +753,7 @@ private:
void Device::executeHandshakeReq(
const std::string& deviceIP,
sscl::Callback<Device::executeHandshakeReqCbFn> callback
sscl::cps::Callback<Device::executeHandshakeReqCbFn> callback
)
{
// Get the command endpoint from the UdpCommandDemuxer
@@ -803,7 +803,7 @@ void Device::executeHandshakeReq(
}
}
void Device::disconnectReq(sscl::Callback<Device::disconnectReqCbFn> callback)
void Device::disconnectReq(sscl::cps::Callback<Device::disconnectReqCbFn> callback)
{
// Stop heartbeat first
stopHeartbeat();
@@ -1331,7 +1331,7 @@ std::optional<std::string> Device::getSmoIp(const std::string& deviceIP)
// Base class for both enable and disable pcloud data requests
template<typename CallbackType>
class EnDisablePcloudDataReq
: public sscl::NonPostedAsynchronousContinuation<CallbackType>
: public sscl::cps::NonPostedAsynchronousContinuation<CallbackType>
{
public:
enum class SocketState
@@ -1362,8 +1362,8 @@ public:
protected:
EnDisablePcloudDataReq(
Device& dev,
sscl::Callback<CallbackType> cb)
: sscl::NonPostedAsynchronousContinuation<CallbackType>(std::move(cb)),
sscl::cps::Callback<CallbackType> cb)
: sscl::cps::NonPostedAsynchronousContinuation<CallbackType>(std::move(cb)),
device(dev),
timeoutTimer(device.componentThread->getIoService())
{}
@@ -1608,11 +1608,11 @@ class Device::EnablePcloudDataReq
{
public:
friend void Device::enablePcloudDataReq(
sscl::Callback<Device::enablePcloudDataReqCbFn> callback);
sscl::cps::Callback<Device::enablePcloudDataReqCbFn> callback);
EnablePcloudDataReq(
Device& dev,
sscl::Callback<Device::enablePcloudDataReqCbFn> cb)
sscl::cps::Callback<Device::enablePcloudDataReqCbFn> cb)
: EnDisablePcloudDataReq<Device::enablePcloudDataReqCbFn>(dev, std::move(cb))
{}
@@ -1643,11 +1643,11 @@ class Device::DisablePcloudDataReq
{
public:
friend void Device::disablePcloudDataReq(
sscl::Callback<Device::disablePcloudDataReqCbFn> callback);
sscl::cps::Callback<Device::disablePcloudDataReqCbFn> callback);
DisablePcloudDataReq(
Device& dev,
sscl::Callback<Device::disablePcloudDataReqCbFn> cb)
sscl::cps::Callback<Device::disablePcloudDataReqCbFn> cb)
: EnDisablePcloudDataReq<Device::disablePcloudDataReqCbFn>(
dev, std::move(cb))
{}
@@ -1675,7 +1675,7 @@ private:
};
void Device::enablePcloudDataReq(
sscl::Callback<Device::enablePcloudDataReqCbFn> callback
sscl::cps::Callback<Device::enablePcloudDataReqCbFn> callback
)
{
auto request = std::make_shared<EnablePcloudDataReq>(
@@ -1702,7 +1702,7 @@ void Device::enablePcloudDataReq(
}
void Device::disablePcloudDataReq(
sscl::Callback<Device::disablePcloudDataReqCbFn> callback
sscl::cps::Callback<Device::disablePcloudDataReqCbFn> callback
)
{
auto request = std::make_shared<DisablePcloudDataReq>(
@@ -1858,7 +1858,7 @@ void Device::unregisterUdpCommandHandler(
// SetReturnModeReq continuation class
class Device::SetReturnModeReq
: public sscl::NonPostedAsynchronousContinuation<Device::setReturnModeReqCbFn>
: public sscl::cps::NonPostedAsynchronousContinuation<Device::setReturnModeReqCbFn>
{
public:
enum class SocketState
@@ -1890,12 +1890,12 @@ public:
public:
friend void Device::setReturnModeReq(
uint8_t returnMode,
sscl::Callback<Device::setReturnModeReqCbFn> callback);
sscl::cps::Callback<Device::setReturnModeReqCbFn> callback);
SetReturnModeReq(
Device& dev, uint8_t mode,
sscl::Callback<Device::setReturnModeReqCbFn> cb)
: sscl::NonPostedAsynchronousContinuation<Device::setReturnModeReqCbFn>(
sscl::cps::Callback<Device::setReturnModeReqCbFn> cb)
: sscl::cps::NonPostedAsynchronousContinuation<Device::setReturnModeReqCbFn>(
std::move(cb)),
device(dev), returnMode(mode),
timeoutTimer(device.componentThread->getIoService())
@@ -2107,7 +2107,7 @@ public:
// GetReturnModeReq continuation class
class Device::GetReturnModeReq
: public sscl::NonPostedAsynchronousContinuation<Device::getReturnModeReqCbFn>
: public sscl::cps::NonPostedAsynchronousContinuation<Device::getReturnModeReqCbFn>
{
public:
enum class SocketState
@@ -2137,12 +2137,12 @@ public:
public:
friend void Device::getReturnModeReq(
sscl::Callback<Device::getReturnModeReqCbFn> callback);
sscl::cps::Callback<Device::getReturnModeReqCbFn> callback);
GetReturnModeReq(
Device& dev,
sscl::Callback<Device::getReturnModeReqCbFn> cb)
: sscl::NonPostedAsynchronousContinuation<Device::getReturnModeReqCbFn>(
sscl::cps::Callback<Device::getReturnModeReqCbFn> cb)
: sscl::cps::NonPostedAsynchronousContinuation<Device::getReturnModeReqCbFn>(
std::move(cb)),
device(dev),
timeoutTimer(device.componentThread->getIoService())
@@ -2351,7 +2351,7 @@ public:
void Device::setReturnModeReq(
uint8_t returnMode,
sscl::Callback<Device::setReturnModeReqCbFn> callback
sscl::cps::Callback<Device::setReturnModeReqCbFn> callback
)
{
auto request = std::make_shared<SetReturnModeReq>(
@@ -2378,7 +2378,7 @@ void Device::setReturnModeReq(
}
void Device::getReturnModeReq(
sscl::Callback<Device::getReturnModeReqCbFn> callback
sscl::cps::Callback<Device::getReturnModeReqCbFn> callback
)
{
auto request = std::make_shared<GetReturnModeReq>(
+10 -10
View File
@@ -18,7 +18,7 @@
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/posix/stream_descriptor.hpp>
#include "protocol.h"
#include <spinscale/callback.h>
#include <spinscale/cps/callback.h>
#include <spinscale/spinLock.h>
// Custom hash function for std::pair<uint8_t, uint8_t>
@@ -163,20 +163,20 @@ public:
getReturnModeReqCbFn;
// Async connection methods
void connectReq(sscl::Callback<connectReqCbFn> callback);
void connectReq(sscl::cps::Callback<connectReqCbFn> callback);
void connectToKnownDeviceReq(
sscl::Callback<connectToKnownDeviceReqCbFn> callback);
sscl::cps::Callback<connectToKnownDeviceReqCbFn> callback);
void connectByDeviceIdentifierReq(
sscl::Callback<connectByDeviceIdentifierReqCbFn> callback);
sscl::cps::Callback<connectByDeviceIdentifierReqCbFn> callback);
void executeHandshakeReq(
const std::string& deviceIP,
sscl::Callback<executeHandshakeReqCbFn> callback);
void disconnectReq(sscl::Callback<disconnectReqCbFn> callback);
void enablePcloudDataReq(sscl::Callback<enablePcloudDataReqCbFn> callback);
void disablePcloudDataReq(sscl::Callback<disablePcloudDataReqCbFn> callback);
sscl::cps::Callback<executeHandshakeReqCbFn> callback);
void disconnectReq(sscl::cps::Callback<disconnectReqCbFn> callback);
void enablePcloudDataReq(sscl::cps::Callback<enablePcloudDataReqCbFn> callback);
void disablePcloudDataReq(sscl::cps::Callback<disablePcloudDataReqCbFn> callback);
void setReturnModeReq(
uint8_t returnMode, sscl::Callback<setReturnModeReqCbFn> callback);
void getReturnModeReq(sscl::Callback<getReturnModeReqCbFn> callback);
uint8_t returnMode, sscl::cps::Callback<setReturnModeReqCbFn> callback);
void getReturnModeReq(sscl::cps::Callback<getReturnModeReqCbFn> callback);
public:
comms::DiscoveredDevice discoveredDevice;
+6 -6
View File
@@ -1,6 +1,6 @@
#include <boostAsioLinkageFix.h>
#include <stdexcept>
#include <spinscale/callback.h>
#include <spinscale/cps/callback.h>
#include <boost/asio/posix/stream_descriptor.hpp>
#include "livoxProto1.h"
#include "device.h"
@@ -16,7 +16,7 @@ void livoxProto1_getOrCreateDeviceReq(
int commandTimeoutMs, int retryDelayMs,
const std::string& smoIp, uint8_t smoSubnetNbits,
uint16_t dataPort, uint16_t cmdPort, uint16_t imuPort,
sscl::Callback<livoxProto1_getOrCreateDeviceReqCbFn> callback
sscl::cps::Callback<livoxProto1_getOrCreateDeviceReqCbFn> callback
)
{
// Get the global DeviceManager instance
@@ -39,7 +39,7 @@ void livoxProto1_getOrCreateDeviceReq(
void livoxProto1_destroyDeviceReq(
std::shared_ptr<livoxProto1::Device> device,
sscl::Callback<livoxProto1_destroyDeviceReqCbFn> callback
sscl::cps::Callback<livoxProto1_destroyDeviceReqCbFn> callback
)
{
auto& protoState = livoxProto1::getProtoState();
@@ -67,7 +67,7 @@ void livoxProto1_exit(void)
void livoxProto1_device_enablePcloudDataReq(
std::shared_ptr<livoxProto1::Device> device,
sscl::Callback<livoxProto1_device_enablePcloudDataReqCbFn> callback
sscl::cps::Callback<livoxProto1_device_enablePcloudDataReqCbFn> callback
)
{
if (!device)
@@ -81,7 +81,7 @@ void livoxProto1_device_enablePcloudDataReq(
void livoxProto1_device_disablePcloudDataReq(
std::shared_ptr<livoxProto1::Device> device,
sscl::Callback<livoxProto1_device_disablePcloudDataReqCbFn> callback
sscl::cps::Callback<livoxProto1_device_disablePcloudDataReqCbFn> callback
)
{
if (!device)
@@ -95,7 +95,7 @@ void livoxProto1_device_disablePcloudDataReq(
void livoxProto1_device_getReturnModeReq(
std::shared_ptr<livoxProto1::Device> device,
sscl::Callback<livoxProto1_device_getReturnModeReqCbFn> callback
sscl::cps::Callback<livoxProto1_device_getReturnModeReqCbFn> callback
)
{
if (!device)
+6 -6
View File
@@ -6,7 +6,7 @@
#include <string>
#include <cstdint>
#include <functional>
#include <spinscale/callback.h>
#include <spinscale/cps/callback.h>
#include <boost/asio/posix/stream_descriptor.hpp>
// Forward declarations
@@ -64,30 +64,30 @@ typedef void livoxProto1_getOrCreateDeviceReqFn(
int commandTimeoutMs, int retryDelayMs,
const std::string& smoIp, uint8_t smoSubnetNbits,
uint16_t dataPort, uint16_t cmdPort, uint16_t imuPort,
sscl::Callback<livoxProto1_getOrCreateDeviceReqCbFn> callback);
sscl::cps::Callback<livoxProto1_getOrCreateDeviceReqCbFn> callback);
typedef std::function<void(bool success)> livoxProto1_destroyDeviceReqCbFn;
typedef void livoxProto1_destroyDeviceReqFn(
std::shared_ptr<livoxProto1::Device> device,
sscl::Callback<livoxProto1_destroyDeviceReqCbFn> callback);
sscl::cps::Callback<livoxProto1_destroyDeviceReqCbFn> callback);
typedef std::function<void(bool success)>
livoxProto1_device_enablePcloudDataReqCbFn;
typedef void livoxProto1_device_enablePcloudDataReqFn(
std::shared_ptr<livoxProto1::Device> device,
sscl::Callback<livoxProto1_device_enablePcloudDataReqCbFn> callback);
sscl::cps::Callback<livoxProto1_device_enablePcloudDataReqCbFn> callback);
typedef std::function<void(bool success)>
livoxProto1_device_disablePcloudDataReqCbFn;
typedef void livoxProto1_device_disablePcloudDataReqFn(
std::shared_ptr<livoxProto1::Device> device,
sscl::Callback<livoxProto1_device_disablePcloudDataReqCbFn> callback);
sscl::cps::Callback<livoxProto1_device_disablePcloudDataReqCbFn> callback);
typedef std::function<void(bool success, uint8_t returnMode)>
livoxProto1_device_getReturnModeReqCbFn;
typedef void livoxProto1_device_getReturnModeReqFn(
std::shared_ptr<livoxProto1::Device> device,
sscl::Callback<livoxProto1_device_getReturnModeReqCbFn> callback);
sscl::cps::Callback<livoxProto1_device_getReturnModeReqCbFn> callback);
typedef std::shared_ptr<boost::asio::posix::stream_descriptor>
livoxProto1_getPcloudDataFdDescFn(void);
+35
View File
@@ -0,0 +1,35 @@
Ok. We're extending the DAPS language to support specifying at most 1 postrin and at most 1 negtrin for each DAP spec. I.e: we'll no longer treat negtrins/postrins as their own qualeifaceapis attached to a deviceselector, but rather now negtrins and postrins will be attached to the nontrins they are triggered by. So looking at avia0.dapss:
```
// This line will be entirely deleted:
+edev|avia0|postrin(
from-stimbuff=pcloudAmbience|
interest-pc=85|
passband-count-lt-val=8
)
|livoxGen1()|livoxProto1(SMO_IP)|3JEDK380010Z39||
// And this one will be deleted too:
+edev|avia0|negtrin(
from-stimbuff=pcloudAmbience|
interest-pc=85|distraction-pc=90|intolerable-pc=95|
passband-count-gt-val=120
)
|livoxGen1()|livoxProto1(SMO_IP)|3JEDK380010Z39
// And this line will be modified from:
+edev|avia0|pcloudAmbience()|livoxGen1()|livoxProto1(SMO_IP)|3JEDK380010Z39||
// To:
+edev|avia0| postrin(interest-pc=85)| negtrin(interest-pc=85) |pcloudAmbience(passband-count-lt-val=8|passband-count-gt-val=120)|livoxGen1()|livoxProto1(SMO_IP)|3JEDK380010Z39||
```
So we're adding the ability to spec intrins on nontrin stimbuffs. Any DAP spec can declare 0 or 1 negtrin and 0 or 1 postrin. These must be declared before the first qualeIfaceApi.
Modify the current DAP spec lex/yacc files to support thissyntax; as well as the current DeviceAttachmentSpec struct type too. And update the current stimbuff devices if they use the current postrin/negtrin parameters. Let them look for these new postrin and negtrin specs and their params instead. For the official language-level spec, you can use these names for the new specifiers:
```
sensor-type|dev-identifier|postrin-spec(postrin-params)|negtrin-spec(negtrin-params)|quale-iface-api(quale-iface-api-params)|stim-buff-api(api-params)|provider(provider-params)|dev-selector
```
NB: The postrin and negtrin spec have no ordering requirement with respect to one another. They are only ordered with respect to the other specifiers in the DAP specs -- i.e: again, they must precede the qualeIfaceApi specifier. So the negtrin may appear before the postrin; and vice versa.
+3 -3
View File
@@ -9,7 +9,7 @@
#include <vector>
#include <preprocessor.h>
#include <user/deviceAttachmentSpec.h>
#include <spinscale/callback.h>
#include <spinscale/cps/callback.h>
#include <spinscale/componentThread.h>
#define CL_TARGET_OPENCL_VERSION 120
#include <CL/cl.h>
@@ -57,10 +57,10 @@ typedef int (sal_mlo_finalizeIndFn)(void);
typedef void (sal_mlo_attachDeviceReqFn)(
const std::shared_ptr<device::DeviceAttachmentSpec>& desc,
const std::shared_ptr<sscl::ComponentThread>& componentThread,
sscl::Callback<sal_mlo_attachDeviceReqCbFn> cb);
sscl::cps::Callback<sal_mlo_attachDeviceReqCbFn> cb);
typedef void (sal_mlo_detachDeviceReqFn)(
const std::shared_ptr<device::DeviceAttachmentSpec>& desc,
sscl::Callback<sal_mlo_detachDeviceReqCbFn> cb);
sscl::cps::Callback<sal_mlo_detachDeviceReqCbFn> cb);
/**
* @brief Hooks provided by Salmanoff to senseApi libraries.
+1
View File
@@ -7,6 +7,7 @@ add_library(smocore STATIC
mind.cpp
mindComponent.cpp
componentThread.cpp
componentThreadTags.cpp
opts.cpp
# Body
+28 -119
View File
@@ -1,15 +1,12 @@
#include <iostream>
#include <stdexcept>
#include <opts.h>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/asynchronousLoop.h>
#include <spinscale/callback.h>
#include <spinscale/callableTracer.h>
#include <spinscale/puppetApplication.h>
#include <body/body.h>
#include <body/bodyThread.h>
#include <componentThread.h>
#include <deviceManager/deviceManager.h>
#include <mind.h>
#include <stimBuffApis/stimBuffApiManager.h>
#include <deviceManager/deviceManager.h>
namespace smo {
namespace body {
@@ -19,26 +16,8 @@ Body::Body(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread)
{
}
class Body::InitializeReq
: public sscl::PostedAsynchronousContinuation<bodyLifetimeMgmtOpCbFn>
BodyViralPostingInvoker<bool> Body::initializeCReq()
{
public:
InitializeReq(
sscl::PuppetApplication &parent,
const std::shared_ptr<sscl::ComponentThread> &caller,
sscl::Callback<bodyLifetimeMgmtOpCbFn> callback)
: sscl::PostedAsynchronousContinuation<bodyLifetimeMgmtOpCbFn>(caller, callback),
parent(parent)
{}
private:
sscl::PuppetApplication &parent;
public:
void initializeReq1_posted(
[[maybe_unused]] std::shared_ptr<InitializeReq> context
)
{
auto self = sscl::ComponentThread::getSelf();
if (self->id != SmoThreadId::BODY)
{
@@ -57,12 +36,12 @@ public:
* Body instead since it's meant to handle device-management operations.
*/
// Upcast to Mind to access Mind-specific members
Mind &mind = static_cast<Mind&>(context->parent);
Mind &mind = static_cast<Mind&>(parent);
stim_buff::StimBuffApiManager::getInstance()
.loadAllStimBuffApiLibsFromOptions(mind.body.thread);
/** EXPLANATION:
* Consider body::initializeReq to have been called if even one of its
* Consider body::initializeCReq to have been called if even one of its
* operations was executed at all, whether successfully or
* unsuccessfully.
*/
@@ -81,41 +60,22 @@ public:
if (OptionParser::getOptions().verbose)
{
std::cout << __func__ << ": About to attachAllSenseDevicesFromSpecs"
std::cout << __func__ << ": About to attachAllUnattachedDevicesFromCmdline"
<< '\n';
}
device::DeviceManager::getInstance()
.attachAllUnattachedDevicesFromCmdlineReq(
{context, std::bind(
&InitializeReq::initializeReq2,
context.get(), context,
std::placeholders::_1)});
}
void initializeReq2(
[[maybe_unused]] std::shared_ptr<InitializeReq> context,
sscl::AsynchronousLoop &results
)
{
sscl::MultiOperationResultSet attachResults = co_await
device::DeviceManager::getInstance()
.attachAllUnattachedDevicesFromCmdlineCReq();
std::cout << "Mrntt: attached "
<< results.nSucceeded << " of " << results.nTotal
<< attachResults.nSucceeded << " of " << attachResults.nTotal
<< " sense devices." << "\n";
callOriginalCb(results.nSucceeded > 0);
}
};
co_return attachResults.nSucceeded > 0;
}
class Body::FinalizeReq
: public InitializeReq
BodyViralPostingInvoker<bool> Body::finalizeCReq()
{
public:
using InitializeReq::InitializeReq;
public:
void finalizeReq1_posted(
[[maybe_unused]] std::shared_ptr<FinalizeReq> context
)
{
auto self = sscl::ComponentThread::getSelf();
if (self->id != SmoThreadId::BODY)
{
@@ -123,80 +83,29 @@ public:
+ ": Must be executed on Body thread");
}
std::cout << "Mrntt: About to detach all sense devices." << "\n";
device::DeviceManager::getInstance().detachAllAttachedDeviceRoles(
{context, std::bind(
&FinalizeReq::finalizeReq2,
context.get(), context,
std::placeholders::_1)});
}
void finalizeReq2(
[[maybe_unused]] std::shared_ptr<FinalizeReq> context,
sscl::AsynchronousLoop &results
)
{
std::cout << "Mrntt: Successfully detached "
<< results.nSucceeded << " of " << results.nTotal
<< " sense devices." << "\n";
std::cout << "Mrntt: About to finalize all stim buff api libs." << "\n";
stim_buff::StimBuffApiManager::getInstance().finalizeAllStimBuffApiLibs();
std::cout << "Mrntt: About to unload all stim buff api libs." << "\n";
stim_buff::StimBuffApiManager::getInstance().unloadAllStimBuffApiLibs();
callOriginalCb(results.nSucceeded == results.nTotal);
}
};
void Body::initializeReq(sscl::Callback<bodyLifetimeMgmtOpCbFn> callback)
{
auto mrntt = sscl::ComponentThread::getSelf();
if (mrntt->id != SmoThreadId::MRNTT)
{
throw std::runtime_error(std::string(__func__)
+ ": Must be invoked by Mrntt thread");
}
auto request = std::make_shared<InitializeReq>(
parent, mrntt, callback);
thread->getIoService().post(
STC(std::bind(
&InitializeReq::initializeReq1_posted,
request.get(), request)));
}
void Body::finalizeReq(sscl::Callback<bodyLifetimeMgmtOpCbFn> callback)
{
auto mrntt = sscl::ComponentThread::getSelf();
if (mrntt->id != SmoThreadId::MRNTT)
{
std::cerr << __func__ << ": Must be invoked by Mrntt thread"
<< std::endl;
callback.callbackFn(false);
return;
}
// Upcast to Mind to access Mind-specific members
Mind &mind = static_cast<Mind&>(parent);
if (!mind.bodyComponentInitialized)
{
std::cout << "Mrntt: Body component not initialized. "
<< "Skipping finalization." << "\n";
callback.callbackFn(true);
return;
co_return true;
}
auto request = std::make_shared<FinalizeReq>(
parent, mrntt, callback);
std::cout << "Mrntt: About to detach all sense devices." << "\n";
sscl::MultiOperationResultSet detachResults = co_await
device::DeviceManager::getInstance().detachAllAttachedDeviceRolesCReq();
std::cout << "Mrntt: Successfully detached "
<< detachResults.nSucceeded << " of " << detachResults.nTotal
<< " sense devices." << "\n";
thread->getIoService().post(
STC(std::bind(
&FinalizeReq::finalizeReq1_posted,
request.get(), request)));
std::cout << "Mrntt: About to finalize all stim buff api libs." << "\n";
stim_buff::StimBuffApiManager::getInstance().finalizeAllStimBuffApiLibs();
std::cout << "Mrntt: About to unload all stim buff api libs." << "\n";
stim_buff::StimBuffApiManager::getInstance().unloadAllStimBuffApiLibs();
co_return detachResults.nSucceeded == detachResults.nTotal;
}
} // namespace body
+85
View File
@@ -0,0 +1,85 @@
#include <stdexcept>
#include <body/bodyThread.h>
#include <componentThread.h>
#include <director/directorThread.h>
#include <marionette/marionette.h>
#include <marionette/marionetteThread.h>
#include <mind.h>
#include <simulator/simulatorThread.h>
#include <subconsciousThread.h>
#include <world/worldThread.h>
namespace smo {
namespace mrntt {
boost::asio::io_service &MrnttThreadTag::io_service() noexcept
{
return thread->getIoService();
}
} // namespace mrntt
namespace body {
boost::asio::io_service &BodyThreadTag::io_service()
{
if (!mind::globalMind) {
throw std::runtime_error(
"BodyThreadTag: globalMind not initialized");
}
return mind::globalMind->body.thread->getIoService();
}
} // namespace body
namespace director {
boost::asio::io_service &DirectorThreadTag::io_service()
{
if (!mind::globalMind) {
throw std::runtime_error(
"DirectorThreadTag: globalMind not initialized");
}
return mind::globalMind->director.thread->getIoService();
}
} // namespace director
namespace simulator {
boost::asio::io_service &SimulatorThreadTag::io_service()
{
if (!mind::globalMind) {
throw std::runtime_error(
"SimulatorThreadTag: globalMind not initialized");
}
return mind::globalMind->canvas.thread->getIoService();
}
} // namespace simulator
boost::asio::io_service &SubconsciousThreadTag::io_service()
{
if (!mind::globalMind) {
throw std::runtime_error(
"SubconsciousThreadTag: globalMind not initialized");
}
return mind::globalMind->subconscious.thread->getIoService();
}
boost::asio::io_service &WorldThreadTag::io_service()
{
if (!mind::globalMind) {
throw std::runtime_error(
"WorldThreadTag: globalMind not initialized");
}
return mind::globalMind->world.thread->getIoService();
}
} // namespace smo
@@ -33,23 +33,27 @@ std::string DeviceManager::readDapSpecFile(const std::string& filename)
void DeviceManager::collateAllDapSpecs(void)
{
OptionParser &options = OptionParser::getOptions();
allDapSpecs = options.dapSpecs;
DeviceManager &dm = getInstance();
dm.s.rsrc.allDapSpecs = options.dapSpecs;
for (const auto& file : options.dapSpecFiles)
{
std::string fileContent = readDapSpecFile(file);
if (!allDapSpecs.empty()) {
allDapSpecs += "||";
if (!dm.s.rsrc.allDapSpecs.empty()) {
dm.s.rsrc.allDapSpecs += "||";
}
allDapSpecs += fileContent;
dm.s.rsrc.allDapSpecs += fileContent;
}
}
void DeviceManager::parseAllDapSpecs(void)
{
DeviceManager &dm = getInstance();
auto file_deleter = [](FILE* f) { if (f) fclose(f); };
std::unique_ptr<FILE, decltype(file_deleter)> input(
fmemopen((void*)allDapSpecs.c_str(), allDapSpecs.size(), "r"),
fmemopen(
(void*)dm.s.rsrc.allDapSpecs.c_str(),
dm.s.rsrc.allDapSpecs.size(), "r"),
file_deleter);
if (!input)
@@ -91,7 +91,7 @@ interoceptor_spec:
*static_cast<smo::device::InteroceptorDevAttachmentSpec *>($3));
spec->sensorType = $1;
smo::device::DeviceManager::commandLineDASpecs.push_back(*spec);
smo::device::DeviceManager::getInstance().s.rsrc.commandLineDASpecs.push_back(*spec);
delete $3;
}
@@ -103,7 +103,7 @@ extrospector_spec:
*static_cast<smo::device::ExtrospectorDevAttachmentSpec *>($3));
spec->sensorType = $1;
smo::device::DeviceManager::commandLineDASpecs.push_back(*spec);
smo::device::DeviceManager::getInstance().s.rsrc.commandLineDASpecs.push_back(*spec);
delete $3;
}
File diff suppressed because it is too large Load Diff
+70 -10
View File
@@ -1,22 +1,20 @@
#include <config.h>
#include <chrono>
#include <iostream>
#include <functional>
#include <componentThread.h>
#include <spinscale/callback.h>
#include <deviceManager/deviceReattacher.h>
#include <deviceManager/deviceManager.h>
#include <marionette/marionetteThread.h>
namespace smo {
namespace device {
static void reattachmentCb(sscl::AsynchronousLoop& results)
{
if (results.nTotal == 0) { return; }
namespace {
std::cout << "DeviceReattacher: Successfully reattached "
<< results.nSucceeded << " of " << results.nTotal << " devices"
<< std::endl;
}
constexpr unsigned int reattachInFlightStaleThresholdMultiplier = 4;
} // namespace
DeviceReattacher::DeviceReattacher(
DeviceManager& parent, std::shared_ptr<sscl::ComponentThread> ioThread)
@@ -25,6 +23,22 @@ timer(ioThread->getIoService())
{
}
mrntt::MrnttNonViralPostingInvoker DeviceReattacher::reattachKnownListCReq(
[[maybe_unused]] std::exception_ptr &exceptionPtr,
[[maybe_unused]] std::function<void()> callback)
{
sscl::MultiOperationResultSet results = co_await
parent.attachAllUnattachedDevicesFromKnownListCReq();
if (results.nTotal > 0)
{
std::cout << "DeviceReattacher: Successfully reattached "
<< results.nSucceeded << " of " << results.nTotal
<< " devices" << std::endl;
}
co_return;
}
void DeviceReattacher::start()
{
shouldContinue = true;
@@ -36,6 +50,14 @@ void DeviceReattacher::stop()
{
sscl::SpinLock::Guard lock(shouldContinueLock);
shouldContinue = false;
reattachOpInFlight = false;
/** EXPLANATION:
* Do not call reattachCReqInvoker.reset() here. Forcibly destroying
* the invoker would tear down an in-flight reattach coroutine frame
* mid-operation. During normal program teardown the optional (and
* its invoker) are destroyed with the rest of the binary anyway; leave
* a running reattach time to finish if shutdown races with it.
*/
}
timer.cancel();
@@ -56,6 +78,21 @@ void DeviceReattacher::scheduleNextTimeout()
std::bind(&DeviceReattacher::onTimeout, this, std::placeholders::_1));
}
void DeviceReattacher::holdReattachCReq()
{
reattachOpInFlight = true;
lastReattachReqTimestamp = std::chrono::steady_clock::now();
reattachCReqInvoker.reset();
reattachCReqInvoker.emplace(reattachKnownListCReq(
reattachLifetimeExceptionPtr,
[this]()
{
sscl::SpinLock::Guard lock(shouldContinueLock);
reattachOpInFlight = false;
}));
}
void DeviceReattacher::onTimeout(const boost::system::error_code& error)
{
// Timer was cancelled, which is expected when stopping
@@ -75,9 +112,32 @@ void DeviceReattacher::onTimeout(const boost::system::error_code& error)
return;
}
const auto staleThreshold = std::chrono::milliseconds(
reattachInFlightStaleThresholdMultiplier
* CONFIG_MRNTT_DEVMGR_REATTACHER_PERIOD_MS);
// Attempt to reattach all unattached devices from the known list
parent.attachAllUnattachedDevicesFromKnownListReq(
{ nullptr, reattachmentCb});
if (!reattachOpInFlight)
{
holdReattachCReq();
}
else
{
const auto elapsedSinceLastReattachReq =
std::chrono::steady_clock::now() - lastReattachReqTimestamp;
if (elapsedSinceLastReattachReq >= staleThreshold)
{
std::cerr << "DeviceReattacher: Reattach op still in flight after "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
elapsedSinceLastReattachReq).count()
<< "ms (threshold "
<< staleThreshold.count()
<< "ms); forcing a new reattach request."
<< std::endl;
holdReattachCReq();
}
}
// Schedule the next timeout
scheduleNextTimeout();
+3 -9
View File
@@ -4,8 +4,7 @@
#include <spinscale/puppetApplication.h>
#include <spinscale/component.h>
#include <mindComponent.h>
#include <functional>
#include <spinscale/callback.h>
#include <body/bodyThread.h>
namespace smo {
@@ -20,13 +19,8 @@ public:
Body(Mind &parent, const std::shared_ptr<sscl::PuppetThread> &thread);
~Body() = default;
typedef std::function<void(bool)> bodyLifetimeMgmtOpCbFn;
void initializeReq(sscl::Callback<bodyLifetimeMgmtOpCbFn> callback);
void finalizeReq(sscl::Callback<bodyLifetimeMgmtOpCbFn> callback);
private:
class InitializeReq;
class FinalizeReq;
BodyViralPostingInvoker<bool> initializeCReq();
BodyViralPostingInvoker<bool> finalizeCReq();
};
} // namespace body
+30
View File
@@ -0,0 +1,30 @@
#ifndef SMO_BODY_THREAD_H
#define SMO_BODY_THREAD_H
#include <boost/asio/io_service.hpp>
#include <spinscale/co/invokers.h>
#include <spinscale/co/postingPromise.h>
namespace smo {
namespace body {
struct BodyThreadTag
{
static boost::asio::io_service &io_service();
};
template <typename T>
using BodyPostingPromise =
sscl::co::TaggedPostingPromise<T, BodyThreadTag>;
using BodyNonViralPostingInvoker =
sscl::co::NonViralPostingInvoker<BodyPostingPromise>;
template <typename T>
using BodyViralPostingInvoker =
sscl::co::ViralPostingInvoker<BodyPostingPromise, T>;
} // namespace body
} // namespace smo
#endif // SMO_BODY_THREAD_H
+5 -4
View File
@@ -7,7 +7,7 @@
#include <sstream>
#include <user/deviceAttachmentSpec.h>
#include <deviceManager/deviceRole.h>
#include <spinscale/qutex.h>
#include <spinscale/co/coQutex.h>
namespace smo {
namespace device {
@@ -15,8 +15,9 @@ namespace device {
class Device
{
public:
Device(const std::string& identifier)
: deviceIdentifier(identifier), qutex("Device-" + identifier)
explicit Device(const std::string& identifier)
: deviceIdentifier(identifier),
qutex("Device-" + identifier)
{}
std::string stringify() const
@@ -33,7 +34,7 @@ public:
public:
std::string deviceIdentifier;
std::vector<std::shared_ptr<DeviceRole>> deviceRoles;
sscl::Qutex qutex;
sscl::co::CoQutex qutex;
};
} // namespace device
+43 -54
View File
@@ -7,15 +7,16 @@
#include <opts.h>
#include <utility>
#include <iostream>
#include <functional>
#include <user/senseApiDesc.h>
#include <user/deviceAttachmentSpec.h>
#include <spinscale/asynchronousLoop.h>
#include <deviceManager/device.h>
#include <deviceManager/deviceRole.h>
#include <deviceManager/deviceReattacher.h>
#include <spinscale/callback.h>
#include <spinscale/qutex.h>
#include <cpsBoundary/stimBuffDeviceAReq.h>
#include <marionette/marionetteThread.h>
#include <spinscale/co/coQutex.h>
#include <spinscale/multiOperationResultSet.h>
#include <spinscale/sharedResourceGroup.h>
namespace smo {
namespace device {
@@ -25,6 +26,13 @@ class DeviceReattacher;
class DeviceManager
{
public:
struct DeviceAttachmentIndResult
{
bool success = false;
std::shared_ptr<DeviceRole> deviceRole;
std::shared_ptr<DeviceAttachmentSpec> deviceSpec;
};
static DeviceManager& getInstance()
{
static DeviceManager instance;
@@ -45,74 +53,55 @@ public:
static const std::string stringifyDeviceSpecs(void);
typedef std::function<void(
bool success, std::shared_ptr<DeviceRole> deviceRole,
std::shared_ptr<DeviceAttachmentSpec> deviceSpec)>
newDeviceAttachmentSpecIndCbFn;
typedef std::function<void(
bool success, std::shared_ptr<DeviceAttachmentSpec> deviceSpec)>
removeDeviceAttachmentSpecReqCbFn;
mrntt::MrnttViralPostingInvoker<DeviceAttachmentIndResult>
newDeviceAttachmentSpecIndCReq(const DeviceAttachmentSpec &spec);
void newDeviceAttachmentSpecInd(
const DeviceAttachmentSpec &spec,
sscl::Callback<newDeviceAttachmentSpecIndCbFn> callback);
void removeDeviceAttachmentSpecReq(
const DeviceAttachmentSpec &spec,
sscl::Callback<removeDeviceAttachmentSpecReqCbFn> callback);
mrntt::MrnttViralPostingInvoker<DeviceAttachmentIndResult>
removeDeviceAttachmentSpecCReq(const DeviceAttachmentSpec &spec);
// Device attachment/detachment methods moved from SenseApiManager
typedef stim_buff::sal_mlo_attachDeviceReqCbFn attachStimBuffDeviceReqCbFn;
typedef stim_buff::sal_mlo_detachDeviceReqCbFn detachStimBuffDeviceReqCbFn;
mrntt::MrnttViralPostingInvoker<cpsBoundary::StimBuffDeviceOpResult>
attachStimBuffDeviceCReq(
const std::shared_ptr<DeviceAttachmentSpec>& spec);
void attachStimBuffDeviceReq(
const std::shared_ptr<DeviceAttachmentSpec>& spec,
sscl::Callback<attachStimBuffDeviceReqCbFn> cb);
void detachStimBuffDeviceReq(
const std::shared_ptr<DeviceAttachmentSpec>& spec,
sscl::Callback<detachStimBuffDeviceReqCbFn> cb);
mrntt::MrnttViralPostingInvoker<cpsBoundary::StimBuffDeviceOpResult>
detachStimBuffDeviceCReq(
const std::shared_ptr<DeviceAttachmentSpec>& spec);
typedef std::function<void(sscl::AsynchronousLoop &results)>
attachAllUnattachedDevicesFromReqCbFn;
typedef std::function<void(sscl::AsynchronousLoop &results)>
detachAllAttachedDeviceRolesCbFn;
mrntt::MrnttViralPostingInvoker<sscl::MultiOperationResultSet>
attachAllUnattachedDevicesFromCReq(
const std::shared_ptr<std::vector<DeviceAttachmentSpec>> &specs);
void attachAllUnattachedDevicesFromReq(
const std::shared_ptr<std::vector<DeviceAttachmentSpec>> &specs,
sscl::Callback<attachAllUnattachedDevicesFromReqCbFn> cb);
void attachAllUnattachedDevicesFromKnownListReq(
sscl::Callback<attachAllUnattachedDevicesFromReqCbFn> cb);
void attachAllUnattachedDevicesFromCmdlineReq(
sscl::Callback<attachAllUnattachedDevicesFromReqCbFn> cb);
void detachAllAttachedDeviceRoles(
sscl::Callback<detachAllAttachedDeviceRolesCbFn> cb);
mrntt::MrnttViralPostingInvoker<sscl::MultiOperationResultSet>
attachAllUnattachedDevicesFromKnownListCReq();
mrntt::MrnttViralPostingInvoker<sscl::MultiOperationResultSet>
attachAllUnattachedDevicesFromCmdlineCReq();
mrntt::MrnttViralPostingInvoker<sscl::MultiOperationResultSet>
detachAllAttachedDeviceRolesCReq();
private:
DeviceManager()
: qutex("DeviceManager"), deviceReattacher(nullptr)
: s("DeviceManager")
{}
~DeviceManager();
DeviceManager(const DeviceManager&) = delete;
DeviceManager& operator=(const DeviceManager&) = delete;
public:
sscl::Qutex qutex;
struct Resources
{
std::vector<std::shared_ptr<DeviceAttachmentSpec>> deviceAttachmentSpecs;
std::vector<std::shared_ptr<Device>> devices;
std::vector<std::shared_ptr<DeviceRole>> attachedDeviceRoles;
std::vector<DeviceAttachmentSpec> commandLineDASpecs;
std::string allDapSpecs;
static std::vector<std::shared_ptr<DeviceAttachmentSpec>>
deviceAttachmentSpecs;
static std::vector<std::shared_ptr<Device>> devices;
static std::vector<std::shared_ptr<DeviceRole>> attachedDeviceRoles;
static std::vector<DeviceAttachmentSpec> commandLineDASpecs;
};
sscl::SharedResourceGroup<sscl::co::CoQutex, Resources> s;
private:
std::unique_ptr<DeviceReattacher> deviceReattacher;
class NewDeviceAttachmentSpecInd;
class RemoveDeviceAttachmentSpecReq;
class AttachStimBuffDeviceReq;
typedef AttachStimBuffDeviceReq DetachStimBuffDeviceReq;
class AttachAllUnattachedDevicesFromReq;
class AttachAllUnattachedDevicesFromKnownListReq;
class DetachAllAttachedDeviceRoles;
};
} // namespace device
@@ -3,8 +3,14 @@
#include <boostAsioLinkageFix.h>
#include <atomic>
#include <chrono>
#include <exception>
#include <functional>
#include <memory>
#include <optional>
#include <boost/asio/deadline_timer.hpp>
#include <marionette/marionetteThread.h>
#include <spinscale/multiOperationResultSet.h>
#include <spinscale/spinLock.h>
namespace smo {
@@ -20,7 +26,6 @@ public:
DeviceManager& parent, std::shared_ptr<sscl::ComponentThread> ioThread);
~DeviceReattacher() = default;
// Non-copyable
DeviceReattacher(const DeviceReattacher&) = delete;
DeviceReattacher& operator=(const DeviceReattacher&) = delete;
@@ -31,12 +36,21 @@ public:
private:
void scheduleNextTimeout();
void onTimeout(const boost::system::error_code& error);
void holdReattachCReq();
mrntt::MrnttNonViralPostingInvoker reattachKnownListCReq(
std::exception_ptr &exceptionPtr,
std::function<void()> callback);
DeviceManager &parent;
std::shared_ptr<sscl::ComponentThread> ioThread;
sscl::SpinLock shouldContinueLock;
bool shouldContinue;
boost::asio::deadline_timer timer;
std::exception_ptr reattachLifetimeExceptionPtr;
std::optional<mrntt::MrnttNonViralPostingInvoker> reattachCReqInvoker;
bool reattachOpInFlight = false;
std::chrono::steady_clock::time_point lastReattachReqTimestamp{};
};
} // namespace device
+27
View File
@@ -0,0 +1,27 @@
#ifndef SMO_DIRECTOR_THREAD_H
#define SMO_DIRECTOR_THREAD_H
#include <boost/asio/io_service.hpp>
#include <spinscale/co/invokers.h>
#include <spinscale/co/postingPromise.h>
namespace smo {
namespace director {
struct DirectorThreadTag
{
static boost::asio::io_service &io_service();
};
template <typename T>
using DirectorPostingPromise =
sscl::co::TaggedPostingPromise<T, DirectorThreadTag>;
template <typename T>
using DirectorViralPostingInvoker =
sscl::co::ViralPostingInvoker<DirectorPostingPromise, T>;
} // namespace director
} // namespace smo
#endif // SMO_DIRECTOR_THREAD_H
+20 -8
View File
@@ -4,9 +4,12 @@
#include <boost/asio/signal_set.hpp>
#include <cstdint>
#include <atomic>
#include <exception>
#include <functional>
#include <memory>
#include <optional>
#include <spinscale/component.h>
#include <marionette/marionetteThread.h>
namespace sscl {
@@ -26,11 +29,17 @@ public:
{}
~MarionetteComponent() = default;
public:
typedef std::function<void(bool)> mrnttLifetimeMgmtOpCbFn;
void initializeReq(sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback);
void finalizeReq(sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback);
// Intentionally doesn't take a callback.
void holdInitializeCReq(std::function<void()> completion);
void holdFinalizeCReq(std::function<void()> completion);
MrnttNonViralPostingInvoker initializeCReq(
std::exception_ptr &exceptionPtr,
std::function<void()> callback);
MrnttNonViralPostingInvoker finalizeCReq(
std::exception_ptr &exceptionPtr,
std::function<void()> callback);
void exceptionInd();
void handleLoopExceptionHook() override;
@@ -47,11 +56,14 @@ protected:
void handleTryBlock1UnknownException() override;
private:
class MrnttLifetimeMgmtOp;
class TerminationEvent;
std::unique_ptr<boost::asio::signal_set> signals;
bool callShutdownSalmanoff = false;
std::optional<MrnttNonViralPostingInvoker> initializeCReqInvoker;
std::optional<MrnttNonViralPostingInvoker> finalizeCReqInvoker;
public:
std::exception_ptr initializeLifetimeExceptionPtr;
std::exception_ptr finalizeLifetimeExceptionPtr;
};
extern std::shared_ptr<sscl::PuppeteerThread> thread;
@@ -0,0 +1,37 @@
#ifndef SMO_MARIONETTE_THREAD_H
#define SMO_MARIONETTE_THREAD_H
#include <boost/asio/io_service.hpp>
#include <spinscale/co/invokers.h>
#include <spinscale/co/postingPromise.h>
namespace smo {
namespace mrntt {
struct MrnttThreadTag
{
static boost::asio::io_service &io_service() noexcept;
};
template <typename T>
using MrnttPostingPromise =
sscl::co::TaggedPostingPromise<T, MrnttThreadTag>;
using MrnttNonViralPostingInvoker =
sscl::co::NonViralPostingInvoker<MrnttPostingPromise>;
template <typename T>
using MrnttViralPostingInvoker =
sscl::co::ViralPostingInvoker<MrnttPostingPromise, T>;
using MrnttViralNonPostingInvoker =
sscl::co::ViralNonPostingInvoker<void>;
template <typename T>
using MrnttViralNonPostingInvokerT =
sscl::co::ViralNonPostingInvoker<T>;
} // namespace mrntt
} // namespace smo
#endif // SMO_MARIONETTE_THREAD_H
+3 -8
View File
@@ -2,16 +2,15 @@
#define _MIND_H
#include <config.h>
#include <functional>
#include <memory>
#include <string>
#include <spinscale/callback.h>
#include <spinscale/puppetApplication.h>
#include <spinscale/component.h>
#include <componentThread.h>
#include <mindThread.h>
#include <mindComponent.h>
#include <marionette/marionetteThread.h>
#include <director/director.h>
#include <simulator/simulator.h>
#include <body/body.h>
@@ -25,9 +24,8 @@ public:
Mind(void);
~Mind(void) = default;
typedef std::function<void(bool)> mindLifetimeMgmtOpCbFn;
void initializeReq(sscl::Callback<mindLifetimeMgmtOpCbFn> callback);
void finalizeReq(sscl::Callback<mindLifetimeMgmtOpCbFn> callback);
mrntt::MrnttViralNonPostingInvokerT<bool> initializeCReq();
mrntt::MrnttViralNonPostingInvokerT<bool> finalizeCReq();
// ComponentThread access methods
std::shared_ptr<MindThread> getComponentThread(sscl::ThreadId id) const;
@@ -46,9 +44,6 @@ public:
private:
friend class body::Body;
bool bodyComponentInitialized = false;
private:
class MindLifetimeMgmtOp;
};
namespace mind {
@@ -0,0 +1,27 @@
#ifndef SMO_SIMULATOR_THREAD_H
#define SMO_SIMULATOR_THREAD_H
#include <boost/asio/io_service.hpp>
#include <spinscale/co/invokers.h>
#include <spinscale/co/postingPromise.h>
namespace smo {
namespace simulator {
struct SimulatorThreadTag
{
static boost::asio::io_service &io_service();
};
template <typename T>
using SimulatorPostingPromise =
sscl::co::TaggedPostingPromise<T, SimulatorThreadTag>;
template <typename T>
using SimulatorViralPostingInvoker =
sscl::co::ViralPostingInvoker<SimulatorPostingPromise, T>;
} // namespace simulator
} // namespace smo
#endif // SMO_SIMULATOR_THREAD_H
+10 -4
View File
@@ -8,7 +8,8 @@
#include <dlfcn.h>
#include <functional>
#include <user/senseApiDesc.h>
#include <spinscale/qutex.h>
#include <spinscale/co/coQutex.h>
#include <spinscale/sharedResourceGroup.h>
namespace smo {
namespace stim_buff {
@@ -31,10 +32,11 @@ public:
StimBuffApiLib(
const std::string& path, void *_dlopen_handle,
SMO_GET_STIM_BUFF_API_DESC_FN_TYPEDEF *descFn)
: libraryPath(path), qutex("StimBuffApiLib-" + path),
: libraryPath(path),
isBeingDestroyed(false),
dlopen_handle(_dlopen_handle, DlCloser()),
SMO_GET_STIM_BUFF_API_DESC_FN_NAME(descFn)
SMO_GET_STIM_BUFF_API_DESC_FN_NAME(descFn),
s("StimBuffApiLib-" + path)
{}
void setStimBuffApiDesc(const StimBuffApiDesc &desc)
@@ -51,7 +53,6 @@ public:
public:
std::string libraryPath;
sscl::Qutex qutex;
std::atomic<bool> isBeingDestroyed;
std::unique_ptr<void, DlCloser> dlopen_handle;
/* UNIMPLEMENTED: API-specific cmdline options. These affect this specific
@@ -77,6 +78,11 @@ public:
*/
StimBuffApiDesc stimBuffApiDesc;
struct StimBuffApiLibResources
{};
sscl::SharedResourceGroup<sscl::co::CoQutex, StimBuffApiLibResources> s;
std::string stringify() const {
std::string result = "Library Path: " + libraryPath + "\n";
result += "Stim Buff API Descriptor: " + stimBuffApiDesc.stringify() + "\n";
@@ -7,11 +7,10 @@
#include <string>
#include <optional>
#include <functional>
#include <spinscale/asynchronousLoop.h>
#include <stimBuffApis/stimBuffApiLib.h>
#include <user/deviceAttachmentSpec.h>
#include <spinscale/callback.h>
#include <spinscale/qutex.h>
#include <spinscale/co/coQutex.h>
#include <spinscale/sharedResourceGroup.h>
namespace smo {
namespace stim_buff {
@@ -19,6 +18,11 @@ namespace stim_buff {
class StimBuffApiManager
{
public:
struct Resources
{
std::vector<std::shared_ptr<StimBuffApiLib>> stimBuffApiLibs;
};
static StimBuffApiManager& getInstance()
{
static StimBuffApiManager instance;
@@ -54,17 +58,16 @@ public:
private:
StimBuffApiManager()
: qutex("StimBuffApiManager")
: s("StimBuffApiManager")
{}
~StimBuffApiManager() = default;
StimBuffApiManager(const StimBuffApiManager&) = delete;
StimBuffApiManager& operator=(const StimBuffApiManager&) = delete;
std::vector<std::shared_ptr<StimBuffApiLib>> stimBuffApiLibs;
public:
sscl::Qutex qutex;
sscl::SharedResourceGroup<sscl::co::CoQutex, Resources> s;
public:
static std::optional<std::string> searchForLibInSmoSearchPaths(
+25
View File
@@ -0,0 +1,25 @@
#ifndef SMO_SUBCONSCIOUS_THREAD_H
#define SMO_SUBCONSCIOUS_THREAD_H
#include <boost/asio/io_service.hpp>
#include <spinscale/co/invokers.h>
#include <spinscale/co/postingPromise.h>
namespace smo {
struct SubconsciousThreadTag
{
static boost::asio::io_service &io_service();
};
template <typename T>
using SubconsciousPostingPromise =
sscl::co::TaggedPostingPromise<T, SubconsciousThreadTag>;
template <typename T>
using SubconsciousViralPostingInvoker =
sscl::co::ViralPostingInvoker<SubconsciousPostingPromise, T>;
} // namespace smo
#endif // SMO_SUBCONSCIOUS_THREAD_H
+25
View File
@@ -0,0 +1,25 @@
#ifndef SMO_WORLD_THREAD_H
#define SMO_WORLD_THREAD_H
#include <boost/asio/io_service.hpp>
#include <spinscale/co/invokers.h>
#include <spinscale/co/postingPromise.h>
namespace smo {
struct WorldThreadTag
{
static boost::asio::io_service &io_service();
};
template <typename T>
using WorldPostingPromise =
sscl::co::TaggedPostingPromise<T, WorldThreadTag>;
template <typename T>
using WorldViralPostingInvoker =
sscl::co::ViralPostingInvoker<WorldPostingPromise, T>;
} // namespace smo
#endif // SMO_WORLD_THREAD_H
+59 -158
View File
@@ -1,63 +1,61 @@
#include <cstdlib>
#include <iostream>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/asynchronousLoop.h>
#include <spinscale/callback.h>
#include <spinscale/callableTracer.h>
#include <spinscale/component.h>
#include <marionette/marionette.h>
#include <stdexcept>
#include <boost/asio/post.hpp>
#include <componentThread.h>
#include <deviceManager/deviceManager.h>
#include <marionette/marionette.h>
#include <marionette/marionetteThread.h>
#include <mindManager/mindManager.h>
#include <spinscale/componentThread.h>
namespace smo {
namespace mrntt {
class MarionetteComponent::MrnttLifetimeMgmtOp
: public sscl::PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>
namespace {
void assertMarionetteThread()
{
public:
MrnttLifetimeMgmtOp(
MarionetteComponent &parent,
const std::shared_ptr<sscl::ComponentThread> &caller,
sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback)
: sscl::PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>(
caller, callback),
parent(parent)
{}
private:
MarionetteComponent &parent;
public:
void initializeReq1_posted(
[[maybe_unused]] std::shared_ptr<MrnttLifetimeMgmtOp> context
)
{
auto self = sscl::ComponentThread::getSelf();
if (self->id != smo::SmoThreadId::MRNTT)
if (self->id != SmoThreadId::MRNTT)
{
throw std::runtime_error(std::string(__func__)
throw std::runtime_error(
std::string(__func__)
+ ": Must be executed on Marionette thread");
}
}
} // namespace
void MarionetteComponent::holdInitializeCReq(
std::function<void()> completion)
{
initializeCReqInvoker.emplace(initializeCReq(
initializeLifetimeExceptionPtr, std::move(completion)));
}
void MarionetteComponent::holdFinalizeCReq(
std::function<void()> completion)
{
finalizeCReqInvoker.emplace(finalizeCReq(
finalizeLifetimeExceptionPtr, std::move(completion)));
}
MrnttNonViralPostingInvoker MarionetteComponent::initializeCReq(
[[maybe_unused]] std::exception_ptr &exceptionPtr,
[[maybe_unused]] std::function<void()> callback)
{
assertMarionetteThread();
smo::mind::globalMind = std::make_shared<smo::Mind>();
smo::mind::globalMind->initializeReq({context, std::bind(
&MrnttLifetimeMgmtOp::initializeReq2,
this, context, std::placeholders::_1)});
}
void initializeReq2(
std::shared_ptr<MrnttLifetimeMgmtOp> context,
bool success
)
{
if (!success)
bool mindInitialized = co_await smo::mind::globalMind->initializeCReq();
if (!mindInitialized)
{
std::cerr << __func__ << ": Failed to initialize globalMind"
<< std::endl;
context->callOriginalCb(false);
return;
co_return;
}
smo::device::DeviceManager::getInstance().initializeDeviceReattacher();
@@ -65,126 +63,30 @@ public:
// Call negtrinEventInd on the Director in the final callback
smo::mind::globalMind->director.negtrinEventInd();
context->callOriginalCb(success);
}
co_return;
}
void finalizeReq1_posted(
[[maybe_unused]] std::shared_ptr<MrnttLifetimeMgmtOp> context
)
{
auto self = sscl::ComponentThread::getSelf();
if (self->id != smo::SmoThreadId::MRNTT)
{
throw std::runtime_error(std::string(__func__)
+ ": Must be executed on Marionette thread");
}
MrnttNonViralPostingInvoker MarionetteComponent::finalizeCReq(
[[maybe_unused]] std::exception_ptr &exceptionPtr,
[[maybe_unused]] std::function<void()> callback)
{
assertMarionetteThread();
smo::device::DeviceManager::getInstance().finalizeDeviceReattacher();
/** FIXME:
* It may be necessary to add a delay here to ensure that all in-flight
* timer timeouts have finished executing? Or some other mechanism.
*
* We need some way to ensure that in-flight timeouts don't get fired
* during the finalize sequence. This is because they may depend on
* state that is being finalized or has been finalized at the point
* when they timeout.
*
* This seems to be actually happening with the delayed calls to
* AttachDeviceReq::attachDeviceReq2() inside of livoxGen1.cpp.
*
* One tactic might be to shut down device reattacher before finalizing
* and pause for a bit before continuing to shutdown other components.
*/
smo::mind::globalMind->finalizeReq({context, std::bind(
&MrnttLifetimeMgmtOp::finalizeReq2,
this, context, std::placeholders::_1)});
if (!smo::mind::globalMind)
{
co_return;
}
void finalizeReq2(
std::shared_ptr<MrnttLifetimeMgmtOp> context,
bool success
)
{
if (!success)
bool mindFinalized = co_await smo::mind::globalMind->finalizeCReq();
if (!mindFinalized)
{
std::cerr << __func__ << ": globalMind finalization failed"
<< std::endl;
context->callOriginalCb(false);
return;
}
context->callOriginalCb(success);
}
};
class MarionetteComponent::TerminationEvent
: public sscl::PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>
{
public:
TerminationEvent(
const std::shared_ptr<sscl::ComponentThread> &caller)
: sscl::PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>(
caller, {nullptr, nullptr})
{}
public:
void exceptionInd1_posted(
[[maybe_unused]] std::shared_ptr<TerminationEvent> context
)
{
auto self = sscl::ComponentThread::getSelf();
if (self->id != smo::SmoThreadId::MRNTT)
{
throw std::runtime_error(std::string(__func__)
+ ": Must be executed on Marionette thread");
}
smo::mrntt::mrntt.finalizeReq({nullptr, std::bind(
&smo::mrntt::marionetteFinalizeReqCb,
std::placeholders::_1)});
}
};
void MarionetteComponent::initializeReq(
sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback)
{
auto mrntt = sscl::ComponentThread::getSelf();
if (mrntt->id != smo::SmoThreadId::MRNTT)
{
throw std::runtime_error(std::string(__func__)
+ ": Must be executed on Marionette thread");
}
auto request = std::make_shared<MrnttLifetimeMgmtOp>(
*this, mrntt, callback);
mrntt->getIoService().post(
STC(std::bind(
&MrnttLifetimeMgmtOp::initializeReq1_posted,
request.get(), request)));
}
void MarionetteComponent::finalizeReq(
sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback)
{
auto mrntt = sscl::ComponentThread::getSelf();
if (mrntt->id != smo::SmoThreadId::MRNTT)
{
throw std::runtime_error(std::string(__func__)
+ ": Must be executed on Marionette thread");
}
auto request = std::make_shared<MrnttLifetimeMgmtOp>(
*this, mrntt, callback);
mrntt->getIoService().post(
STC(std::bind(
&MrnttLifetimeMgmtOp::finalizeReq1_posted,
request.get(), request)));
co_return;
}
void MarionetteComponent::handleLoopExceptionHook()
@@ -195,16 +97,15 @@ void MarionetteComponent::handleLoopExceptionHook()
void MarionetteComponent::exceptionInd()
{
auto faultyThread = sscl::ComponentThread::getSelf();
auto mrntt = sscl::ComponentThread::getPptr();
auto puppeteer = sscl::ComponentThread::getPptr();
auto request = std::make_shared<TerminationEvent>(
faultyThread);
mrntt->getIoService().post(
STC(std::bind(
&TerminationEvent::exceptionInd1_posted,
request.get(), request)));
boost::asio::post(
puppeteer->getIoService(),
[]
{
mrntt.holdFinalizeCReq(
[]() { marionetteFinalizeReqCb(true); });
});
}
} // namespace mrntt
+10 -9
View File
@@ -33,9 +33,8 @@ void marionetteInitializeReqCb(bool success)
std::cerr << __func__ << ": Failed to initialize Marionette. Shutting down."
<< '\n';
mrntt.finalizeReq({nullptr, std::bind(
&smo::mrntt::marionetteFinalizeReqCb,
std::placeholders::_1)});
mrntt.holdFinalizeCReq(
[]() { marionetteFinalizeReqCb(true); });
}
void marionetteFinalizeReqCb(bool success)
@@ -83,9 +82,8 @@ void MarionetteComponent::postJoltHook()
default:
break;
}
mrntt.finalizeReq({nullptr, std::bind(
&marionetteFinalizeReqCb,
std::placeholders::_1)});
mrntt.holdFinalizeCReq(
[]() { marionetteFinalizeReqCb(true); });
});
}
@@ -131,9 +129,12 @@ void MarionetteComponent::preLoopHook()
smo::initializeSalmanoff();
callShutdownSalmanoff = true;
initializeReq(sscl::Callback<mrnttLifetimeMgmtOpCbFn>{
nullptr,
std::bind(&marionetteInitializeReqCb, std::placeholders::_1)});
holdInitializeCReq(
[]
{
marionetteInitializeReqCb(
!mrntt.initializeLifetimeExceptionPtr);
});
std::cout << "PuppeteerThread::main: Entering event loop" << "\n";
}
+24 -134
View File
@@ -2,11 +2,6 @@
#include <iostream>
#include <opts.h>
#include <componentThread.h>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/asynchronousLoop.h>
#include <spinscale/callback.h>
#include <spinscale/callableTracer.h>
#include <spinscale/componentThread.h>
#include <mind.h>
#include <mindThread.h>
#include <director/director.h>
@@ -111,121 +106,8 @@ Mind::getMindThreads() const
return mindThreads;
}
class Mind::MindLifetimeMgmtOp
: public sscl::PostedAsynchronousContinuation<mindLifetimeMgmtOpCbFn>
mrntt::MrnttViralNonPostingInvokerT<bool> Mind::initializeCReq()
{
public:
MindLifetimeMgmtOp(
Mind &parent, const std::shared_ptr<sscl::ComponentThread> &caller,
sscl::Callback<mindLifetimeMgmtOpCbFn> callback)
: sscl::PostedAsynchronousContinuation<mindLifetimeMgmtOpCbFn>(
caller, callback),
parent(parent)
{}
public:
Mind &parent;
public:
void initializeReq1_posted(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
/* Jolt the threads, then start them */
parent.joltAllPuppetThreadsReq(
{context, std::bind(
&MindLifetimeMgmtOp::initializeReq2,
context.get(), context)});
}
void initializeReq2(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
std::cout << "Mrntt: All mind threads JOLTed." << "\n";
parent.startAllPuppetThreadsReq(
{context, std::bind(
&MindLifetimeMgmtOp::initializeReq3,
context.get(), context)});
}
void initializeReq3(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
std::cout << "Mrntt: All mind threads started." << "\n";
parent.body.initializeReq(
{context, std::bind(
&MindLifetimeMgmtOp::initializeReq4,
context.get(), context, std::placeholders::_1)});
}
void initializeReq4(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context,
bool success
)
{
std::cout << "Mrntt: Body component initialized." << "\n";
callOriginalCb(success);
}
void finalizeReq1_posted(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
parent.body.finalizeReq(
{context, std::bind(
&MindLifetimeMgmtOp::finalizeReq2,
context.get(), context, std::placeholders::_1)});
}
void finalizeReq2(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context,
bool success
)
{
if (!success) {
std::cerr << "Mrntt: Body component failed to finalize." << "\n";
} else {
std::cout << "Mrntt: Body component finalized." << "\n";
}
/* If the threads haven't been jolted, we need to do that first, because
* otherwise they'll just enter their main loops and wait for control
* messages from mrntt after processing the exit request.
*/
parent.joltAllPuppetThreadsReq(
{context, std::bind(
&MindLifetimeMgmtOp::finalizeReq3,
context.get(), context)});
}
void finalizeReq3(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
std::cout << "Mrntt: All mind threads JOLTed for finalization." << "\n";
parent.exitAllPuppetThreadsReq(
{context, std::bind(
&MindLifetimeMgmtOp::finalizeReq4,
context.get(), context)});
}
void finalizeReq4(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
std::cout << "Mrntt: All mind threads exited." << "\n";
callOriginalCb(true);
}
};
void Mind::initializeReq(sscl::Callback<mindLifetimeMgmtOpCbFn> callback)
{
/* Distribute threads across available CPUs */
try
{
distributeAndPinThreadsAcrossCpus();
@@ -237,26 +119,34 @@ void Mind::initializeReq(sscl::Callback<mindLifetimeMgmtOpCbFn> callback)
"Error: " << e.what() << "\n";
}
const auto& caller = sscl::ComponentThread::getSelf();
auto request = std::make_shared<MindLifetimeMgmtOp>(
*this, caller, callback);
co_await joltAllPuppetThreadsCReq();
std::cout << "Mrntt: All mind threads JOLTed." << "\n";
mrntt::mrntt.thread->getIoService().post(
STC(std::bind(
&MindLifetimeMgmtOp::initializeReq1_posted,
request.get(), request)));
co_await startAllPuppetThreadsCReq();
std::cout << "Mrntt: All mind threads started." << "\n";
bool bodyInitialized = co_await body.initializeCReq();
std::cout << "Mrntt: Body component initialized." << "\n";
co_return bodyInitialized;
}
void Mind::finalizeReq(sscl::Callback<mindLifetimeMgmtOpCbFn> callback)
mrntt::MrnttViralNonPostingInvokerT<bool> Mind::finalizeCReq()
{
const auto& caller = sscl::ComponentThread::getSelf();
auto request = std::make_shared<MindLifetimeMgmtOp>(
*this, caller, callback);
bool bodyFinalized = co_await body.finalizeCReq();
if (!bodyFinalized) {
std::cerr << "Mrntt: Body component failed to finalize." << "\n";
} else {
std::cout << "Mrntt: Body component finalized." << "\n";
}
mrntt::mrntt.thread->getIoService().post(
STC(std::bind(
&MindLifetimeMgmtOp::finalizeReq1_posted,
request.get(), request)));
co_await joltAllPuppetThreadsCReq();
std::cout << "Mrntt: All mind threads JOLTed for finalization." << "\n";
co_await exitAllPuppetThreadsCReq();
std::cout << "Mrntt: All mind threads exited." << "\n";
co_return bodyFinalized;
}
} // namespace smo
+2 -2
View File
@@ -7,7 +7,7 @@
#include <sys/stat.h>
#include <sstream>
#include <opts.h>
#include <spinscale/callableTracer.h>
#include <spinscale/cps/callableTracer.h>
OptionParser::Exception::Exception(std::string message_)
@@ -107,7 +107,7 @@ void OptionParser::parseArguments(int argc, char *argv[], char **envp)
}
}
sscl::CallableTracer::optTraceCallables = traceCallables;
sscl::cps::CallableTracer::optTraceCallables = traceCallables;
}
std::string OptionParser::getUsage() const
+16 -17
View File
@@ -5,10 +5,6 @@
#include <stimBuffApis/stimBuffApiManager.h>
#include <stimBuffApis/stimBuffApiLib.h>
#include <opts.h>
#include <spinscale/asynchronousBridge.h>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/asynchronousLoop.h>
#include <spinscale/callback.h>
#include <user/senseApiDesc.h>
#include <mind.h>
#include <deviceManager/deviceManager.h>
@@ -198,47 +194,50 @@ StimBuffApiLib& StimBuffApiManager::loadStimBuffApiLib(
auto lib = std::make_shared<StimBuffApiLib>(
libraryPath, dlopen_handle.release(), func);
lib->setStimBuffApiDesc(libApiDesc);
stimBuffApiLibs.push_back(lib);
return *stimBuffApiLibs.back();
getInstance().s.rsrc.stimBuffApiLibs.push_back(lib);
return *getInstance().s.rsrc.stimBuffApiLibs.back();
}
std::optional<std::shared_ptr<StimBuffApiLib>>
StimBuffApiManager::getStimBuffApiLib(const std::string& libraryPath)
{
auto it = std::find_if(stimBuffApiLibs.begin(), stimBuffApiLibs.end(),
auto &libs = getInstance().s.rsrc.stimBuffApiLibs;
auto it = std::find_if(libs.begin(), libs.end(),
[&libPath = libraryPath](const std::shared_ptr<StimBuffApiLib>& lib) {
return lib->libraryPath == libPath;
}
);
if (it != stimBuffApiLibs.end()) { return *it; }
if (it != libs.end()) { return *it; }
return std::nullopt;
}
std::optional<std::shared_ptr<StimBuffApiLib>>
StimBuffApiManager::getStimBuffApiLibByApiName(const std::string& apiName)
{
auto it = std::find_if(stimBuffApiLibs.begin(), stimBuffApiLibs.end(),
auto &libs = getInstance().s.rsrc.stimBuffApiLibs;
auto it = std::find_if(libs.begin(), libs.end(),
[&apiName](const std::shared_ptr<StimBuffApiLib>& lib) {
return lib->stimBuffApiDesc.name == apiName;
}
);
if (it != stimBuffApiLibs.end()) { return *it; }
if (it != libs.end()) { return *it; }
return std::nullopt;
}
void StimBuffApiManager::unloadStimBuffApiLib(const std::string& libraryPath)
{
auto it = std::find_if(stimBuffApiLibs.begin(), stimBuffApiLibs.end(),
auto &libs = getInstance().s.rsrc.stimBuffApiLibs;
auto it = std::find_if(libs.begin(), libs.end(),
[&lpath = libraryPath](const std::shared_ptr<StimBuffApiLib>& lib) {
return lib->libraryPath == lpath;
}
);
if (it != stimBuffApiLibs.end())
if (it != libs.end())
{
stimBuffApiLibs.erase(it);
libs.erase(it);
return;
}
@@ -248,7 +247,7 @@ void StimBuffApiManager::unloadStimBuffApiLib(const std::string& libraryPath)
void StimBuffApiManager::unloadAllStimBuffApiLibs(void)
{
stimBuffApiLibs.clear();
getInstance().s.rsrc.stimBuffApiLibs.clear();
}
void StimBuffApiManager::loadAllStimBuffApiLibsFromOptions(
@@ -264,7 +263,7 @@ void StimBuffApiManager::loadAllStimBuffApiLibsFromOptions(
std::string StimBuffApiManager::stringifyLibs() const
{
std::string result;
for (const auto& lib : stimBuffApiLibs) {
for (const auto& lib : getInstance().s.rsrc.stimBuffApiLibs) {
result += lib->stringify() + "\n";
}
return result;
@@ -303,14 +302,14 @@ void StimBuffApiManager::finalizeStimBuffApiLib(StimBuffApiLib& lib)
void StimBuffApiManager::initializeAllStimBuffApiLibs(void)
{
for (auto& lib : stimBuffApiLibs) {
for (auto& lib : getInstance().s.rsrc.stimBuffApiLibs) {
initializeStimBuffApiLib(*lib);
}
}
void StimBuffApiManager::finalizeAllStimBuffApiLibs(void)
{
for (auto& lib : stimBuffApiLibs) {
for (auto& lib : getInstance().s.rsrc.stimBuffApiLibs) {
finalizeStimBuffApiLib(*lib);
}
}
@@ -17,11 +17,11 @@
#include <boost/system/error_code.hpp>
#include <livoxProto1/device.h>
#include <livoxProto1/livoxProto1.h>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/cps/asynchronousContinuation.h>
#include <spinscale/asynchronousLoop.h>
#include <spinscale/asynchronousBridge.h>
#include <spinscale/callback.h>
#include <spinscale/callableTracer.h>
#include <spinscale/cps/asynchronousBridge.h>
#include <spinscale/cps/callback.h>
#include <spinscale/cps/callableTracer.h>
#include <spinscale/spinLock.h>
#include "ioUringAssemblyEngine.h"
#include "pcloudStimulusProducer.h"
@@ -175,7 +175,7 @@ void IoUringAssemblyEngine::finalize()
{
auto& ioService = smoHooksPtr->ComponentThread_getSelf()->getIoService();
sscl::AsynchronousBridge bridge(ioService);
sscl::cps::AsynchronousBridge bridge(ioService);
boost::asio::deadline_timer timeoutTimer(ioService);
/** EXPLANATION:
@@ -420,15 +420,15 @@ cleanup_eventfd:
// Continuation class for assembleFrameReq
class IoUringAssemblyEngine::AssembleFrameReq
: public sscl::PostedAsynchronousContinuation<
: public sscl::cps::PostedAsynchronousContinuation<
IoUringAssemblyEngine::assembleFrameReqCbFn>
{
public:
AssembleFrameReq(
IoUringAssemblyEngine& engine_,
const std::shared_ptr<sscl::ComponentThread>& caller,
sscl::Callback<IoUringAssemblyEngine::assembleFrameReqCbFn> cb)
: sscl::PostedAsynchronousContinuation<
sscl::cps::Callback<IoUringAssemblyEngine::assembleFrameReqCbFn> cb)
: sscl::cps::PostedAsynchronousContinuation<
IoUringAssemblyEngine::assembleFrameReqCbFn>(caller, cb),
engine(engine_),
loop(engine_.frameAssemblyDesc->numSlots),
@@ -635,7 +635,7 @@ public:
};
void IoUringAssemblyEngine::assembleFrameReq(
sscl::Callback<assembleFrameReqCbFn> cb)
sscl::cps::Callback<assembleFrameReqCbFn> cb)
{
{
sscl::SpinLock::Guard lock(shouldAcceptRequestsLock);
@@ -16,9 +16,9 @@
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/posix/stream_descriptor.hpp>
#include <livoxProto1/device.h>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/cps/asynchronousContinuation.h>
#include <spinscale/asynchronousLoop.h>
#include <spinscale/callback.h>
#include <spinscale/cps/callback.h>
#include <spinscale/spinLock.h>
#include <user/frameAssemblyDesc.h>
#include <user/stagingBuffer.h>
@@ -42,7 +42,7 @@ public:
void finalize();
typedef std::function<void(bool, sscl::AsynchronousLoop)> assembleFrameReqCbFn;
void assembleFrameReq(sscl::Callback<assembleFrameReqCbFn> cb);
void assembleFrameReq(sscl::cps::Callback<assembleFrameReqCbFn> cb);
// Telemetry helpers
static size_t computePointsPerFrame(int returnMode, size_t nDgramsPerFrame)
+10 -10
View File
@@ -11,11 +11,11 @@
#include <user/senseApiDesc.h>
#include <user/deviceAttachmentSpec.h>
#include <user/intrinThresholdParams.h>
#include <spinscale/callback.h>
#include <spinscale/cps/callback.h>
#include <livoxProto1/livoxProto1.h>
#include <livoxProto1/device.h>
#include <livoxProto1/protocol.h>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/cps/asynchronousContinuation.h>
#include <boost/asio/deadline_timer.hpp>
#include "pcloudStimulusProducer.h"
#include "livoxGen1.h"
@@ -91,13 +91,13 @@ LivoxProto1DllState livoxProto1;
// Continuation classes for async operations
class AttachDeviceReq
: public sscl::NonPostedAsynchronousContinuation<sal_mlo_attachDeviceReqCbFn>
: public sscl::cps::NonPostedAsynchronousContinuation<sal_mlo_attachDeviceReqCbFn>
{
public:
AttachDeviceReq(
const std::shared_ptr<smo::device::DeviceAttachmentSpec>& spec,
sscl::Callback<sal_mlo_attachDeviceReqCbFn> cb)
: sscl::NonPostedAsynchronousContinuation<sal_mlo_attachDeviceReqCbFn>(
sscl::cps::Callback<sal_mlo_attachDeviceReqCbFn> cb)
: sscl::cps::NonPostedAsynchronousContinuation<sal_mlo_attachDeviceReqCbFn>(
std::move(cb)),
spec(spec)
{}
@@ -360,14 +360,14 @@ public:
};
class DetachDeviceReq
: public sscl::NonPostedAsynchronousContinuation<sal_mlo_detachDeviceReqCbFn>
: public sscl::cps::NonPostedAsynchronousContinuation<sal_mlo_detachDeviceReqCbFn>
{
public:
DetachDeviceReq(
const std::shared_ptr<smo::device::DeviceAttachmentSpec>& spec,
const std::shared_ptr<StimulusBuffer>& stimBuffer,
sscl::Callback<sal_mlo_detachDeviceReqCbFn> cb)
: sscl::NonPostedAsynchronousContinuation<sal_mlo_detachDeviceReqCbFn>(
sscl::cps::Callback<sal_mlo_detachDeviceReqCbFn> cb)
: sscl::cps::NonPostedAsynchronousContinuation<sal_mlo_detachDeviceReqCbFn>(
std::move(cb)),
spec(spec), stimBuffer(stimBuffer)
{}
@@ -625,7 +625,7 @@ extern "C" int livoxGen1_finalizeInd(void)
extern "C" void livoxGen1_attachDeviceReq(
const std::shared_ptr<smo::device::DeviceAttachmentSpec>& desc,
const std::shared_ptr<sscl::ComponentThread>& componentThread,
sscl::Callback<smo::stim_buff::sal_mlo_attachDeviceReqCbFn> cb
sscl::cps::Callback<smo::stim_buff::sal_mlo_attachDeviceReqCbFn> cb
)
{
if (!livoxProto1.livoxProto1_getOrCreateDeviceReq)
@@ -825,7 +825,7 @@ extern "C" void livoxGen1_attachDeviceReq(
extern "C" void livoxGen1_detachDeviceReq(
const std::shared_ptr<smo::device::DeviceAttachmentSpec>& desc,
sscl::Callback<smo::stim_buff::sal_mlo_detachDeviceReqCbFn> cb
sscl::cps::Callback<smo::stim_buff::sal_mlo_detachDeviceReqCbFn> cb
)
{
// Case 1: Check if StimBuffer doesn't exist (early return)
@@ -9,9 +9,9 @@
#include <algorithm>
#include <boost/system/error_code.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/asynchronousBridge.h>
#include <spinscale/callback.h>
#include <spinscale/cps/asynchronousContinuation.h>
#include <spinscale/cps/asynchronousBridge.h>
#include <spinscale/cps/callback.h>
#include <spinscale/asynchronousLoop.h>
#include <componentThread.h>
#include <user/stimulusFrame.h>
@@ -229,7 +229,7 @@ void OpenClCollatingAndMeshingEngine::finalize()
int delayMs = std::max(OCLCOLLMESH_ENGN_FINALIZE_DELAY_MS, 0);
auto& ioService = smoHooksPtr->ComponentThread_getSelf()->getIoService();
sscl::AsynchronousBridge bridge(ioService);
sscl::cps::AsynchronousBridge bridge(ioService);
boost::asio::deadline_timer timeoutTimer(ioService);
/** EXPLANATION:
@@ -1008,7 +1008,7 @@ void OpenClCollatingAndMeshingEngine::produceAmbienceStimulusFrame(
}
class OpenClCollatingAndMeshingEngine::CompactCollateAndMeshFrameReq
: public sscl::PostedAsynchronousContinuation<compactCollateAndMeshFrameReqCbFn>
: public sscl::cps::PostedAsynchronousContinuation<compactCollateAndMeshFrameReqCbFn>
{
private:
OpenClCollatingAndMeshingEngine& engine;
@@ -1027,8 +1027,8 @@ public:
std::optional<AmbienceProductionDesc> lightAmbienceProductionDesc_,
std::optional<AmbienceProductionDesc> darkAmbienceProductionDesc_,
const std::shared_ptr<sscl::ComponentThread>& caller,
sscl::Callback<compactCollateAndMeshFrameReqCbFn> cb)
: sscl::PostedAsynchronousContinuation<compactCollateAndMeshFrameReqCbFn>(
sscl::cps::Callback<compactCollateAndMeshFrameReqCbFn> cb)
: sscl::cps::PostedAsynchronousContinuation<compactCollateAndMeshFrameReqCbFn>(
caller, cb),
engine(engine_),
frameAssemblyResult(asyncLoop), stimulusFrame(stimulusFrame_),
@@ -1253,7 +1253,7 @@ void OpenClCollatingAndMeshingEngine::compactCollateAndMeshFrameReq(
std::optional<std::reference_wrapper<StimulusFrame>> intensityStimFrame,
std::optional<AmbienceProductionDesc> lightAmbienceProductionDesc,
std::optional<AmbienceProductionDesc> darkAmbienceProductionDesc,
sscl::Callback<compactCollateAndMeshFrameReqCbFn> callback)
sscl::cps::Callback<compactCollateAndMeshFrameReqCbFn> callback)
{
{
sscl::SpinLock::Guard lock(shouldAcceptRequestsLock);
@@ -14,7 +14,7 @@
#define CL_TARGET_OPENCL_VERSION 120
#include <CL/cl.h>
#include <spinscale/asynchronousLoop.h>
#include <spinscale/callback.h>
#include <spinscale/cps/callback.h>
#include <spinscale/spinLock.h>
#include <user/stimulusFrame.h>
#include <user/stagingBuffer.h>
@@ -94,7 +94,7 @@ public:
std::optional<std::reference_wrapper<StimulusFrame>> intensityStimFrame,
std::optional<AmbienceProductionDesc> lightAmbienceProductionDesc,
std::optional<AmbienceProductionDesc> darkAmbienceProductionDesc,
sscl::Callback<compactCollateAndMeshFrameReqCbFn> callback);
sscl::cps::Callback<compactCollateAndMeshFrameReqCbFn> callback);
private:
// Callback function types
@@ -426,7 +426,7 @@ void PcloudStimulusProducer::stimFrameProductionTimesliceInd()
}
class PcloudStimulusProducer::ProduceFrameReq
: public sscl::PostedAsynchronousContinuation<produceFrameReqCbFn>
: public sscl::cps::PostedAsynchronousContinuation<produceFrameReqCbFn>
{
private:
PcloudStimulusProducer& pcloudProducer;
@@ -440,8 +440,8 @@ public:
ProduceFrameReq(
PcloudStimulusProducer& producer,
const std::shared_ptr<sscl::ComponentThread>& caller,
sscl::Callback<produceFrameReqCbFn> cb)
: sscl::PostedAsynchronousContinuation<produceFrameReqCbFn>(caller, cb),
sscl::cps::Callback<produceFrameReqCbFn> cb)
: sscl::cps::PostedAsynchronousContinuation<produceFrameReqCbFn>(caller, cb),
pcloudProducer(producer),
frameAssemblyResult(0),
stimulusFrame(producer.tempStimulusFrame)
@@ -724,7 +724,7 @@ public:
};
void PcloudStimulusProducer::produceFrameReq(
sscl::Callback<produceFrameReqCbFn> callback)
sscl::cps::Callback<produceFrameReqCbFn> callback)
{
/** EXPLANATION:
* We shouldn't acquire the StimulusProducer::shouldContinueLock here because
@@ -6,8 +6,8 @@
#include <user/stimulusProducer.h>
#include <user/stimulusFrame.h>
#include <livoxProto1/device.h>
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/callback.h>
#include <spinscale/cps/asynchronousContinuation.h>
#include <spinscale/cps/callback.h>
#include <user/stagingBuffer.h>
#include "ioUringAssemblyEngine.h"
#include "livoxPcloudFrameDumper.h"
@@ -87,7 +87,7 @@ protected:
typedef std::function<void()> produceFrameReqCbFn;
public:
void produceFrameReq(sscl::Callback<produceFrameReqCbFn> callback);
void produceFrameReq(sscl::cps::Callback<produceFrameReqCbFn> callback);
size_t nDgramsPerStagingBufferFrame;
std::shared_ptr<livoxProto1::Device> device;
+3 -3
View File
@@ -8,7 +8,7 @@
#include <xcb/xcb.h>
#include <user/senseApiDesc.h>
#include <user/deviceAttachmentSpec.h>
#include <spinscale/callback.h>
#include <spinscale/cps/callback.h>
#include <xcbXorg/xcbXorg.h>
#include "xcbWindow.h"
@@ -277,7 +277,7 @@ static int xcbWindow_finalizeInd(void)
static void xcbWindow_attachDeviceReq(
const std::shared_ptr<smo::device::DeviceAttachmentSpec>& desc,
const std::shared_ptr<sscl::ComponentThread>& componentThread,
sscl::Callback<smo::stim_buff::sal_mlo_attachDeviceReqCbFn> cb
sscl::cps::Callback<smo::stim_buff::sal_mlo_attachDeviceReqCbFn> cb
)
{
// Not used yet, but may be used later.
@@ -302,7 +302,7 @@ static void xcbWindow_attachDeviceReq(
static void xcbWindow_detachDeviceReq(
const std::shared_ptr<smo::device::DeviceAttachmentSpec>& spec,
sscl::Callback<smo::stim_buff::sal_mlo_detachDeviceReqCbFn> cb
sscl::cps::Callback<smo::stim_buff::sal_mlo_detachDeviceReqCbFn> cb
)
{
auto it = std::find_if(g_attachedWindows.begin(), g_attachedWindows.end(),
+9 -9
View File
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include <spinscale/qutex.h>
#include <spinscale/lockerAndInvokerBase.h>
#include <spinscale/cps/qutex.h>
#include <spinscale/cps/lockerAndInvokerBase.h>
#include <memory>
#include <thread>
#include <chrono>
@@ -9,18 +9,18 @@
namespace smo {
// Mock implementation of LockerAndInvokerBase for testing
class MockLockerAndInvoker : public LockerAndInvokerBase {
class MockLockerAndInvoker : public sscl::cps::LockerAndInvokerBase {
public:
explicit MockLockerAndInvoker(const void* addr)
: LockerAndInvokerBase(addr), awakened(false) {}
: sscl::cps::LockerAndInvokerBase(addr), awakened(false) {}
bool awakened;
sscl::Qutex* registeredQutex = nullptr;
List::iterator queueIterator;
sscl::cps::Qutex* registeredQutex = nullptr;
sscl::cps::LockerAndInvokerBase::List::iterator queueIterator;
List::iterator getLockvokerIteratorForQutex(sscl::Qutex& qutex) override {
sscl::cps::LockerAndInvokerBase::List::iterator getLockvokerIteratorForQutex(sscl::cps::Qutex& qutex) override {
registeredQutex = &qutex;
queueIterator = qutex.registerInQueue(std::shared_ptr<LockerAndInvokerBase>(this));
queueIterator = qutex.registerInQueue(std::shared_ptr<sscl::cps::LockerAndInvokerBase>(this));
return queueIterator;
}
@@ -44,7 +44,7 @@ protected:
// Clean up
}
sscl::Qutex qutex;
sscl::cps::Qutex qutex;
std::shared_ptr<MockLockerAndInvoker> mock1, mock2, mock3, mock4, mock5;
// Unique addresses for testing