[riot-devel] Current best practice for sleeping in a device driver?

Kaspar Schleiser kaspar at schleiser.de
Thu Sep 13 11:00:22 CEST 2018


Hi Pekka,

On 9/13/18 10:33 AM, Nikander Pekka wrote:
> It turns out that locking the mutex twice sometimes blocks the thread.  Or at least it does that in my implementation, when running with multiple other threads.

If I understand correctly, you'd like to block a user thread until an
ISR arrives, and there might be multiple user threads, right?

// initialize mutex locked
mutex_t isr_lock = MUTEX_INITIALIZE_LOCKED;

isr() {
  // to wake thread:
  mutex_unlock(&isr_mutex);
}

user_thread() {
	// I guess only one user thread should interact with the device
	mutex_lock(&driver_mutex);

	// this blocks until ISR unlocks
	mutex_lock(&isr_mutex);

	// possibly disable ISR here

	... // handle ISR

	if (mutex_trylock(&isr_mutex)) {
		// mutex was unlocked since last lock
		// handle if necessary
	}
	
	// posibly restore ISR here
	// release driver
	mutex_unlock(&driver_mutex);
}
	
Does this capture your use case?

Kaspar
	


More information about the devel mailing list