[riot-notifications] [RIOT-OS/RIOT] core: Added thread_signal & thread_await_signal (#11006)

Marian Buschsieweke notifications at github.com
Wed Feb 13 12:04:58 CET 2019


I assume what you wanted to write is:

``` C
typedef mutex_t thread_signal_t;
thread_signal(thread_signal_t *signal) {
 mutex_unlock(signal);
}
thread_signal_await(thread_signal_t *signal) {
 mutex_lock(signal);
 mutex_lock(signal);
 mutex_unlock(signal);
}
```

(Only calling `mutex_lock()` while the mutex is locked will block.)

This would behave exactly as `thread_sleep()` and `thread_wakeup()`: If `mutex_unlock()` would be called without the mutex being locked, nothing would happen. If afterwards the other thread would call `mutex_lock()` it would block - but for the use case `thread_signal()` and `thread_await_signal()` want to address this will not work.

Using mutex to implement it could be done like this:

``` C
typedef enum {
    THREAD_AWAIT_SIGNAL,
    THREAD_SIGNAL_RECEIVED,
} thread_signal_status_t;

typedef struct {
    mutex_t mutex;
    thread_signal_status_t status;
} thread_signal_t;

thread_signal(thread_signal_t *signal) {
    unsigned irq_status = irq_disable();
    signal->status = THREAD_SIGNAL_RECEIVED;
    mutex_unlock(&signal->mutex);
    irq_restore(irq_status);
}

thread_await_signal(thread_signal_t *signal){
    mutex_lock(&signal->mutex);
    if (signal->status == THREAD_SIGNAL_RECEIVED) {
        signal->status = THREAD_AWAIT_SIGNAL;
        mutex_unlock(&signal->mutex);
        return;
    }
    mutex_lock(&signal->mutex);
    signal->status = THREAD_AWAIT_SIGNAL;
    mutex_unlock(&signal->mutex);
}
```

Should I implement it as sketched above, so that we can compare the two approaches?

-- 
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/11006#issuecomment-463157249
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190213/477f138a/attachment.html>


More information about the notifications mailing list