Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What's wrong with that? If you're writing code for pedagogical purposes, it doesn't necessarily need to be as concise as possible.


I know some people who object to one-line read-modify-write of hardware registers, the idea being that glancing at the code the non-atomicity of the operation might be lost. I also generally don't use raw volatile pointers directly when dealing with hardware registers, too easy to forget a "volatile" somewhere and have the code behave erratically. I always use tiny read32/write32 wrappers instead.


Absolutely. Modern compilers are trivially able to inline such operations, so there's no reason not to make your intentions and separation of operations crystal clear. After all, you're going to read the code a lot more often than you'll write it.


Yes and read/write barriers are also sometimes necessary, to prevent re-ordering by the compiler and processor.


You have to be careful though in some cases. For instance, if you read two MMIO registers in a one liner, there's no way to guarantee the ordering between them.


Yes, wrappers are the way to go. e.g. Linux kernel


In fact, in my firmware writing style, I tend to separate MMIO accesses from mutation to make it clear when those bus accesses occur.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: