diff options
author | Richard Braun <rbraun@sceen.net> | 2018-04-01 07:01:19 +0200 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2018-04-01 07:01:19 +0200 |
commit | cb86e4abaa7f3419ea0e096e5ca80ede5505a696 (patch) | |
tree | fe16a41d98641b45bc35177806fd16a4a09f6001 /kern/bulletin.c | |
parent | 076476fe6c0f4b947d4441afcfc97561bb23711c (diff) |
kern/bulletin: new module
Diffstat (limited to 'kern/bulletin.c')
-rw-r--r-- | kern/bulletin.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/kern/bulletin.c b/kern/bulletin.c new file mode 100644 index 00000000..bbb1bfc3 --- /dev/null +++ b/kern/bulletin.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2017-2018 Richard Braun. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <assert.h> +#include <stdint.h> + +#include <kern/bulletin.h> +#include <kern/list.h> +#include <kern/rcu.h> +#include <kern/spinlock.h> +#include <kern/thread.h> + +static void +bulletin_sub_init(struct bulletin_sub *sub, + bulletin_notif_fn_t notif_fn, void *arg) +{ + sub->notif_fn = notif_fn; + sub->arg = arg; +} + +static void +bulletin_sub_notify(const struct bulletin_sub *sub, uintptr_t value) +{ + sub->notif_fn(value, sub->arg); +} + +void +bulletin_init(struct bulletin *bulletin) +{ + spinlock_init(&bulletin->lock); + list_init(&bulletin->subs); +} + +void +bulletin_subscribe(struct bulletin *bulletin, struct bulletin_sub *sub, + bulletin_notif_fn_t notif_fn, void *arg) +{ + bulletin_sub_init(sub, notif_fn, arg); + + spinlock_lock(&bulletin->lock); + list_rcu_insert_tail(&bulletin->subs, &sub->node); + spinlock_unlock(&bulletin->lock); +} + +void +bulletin_unsubscribe(struct bulletin *bulletin, struct bulletin_sub *sub) +{ + spinlock_lock(&bulletin->lock); + list_rcu_remove(&sub->node); + spinlock_unlock(&bulletin->lock); + + rcu_wait(); +} + +void +bulletin_publish(struct bulletin *bulletin, uintptr_t value) +{ + struct bulletin_sub *sub; + + assert(!thread_interrupted()); + + rcu_read_enter(); + + list_rcu_for_each_entry(&bulletin->subs, sub, node) { + bulletin_sub_notify(sub, value); + } + + rcu_read_leave(); +} |