bulletin.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. *
  18. * Minimalist publish-subscribe mechanism.
  19. */
  20. #ifndef KERN_BULLETIN_H
  21. #define KERN_BULLETIN_H
  22. #include <stdint.h>
  23. #include <kern/list_types.h>
  24. #include <kern/macros.h>
  25. #include <kern/spinlock_types.h>
  26. #include <kern/work.h>
  27. /*
  28. * Type for bulletin notification functions.
  29. *
  30. * The value is passed from the publisher unmodified, and can safely be
  31. * cast into a pointer. Notification functions run in the context of the
  32. * publisher.
  33. */
  34. typedef void (*bulletin_notif_fn_t) (uintptr_t, void *);
  35. // Bulletin subscriber.
  36. struct bulletin_sub
  37. {
  38. struct list node;
  39. bulletin_notif_fn_t notif_fn;
  40. void *arg;
  41. };
  42. struct bulletin
  43. {
  44. struct spinlock lock;
  45. struct list subs;
  46. };
  47. void bulletin_init (struct bulletin *bulletin);
  48. /*
  49. * Subscribe to a bulletin.
  50. *
  51. * Once subscribed, the notification function is called with its argument
  52. * each time the bulletin is published.
  53. */
  54. void bulletin_subscribe (struct bulletin *bulletin, struct bulletin_sub *sub,
  55. bulletin_notif_fn_t notif_fn, void *arg);
  56. /*
  57. * Unsubscribe from a bulletin.
  58. *
  59. * On return, the subscriber notification function may not be called any more.
  60. *
  61. * This function synchronizes with RCU.
  62. */
  63. void bulletin_unsubscribe (struct bulletin *bulletin, struct bulletin_sub *sub);
  64. /*
  65. * Publish a bulletin.
  66. *
  67. * All subscribers are notified by calling their notification function, with
  68. * the given value passed unmodified.
  69. */
  70. void bulletin_publish (struct bulletin *bulletin, uintptr_t value);
  71. #endif