6 ms·
Practical Guide to Bare Metal C++
- monocasa 9y agoI traditionally haven't been the biggest fan of this book. Most of what they list is stuff that just looks like using C++ just for the sake of it, with no real benefit over a C codebase. For instance they don't appear to use RAII at all, even in places where it's an obvious win (InterruptLock).
- vowelless 9y agoI tried searching the book for RAII and there are no hits... Do they really not cover it or is the search busted ?
- mbel 9y agoI don't think RAII is a concept complex enough to write a book about it... Probably a blog post or Wikipedia page is enough to explain it fully.
- monocasa 9y agoIt's worth a chapter in this book IMO. As the maintainer of a C++14 proprietary RTOS, it's probably the most convincing argument to the C guys as to why C++ can benefit embedded codebases. Like not being able to forget to re-drop thread priority when you've temporarily increased it. Or one of my favorite patterns is returning the object representing the temporary higher priority. If the caller doesn't do anything with the return value, the destructor is called and thread priority reverts to what it was. However, it gives you a chance for the caller to hold on to that high priority object, and get more work done essentially atomically.
- xyzzy_plugh 9y agoBut the compiler doesn't optimize out the constructor, right? It can't. In an RTOS of all places, that seems awfully expensive. One generally avoids wasted resources in embedded -- the trade-off is it requires more care during development. I've never seen RAII work well in an embedded system. Not being able to handle exceptions or errors during destruction ruins everything.
- monocasa 9y ago> But the compiler doesn't optimize out the constructor, right? It can't. > In an RTOS of all places, that seems awfully expensive. One generally avoids wasted resources in embedded -- the trade-off is it requires more care during development. The compiler has no issues "optimizing out" the constructor in my experience. The asm looks just like the equivalent C in all of the cases I've seen. > I've never seen RAII work well in an embedded system. Not being able to handle exceptions or errors during destruction ruins everything. I mean, we disallow exceptions anyway. And we use RAII for objects where there isn't a weird failure case to be handled like my example of thread priority guards.
- fdsfsafasfdas 9y agoDisabling exceptions has solved this. There are no need for exceptions in the kernel, and the cost is high.
- concede_pluto 9y agoIsn't table-driven stack unwinding cheaper than having a bunch of functions each check an error code and return to their caller?
- monocasa 9y agoThe practicalities of bare metal programming means that a lot of the time an individual work item has to jump contexts (ie. stacks and register sets) in a way that doesn't map cleanly to C++ style exceptions.
- asveikau 9y agoIf someone writes this ... > I haven't found many practical guides or tutorials of how to use C++ superiority and boost development process compared to conventional approach of using “C” programming language. ... and does not mention RAII in their quest to write such a guide, they have seriously failed at their goal.
- butterisgood 9y agoThe first advantage listed of C++ is "code reuse" which is where I stopped reading. The first advantage for me of C++ everywhere IS RAII.
- ensiferum 9y agoWithout exceptions most of your STL containers become unsable, unless aborting is an option when otherwise bad_alloc would get thrown. Also without exceptions you really need double initialize pattern for your class types. I.e. a ctor that cannot (and will not throw) and then a "error_code Init(...)" method that can return an error code.
- sillysaurus3 9y agoSure, just abort. It's a fine choice in a bad_alloc situation.
- ensiferum 9y agoWhy? Would you also abort on other resource allocation errors, such as opening a socket, or creating a process wide mutex or whatever? I mean sometimes for some apps aborting on such errors (on some embedded platforms where they must work within specific resource constraints) can be an OK strategy but in general people seem to have this special idea about "bad alloc" conditions. The usual misconceptions are that a) it cannot happen and b) nothing can be done. Both are false.
- bluGill 9y agoIf the system is out of memory there isn't much you can do. The general answer is you don't run out of memory in the first place. Linux will kernel panic in similar situations and you are done there too. (in most cases linux unwinds. fails the operation, and continues; but there cases where the only thing left to do is panic and halt)
- Jach 9y agoI heard Lisp machines would just stop everything and give you a REPL so you have a chance to clean things up, or just reboot. I don't see why modern systems can't reserve some amount of memory for such a recovery process. In my Linux experience I'm either saved by the OOM Killer (which is dubious 'safety' at best) or the system just hardlocks and I have to either REISUB or sometimes force a power off.
- microcolonel 9y agoThis is a fine "long answer" to the question how do I write C++ on bare metal?. The short answer is don't, medium is if you don't know why every major operating system at every size is written in C, then you have a lot of learning to do.
- dragontamer 9y agoWindows is well known to have portions of the kernel written in C++, and MacOSX has huge portions written in ObjectiveC. The greater part of Android is Java-based. Only Linux is really purely C-based, since Linus really hates C++ with a passion. But there's a reason why even the GCC project has moved over to C++. http://lordjeb.com/2013/09/19/modern-c-in-the-windows-kernel/ http://lordjeb.com/2013/09/19/modern-c-in-the-windows-kernel...
- microcolonel 9y ago"Only linux" And every BSD, Magenta (a brand new kernel), ThreadX, FreeRTOS, QNX, Integrity, HP-UX, SeL4, Illumos, TI-RTOS, RTEMS... XNU does not appear to have any Objective-C in the kernel, and it has no C++ in the scheduler, syscall handler, or any other critical section. I doubt Windows has C++ in any such place either. Inside the XNU tarball (the actual kernel inside OS X) there are 1,078 C source code files containing 652,124 source (not comment, not whitespace) lines. There are exactly 10 Objective-C files containing a total of 1787 code lines, and it's not certain they are even built as part of the kernel binary. To be fair, there is some amount of C++ in XNU these days. XNU contains 97 C++ files totalling 68,673 code lines, it seems mostly in iokit[50kSLOC] and libkern[17kSLOC] (infrastructure to support the iokit C++ code, maybe).
- monocasa 9y agoDarwin's whole driver model is in C++. Look up "IOKit"
- microcolonel 9y agoI'm aware that I/O Kit is written in C++, I added that to my comment (maybe you saw the old version). Drivers are not really kernel per se, there are plenty of systems which have no drivers in the kernel (usually except RTC, which is in kernel in XNU anyway). Also, it's not really C++, it's a pretty serious subset (lacking templates, multiple inheritance, exceptions, reflection, funky new casts [reinterpret_cast, static_cast, dynamic_cast, const_cast], and even namespaces(!)).
- victor106 9y agoRUST
- dang 9y agohttps://news.ycombinator.com/item?id=12138374 https://news.ycombinator.com/item?id=12138374