[riot-notifications] [RIOT-OS/RIOT] core/mutex: Add mutex_cancel (#15442)

Marian Buschsieweke notifications at github.com
Mon Nov 16 10:49:42 CET 2020


@chrysn:

> Given that we have the mechanism of waiting for thread flags, can't a function like "lock this mutex or time out" just be implemented using `thread_flags_wait_one(THREAD_FLAG_MUTEX_READY | THREAD_FLAG_TIMEOUT)`?

That would be better, but it will sadly not work in general. E.g. let's say we have

``` C
extern mutex_t mut_a, mut_b;

void * thread_impl(void *arg) {
    while (1) {
        if (mutex_lock(&mut_a) < 0) {
            continue;
        }
        if (mutex_lock(&mut_b) == 0) {
            perform_task(a);
            mutex_unlock(&mut_b);
        }
        mutex_unlock(&mut_a);
    }
}
```

In that case, the thread could either be blocked waiting for `mut_a` or for `mut_b`. We need to make sure that we don't cancel the wrong mutex here. We cannot assume that the threads are somehow synchronized - after all the mutex is provided as a tool to synchronize threads.

(Agreed, your argument was in the context of the `xtimer_mutex_lock_timeout()` use case. But this problem applies there just as well.)

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/15442#issuecomment-727866255
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20201116/1ee49cfe/attachment-0001.htm>


More information about the notifications mailing list