rsFifoSocket.cpp revision 0b575de8ed0b628d84d256f5846500b0385979bd
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#ifndef RS_SERVER
28#include "utils/Timers.h"
29#include "utils/StopWatch.h"
30#endif
31
32using namespace android;
33using namespace android::renderscript;
34
35FifoSocket::FifoSocket() {
36    mShutdown = false;
37}
38
39FifoSocket::~FifoSocket() {
40
41}
42
43bool FifoSocket::init(bool supportNonBlocking, bool supportReturnValues, size_t maxDataSize) {
44    int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
45    return false;
46}
47
48void FifoSocket::shutdown() {
49    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]);
55}
56
57bool FifoSocket::writeAsync(const void *data, size_t bytes, bool waitForSpace) {
58    if (bytes == 0) {
59        return true;
60    }
61    //ALOGE("writeAsync %p %i", data, bytes);
62    size_t ret = ::send(sv[0], data, bytes, 0);
63    rsAssert(ret == bytes);
64    if (ret != bytes) {
65        ALOGE("writeAsync %p %zu  ret %zu", data, bytes, ret);
66    }
67    return true;
68}
69
70void FifoSocket::writeWaitReturn(void *retData, size_t retBytes) {
71    if (mShutdown) {
72        return;
73    }
74
75    //ALOGE("writeWaitReturn %p %i", retData, retBytes);
76    size_t ret = ::recv(sv[0], retData, retBytes, MSG_WAITALL);
77    //ALOGE("writeWaitReturn %i", ret);
78    rsAssert(ret == retBytes);
79}
80
81size_t FifoSocket::read(void *data, size_t bytes) {
82    if (mShutdown) {
83        return 0;
84    }
85
86    //ALOGE("read %p %i", data, bytes);
87    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    }
93    return ret;
94}
95
96bool 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;
103}
104
105
106void 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);
111}
112
113
114