简体   繁体   中英

Embedded Linux. Execute block of code atomically

Currently, I have a user space application on an embedded linux device (version 2.6.28.10) which is toggling GPIO pins to control some custom hardware. The writes to the GPIO registers occur from separate pthreads all of which need access to the device. Most of the time this works fine since there is a semaphore preventing multiple threads from accessing the section that does the GPIO writes simultaneously.

The problem is that in the middle of writing to these GPIO registers (about 24 8-bit writes) another thread will interrupt the current thread and though the interrupting thread cannot modify the registers due to the semaphore, control is not returned to the thread that is writing to the GPIOs for several milliseconds which, of course, makes the data sent to the external device invalid.

Is there a method in linux to ensure that this set of GPIO writes (taking a total of about 50 usecs) is not interrupted by another thread?

It is not clear from your question how this thread interrupts the working thread, and what kind of a thread it is (user space, or kernel space, or an actual interrupt).

If the interrupting thread is a user space thread, you can just have it respect the mutual exclusion semaphore. Another option would be to temporarily raise the priority of the working thread so that the CPU cannot be taken away from it (if such a facility is available).

If the interrupting thread is a kernel space thread, you can try and use a semaphore implementation that can be invoked from the kernel as well (System V semaphores). These are heavier though.

If the interrupting thread is actually an interrupt then you would need to build a kernel component that is invoked by your user space threads, which in turn disables the interrupts in the critical section. This will affect the performance negatively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM