Skip to content

Commit

Permalink
Add join to RTML_Monitor.
Browse files Browse the repository at this point in the history
  • Loading branch information
anmaped committed Jun 18, 2024
1 parent d424e7d commit 74e1314
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 45 deletions.
18 changes: 14 additions & 4 deletions src/periodicmonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ template <char... name> class RTML_monitor {

/** Sets new monitor period. */
void setPeriod(const useconds_t &p);

int join();
};

template <char... name>
RTML_monitor<name...>::RTML_monitor(const useconds_t period)
: _task(task(id, loop, 50, SCHED_OTHER, period, this)) {}
: _task(task(id, loop, 50, DEFAULT_SCHED, period, this)) {}

template <char... name>
RTML_monitor<name...>::RTML_monitor(const useconds_t period,
Expand All @@ -141,8 +143,8 @@ template <char... name> void *RTML_monitor<name...>::loop(void *ptr) {

template <char... name> int RTML_monitor<name...>::enable() {

if (_task.st != RUNNING)
_task.st = RUNNING;
if (_task.st != ACTIVATE)
_task.st = ACTIVATE;

return P_OK;
}
Expand All @@ -155,7 +157,7 @@ template <char... name> int RTML_monitor<name...>::disable() {
}

template <char... name> bool RTML_monitor<name...>::isRunning() const {
return !_task.running;
return _task.st == RUNNING;
}

template <char... name>
Expand All @@ -168,4 +170,12 @@ void RTML_monitor<name...>::setPeriod(const useconds_t &p) {
_task.period = p;
}

template <char... name> int RTML_monitor<name...>::join() {
void *ret = NULL;
if (pthread_join(_task.thread, &ret))
return 1;

return 0;
}

#endif // RTML_PERIODICMONITOR_H
38 changes: 19 additions & 19 deletions src/task_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,31 @@
#ifndef _TASK_COMPAT_H_
#define _TASK_COMPAT_H_

#ifdef __HW__
#define STACK_SIZE 0
#define SCHED_OTHER 0
#define SCHED_FIFO 0
#elif defined(__NUTTX__)
#ifdef __NUTTX__
#include <errno.h>
#include <pthread.h>
#include <sys/types.h>
#define STACK_SIZE 6000
#define DEFAULT_SCHED SCHED_OTHER
#elif defined(__FREERTOS__)
#include <FreeRTOS_POSIX.h>
#include <FreeRTOS_POSIX/errno.h>
#include <FreeRTOS_POSIX/pthread.h>
#include <FreeRTOS_POSIX/sys/types.h>
#include <FreeRTOS_POSIX/time.h>
#define STACK_SIZE 6000
#else
#define DEFAULT_SCHED SCHED_OTHER
#elif defined(__linux__)
#include <errno.h>
#include <pthread.h>
#include <sys/types.h>
#define STACK_SIZE 1000000
#define DEFAULT_SCHED SCHED_FIFO
#else
#define STACK_SIZE 0
#define SCHED_OTHER 0
#define SCHED_FIFO 0
#warning "Tasks are not supported!"
#endif

#include "debug_compat.h"
Expand All @@ -69,7 +73,7 @@
return val; \
}

enum status { ACTIVATION, RUNNING, DELAY, ABORT, ABORTED, UNACTIVATE };
enum status { ACTIVATE, UNACTIVATE, RUNNING, DELAY, ABORT, ABORTED };

#ifdef __HW__

Expand All @@ -83,16 +87,16 @@ struct task {

int priority;

bool running;

status st;

void *(*run)(void *);

void *run_payload;

task(char const *id, void *(*loop)(void *), const int prio, const int sch_policy, const useconds_t p, void *payload=NULL): \
tid(id), period(p), sched_policy(sch_policy), priority(prio), run(loop), run_payload(payload) {}
task(char const *id, void *(*loop)(void *), const int prio,
const int sch_policy, const useconds_t p, void *payload = NULL)
: tid(id), period(p), sched_policy(sch_policy), priority(prio), run(loop),
run_payload(payload) {}
};

#endif
Expand All @@ -115,8 +119,6 @@ struct task {

const int priority;

bool running;

status st;

void *(*run)(void *);
Expand Down Expand Up @@ -151,14 +153,13 @@ struct task {

DEBUGV_APPEND("\n");

running = false;
st = ACTIVATION;
st = UNACTIVATE;

return 0;
}

task(char const *id, void *(*loop)(void *), const int prio,
const int sch_policy, const useconds_t p, void *payload=NULL)
const int sch_policy, const useconds_t p, void *payload = NULL)
: tid(id), period(p), sched_policy(sch_policy), priority(prio), run(loop),
run_payload(payload) {
create_task(
Expand All @@ -168,8 +169,8 @@ struct task {

// trap to block task start
for (;;) {
if(ttask->st == RUNNING) {
ttask->running = true;
if (ttask->st == ACTIVATE) {
ttask->st = RUNNING;
break;
}
}
Expand Down Expand Up @@ -227,7 +228,6 @@ struct task {
ttask->run(ttask->run_payload);

if (ttask->st == ABORT) {
ttask->running = false;
ttask->st = ABORTED;
return NULL;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/custom-rtm-monitor/Rtm_monitor_8ea7.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ template <class T> class Rtm_monitor_8ea7_0 : public RTML_monitor<'8','e','a','7

public:
Rtm_monitor_8ea7_0(useconds_t p, T &trc)
: RTML_monitor(p, SCHED_OTHER, 50), trace(trc), _out(T_UNKNOWN) {
: RTML_monitor(p, DEFAULT_SCHED, 50), trace(trc), _out(T_UNKNOWN) {
tzero = clockgettime() + 3000000000L;
}
Rtm_monitor_8ea7_0(useconds_t p, T &trc, int sche, int prio)
Expand All @@ -57,6 +57,7 @@ template <class T> class Rtm_monitor_8ea7_0 : public RTML_monitor<'8','e','a','7
}

three_valued_type &getVeredict() { return _out; }

};

#endif // RTM_MONITOR_8EA7_H
7 changes: 1 addition & 6 deletions tests/rtm_monitor_test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ int main() {

rtm_mon0.disable();

// wait till all tasks are finished (threads are not destroyed)
while (rtm_mon0.isRunning()) {
nanosleep((const struct timespec[]){{1, 0L}}, NULL);
};
rtm_mon0.join();

auto v = rtm_mon0.getVeredict();

Expand All @@ -68,8 +65,6 @@ int rtm_monitor_test1() {

#else

#include <stdio.h>

extern "C" int rtm_monitor_test1();

int rtm_monitor_test1() {
Expand Down
7 changes: 1 addition & 6 deletions tests/rtm_monitor_test2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ int main() {

rtm_mon0.disable();

// wait till all tasks are finished (threads are not destroyed)
while (rtm_mon0.isRunning()) {
nanosleep((const struct timespec[]){{1, 0L}}, NULL);
};
rtm_mon0.join();

auto v = rtm_mon0.getVeredict();

Expand All @@ -75,8 +72,6 @@ int rtm_monitor_test2() {

#else

#include <stdio.h>

extern "C" int rtm_monitor_test2();

int rtm_monitor_test2() {
Expand Down
21 changes: 12 additions & 9 deletions tests/rtmlib_atomic_reader_and_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ int rtmlib_atomic_reader_and_writer() {
__attribute__((unused)) __task producer_1 =
__task("producer1", producer1, PRIO, SCHED_OTHER, 6000);

consumer_1.st = RUNNING;
consumer_2.st = RUNNING;
producer_1.st = RUNNING;
consumer_1.st = ACTIVATE;
consumer_2.st = ACTIVATE;
producer_1.st = ACTIVATE;

// random load
nanosleep((const struct timespec[]){{30, 0L}}, NULL);
Expand All @@ -143,10 +143,15 @@ int rtmlib_atomic_reader_and_writer() {

printf("Waiting running tasks...\n");

// wait till all tasks are finished (threads are not destroyed)
while (consumer_1.st != ABORTED || consumer_2.st != ABORTED ||
producer_1.st != ABORTED) {
};
void *ret = NULL;
if (pthread_join(consumer_1.thread, &ret))
return 1;

if (pthread_join(consumer_2.thread, &ret))
return 1;

if (pthread_join(producer_1.thread, &ret))
return 1;

printf("%s \033[0;32msuccess.\e[0m\n", __FILE__);

Expand All @@ -155,8 +160,6 @@ int rtmlib_atomic_reader_and_writer() {

#else

#include <stdio.h>

extern "C" int rtmlib_atomic_reader_and_writer();

int rtmlib_atomic_reader_and_writer() {
Expand Down

0 comments on commit 74e1314

Please sign in to comment.