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