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

Marian Buschsieweke notifications at github.com
Wed Nov 18 21:11:10 CET 2020


@maribu commented on this pull request.



> +              thread_getpid());
+        irq_restore(irq_state);
+    }
+    else {
+        _block(mutex, irq_state);
+    }
+}
+
+int mutex_lock_cancelable(mutex_cancel_t *mc)
+{
+    unsigned irq_state = irq_disable();
+
+    DEBUG("PID[%" PRIkernel_pid "]: mutex_lock_cancelable()\n",
+          thread_getpid());
+
+    if (mc->cancelled) {

This won't work. The usual use case looks like this:

```C
static void timeout_cb(void *_arg) {
    mutex_cancel(arg);
}

int ztimer_mutex_lock_timeout(ztimer_clock_t *clock, mutex_t *mutex,
                              uint32_t timeout)
{
    mutex_cancel_t mc = mutex_cancel_init(mutex);
    ztimer_t t;
    t.callback = timeout_cb;
    t.arg = &mc;
    ztimer_set(clock, &t, timeout);
    if (0 == mutex_lock_cancelable(mutex)) {
        /* BANG! */
        ztimer_remove(clock, &t);
        return 0;
    }
    return -ECANCELED;
}
```

Let's say the timer expired at `/* BANG! */`. In that case `mc->cancel` will be set to `1` and `mutex_lock_cancelable()` has no chance to reset it to zero. The `mutex_cancel_t` structure must always be reset only after the IRQ triggering the cancel is disabled (or the thread sending the cancel is notified to no longer cancel the mutex).

-- 
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#discussion_r526387915
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20201118/ac3c5804/attachment.htm>


More information about the notifications mailing list