1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <unistd.h>
18 #include <errno.h>
19 #include <poll.h>
20 #include <sys/eventfd.h>
21
22 #include "mpp_eventfd.h"
23
mpp_eventfd_get(RK_U32 init)24 RK_S32 mpp_eventfd_get(RK_U32 init)
25 {
26 RK_S32 fd = eventfd(init, 0);
27
28 if (fd < 0)
29 fd = errno;
30
31 return fd;
32 }
33
mpp_eventfd_put(RK_S32 fd)34 RK_S32 mpp_eventfd_put(RK_S32 fd)
35 {
36 if (fd >= 0)
37 close(fd);
38
39 return 0;
40 }
41
mpp_eventfd_read(RK_S32 fd,RK_U64 * val,RK_S64 timeout)42 RK_S32 mpp_eventfd_read(RK_S32 fd, RK_U64 *val, RK_S64 timeout)
43 {
44 RK_S32 ret = 0;
45 struct pollfd nfds;
46 RK_U64 tmp = 0;
47
48 if (NULL == val)
49 val = &tmp;
50
51 nfds.fd = fd;
52 nfds.events = POLLIN;
53
54 ret = poll(&nfds, 1, timeout);
55 if (ret == 1 && (nfds.revents & POLLIN) &&
56 sizeof(RK_U64) == read(fd, val, sizeof(RK_U64))) {
57 ret = 0;
58 } else
59 ret = errno;
60
61 return ret;
62 }
63
mpp_eventfd_write(RK_S32 fd,RK_U64 val)64 RK_S32 mpp_eventfd_write(RK_S32 fd, RK_U64 val)
65 {
66 RK_S32 ret = 0;
67
68 if (sizeof(RK_U64) != write(fd, &val, sizeof(val)))
69 ret = errno;
70
71 return ret;
72 }
73