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