summaryrefslogtreecommitdiffstats
path: root/3rdparty/openpgm-svn-r1085/pgm/include/impl/notify.h
blob: 1db4475d8b00591a2fda52a7cf45caf5b94cf216 (plain) (blame)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* vim:ts=8:sts=4:sw=4:noai:noexpandtab
 * 
 * Low kernel overhead event notify mechanism, or standard pipes.
 *
 * Copyright (c) 2008 Miru Limited.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#if !defined (__PGM_IMPL_FRAMEWORK_H_INSIDE__) && !defined (PGM_COMPILATION)
#       error "Only <framework.h> can be included directly."
#endif

#ifndef __PGM_IMPL_NOTIFY_H__
#define __PGM_IMPL_NOTIFY_H__

typedef struct pgm_notify_t pgm_notify_t;

#ifndef _WIN32
#	include <fcntl.h>
#	include <unistd.h>
#	ifdef CONFIG_HAVE_EVENTFD
#		include <sys/eventfd.h>
#	endif
#else /* _WIN32 */
#	include <memory.h>
#	include <ws2tcpip.h>
#endif
#include <pgm/types.h>
#include <impl/messages.h>

PGM_BEGIN_DECLS

struct pgm_notify_t {
#if defined(CONFIG_HAVE_EVENTFD)
	int eventfd;
#elif !defined(_WIN32)
	int pipefd[2];
#else
	SOCKET s[2];
#endif /* _WIN32 */
};

#if defined(CONFIG_HAVE_EVENTFD)
#	define PGM_NOTIFY_INIT		{ -1 }
#elif !defined(_WIN32)
#	define PGM_NOTIFY_INIT		{ { -1, -1 } }
#else
#	define PGM_NOTIFY_INIT		{ { INVALID_SOCKET, INVALID_SOCKET } }
#endif


static inline
bool
pgm_notify_is_valid (
	pgm_notify_t*	notify
	)
{
	if (PGM_UNLIKELY(NULL == notify))
		return FALSE;
#if defined(CONFIG_HAVE_EVENTFD)
	if (PGM_UNLIKELY(-1 == notify->eventfd))
		return FALSE;
#elif !defined(_WIN32)
	if (PGM_UNLIKELY(-1 == notify->pipefd[0] || -1 == notify->pipefd[1]))
		return FALSE;
#else
	if (PGM_UNLIKELY(INVALID_SOCKET == notify->s[0] || INVALID_SOCKET == notify->s[1]))
		return FALSE;
#endif /* _WIN32 */
	return TRUE;
}

