mpp_eventfd.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include <unistd.h>
  17. #include <errno.h>
  18. #include <poll.h>
  19. #include <sys/eventfd.h>
  20. #include "mpp_eventfd.h"
  21. RK_S32 mpp_eventfd_get(RK_U32 init)
  22. {
  23. RK_S32 fd = eventfd(init, 0);
  24. if (fd < 0)
  25. fd = errno;
  26. return fd;
  27. }
  28. RK_S32 mpp_eventfd_put(RK_S32 fd)
  29. {
  30. if (fd >= 0)
  31. close(fd);
  32. return 0;
  33. }
  34. RK_S32 mpp_eventfd_read(RK_S32 fd, RK_U64 *val, RK_S64 timeout)
  35. {
  36. RK_S32 ret = 0;
  37. struct pollfd nfds;
  38. RK_U64 tmp = 0;
  39. if (NULL == val)
  40. val = &tmp;
  41. nfds.fd = fd;
  42. nfds.events = POLLIN;
  43. ret = poll(&nfds, 1, timeout);
  44. if (ret == 1 && (nfds.revents & POLLIN) &&
  45. sizeof(RK_U64) == read(fd, val, sizeof(RK_U64))) {
  46. ret = 0;
  47. } else
  48. ret = errno;
  49. return ret;
  50. }
  51. RK_S32 mpp_eventfd_write(RK_S32 fd, RK_U64 val)
  52. {
  53. RK_S32 ret = 0;
  54. if (sizeof(RK_U64) != write(fd, &val, sizeof(val)))
  55. ret = errno;
  56. return ret;
  57. }