aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/concurrent/parc_Notifier.h
blob: 44eda3b5b1ed6c208d651990c2d231948ed5b3d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_Notifier.h
 * @ingroup threading
 * @brief Inter-thread/process notification
 *
 * A 1-way event notification system.  The first call to parcNotifier_SetEvent() will post an
 * event to the parcNotifier_Socket().  Subsequent calls will not post an event.  When the
 * event consumer is ready to handle the event, it calls parcNotifier_PauseEvents(), then processes
 * the events, then calls parcNotifier_StartEvents().
 *
 * @code
 * {
 *    // example code on the event consumer's side
 *    struct pollfd pfd;
 *    pfd.fd = parcNotifier_Socket(notifier);
 *    pfd.events = POLLIN;
 *
 *    while(1) {
 *       if (poll(&fd, 1, -1)) {
 *           parcNotifier_PauseEvents(notifier);
 *
 *           // process events, such as reading from a RingBuffer
 *           void *data;
 *           while (parcRingBuffer1x1_Get(ring, &data)) {
 *              // handle data
 *           }
 *
 *           parcNotifier_StartEvents(notifier);
 *       }
 *    }
 * }
 * @endcode
 *
 * The notification system guarantees that no notifications will be missed.  However, there may be
 * extra notifications.  For example, in the above code, if an event is signalled between the
 * parcNotifier_PauseEvents() and parcRingBuffer1x1_Get() calls, then on parcNotifier_StartEvents()
 * an extra event will be triggered, even though the ring buffer is empty.
 *
 */

#ifndef libparc_parc_Notifier_h
#define libparc_parc_Notifier_h

#include <stdbool.h>

struct parc_notifier;
typedef struct parc_notifier PARCNotifier;

/**
 * Create a new instance of `PARCNotifier`
 *
 * @return  A new instance of `PARCNotifier`
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
PARCNotifier *parcNotifier_Create(void);

/**
 * Increase the number of references to a `PARCNotifier`.
 *
 * Note that new `PARCNotifier` is not created,
 * only that the given `PARCNotifier` reference count is incremented.
 * Discard the reference by invoking `parcNotifier_Release`.
 *
 * @param [in] instance A pointer to a `PARCNotifier` instance.
 *
 * @return The input `PARCNotifier` pointer.
 *
 * Example:
 * @code
 * {
 *     PARCNotifier *a = parcNotifier_Create(...);
 *
 *     PARCNotifier *b = parcNotifier_Acquire(a);
 *
 *     parcNotifier_Release(&a);
 *     parcNotifier_Release(&b);
 * }
 * @endcode
 */
PARCNotifier *parcNotifier_Acquire(const PARCNotifier *notifier);

/**
 * Release a previously acquired reference to the specified instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's implementation will perform
 * additional cleanup and release other privately held references.
 *
 * @param [in,out] instancePtr A pointer to a pointer to the instance to release, which will be set to NULL.
 *
 * Example:
 * @code
 * {
 *     PARCNotifier *a = parcNotifier_Create(...);
 *
 *     parcNotifier_Release(&a);
 * }
 * @endcode
 */
void parcNotifier_Release(PARCNotifier **notifier);

/**
 * Fetches the notification socket
 *
 * The notification socket may be used in select() or poll() or similar
 * functions.  You should not read or write to the socket.
 *
 * @param [in] notifier The instance of `PARCNotifier`
 *
 * @return The notification socket.
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
int parcNotifier_Socket(PARCNotifier *notifier);

/**
 * Sends a notification to the notifier socket
 *
 * @param [in] notifier The instance of `PARCNotifier`
 *
 * @return True is successsful, else false.
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
bool parcNotifier_Notify(PARCNotifier *notifier);

/**
 * Pause the event stream of the Notifier
 *
 * @param [in] notifier The instance of `PARCNotifier`
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
void parcNotifier_PauseEvents(PARCNotifier *notifier);

/**
 * Restart the event stream of the Notifier
 *
 * @param [in] notifier The instance of `PARCNotifier`
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
void parcNotifier_StartEvents(PARCNotifier *notifier);
#endif // libparc_parc_Notifier_h