blob: 51f3f07ee342a86c2b290b710530cfd6642b29fc [file] [log] [blame]
Jason Sams20087472011-05-06 14:14:30 -07001/*
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"
Jason Sams20087472011-05-06 14:14:30 -070018
19#include <stdio.h>
20#include <stdlib.h>
21#include <ctype.h>
22#include <unistd.h>
Jason Sams5f27d6f2012-02-07 15:32:08 -080023#include <poll.h>
Jason Sams20087472011-05-06 14:14:30 -070024#include <sys/types.h>
25#include <sys/socket.h>
26
27using namespace android;
28using namespace android::renderscript;
29
30FifoSocket::FifoSocket() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080031 mShutdown = false;
Jason Sams20087472011-05-06 14:14:30 -070032}
33
34FifoSocket::~FifoSocket() {
35
36}
37
Jason Sams5f27d6f2012-02-07 15:32:08 -080038bool FifoSocket::init(bool supportNonBlocking, bool supportReturnValues, size_t maxDataSize) {
Jason Sams20087472011-05-06 14:14:30 -070039 int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
40 return false;
41}
42
43void FifoSocket::shutdown() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080044 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]);
Jason Sams20087472011-05-06 14:14:30 -070050}
51
Jason Sams5f27d6f2012-02-07 15:32:08 -080052bool FifoSocket::writeAsync(const void *data, size_t bytes, bool waitForSpace) {
Jason Sams1a4efa32011-05-17 15:01:29 -070053 if (bytes == 0) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080054 return true;
Jason Sams1a4efa32011-05-17 15:01:29 -070055 }
Steve Blockaf12ac62012-01-06 19:20:56 +000056 //ALOGE("writeAsync %p %i", data, bytes);
Jason Sams1a4efa32011-05-17 15:01:29 -070057 size_t ret = ::send(sv[0], data, bytes, 0);
Jason Sams20087472011-05-06 14:14:30 -070058 rsAssert(ret == bytes);
Jason Samsbd726b22012-10-09 16:10:34 -070059 if (ret != bytes) {
60 ALOGE("writeAsync %p %zu ret %zu", data, bytes, ret);
61 }
Jason Sams5f27d6f2012-02-07 15:32:08 -080062 return true;
Jason Sams20087472011-05-06 14:14:30 -070063}
64
65void FifoSocket::writeWaitReturn(void *retData, size_t retBytes) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080066 if (mShutdown) {
67 return;
68 }
69
Steve Blockaf12ac62012-01-06 19:20:56 +000070 //ALOGE("writeWaitReturn %p %i", retData, retBytes);
Jason Sams5f27d6f2012-02-07 15:32:08 -080071 size_t ret = ::recv(sv[0], retData, retBytes, MSG_WAITALL);
Steve Blockaf12ac62012-01-06 19:20:56 +000072 //ALOGE("writeWaitReturn %i", ret);
Jason Sams20087472011-05-06 14:14:30 -070073 rsAssert(ret == retBytes);
74}
75
76size_t FifoSocket::read(void *data, size_t bytes) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080077 if (mShutdown) {
78 return 0;
79 }
80
Steve Blockaf12ac62012-01-06 19:20:56 +000081 //ALOGE("read %p %i", data, bytes);
Jason Sams5f27d6f2012-02-07 15:32:08 -080082 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 }
Jason Sams20087472011-05-06 14:14:30 -070088 return ret;
89}
90
Jason Sams5f27d6f2012-02-07 15:32:08 -080091bool 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;
Jason Sams20087472011-05-06 14:14:30 -070098}
99
100
Jason Sams5f27d6f2012-02-07 15:32:08 -0800101void FifoSocket::readReturn(const void *data, size_t bytes) {
102 //ALOGE("readReturn %p %Zu", data, bytes);
103 size_t ret = ::send(sv[1], data, bytes, 0);
104 //ALOGE("readReturn %Zu", ret);
105 //rsAssert(ret == bytes);
Jason Sams20087472011-05-06 14:14:30 -0700106}
107
108