static inline
int
pgm_notify_init (
	pgm_notify_t*	notify
	)
{
	pgm_assert (NULL != notify);

#if defined(CONFIG_HAVE_EVENTFD)
	notify->eventfd = -1;
	int retval = eventfd (0, 0);
	if (-1 == retval)
		return retval;
	notify->eventfd = retval;
	const int fd_flags = fcntl (notify->eventfd, F_GETFL);
	if (-1 != fd_flags)
		retval = fcntl (notify->eventfd, F_SETFL, fd_flags | O_NONBLOCK);
	return 0;
#elif !defined(_WIN32)
	notify->pipefd[0] = notify->pipefd[1] = -1;
	int retval = pipe (notify->pipefd);
	pgm_assert (0 == retval);
/* set non-blocking */
/* write-end */
	int fd_flags = fcntl (notify->pipefd[1], F_GETFL);
	if (fd_flags != -1)
		retval = fcntl (notify->pipefd[1], F_SETFL, fd_flags | O_NONBLOCK);
	pgm_assert (notify->pipefd[1]);
/* read-end */
	fd_flags = fcntl (notify->pipefd[0], F_GETFL);
	if (fd_flags != -1)
		retval = fcntl (notify->pipefd[0], F_SETFL, fd_flags | O_NONBLOCK);
	pgm_assert (notify->pipefd[0]);
	return retval;
#else
/* use loopback sockets to simulate a pipe suitable for win32/select() */
	struct sockaddr_in addr;
	SOCKET listener;
	int addrlen = sizeof (addr);

	notify->s[0] = notify->s[1] = INVALID_SOCKET;

	listener = socket (AF_INET, SOCK_STREAM, 0);
	pgm_assert (listener != INVALID_SOCKET);

	memset (&addr, 0, sizeof (addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = inet_addr ("127.0.0.1");
	pgm_assert (addr.sin_addr.s_addr != INADDR_NONE);

	int rc = bind (listener, (const struct sockaddr*)&addr, sizeof (addr));
	pgm_assert (rc != SOCKET_ERROR);

	rc = getsockname (listener, (struct sockaddr*)&addr, &addrlen);
	pgm_assert (rc != SOCKET_ERROR);

// Listen for incoming connections.
	rc = listen (listener, 1);
	pgm_assert (rc != SOCKET_ERROR);

// Create the socket.
	notify->s[1] = WSASocket (AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
	pgm_assert (notify->s[1] != INVALID_SOCKET);

// Connect to the remote peer.
	rc = connect (notify->s[1], (struct sockaddr*)&addr, addrlen);
	pgm_assert (rc != SOCKET_ERROR);

// Accept connection.
	notify->s[0] = accept (listener, NULL, NULL);
	pgm_assert (notify->s[0] != INVALID_SOCKET);

// Set read-end to non-blocking mode
	const unsigned long one = 1;
	rc = ioctlsocket (notify->s[0], FIONBIO, &one);
	pgm_assert (rc != SOCKET_ERROR);

// We don't need the listening socket anymore. Close it.
	rc = closesocket (listener);
	pgm_assert (rc != SOCKET_ERROR);

	return 0;
#endif
}

static inline
int
pgm_notify_destroy (
	pgm_notify_t*	notify
	)
{
	pgm_assert (NULL != notify);

#if defined(CONFIG_HAVE_EVENTFD)
	if (-1 != notify->eventfd) {
		close (notify->eventfd);
		notify->eventfd = -1;
	}
#elif !defined(_WIN32)
	if (-1 != notify->pipefd[0]) {
		close (notify->pipefd[0]);
		notify->pipefd[0] = -1;
	}
	if (-1 != notify->pipefd[1]) {
		close (notify->pipefd[1]);
		notify->pipefd[1] = -1;
	}
#else
	if (INVALID_SOCKET != notify->s[0]) {
		closesocket (notify->s[0]);
		notify->s[0] = INVALID_SOCKET;
	}
	if (INVALID_SOCKET != notify->s[1]) {
		closesocket (notify->s[1]);
		notify->s[1] = INVALID_SOCKET;
	}
#endif
	return 0;
}

static inline
int
pgm_notify_send (
	pgm_notify_t*	notify
	)
{
	pgm_assert (NULL != notify);

#if defined(CONFIG_HAVE_EVENTFD)
	pgm_assert (-1 != notify->eventfd);
	uint64_t u = 1;
	ssize_t s = write (notify->eventfd, &u, sizeof(u));
	return (s == sizeof(u));
#elif !defined(_WIN32)
	pgm_assert (-1 != notify->pipefd[1]);
	const char one = '1';
	return (1 == write (notify->pipefd[1], &one, sizeof(one)));
#else
	pgm_assert (INVALID_SOCKET != notify->s[1]);
	const char one = '1';
	return (1 == send (notify->s[1], &one, sizeof(one), 0));
#endif
}

static inline
int
pgm_notify_read (
	pgm_notify_t*	notify
	)
{
	pgm_assert (NULL != notify);

#if defined(CONFIG_HAVE_EVENTFD)
	pgm_assert (-1 != notify->eventfd);
	uint64_t u;
	return (sizeof(u) == read (notify->eventfd, &u, sizeof(u)));
#elif !defined(_WIN32)
	pgm_assert (-1 != notify->pipefd[0]);
	char buf;
	return (sizeof(buf) == read (notify->pipefd[0], &buf, sizeof(buf)));
#else
	pgm_assert (INVALID_SOCKET != notify->s[0]);
	char buf;
	return (sizeof(buf) == recv (notify->s[0], &buf, sizeof(buf), 0));
#endif
}

static inline
void
pgm_notify_clear (
	pgm_notify_t*	notify
	)
{
	pgm_assert (NULL != notify);

#if defined(CONFIG_HAVE_EVENTFD)
	pgm_assert (-1 != notify->eventfd);
	uint64_t u;
	while (sizeof(u) == read (notify->eventfd, &u, sizeof(u)));
#elif !defined(_WIN32)
	pgm_assert (-1 != notify->pipefd[0]);
	char buf;
	while (sizeof(buf) == read (notify->pipefd[0], &buf, sizeof(buf)));
#else
	pgm_assert (INVALID_SOCKET != notify->s[0]);
	char buf;
	while (sizeof(buf) == recv (notify->s[0], &buf, sizeof(buf), 0));
#endif
}

static inline
int
pgm_notify_get_fd (
	pgm_notify_t*	notify
	)
{
	pgm_assert (NULL != notify);

#if defined(CONFIG_HAVE_EVENTFD)
	pgm_assert (-1 != notify->eventfd);
	return notify->eventfd;
#elif !defined(_WIN32)
	pgm_assert (-1 != notify->pipefd[0]);
	return notify->pipefd[0];
#else
	pgm_assert (INVALID_SOCKET != notify->s[0]);
	return notify->s[0];
#endif
}

PGM_END_DECLS

#endif /* __PGM_IMPL_NOTIFY_H__ */