blob: c10ec4fd64620a4a8e2fadd6fd515a1ceef386e9 [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
Tim Murray0b575de2013-03-15 15:56:43 -070027#ifndef RS_SERVER
28#include "utils/Timers.h"
29#include "utils/StopWatch.h"
30#endif
31
Jason Sams20087472011-05-06 14:14:30 -070032using namespace android;
33using namespace android::renderscript;
34
35FifoSocket::FifoSocket() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080036 mShutdown = false;
Jason Sams20087472011-05-06 14:14:30 -070037}
38
39FifoSocket::~FifoSocket() {
40
41}
42
Jason Sams5f27d6f2012-02-07 15:32:08 -080043bool FifoSocket::init(bool supportNonBlocking, bool supportReturnValues, size_t maxDataSize) {
Jason Sams20087472011-05-06 14:14:30 -070044 int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
45 return false;
46}
47
48void FifoSocket::shutdown() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080049 mShutdown = true;
50 uint64_t d = 0;
51 ::send(sv[0], &d, sizeof(d), 0);
52 ::send(sv[1], &d, sizeof(d), 0);
53 close(sv[0]);
54 close(sv[1]);
Jason Sams20087472011-05-06 14:14:30 -070055}
56
Jason Sams5f27d6f2012-02-07 15:32:08 -080057bool FifoSocket::writeAsync(const void *data, size_t bytes, bool waitForSpace) {
Jason Sams1a4efa32011-05-17 15:01:29 -070058 if (bytes == 0) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080059 return true;
Jason Sams1a4efa32011-05-17 15:01:29 -070060 }
Steve Blockaf12ac62012-01-06 19:20:56 +000061 //ALOGE("writeAsync %p %i", data, bytes);
Jason Sams1a4efa32011-05-17 15:01:29 -070062 size_t ret = ::send(sv[0], data, bytes, 0);
Jason Sams20087472011-05-06 14:14:30 -070063 rsAssert(ret == bytes);
Jason Samsbd726b22012-10-09 16:10:34 -070064 if (ret != bytes) {
65 ALOGE("writeAsync %p %zu ret %zu", data, bytes, ret);
66 }
Jason Sams5f27d6f2012-02-07 15:32:08 -080067 return true;
Jason Sams20087472011-05-06 14:14:30 -070068}
69
70void FifoSocket::writeWaitReturn(void *retData, size_t retBytes) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080071 if (mShutdown) {
72 return;
73 }
74
Steve Blockaf12ac62012-01-06 19:20:56 +000075 //ALOGE("writeWaitReturn %p %i", retData, retBytes);
Jason Sams5f27d6f2012-02-07 15:32:08 -080076 size_t ret = ::recv(sv[0], retData, retBytes, MSG_WAITALL);
Steve Blockaf12ac62012-01-06 19:20:56 +000077 //ALOGE("writeWaitReturn %i", ret);
Jason Sams20087472011-05-06 14:14:30 -070078 rsAssert(ret == retBytes);
79}
80
81size_t FifoSocket::read(void *data, size_t bytes) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080082 if (mShutdown) {
83 return 0;
84 }
85
Steve Blockaf12ac62012-01-06 19:20:56 +000086 //ALOGE("read %p %i", data, bytes);
Jason Sams5f27d6f2012-02-07 15:32:08 -080087 size_t ret = ::recv(sv[1], data, bytes, MSG_WAITALL);
88 rsAssert(ret == bytes || mShutdown);
89 //ALOGE("read ret %i bytes %i", ret, bytes);
90 if (mShutdown) {
91 ret = 0;
92 }
Jason Sams20087472011-05-06 14:14:30 -070093 return ret;
94}
95
Jason Sams5f27d6f2012-02-07 15:32:08 -080096bool FifoSocket::isEmpty() {
97 struct pollfd p;
98 p.fd = sv[1];
99 p.events = POLLIN;
100 int r = poll(&p, 1, 0);
101 //ALOGE("poll r=%i", r);
102 return r == 0;
Jason Sams20087472011-05-06 14:14:30 -0700103}
104
105
Jason Sams5f27d6f2012-02-07 15:32:08 -0800106void FifoSocket::readReturn(const void *data, size_t bytes) {
107 //ALOGE("readReturn %p %Zu", data, bytes);
108 size_t ret = ::send(sv[1], data, bytes, 0);
109 //ALOGE("readReturn %Zu", ret);
110 //rsAssert(ret == bytes);
Jason Sams20087472011-05-06 14:14:30 -0700111}
112
113