bulletin.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2017-2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <assert.h>
  18. #include <stdint.h>
  19. #include <kern/bulletin.h>
  20. #include <kern/list.h>
  21. #include <kern/rcu.h>
  22. #include <kern/spinlock.h>
  23. #include <kern/thread.h>
  24. static void
  25. bulletin_sub_init (struct bulletin_sub *sub,
  26. bulletin_notif_fn_t notif_fn, void *arg)
  27. {
  28. sub->notif_fn = notif_fn;
  29. sub->arg = arg;
  30. }
  31. static void
  32. bulletin_sub_notify (const struct bulletin_sub *sub, uintptr_t value)
  33. {
  34. sub->notif_fn (value, sub->arg);
  35. }
  36. void
  37. bulletin_init (struct bulletin *bulletin)
  38. {
  39. spinlock_init (&bulletin->lock);
  40. list_init (&bulletin->subs);
  41. }
  42. void
  43. bulletin_subscribe (struct bulletin *bulletin, struct bulletin_sub *sub,
  44. bulletin_notif_fn_t notif_fn, void *arg)
  45. {
  46. bulletin_sub_init (sub, notif_fn, arg);
  47. SPINLOCK_GUARD (&bulletin->lock);
  48. list_rcu_insert_tail (&bulletin->subs, &sub->node);
  49. }
  50. void
  51. bulletin_unsubscribe (struct bulletin *bulletin, struct bulletin_sub *sub)
  52. {
  53. spinlock_lock (&bulletin->lock);
  54. list_rcu_remove (&sub->node);
  55. spinlock_unlock (&bulletin->lock);
  56. rcu_wait ();
  57. }
  58. void
  59. bulletin_publish (struct bulletin *bulletin, uintptr_t value)
  60. {
  61. assert (!thread_interrupted ());
  62. RCU_GUARD ();
  63. struct bulletin_sub *sub;
  64. list_rcu_for_each_entry (&bulletin->subs, sub, node)
  65. bulletin_sub_notify (sub, value);
  66. }