Woman, Life, Freedom


Adaptive AUTOSAR
ARA public interface header documentation
deterministic_client.h
1#ifndef DETERMINISTIC_CLIENT_H
2#define DETERMINISTIC_CLIENT_H
3
4#include <stdint.h>
5#include <future>
6#include <chrono>
7#include <mutex>
8#include <condition_variable>
9#include "../core/result.h"
10#include "./worker_runnable.h"
11#include "./helper/atomic_optional.h"
12
13namespace ara
14{
15 namespace exec
16 {
18 enum class ActivationReturnType : uint8_t
19 {
22 kInit = 2,
23 kRun = 3,
24 kTerminate = 4
25 };
26
29 {
30 public:
32 using TimeStamp = std::chrono::time_point<std::chrono::steady_clock>;
33
35 static const uint64_t cCycleDelayMs{10};
36
37 private:
38 static std::atomic_uint8_t mCounter;
39 static std::future<void> mFuture;
40 static bool mRunning;
41 static std::mutex mCycleMutex;
42 static std::condition_variable mCycleConditionVariable;
43 static std::default_random_engine mGenerator;
44 static std::uniform_int_distribution<uint64_t> mDistribution;
46 static uint64_t mRandomNumber;
47 static TimeStamp mActivationTime;
48 WorkerThread mWorkerThread;
49
50 static void activateCycle();
51
52 public:
55
59
67 template <typename ValueType, typename Container>
69 WorkerRunnable<ValueType> &runnableObj,
70 Container &container) noexcept
71 {
72 static_assert(
73 std::is_same<ValueType, typename Container::value_type>::value,
74 "Container value type mismatch!");
75
76 for (auto element : container)
77 {
78 runnableObj.Run(element, mWorkerThread);
79 }
80
82 return _result;
83 }
84
87 uint64_t GetRandom() noexcept;
88
91 void SetRandomSeed(uint64_t seed) noexcept;
92
95 core::Result<TimeStamp> GetActivationTime() noexcept;
96
99 core::Result<TimeStamp> GetNextActivationTime();
100 };
101 }
102}
103
104#endif
A wrapper around the callee's return value and its possible error.
Definition: result.h:16
Class that utilizes a client to have deterministic behaviour.
Definition: deterministic_client.h:29
static const uint64_t cCycleDelayMs
Theoretical cyclic delay in millisecond.
Definition: deterministic_client.h:35
void SetRandomSeed(uint64_t seed) noexcept
Set a seed number to the deterministic random number generator.
Definition: deterministic_client.cpp:70
uint64_t GetRandom() noexcept
Get a deterministic random number.
Definition: deterministic_client.cpp:65
std::chrono::time_point< std::chrono::steady_clock > TimeStamp
Steady clock time point for deterministic cycles.
Definition: deterministic_client.h:32
core::Result< TimeStamp > GetActivationTime() noexcept
Get the last activation time.
Definition: deterministic_client.cpp:75
ara::core::Result< void > RunWorkerPool(WorkerRunnable< ValueType > &runnableObj, Container &container) noexcept
Run a deterministic worker pool to process a container via a runnable object.
Definition: deterministic_client.h:68
core::Result< TimeStamp > GetNextActivationTime()
Get the next comming activation time.
Definition: deterministic_client.cpp:81
core::Result< ActivationReturnType > WaitForActivation()
Blocks the caller till reaching the next activation time.
Definition: deterministic_client.cpp:56
DeterministicClient worker runnable abstract class.
Definition: worker_runnable.h:14
DeterministicClient random number generator.
Definition: worker_thread.h:14
A wrapper around a possible atomic value.
Definition: atomic_optional.h:20
ActivationReturnType
Application deterministic behaviour lifecycle action.
Definition: deterministic_client.h:19
@ kTerminate
Application shall terminate itself.
@ kServiceDiscovery
Application shall discover its service server.
@ kRegisterService
Application shall register its service server.
@ kInit
Application shall intialize itself.
@ kRun
Application shall perform its normal operation.