tor  master
pubsub.h
Go to the documentation of this file.
1 /* Copyright (c) 2016-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
42 #ifndef TOR_PUBSUB_H
43 #define TOR_PUBSUB_H
44 
45 #include "torint.h"
46 
52 #define SUBSCRIBE_ATSTART (1u<<0)
53 
54 #define DECLARE_PUBSUB_STRUCT_TYPES(name) \
55  /* You define this type. */ \
56  typedef struct name ## _event_data_t name ## _event_data_t; \
57  /* You define this type. */ \
58  typedef struct name ## _subscriber_data_t name ## _subscriber_data_t;
59 
60 #define DECLARE_PUBSUB_TOPIC(name) \
61  /* This type is opaque. */ \
62  typedef struct name ## _subscriber_t name ## _subscriber_t; \
63  /* You declare functions matching this type. */ \
64  typedef int (*name ## _subscriber_fn_t)( \
65  name ## _event_data_t *data, \
66  name ## _subscriber_data_t *extra); \
67  /* Call this function to subscribe to a topic. */ \
68  const name ## _subscriber_t *name ## _subscribe( \
69  name##_subscriber_fn_t subscriber, \
70  name##_subscriber_data_t *extra_data, \
71  unsigned flags, \
72  unsigned priority); \
73  /* Call this function to unsubscribe from a topic. */ \
74  int name ## _unsubscribe(const name##_subscriber_t *s);
75 
76 #define DECLARE_NOTIFY_PUBSUB_TOPIC(linkage, name) \
77  /* Call this function to notify all subscribers. Flags not yet used. */ \
78  linkage int name ## _notify(name ## _event_data_t *data, unsigned flags); \
79  /* Call this function to release storage held by the topic. */ \
80  linkage void name ## _clear(void);
81 
90 typedef int (*pubsub_subscriber_fn_t)(void *, void *);
91 
96 typedef struct pubsub_subscriber_t {
102  unsigned priority;
106 
111 typedef struct pubsub_topic_t {
116  uint64_t n_events_fired;
119  unsigned locked;
121 
124  void *subscriber_data,
125  unsigned subscribe_flags,
126  unsigned priority);
128 void pubsub_clear_(pubsub_topic_t *topic);
129 typedef int (*pubsub_notify_fn_t)(pubsub_subscriber_t *subscriber,
130  void *notify_data);
131 int pubsub_notify_(pubsub_topic_t *topic, pubsub_notify_fn_t notify_fn,
132  void *notify_data, unsigned notify_flags);
133 
134 #define IMPLEMENT_PUBSUB_TOPIC(notify_linkage, name) \
135  static pubsub_topic_t name ## _topic_ = { NULL, 0, 0 }; \
136  const name ## _subscriber_t * \
137  name ## _subscribe(name##_subscriber_fn_t subscriber, \
138  name##_subscriber_data_t *extra_data, \
139  unsigned flags, \
140  unsigned priority) \
141  { \
142  const pubsub_subscriber_t *s; \
143  s = pubsub_subscribe_(&name##_topic_, \
144  (pubsub_subscriber_fn_t)subscriber, \
145  extra_data, \
146  flags, \
147  priority); \
148  return (const name##_subscriber_t *)s; \
149  } \
150  int \
151  name ## _unsubscribe(const name##_subscriber_t *subscriber) \
152  { \
153  return pubsub_unsubscribe_(&name##_topic_, \
154  (const pubsub_subscriber_t *)subscriber); \
155  } \
156  static int \
157  name##_call_the_notify_fn_(pubsub_subscriber_t *subscriber, \
158  void *notify_data) \
159  { \
160  name ## _subscriber_fn_t fn; \
161  fn = (name ## _subscriber_fn_t) subscriber->fn; \
162  return fn(notify_data, subscriber->subscriber_data); \
163  } \
164  notify_linkage int \
165  name ## _notify(name ## _event_data_t *event_data, unsigned flags) \
166  { \
167  return pubsub_notify_(&name##_topic_, \
168  name##_call_the_notify_fn_, \
169  event_data, \
170  flags); \
171  } \
172  notify_linkage void \
173  name ## _clear(void) \
174  { \
175  pubsub_clear_(&name##_topic_); \
176  }
177 
178 #endif /* !defined(TOR_PUBSUB_H) */
179 
int pubsub_notify_(pubsub_topic_t *topic, pubsub_notify_fn_t notify_fn, void *notify_data, unsigned notify_flags)
Definition: pubsub.c:91
unsigned locked
Definition: pubsub.h:119
Definition: pubsub.h:111
Definition: pubsub.h:96
unsigned subscriber_flags
Definition: pubsub.h:104
const pubsub_subscriber_t * pubsub_subscribe_(pubsub_topic_t *topic, pubsub_subscriber_fn_t fn, void *subscriber_data, unsigned subscribe_flags, unsigned priority)
Definition: pubsub.c:41
Definition: container.h:18
Header file to define uint32_t and friends.
struct pubsub_topic_t pubsub_topic_t
int pubsub_unsubscribe_(pubsub_topic_t *topic, const pubsub_subscriber_t *sub)
Definition: pubsub.c:68
struct pubsub_subscriber_t pubsub_subscriber_t
unsigned priority
Definition: pubsub.h:102
void * subscriber_data
Definition: pubsub.h:100
void pubsub_clear_(pubsub_topic_t *topic)
Definition: pubsub.c:115
struct smartlist_t * subscribers
Definition: pubsub.h:113
int(* pubsub_subscriber_fn_t)(void *, void *)
Definition: pubsub.h:90
pubsub_subscriber_fn_t fn
Definition: pubsub.h:98
uint64_t n_events_fired
Definition: pubsub.h:116