rtmlib
periodicmonitor.h
Go to the documentation of this file.
1 /*
2  * rtmlib is a Real-Time Monitoring Library.
3  *
4  * Copyright (C) 2018-2020 AndrĂ© Pedro
5  *
6  * This file is part of rtmlib.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef RTML_PERIODICMONITOR_H
23 #define RTML_PERIODICMONITOR_H
24 
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <time.h>
28 
29 #ifdef __NUTTX__
30 #include <sched.h>
31 #endif
32 
33 #include <errno.h>
34 
35 #include "circularbuffer.h"
36 #include "task_compat.h"
37 #include "time_compat.h"
38 
53 class RTML_monitor {
54 private:
56  pthread_t thread;
57 
60 
62  struct Monitor_state {
64  const int sched_policy;
65 
68  const int priority;
69 
73  const size_t stack_size;
74 
77 
79  pthread_mutex_t fmtx;
80 
82  pthread_cond_t cond;
83 
85  useconds_t period;
86 
87  Monitor_state(const int sch, const int prio, const useconds_t p)
88  : sched_policy(sch), priority(prio), status(UNACTIVATE), period(p), stack_size(STACK_SIZE) {};
89 
90  } m_state;
91 
99  static void *loop(void *);
100 
101 protected:
109  virtual void run() = 0;
110 
111 public:
119  RTML_monitor(const useconds_t period);
120 
129  RTML_monitor(const useconds_t period, unsigned int policy,
130  unsigned int priority);
131 
135  int enable();
136 
142  int disable();
143 
149  bool isRunning() const;
150 
155  const useconds_t &getPeriod() const;
156 
158  void setPeriod(const useconds_t &p);
159 };
160 
161 #endif // RTML_PERIODICMONITOR_H
Definition: periodicmonitor.h:59
Definition: periodicmonitor.h:59
Definition: periodicmonitor.h:53
RTML_monitor(const useconds_t period)
Definition: periodicmonitor.cpp:24
pthread_cond_t cond
Definition: periodicmonitor.h:82
const size_t stack_size
Definition: periodicmonitor.h:73
int disable()
Definition: periodicmonitor.cpp:122
Definition: periodicmonitor.h:62
void setPeriod(const useconds_t &p)
Definition: periodicmonitor.cpp:131
status
Definition: task_compat.h:59
Definition: periodicmonitor.h:59
Definition: periodicmonitor.h:59
bool isRunning() const
Definition: periodicmonitor.cpp:127
useconds_t period
Definition: periodicmonitor.h:85
virtual void run()=0
Definition: periodicmonitor.h:59
int enable()
Definition: periodicmonitor.cpp:85
const int sched_policy
Definition: periodicmonitor.h:64
Definition: periodicmonitor.h:59
Monitor_state(const int sch, const int prio, const useconds_t p)
Definition: periodicmonitor.h:87
pthread_mutex_t fmtx
Definition: periodicmonitor.h:79
pthread_t thread
Definition: periodicmonitor.h:56
const useconds_t & getPeriod() const
Definition: periodicmonitor.cpp:129
const int priority
Definition: periodicmonitor.h:68
static void * loop(void *)
Definition: periodicmonitor.cpp:31
mon_status
Definition: periodicmonitor.h:59
struct RTML_monitor::Monitor_state m_state
mon_status status
Definition: periodicmonitor.h:76