I want to use the GPIO pins to wait for a button-press without using a CPU spin loop. My preferred way of using the GPIO pins is via the sysfs interface at
/sys/class/gpio, but it seems to me that there is an inherent race condition in doing so. Namely, if I understand the sysfs interface to GPIO correctly, it seems one must go through the following sequence:
- Read the
valuefile to see whether the desired condition holds true.
- If it does not (the usual case for the first iteration), poll the value file for
POLLPRIto sleep until it changes state, and repeat from step 1.
However, in this procedure, there is a window of opportunity between steps 1 and 2 such that the button is not yet pressed when the value is read, but then pushed right before entering doing the
poll call, in which case this particular button press would effectively be missed.
I mean, I realize that a low-frequency event like button presses doesn't really have a high probability of triggering this race condition, but there are certainly more high-frequency events that could, and even regardless of that, it just seems ugly. Is there a way to avoid this problem?