blob: 44379b23ae3079e8b0741b660c1f45404a11d784 [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"
18#include "utils/Timers.h"
19#include "utils/StopWatch.h"
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <ctype.h>
24#include <unistd.h>
Jason Sams5f27d6f2012-02-07 15:32:08 -080025#include <poll.h>
Jason Sams20087472011-05-06 14:14:30 -070026#include <sys/types.h>
27#include <sys/socket.h>
28
29using namespace android;
30using namespace android::renderscript;
31
32FifoSocket::FifoSocket() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080033 mShutdown = false;
Jason Sams20087472011-05-06 14:14:30 -070034}
35
36FifoSocket::~FifoSocket() {
37
38}
39
Jason Sams5f27d6f2012-02-07 15:32:08 -080040bool FifoSocket::init(bool supportNonBlocking, bool supportReturnValues, size_t maxDataSize) {
Jason Sams20087472011-05-06 14:14:30 -070041 int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
42 return false;
43}
44
45void FifoSocket::shutdown() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080046 mShutdown = true;
47 uint64_t d = 0;
48 ::send(sv[0], &d, sizeof(d), 0);
49 ::send(sv[1], &d, sizeof(d), 0);
50 close(sv[0]);
51 close(sv[1]);
Jason Sams20087472011-05-06 14:14:30 -070052}
53
Jason Sams5f27d6f2012-02-07 15:32:08 -080054bool FifoSocket::writeAsync(const void *data, size_t bytes, bool waitForSpace) {
Jason Sams1a4efa32011-05-17 15:01:29 -070055 if (bytes == 0) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080056 return true;
Jason Sams1a4efa32011-05-17 15:01:29 -070057 }
Steve Blockaf12ac62012-01-06 19:20:56 +000058 //ALOGE("writeAsync %p %i", data, bytes);
Jason Sams1a4efa32011-05-17 15:01:29 -070059 size_t ret = ::send(sv[0], data, bytes, 0);
Jason Sams20087472011-05-06 14:14:30 -070060 rsAssert(ret == bytes);
Jason Samsbd726b22012-10-09 16:10:34 -070061 if (ret != bytes) {
62 ALOGE("writeAsync %p %zu ret %zu", data, bytes, ret);
63 }
Jason Sams5f27d6f2012-02-07 15:32:08 -080064 return true;
Jason Sams20087472011-05-06 14:14:30 -070065}
66
67void FifoSocket::writeWaitReturn(void *retData, size_t retBytes) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080068 if (mShutdown) {
69 return;
70 }
71
Steve Blockaf12ac62012-01-06 19:20:56 +000072 //ALOGE("writeWaitReturn %p %i", retData, retBytes);
Jason Sams5f27d6f2012-02-07 15:32:08 -080073 size_t ret = ::recv(sv[0], retData, retBytes, MSG_WAITALL);
Steve Blockaf12ac62012-01-06 19:20:56 +000074 //ALOGE("writeWaitReturn %i", ret);
Jason Sams20087472011-05-06 14:14:30 -070075 rsAssert(ret == retBytes);
76}
77
78size_t FifoSocket::read(void *data, size_t bytes) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080079 if (mShutdown) {
80 return 0;
81 }
82
Steve Blockaf12ac62012-01-06 19:20:56 +000083 //ALOGE("read %p %i", data, bytes);
Jason Sams5f27d6f2012-02-07 15:32:08 -080084 size_t ret = ::recv(sv[1], data, bytes, MSG_WAITALL);
85 rsAssert(ret == bytes || mShutdown);
86 //ALOGE("read ret %i bytes %i", ret, bytes);
87 if (mShutdown) {
88 ret = 0;
89 }
Jason Sams20087472011-05-06 14:14:30 -070090 return ret;
91}
92
Jason Sams5f27d6f2012-02-07 15:32:08 -080093bool FifoSocket::isEmpty() {
94 struct pollfd p;
95 p.fd = sv[1];
96 p.events = POLLIN;
97 int r = poll(&p, 1, 0);
98 //ALOGE("poll r=%i", r);
99 return r == 0;
Jason Sams20087472011-05-06 14:14:30 -0700100}
101
102
Jason Sams5f27d6f2012-02-07 15:32:08 -0800103void FifoSocket::readReturn(const void *data, size_t bytes) {
104 //ALOGE("readReturn %p %Zu", data, bytes);
105 size_t ret = ::send(sv[1], data, bytes, 0);
106 //ALOGE("readReturn %Zu", ret);
107 //rsAssert(ret == bytes);
Jason Sams20087472011-05-06 14:14:30 -0700108}
109
110