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
|
/*
* Copyright (C) 2013-2020 Argonne National Laboratory, Department of Energy,
* UChicago Argonne, LLC and The HDF Group.
* All rights reserved.
*
* The full copyright notice, including terms governing use, modification,
* and redistribution, is contained in the COPYING file that can be
* found at the root of the source code distribution tree.
*/
#ifndef MERCURY_EVENT_H
#define MERCURY_EVENT_H
#include "mercury_util_config.h"
#ifdef _WIN32
#else
#include <errno.h>
#include <string.h>
#include <unistd.h>
#if defined(HG_UTIL_HAS_SYSEVENTFD_H)
#include <sys/eventfd.h>
#ifndef HG_UTIL_HAS_EVENTFD_T
typedef uint64_t eventfd_t;
#endif
#elif defined(HG_UTIL_HAS_SYSEVENT_H)
#include <sys/event.h>
#define HG_EVENT_IDENT 42 /* User-defined ident */
#endif
#endif
/**
* Purpose: define an event object that can be used as an event
* wait/notify mechanism.
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* Create a new event object.
*
* \return file descriptor on success or negative on failure
*/
HG_UTIL_PUBLIC int hg_event_create(void);
/**
* Destroy an event object.
*
* \param fd [IN] event file descriptor
*
* \return Non-negative on success or negative on failure
*/
HG_UTIL_PUBLIC int hg_event_destroy(int fd);
/**
* Notify for event.
*
* \param fd [IN] event file descriptor
*
* \return Non-negative on success or negative on failure
*/
static HG_UTIL_INLINE int hg_event_set(int fd);
/**
* Get event notification.
*
* \param fd [IN] event file descriptor
* \param notified [IN] boolean set to HG_UTIL_TRUE if event received
*
* \return Non-negative on success or negative on failure
*/
static HG_UTIL_INLINE int hg_event_get(int fd, hg_util_bool_t *notified);
/*---------------------------------------------------------------------------*/
#if defined(_WIN32)
/* TODO */
#elif defined(HG_UTIL_HAS_SYSEVENTFD_H)
#ifdef HG_UTIL_HAS_EVENTFD_T
static HG_UTIL_INLINE int
hg_event_set(int fd)
{
return (eventfd_write(fd, 1) == 0) ? HG_UTIL_SUCCESS : HG_UTIL_FAIL;
}
#else
static HG_UTIL_INLINE int
hg_event_set(int fd)
{
eventfd_t count = 1;
ssize_t s = write(fd, &count, sizeof(eventfd_t));
return (s == sizeof(eventfd_t)) ? HG_UTIL_SUCCESS : HG_UTIL_FAIL;
}
#endif
#elif defined(HG_UTIL_HAS_SYSEVENT_H)
static HG_UTIL_INLINE int
hg_event_set(int fd)
{
struct kevent kev;
struct timespec timeout = {0, 0};
int rc;
EV_SET(&kev, HG_EVENT_IDENT, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
/* Trigger user-defined event */
rc = kevent(fd, &kev, 1, NULL, 0, &timeout);
return (rc == -1) ? HG_UTIL_FAIL : HG_UTIL_SUCCESS;
}
#else
#error "Not supported on this platform."
#endif
/*---------------------------------------------------------------------------*/
#if defined(_WIN32)
#elif defined(HG_UTIL_HAS_SYSEVENTFD_H)
#ifdef HG_UTIL_HAS_EVENTFD_T
static HG_UTIL_INLINE int
hg_event_get(int fd, hg_util_bool_t *signaled)
{
eventfd_t count = 0;
if ((eventfd_read(fd, &count) == 0) && count)
*signaled = HG_UTIL_TRUE;
else {
if (errno == EAGAIN)
*signaled = HG_UTIL_FALSE;
else
return HG_UTIL_FAIL;
}
return HG_UTIL_SUCCESS;
}
#else
static HG_UTIL_INLINE int
hg_event_get(int fd, hg_util_bool_t *signaled)
{
eventfd_t count = 0;
ssize_t s = read(fd, &count, sizeof(eventfd_t));
if ((s == sizeof(eventfd_t)) && count)
*signaled = HG_UTIL_TRUE;
else {
if (errno == EAGAIN)
*signaled = HG_UTIL_FALSE;
else
return HG_UTIL_FAIL;
}
return HG_UTIL_SUCCESS;
}
#endif
#elif defined(HG_UTIL_HAS_SYSEVENT_H)
static HG_UTIL_INLINE int
hg_event_get(int fd, hg_util_bool_t *signaled)
{
struct kevent kev;
int nfds;
struct timespec timeout = {0, 0};
/* Check user-defined event */
nfds = kevent(fd, NULL, 0, &kev, 1, &timeout);
if (nfds == -1)
return HG_UTIL_FAIL;
*signaled = ((nfds > 0) && (kev.ident == HG_EVENT_IDENT)) ? HG_UTIL_TRUE : HG_UTIL_FALSE;
return HG_UTIL_SUCCESS;
}
#else
#error "Not supported on this platform."
#endif
#ifdef __cplusplus
}
#endif
#endif /* MERCURY_EVENT_H */
|