So, chSysLock() and chSysUnlock() basically are not linked to any particular thread and could be used for short time access to any global variable. And this mechanism prevent simultaneous access to this variable from several threads. Is it correct?
I looked to the source code for these functions. For me it seemed like stopping any interrupts events. Does it mean, that even system clock will not interrupt for switching to another task?
Changed my macro M3_DEBUG
Code: Select all
#define M3_DEBUG( level, ... ) { \
chSysLock(); \
if(console_debug && level <= console_debug) \
{ chSysUnlock(); \
chprintf(console_cfg.sc_channel, ## __VA_ARGS__); \
} \
else \
chSysUnlock(); \
}it works, but it is not as elegant as I would like it to be. Adding this macro will increase code space to significant number of bytes for each implementation. Probably will change it later by cloning chprintf itself with comparing level with console_debug inside the function.
However, my original idea for the console debug messages was different. I was planning to use a queue where my debug function/macro put the strings and debug messages thread would send these strings to the console stream. That should prevent corrupting messages from several tasks on the console. I used similar algorithm in my quasi-multitasking project about 7 years ago
The currently used serial driver already has such queues, and the system has only I/O Queues. So, it should be some another mechanism. Besides, I would not like to transfer text data from one queue to another.
The algorithm I'm thinking about now is following (since i'm not fully familiar with the system yet, some or all statements will be wrong
- Create debug messages task("DConsole", for example). Probably with priority of 1 level higher than NORMALPRIO.
- Create EventSource for DConsole. Let's say DM_Event (Debug Message event).
- Each task that requires to bring debug message to console, has it's own global buffer for such messages. So far they will be static without using malloc().
- These tasks register their flags in DM_Event and somehow registers pointer to it's own message buffer. Total up to 32 flags/task. That should be enough so far.
- When a task needs to send message, it forms the message in its buffer with chsprintf(...) or memcpy and set according flag in DM_Event.
- DConsole uses its EventListener, transfer according buffer to the stream with chprintf(...) or another lighter function and clears according flag in DM_Event.
Could you comment, where I'm right and wrong for this mechanism? Is there a better way that is more elegant, requires less resources, more logical, etc...? May be everything is much easy and I'm inventing a bicycle?
There are some notes:
1. Our system uses quite slow stream of input data - sample rates for the signals are lower than 1 Hz. There should be enough time to send message to console and, therefore, tasks probably will not need to check whether their previous requests were served or not. So, no need to check whether flag cleaned.
2. There will be faster task like ModBus slave, but I will try to minimize debug messages from it. Or maybe for this task I will check the flag before filling up the message buffer.
3. I'm aware that shell will be working in parallel to such task and when I type in a command it will corrupt the debug messages, but I don't care.