summaryrefslogtreecommitdiffstats
path: root/3rdparty/openpgm-svn-r1085/pgm/examples/async.h
blob: 788a777a4e38a2197d8de4797157cacca6f365e3 (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
/* vim:ts=8:sts=4:sw=4:noai:noexpandtab
 * 
 * Asynchronous receive thread helper
 *
 * Copyright (c) 2006-2009 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
 */

#ifndef __PGM_ASYNC_H__
#define __PGM_ASYNC_H__

struct async_event_t;

#include <errno.h>
#include <pgm/pgm.h>

#ifdef  __cplusplus
extern "C" {
#endif

struct async_t {
	pgm_sock_t*		sock;
#ifndef _WIN32
	pthread_t		thread;
	int			notify_pipe[2];
	int			destroy_pipe[2];
	pthread_mutex_t		pthread_mutex;
#else
	HANDLE			thread;
	HANDLE			notify_event;
	HANDLE			destroy_event;
	HANDLE			win32_mutex;
#endif
	struct async_event_t    *head, *tail;
	unsigned		length;
	bool			is_destroyed;
};
typedef struct async_t async_t;

int async_create (async_t** restrict, pgm_sock_t*const restrict);
int async_destroy (async_t* const);
ssize_t async_recv (async_t*const restrict, void* restrict, size_t);
ssize_t async_recvfrom (async_t*const restrict, void*restrict, size_t, struct pgm_sockaddr_t*restrict, socklen_t*restrict);

#ifndef _WIN32
static inline int async_get_fd (async_t* async)
{
	if (NULL == async) {
		errno = EINVAL;
		return -1;
	}
	return async->notify_pipe[0];
}
#else
static inline HANDLE async_get_event (async_t* async)
{
	if (NULL == async) {
		errno = EINVAL;
		return NULL;
	}
	return async->notify_event;
}
#endif /* _WIN32 */

#ifdef  __cplusplus
}
#endif

#endif /* __PGM_ASYNC_H__ */