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

> The mix of global buffer and passed-in struct defining the buffer made me sad. So did the lack of atomic increments and decrements (volatile ain't sufficient). Don't get me started on the getchar routine.

It's for MSP430, and seems to be oriented in single core embedded microcontrollers. Those things have no atomic operations, other than disabling interrupts.

You'd be right for multicore chips or if data type is larger than word size or if data is not aligned.

But in this case, this code is correct for this architecture. All of those things you mentioned are perfectly normal and acceptable when programming microcontrollers.

http://www.simplyembedded.org/tutorials/msp430-architecture/

http://en.wikipedia.org/wiki/TI_MSP430



I guess you're assuming the compiler will issue an "increment" instruction for the ++, and that instruction is atomic.

It's not a great coding practice. Move to a different processor (this happens!) and your code turns into a load-add-store, gets interrupted halfway through, and you scratch your head for a while about the mysterious and flaky FIFO failures.

Been there. At least a gesture towards an atomic operation would be nice, when you have data shared between interrupt and non-interrupt code.


How about putting

#if !defined(__AVR__) && !defined(__MSP430__)

#warning "Fix this FIFO - ++/-- possibly not atomic!"

#endif

into the header of that thingy then. Save work on small uCs, and be warned when moving it somewhere else.




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

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

Search: