Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 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" |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <ctype.h> |
| 22 | #include <unistd.h> |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 23 | #include <poll.h> |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
| 26 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 27 | #ifndef RS_SERVER |
| 28 | #include "utils/Timers.h" |
| 29 | #include "utils/StopWatch.h" |
| 30 | #endif |
| 31 | |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 32 | using namespace android; |
| 33 | using namespace android::renderscript; |
| 34 | |
| 35 | FifoSocket::FifoSocket() { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 36 | mShutdown = false; |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | FifoSocket::~FifoSocket() { |
| 40 | |
| 41 | } |
| 42 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 43 | bool FifoSocket::init(bool supportNonBlocking, bool supportReturnValues, size_t maxDataSize) { |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 44 | int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | void FifoSocket::shutdown() { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 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]); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 57 | bool FifoSocket::writeAsync(const void *data, size_t bytes, bool waitForSpace) { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 58 | if (bytes == 0) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 59 | return true; |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 60 | } |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 61 | //ALOGE("writeAsync %p %i", data, bytes); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 62 | size_t ret = ::send(sv[0], data, bytes, 0); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 63 | rsAssert(ret == bytes); |
Jason Sams | bd726b2 | 2012-10-09 16:10:34 -0700 | [diff] [blame] | 64 | if (ret != bytes) { |
| 65 | ALOGE("writeAsync %p %zu ret %zu", data, bytes, ret); |
| 66 | } |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 67 | return true; |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void FifoSocket::writeWaitReturn(void *retData, size_t retBytes) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 71 | if (mShutdown) { |
| 72 | return; |
| 73 | } |
| 74 | |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 75 | //ALOGE("writeWaitReturn %p %i", retData, retBytes); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 76 | size_t ret = ::recv(sv[0], retData, retBytes, MSG_WAITALL); |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 77 | //ALOGE("writeWaitReturn %i", ret); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 78 | rsAssert(ret == retBytes); |
| 79 | } |
| 80 | |
| 81 | size_t FifoSocket::read(void *data, size_t bytes) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 82 | if (mShutdown) { |
| 83 | return 0; |
| 84 | } |
| 85 | |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 86 | //ALOGE("read %p %i", data, bytes); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 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 | } |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 93 | return ret; |
| 94 | } |
| 95 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 96 | bool 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 Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 106 | void 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 Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |