| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/***************************************************************************** |
| 2 |
|
|
* |
| 3 |
|
|
* Copyright 2010 Richard Hacker (lerichi at gmx dot net) |
| 4 |
|
|
* |
| 5 |
|
|
* This file is part of the pdserv library. |
| 6 |
|
|
* |
| 7 |
|
|
* The pdserv library is free software: you can redistribute it and/or modify |
| 8 |
|
|
* it under the terms of the GNU Lesser General Public License as published |
| 9 |
|
|
* by the Free Software Foundation, either version 3 of the License, or (at |
| 10 |
|
|
* your option) any later version. |
| 11 |
|
|
* |
| 12 |
|
|
* The pdserv library is distributed in the hope that it will be useful, but |
| 13 |
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 |
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
|
|
* License for more details. |
| 16 |
|
|
* |
| 17 |
|
|
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
|
|
* along with the pdserv library. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
|
* |
| 20 |
|
|
****************************************************************************/ |
| 21 |
|
|
|
| 22 |
|
|
#ifndef LIB_TASK_H |
| 23 |
|
|
#define LIB_TASK_H |
| 24 |
|
|
|
| 25 |
|
|
#include <vector> |
| 26 |
|
|
#include <set> |
| 27 |
|
|
#include <cstddef> |
| 28 |
|
|
|
| 29 |
|
|
#include "../PThread.h" |
| 30 |
|
|
#include "../Task.h" |
| 31 |
|
|
#include "../TaskStatistics.h" |
| 32 |
|
|
#include "../SessionTask.h" |
| 33 |
|
|
|
| 34 |
|
|
namespace PdServ { |
| 35 |
|
|
class SessionTask; |
| 36 |
|
|
class DataType; |
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
|
|
class Main; |
| 40 |
|
|
class Signal; |
| 41 |
|
|
class Parameter; |
| 42 |
|
|
class SessionTaskData; |
| 43 |
|
|
|
| 44 |
|
|
class Task: public PdServ::Task { |
| 45 |
|
|
public: |
| 46 |
|
|
Task(Main *main, size_t index, double sampleTime, const char *name); |
| 47 |
|
|
virtual ~Task(); |
| 48 |
|
|
|
| 49 |
|
|
Main * const main; |
| 50 |
|
|
|
| 51 |
|
|
Signal* addSignal( unsigned int decimation, |
| 52 |
|
|
const char *path, const PdServ::DataType& datatype, |
| 53 |
|
|
const void *addr, size_t n, const size_t *dim); |
| 54 |
|
|
void setSignalReadLock( |
| 55 |
|
|
void (*fn)(int state, void* priv_data), void *priv_data); |
| 56 |
|
|
void makePersistent(const Signal* s); |
| 57 |
|
|
bool getPersistentValue(const PdServ::Signal* s, |
| 58 |
|
|
char* buf, const struct timespec** t) const; |
| 59 |
|
|
|
| 60 |
|
|
size_t getShmemSpace(double t, size_t limit) const; |
| 61 |
|
|
|
| 62 |
|
|
void prepare(void *start, void *end); |
| 63 |
|
|
void rt_init(); |
| 64 |
|
|
void nrt_init(); |
| 65 |
|
|
// can throw RtProcessExited |
| 66 |
|
|
void nrt_init_persistent(); |
| 67 |
|
|
void updateStatistics( |
| 68 |
|
|
double exec_time, double cycle_time, unsigned int overrun); |
| 69 |
|
|
void rt_update(const struct timespec *); |
| 70 |
|
|
void nrt_update(); |
| 71 |
|
|
void pollSignalValue( |
| 72 |
|
|
const Signal* signal, void* dst, struct timespec* time) const; |
| 73 |
|
|
|
| 74 |
|
|
// can throw RtProcessExited |
| 75 |
|
|
bool subscribe(const Signal* s, SessionTaskData*, bool insert); |
| 76 |
|
|
void getSignalList(const Signal ** s, size_t *n, |
| 77 |
|
|
unsigned int *signalListId); |
| 78 |
|
|
|
| 79 |
|
|
void getCurrentTime(struct timespec* t) const; |
| 80 |
|
|
|
| 81 |
|
|
private: |
| 82 |
|
|
pthread::Mutex mutex; |
| 83 |
|
|
|
| 84 |
|
|
struct timespec m_time; |
| 85 |
|
|
|
| 86 |
|
|
void (*signal_readlock_cb)(int, void *); |
| 87 |
|
|
void *readlock_data; |
| 88 |
|
|
|
| 89 |
|
|
size_t signalTypeCount[4]; |
| 90 |
|
|
size_t signalMemSize; |
| 91 |
|
|
|
| 92 |
|
|
PdServ::TaskStatistics taskStatistics; |
| 93 |
|
|
|
| 94 |
|
|
// Cache of the currently transferred signals |
| 95 |
|
|
const Signal **signalCopyList[4]; |
| 96 |
|
|
|
| 97 |
|
|
std::vector<Signal*> signals; |
| 98 |
|
|
|
| 99 |
|
|
unsigned int seqNo; |
| 100 |
|
|
unsigned int signalListId; |
| 101 |
|
|
|
| 102 |
|
|
// Pointer into shared memory used to communicate changes |
| 103 |
|
|
// to the signal copy list |
| 104 |
|
|
// The non-realtime thread writes using signalListWp whereas the |
| 105 |
|
|
// realtime task reads using signalListRp |
| 106 |
|
|
// This list is null terminated |
| 107 |
|
|
struct SignalList *signalList, *signalListEnd, |
| 108 |
|
|
* volatile *signalListRp, * volatile *signalListWp; |
| 109 |
|
|
|
| 110 |
|
|
// Structure managed by the realtime thread containing a list of |
| 111 |
|
|
// signals which it has to copy into every PDO |
| 112 |
|
|
struct CopyList *copyList[4]; |
| 113 |
|
|
|
| 114 |
|
|
// Process data communication. Points to shared memory |
| 115 |
|
|
struct Pdo *txMemBegin, *txPdo, * volatile *nextTxPdo; |
| 116 |
|
|
const void *txMemEnd; |
| 117 |
|
|
|
| 118 |
|
|
// Reimplemented from PdServ::Task |
| 119 |
|
|
std::list<const PdServ::Signal*> getSignals() const; |
| 120 |
|
|
void prepare(PdServ::SessionTask *) const; |
| 121 |
|
|
void cleanup(const PdServ::SessionTask *) const; |
| 122 |
|
|
// can throw RtProcessExited |
| 123 |
|
|
bool rxPdo(PdServ::SessionTask *, const struct timespec **tasktime, |
| 124 |
|
|
const PdServ::TaskStatistics **taskStatistics) const; |
| 125 |
|
|
|
| 126 |
|
|
// These methods are used in real time context |
| 127 |
|
|
void processSignalList(); |
| 128 |
|
|
void calculateCopyList(); |
| 129 |
|
|
void copyData(const struct timespec* t); |
| 130 |
|
|
|
| 131 |
|
|
typedef std::set<const PdServ::Signal*> PersistentSet; |
| 132 |
|
|
PersistentSet persistentSet; |
| 133 |
|
|
|
| 134 |
|
✗ |
struct Persistent: PdServ::SessionTask { |
| 135 |
|
|
Persistent(Task* t); |
| 136 |
|
|
|
| 137 |
|
|
// Reimplemented from PdServ::SessionTask |
| 138 |
|
|
void newSignal( const PdServ::Signal *); |
| 139 |
|
|
|
| 140 |
|
|
PersistentSet active; |
| 141 |
|
|
}; |
| 142 |
|
|
|
| 143 |
|
|
Persistent *persist; |
| 144 |
|
|
}; |
| 145 |
|
|
|
| 146 |
|
|
#endif // LIB_TASK_H |
| 147 |
|
|
|