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

benpicco notifications at github.com
Fri Nov 13 22:03:40 CET 2020


@benpicco commented on this pull request.

That's pretty neat!
Just the other day I was wondering how to deliver a message to a thread that is waiting for an interrupt with `mutex_lock(&m)` - now I can just 

```C
if (mutex_lock(&m) < 0) {
    msg_receive(&msg);
    …
}
```

Travis is not happy btw.

> @@ -49,6 +49,13 @@ int mutex_lock(mutex_t *mutex)
     }
 
     thread_t *me = thread_get_active();
+#if IS_USED(MODULE_CORE_MUTEX_CANCEL)
+    if (me->wait_data == mutex) {

This needs some explaining 

> @@ -60,10 +67,19 @@ int mutex_lock(mutex_t *mutex)
         thread_add_to_list(&mutex->queue, me);
     }
 
+#if IS_USED(MODULE_CORE_MUTEX_CANCEL)
+    me->wait_data = NULL;

ah so `wait_data` is used to signal if the mutex was aborted 

>  
     irq_restore(irq_state);
     thread_yield_higher();
     /* We were woken up by scheduler. Waker removed us from queue. */
+#if IS_USED(MODULE_CORE_MUTEX_CANCEL)
+    if (me->wait_data) {

before you had 

```C
if (me->wait_data == mutex)
```

is the difference intentional? 

> @@ -131,3 +147,31 @@ void mutex_unlock_and_sleep(mutex_t *mutex)
     irq_restore(irqstate);
     thread_yield_higher();
 }
+
+#if IS_USED(MODULE_CORE_MUTEX_CANCEL)
+void mutex_cancel(mutex_t *mutex, thread_t *thread)
+{
+    unsigned irq_state = irq_disable();
+    if (thread->status >= STATUS_ON_RUNQUEUE) {

```suggestion
    /* thread is still running or about to run */
    if (thread->status >= STATUS_ON_RUNQUEUE) {
```

> +    if ((mutex->queue.next != MUTEX_LOCKED) &&
+            (mutex->queue.next != NULL) &&
+            list_remove(&mutex->queue, (list_node_t *)&thread->rq_entry)) {

```suggestion
    if ((mutex->queue.next != MUTEX_LOCKED)
        && (mutex->queue.next != NULL)
        && list_remove(&mutex->queue, (list_node_t *)&thread->rq_entry)) {
```

looks a bit more tidy, but that's just my personal taste 

-- 
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#pullrequestreview-530408136
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20201113/6e7c1070/attachment-0001.htm>


More information about the notifications mailing list