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" |
| 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 Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 25 | #include <poll.h> |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/socket.h> |
| 28 | |
| 29 | using namespace android; |
| 30 | using namespace android::renderscript; |
| 31 | |
| 32 | FifoSocket::FifoSocket() { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 33 | mShutdown = false; |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | FifoSocket::~FifoSocket() { |
| 37 | |
| 38 | } |
| 39 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 40 | bool FifoSocket::init(bool supportNonBlocking, bool supportReturnValues, size_t maxDataSize) { |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 41 | int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | void FifoSocket::shutdown() { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 46 | 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 Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 54 | bool FifoSocket::writeAsync(const void *data, size_t bytes, bool waitForSpace) { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 55 | if (bytes == 0) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 56 | return true; |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 57 | } |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 58 | //ALOGE("writeAsync %p %i", data, bytes); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 59 | size_t ret = ::send(sv[0], data, bytes, 0); |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 60 | //ALOGE("writeAsync ret %i", ret); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 61 | rsAssert(ret == bytes); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 62 | return true; |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void FifoSocket::writeWaitReturn(void *retData, size_t retBytes) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 66 | if (mShutdown) { |
| 67 | return; |
| 68 | } |
| 69 | |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 70 | //ALOGE("writeWaitReturn %p %i", retData, retBytes); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 71 | size_t ret = ::recv(sv[0], retData, retBytes, MSG_WAITALL); |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 72 | //ALOGE("writeWaitReturn %i", ret); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 73 | rsAssert(ret == retBytes); |
| 74 | } |
| 75 | |
| 76 | size_t FifoSocket::read(void *data, size_t bytes) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 77 | if (mShutdown) { |
| 78 | return 0; |
| 79 | } |
| 80 | |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 81 | //ALOGE("read %p %i", data, bytes); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 82 | 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 Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 88 | return ret; |
| 89 | } |
| 90 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 91 | bool 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 Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 101 | void 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 Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |