The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 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 | // |
| 18 | // FIFO I/O. |
| 19 | // |
| 20 | #ifndef _LIBS_UTILS_PIPE_H |
| 21 | #define _LIBS_UTILS_PIPE_H |
| 22 | |
| 23 | #ifdef HAVE_ANDROID_OS |
| 24 | #error DO NOT USE THIS FILE IN THE DEVICE BUILD |
| 25 | #endif |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | /* |
| 30 | * Simple anonymous unidirectional pipe. |
| 31 | * |
| 32 | * The primary goal is to create an implementation with minimal overhead |
| 33 | * under Linux. Making Windows, Mac OS X, and Linux all work the same way |
| 34 | * is a secondary goal. Part of this goal is to have something that can |
| 35 | * be fed to a select() call, so that the application can sleep in the |
| 36 | * kernel until something interesting happens. |
| 37 | */ |
| 38 | class Pipe { |
| 39 | public: |
| 40 | Pipe(void); |
| 41 | virtual ~Pipe(void); |
| 42 | |
| 43 | /* Create the pipe */ |
| 44 | bool create(void); |
| 45 | |
| 46 | /* Create a read-only pipe, using the supplied handle as read handle */ |
| 47 | bool createReader(unsigned long handle); |
| 48 | /* Create a write-only pipe, using the supplied handle as write handle */ |
| 49 | bool createWriter(unsigned long handle); |
| 50 | |
| 51 | /* Is this object ready to go? */ |
| 52 | bool isCreated(void); |
| 53 | |
| 54 | /* |
| 55 | * Read "count" bytes from the pipe. Returns the amount of data read, |
| 56 | * or 0 if no data available and we're non-blocking. |
| 57 | * Returns -1 on error. |
| 58 | */ |
| 59 | int read(void* buf, int count); |
| 60 | |
| 61 | /* |
| 62 | * Write "count" bytes into the pipe. Returns number of bytes written, |
| 63 | * or 0 if there's no room for more data and we're non-blocking. |
| 64 | * Returns -1 on error. |
| 65 | */ |
| 66 | int write(const void* buf, int count); |
| 67 | |
| 68 | /* Returns "true" if data is available to read */ |
| 69 | bool readReady(void); |
| 70 | |
| 71 | /* Enable or disable non-blocking I/O for reads */ |
| 72 | bool setReadNonBlocking(bool val); |
| 73 | /* Enable or disable non-blocking I/O for writes. Only works on Linux. */ |
| 74 | bool setWriteNonBlocking(bool val); |
| 75 | |
| 76 | /* |
| 77 | * Get the handle. Only useful in some platform-specific situations. |
| 78 | */ |
| 79 | unsigned long getReadHandle(void); |
| 80 | unsigned long getWriteHandle(void); |
| 81 | |
| 82 | /* |
| 83 | * Modify inheritance, i.e. whether or not a child process will get |
| 84 | * copies of the descriptors. Systems with fork+exec allow us to close |
| 85 | * the descriptors before launching the child process, but Win32 |
| 86 | * doesn't allow it. |
| 87 | */ |
| 88 | bool disallowReadInherit(void); |
| 89 | bool disallowWriteInherit(void); |
| 90 | |
| 91 | /* |
| 92 | * Close one side or the other. Useful in the parent after launching |
| 93 | * a child process. |
| 94 | */ |
| 95 | bool closeRead(void); |
| 96 | bool closeWrite(void); |
| 97 | |
| 98 | private: |
| 99 | bool mReadNonBlocking; |
| 100 | bool mWriteNonBlocking; |
| 101 | |
| 102 | unsigned long mReadHandle; |
| 103 | unsigned long mWriteHandle; |
| 104 | }; |
| 105 | |
| 106 | }; // android |
| 107 | |
| 108 | #endif // _LIBS_UTILS_PIPE_H |