rdos wrote:Octocontrabass wrote:rdos wrote:Well, you typically want to enable & disable interrupts in IRQs, you might need spin-locks, and AFAIK, C doesn't support any of that without various hacks.
If you really want to avoid using non-standard inline assembly extensions, you can call a separate function written entirely in assembly. Linking C and assembly together is not a hack.
Spin locks can be implemented using atomic_flag from <stdatomic.h>, which has been part of C since C11.
I prefer to code this in assembly, not in C. There is no advantage of having IRQ handlers or spinlocks in C. They will be cluttered by stuff that make them more unreadable than pure assembly code.
you don't need any tricks or 'fancy constructs' here. the aforementioned atomics work trivially and cleanly.
i'd go as far as to say that asm ("cli") is neither a hack nor unclean. it conveys a clear purpose. of course, you should hide such an architecture specific detail behind a layer of indirection. ditto for many other cases of inline assembly, especially extended inline assembly.
My opinion is that if you cannot write C code without using tricks or fancy constructs, write it in assembly instead. Atomic variables or variables where the optimizer is not allowed to remove references are good examples of code that is better done in assembly. Simply because the assembler won't try to remove constructs it doesn't find useful.
compilers don't just decide some code is not useful. if the compiler removed a reference, it wasn't needed, and you failed to convey your intention in your code. it'd have confused a human reader too.
The discussion was more about C++, where you never know the side-effects of even trivial code. C without exception handling is more appropriate, as then you know the side effects of the code without needing to look at the generated code.
that is not how this works. c++ code that you write does what you write it to do.
again, exceptions are neither required nor impossible to disable (nor specific to c++. -fexceptions works for C)
In fact, in the C based drivers I have, I decided that the syscall interface is better done in assembly, and then I would define C based handler procedures with a specific register call interface. That works well for complicated drivers like hid, audio codecs and font utilities.
it is not particularly odd to write stubs in assembly. your original description makes it sound like the entirety of your irq code is in assembly, which'd be an utter waste of time.