rsFifoSocket.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  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 "rsFifoSocket.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <ctype.h>
  20. #include <unistd.h>
  21. #include <poll.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. namespace android {
  25. namespace renderscript {
  26. FifoSocket::FifoSocket() {
  27. mShutdown = false;
  28. }
  29. FifoSocket::~FifoSocket() {
  30. }
  31. bool FifoSocket::init(bool supportNonBlocking, bool supportReturnValues, size_t maxDataSize) {
  32. int ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv);
  33. return (ret == 0);
  34. }
  35. void FifoSocket::shutdown() {
  36. mShutdown = true;
  37. uint64_t d = 0;
  38. ::send(sv[0], &d, sizeof(d), 0);
  39. ::send(sv[1], &d, sizeof(d), 0);
  40. close(sv[0]);
  41. close(sv[1]);
  42. }
  43. bool FifoSocket::writeAsync(const void *data, size_t bytes, bool waitForSpace) {
  44. if (bytes == 0) {
  45. return true;
  46. }
  47. //ALOGE("writeAsync %p %i", data, bytes);
  48. size_t ret = ::send(sv[0], data, bytes, 0);
  49. rsAssert(ret == bytes);
  50. if (ret != bytes) {
  51. ALOGE("writeAsync %p %zu ret %zu", data, bytes, ret);
  52. }
  53. return true;
  54. }
  55. void FifoSocket::writeWaitReturn(void *retData, size_t retBytes) {
  56. if (mShutdown) {
  57. return;
  58. }
  59. //ALOGE("writeWaitReturn %p %i", retData, retBytes);
  60. size_t ret = ::recv(sv[0], retData, retBytes, MSG_WAITALL);
  61. //ALOGE("writeWaitReturn %i", ret);
  62. rsAssert(ret == retBytes);
  63. }
  64. size_t FifoSocket::read(void *data, size_t bytes) {
  65. if (mShutdown) {
  66. return 0;
  67. }
  68. //ALOGE("read %p %i", data, bytes);
  69. size_t ret = ::recv(sv[1], data, bytes, MSG_WAITALL);
  70. rsAssert(ret == bytes || mShutdown);
  71. //ALOGE("read ret %i bytes %i", ret, bytes);
  72. if (mShutdown) {
  73. ret = 0;
  74. }
  75. return ret;
  76. }
  77. bool FifoSocket::isEmpty() {
  78. struct pollfd p;
  79. p.fd = sv[1];
  80. p.events = POLLIN;
  81. int r = poll(&p, 1, 0);
  82. //ALOGE("poll r=%i", r);
  83. return r == 0;
  84. }
  85. void FifoSocket::readReturn(const void *data, size_t bytes) {
  86. ::send(sv[1], data, bytes, 0);
  87. }
  88. } // namespace renderscript
  89. } // namespace android