pubsub.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file pubsub.h
  5. * \brief Macros to implement publish/subscribe abstractions.
  6. *
  7. * To use these macros, call DECLARE_PUBSUB_TOPIC() with an identifier to use
  8. * as your topic. Below, I'm going to assume you say DECLARE_PUBSUB_TOPIC(T).
  9. *
  10. * Doing this will declare the following types:
  11. * typedef struct T_event_data_t T_event_data_t; // you define this struct
  12. * typedef struct T_subscriber_data_t T_subscriber_data_t; // this one too.
  13. * typedef struct T_subscriber_t T_subscriber_t; // opaque
  14. * typedef int (*T_subscriber_fn_t)(T_event_data_t*, T_subscriber_data_t*);
  15. *
  16. * and it will declare the following functions:
  17. * const T_subscriber_t *T_subscribe(T_subscriber_fn_t,
  18. * T_subscriber_data_t *,
  19. * unsigned flags,
  20. * unsigned priority);
  21. * int T_unsubscribe(const T_subscriber_t *)
  22. *
  23. * Elsewhere you can say DECLARE_NOTIFY_PUBSUB_TOPIC(static, T), which declares:
  24. * static int T_notify(T_event_data_t *, unsigned notify_flags);
  25. * static void T_clear(void);
  26. *
  27. * And in some C file, you would define these functions with:
  28. * IMPLEMENT_PUBSUB_TOPIC(static, T).
  29. *
  30. * The implementations will be small typesafe wrappers over generic versions
  31. * of the above functions.
  32. *
  33. * To use the typesafe functions, you add any number of subscribers with
  34. * T_subscribe(). Each has an associated function pointer, data pointer,
  35. * and priority. Later, you can invoke T_notify() to declare that the
  36. * event has occurred. Each of the subscribers will be invoked once.
  37. **/
  38. #ifndef TOR_PUBSUB_H
  39. #define TOR_PUBSUB_H
  40. #include "torint.h"
  41. /**
  42. * Flag for T_subscribe: die with an assertion failure if the event
  43. * have ever been published before. Used when a subscriber must absolutely
  44. * never have missed an event.
  45. */
  46. #define SUBSCRIBE_ATSTART (1u<<0)
  47. #define DECLARE_PUBSUB_STRUCT_TYPES(name) \
  48. /* You define this type. */ \
  49. typedef struct name ## _event_data_t name ## _event_data_t; \
  50. /* You define this type. */ \
  51. typedef struct name ## _subscriber_data_t name ## _subscriber_data_t;
  52. #define DECLARE_PUBSUB_TOPIC(name) \
  53. /* This type is opaque. */ \
  54. typedef struct name ## _subscriber_t name ## _subscriber_t; \
  55. /* You declare functions matching this type. */ \
  56. typedef int (*name ## _subscriber_fn_t)( \
  57. name ## _event_data_t *data, \
  58. name ## _subscriber_data_t *extra); \
  59. /* Call this function to subscribe to a topic. */ \
  60. const name ## _subscriber_t *name ## _subscribe( \
  61. name##_subscriber_fn_t subscriber, \
  62. name##_subscriber_data_t *extra_data, \
  63. unsigned flags, \
  64. unsigned priority); \
  65. /* Call this function to unsubscribe from a topic. */ \
  66. int name ## _unsubscribe(const name##_subscriber_t *s);
  67. #define DECLARE_NOTIFY_PUBSUB_TOPIC(linkage, name) \
  68. /* Call this function to notify all subscribers. Flags not yet used. */ \
  69. linkage int name ## _notify(name ## _event_data_t *data, unsigned flags); \
  70. /* Call this function to release storage held by the topic. */ \
  71. linkage void name ## _clear(void);
  72. /**
  73. * Type used to hold a generic function for a subscriber.
  74. *
  75. * [Yes, it is safe to cast to this, so long as we cast back to the original
  76. * type before calling. From C99: "A pointer to a function of one type may be
  77. * converted to a pointer to a function of another type and back again; the
  78. * result shall compare equal to the original pointer."]
  79. */
  80. typedef int (*pubsub_subscriber_fn_t)(void *, void *);
  81. /**
  82. * Helper type to implement pubsub abstraction. Don't use this directly.
  83. * It represents a subscriber.
  84. */
  85. typedef struct pubsub_subscriber_t {
  86. /** Function to invoke when the event triggers. */
  87. pubsub_subscriber_fn_t fn;
  88. /** Data associated with this subscriber. */
  89. void *subscriber_data;
  90. /** Priority for this subscriber. Low priorities happen first. */
  91. unsigned priority;
  92. /** Flags set on this subscriber. Not yet used.*/
  93. unsigned subscriber_flags;
  94. } pubsub_subscriber_t;
  95. /**
  96. * Helper type to implement pubsub abstraction. Don't use this directly.
  97. * It represents a topic, and keeps a record of subscribers.
  98. */
  99. typedef struct pubsub_topic_t {
  100. /** List of subscribers to this topic. May be NULL. */
  101. struct smartlist_t *subscribers;
  102. /** Total number of times that pubsub_notify_() has ever been called on this
  103. * topic. */
  104. uint64_t n_events_fired;
  105. /** True iff we're running 'notify' on this topic, and shouldn't allow
  106. * any concurrent modifications or events. */
  107. unsigned locked;
  108. } pubsub_topic_t;
  109. const pubsub_subscriber_t *pubsub_subscribe_(pubsub_topic_t *topic,
  110. pubsub_subscriber_fn_t fn,
  111. void *subscriber_data,
  112. unsigned subscribe_flags,
  113. unsigned priority);
  114. int pubsub_unsubscribe_(pubsub_topic_t *topic, const pubsub_subscriber_t *sub);
  115. void pubsub_clear_(pubsub_topic_t *topic);
  116. typedef int (*pubsub_notify_fn_t)(pubsub_subscriber_t *subscriber,
  117. void *notify_data);
  118. int pubsub_notify_(pubsub_topic_t *topic, pubsub_notify_fn_t notify_fn,
  119. void *notify_data, unsigned notify_flags);
  120. #define IMPLEMENT_PUBSUB_TOPIC(notify_linkage, name) \
  121. static pubsub_topic_t name ## _topic_ = { NULL, 0, 0 }; \
  122. const name ## _subscriber_t * \
  123. name ## _subscribe(name##_subscriber_fn_t subscriber, \
  124. name##_subscriber_data_t *extra_data, \
  125. unsigned flags, \
  126. unsigned priority) \
  127. { \
  128. const pubsub_subscriber_t *s; \
  129. s = pubsub_subscribe_(&name##_topic_, \
  130. (pubsub_subscriber_fn_t)subscriber, \
  131. extra_data, \
  132. flags, \
  133. priority); \
  134. return (const name##_subscriber_t *)s; \
  135. } \
  136. int \
  137. name ## _unsubscribe(const name##_subscriber_t *subscriber) \
  138. { \
  139. return pubsub_unsubscribe_(&name##_topic_, \
  140. (const pubsub_subscriber_t *)subscriber); \
  141. } \
  142. static int \
  143. name##_call_the_notify_fn_(pubsub_subscriber_t *subscriber, \
  144. void *notify_data) \
  145. { \
  146. name ## _subscriber_fn_t fn; \
  147. fn = (name ## _subscriber_fn_t) subscriber->fn; \
  148. return fn(notify_data, subscriber->subscriber_data); \
  149. } \
  150. notify_linkage int \
  151. name ## _notify(name ## _event_data_t *event_data, unsigned flags) \
  152. { \
  153. return pubsub_notify_(&name##_topic_, \
  154. name##_call_the_notify_fn_, \
  155. event_data, \
  156. flags); \
  157. } \
  158. notify_linkage void \
  159. name ## _clear(void) \
  160. { \
  161. pubsub_clear_(&name##_topic_); \
  162. }
  163. #endif /* TOR_PUBSUB_H */