Search in sources :

Example 6 with StructPollfd

use of android.system.StructPollfd in project android_frameworks_base by DirtyUnicorns.

the class ZygoteInit method runSelectLoop.

/**
     * Runs the zygote process's select loop. Accepts new connections as
     * they happen, and reads commands from connections one spawn-request's
     * worth at a time.
     *
     * @throws MethodAndArgsCaller in a child process when a main() should
     * be executed.
     */
private static void runSelectLoop(String abiList) throws MethodAndArgsCaller {
    ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>();
    ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>();
    fds.add(sServerSocket.getFileDescriptor());
    peers.add(null);
    while (true) {
        StructPollfd[] pollFds = new StructPollfd[fds.size()];
        for (int i = 0; i < pollFds.length; ++i) {
            pollFds[i] = new StructPollfd();
            pollFds[i].fd = fds.get(i);
            pollFds[i].events = (short) POLLIN;
        }
        try {
            Os.poll(pollFds, -1);
        } catch (ErrnoException ex) {
            throw new RuntimeException("poll failed", ex);
        }
        for (int i = pollFds.length - 1; i >= 0; --i) {
            if ((pollFds[i].revents & POLLIN) == 0) {
                continue;
            }
            if (i == 0) {
                ZygoteConnection newPeer = acceptCommandPeer(abiList);
                peers.add(newPeer);
                fds.add(newPeer.getFileDesciptor());
            } else {
                boolean done = peers.get(i).runOnce();
                if (done) {
                    peers.remove(i);
                    fds.remove(i);
                }
            }
        }
    }
}
Also used : ErrnoException(android.system.ErrnoException) ArrayList(java.util.ArrayList) StructPollfd(android.system.StructPollfd) FileDescriptor(java.io.FileDescriptor)

Example 7 with StructPollfd

use of android.system.StructPollfd in project android_frameworks_base by DirtyUnicorns.

the class UsbMidiDevice method openLocked.

private boolean openLocked() {
    // FIXME - support devices with different number of input and output ports
    FileDescriptor[] fileDescriptors = nativeOpen(mAlsaCard, mAlsaDevice, mSubdeviceCount);
    if (fileDescriptors == null) {
        Log.e(TAG, "nativeOpen failed");
        return false;
    }
    mFileDescriptors = fileDescriptors;
    int inputCount = fileDescriptors.length;
    // last file descriptor returned from nativeOpen() is only used for unblocking Os.poll()
    // in our input thread
    int outputCount = fileDescriptors.length - 1;
    mPollFDs = new StructPollfd[inputCount];
    mInputStreams = new FileInputStream[inputCount];
    for (int i = 0; i < inputCount; i++) {
        FileDescriptor fd = fileDescriptors[i];
        StructPollfd pollfd = new StructPollfd();
        pollfd.fd = fd;
        pollfd.events = (short) OsConstants.POLLIN;
        mPollFDs[i] = pollfd;
        mInputStreams[i] = new FileInputStream(fd);
    }
    mOutputStreams = new FileOutputStream[outputCount];
    mEventSchedulers = new MidiEventScheduler[outputCount];
    for (int i = 0; i < outputCount; i++) {
        mOutputStreams[i] = new FileOutputStream(fileDescriptors[i]);
        MidiEventScheduler scheduler = new MidiEventScheduler();
        mEventSchedulers[i] = scheduler;
        mInputPortReceivers[i].setReceiver(scheduler.getReceiver());
    }
    final MidiReceiver[] outputReceivers = mServer.getOutputPortReceivers();
    // Create input thread which will read from all input ports
    new Thread("UsbMidiDevice input thread") {

        @Override
        public void run() {
            byte[] buffer = new byte[BUFFER_SIZE];
            try {
                while (true) {
                    // Record time of event immediately after waking.
                    long timestamp = System.nanoTime();
                    synchronized (mLock) {
                        if (!mIsOpen)
                            break;
                        // look for a readable FileDescriptor
                        for (int index = 0; index < mPollFDs.length; index++) {
                            StructPollfd pfd = mPollFDs[index];
                            if ((pfd.revents & (OsConstants.POLLERR | OsConstants.POLLHUP)) != 0) {
                                break;
                            } else if ((pfd.revents & OsConstants.POLLIN) != 0) {
                                // clear readable flag
                                pfd.revents = 0;
                                if (index == mInputStreams.length - 1) {
                                    // last file descriptor is used only for unblocking Os.poll()
                                    break;
                                }
                                int count = mInputStreams[index].read(buffer);
                                outputReceivers[index].send(buffer, 0, count, timestamp);
                            }
                        }
                    }
                    // wait until we have a readable port or we are signalled to close
                    Os.poll(mPollFDs, -1);
                }
            } catch (IOException e) {
                Log.d(TAG, "reader thread exiting");
            } catch (ErrnoException e) {
                Log.d(TAG, "reader thread exiting");
            }
            Log.d(TAG, "input thread exit");
        }
    }.start();
    // Create output thread for each output port
    for (int port = 0; port < outputCount; port++) {
        final MidiEventScheduler eventSchedulerF = mEventSchedulers[port];
        final FileOutputStream outputStreamF = mOutputStreams[port];
        final int portF = port;
        new Thread("UsbMidiDevice output thread " + port) {

            @Override
            public void run() {
                while (true) {
                    MidiEvent event;
                    try {
                        event = (MidiEvent) eventSchedulerF.waitNextEvent();
                    } catch (InterruptedException e) {
                        // try again
                        continue;
                    }
                    if (event == null) {
                        break;
                    }
                    try {
                        outputStreamF.write(event.data, 0, event.count);
                    } catch (IOException e) {
                        Log.e(TAG, "write failed for port " + portF);
                    }
                    eventSchedulerF.addEventToPool(event);
                }
                Log.d(TAG, "output thread exit");
            }
        }.start();
    }
    mIsOpen = true;
    return true;
}
Also used : MidiEvent(com.android.internal.midi.MidiEventScheduler.MidiEvent) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) MidiEventScheduler(com.android.internal.midi.MidiEventScheduler) ErrnoException(android.system.ErrnoException) MidiReceiver(android.media.midi.MidiReceiver) FileOutputStream(java.io.FileOutputStream) StructPollfd(android.system.StructPollfd)

Example 8 with StructPollfd

use of android.system.StructPollfd in project android_frameworks_base by AOSPA.

the class ZygoteInit method runSelectLoop.

/**
     * Runs the zygote process's select loop. Accepts new connections as
     * they happen, and reads commands from connections one spawn-request's
     * worth at a time.
     *
     * @throws MethodAndArgsCaller in a child process when a main() should
     * be executed.
     */
private static void runSelectLoop(String abiList) throws MethodAndArgsCaller {
    ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>();
    ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>();
    fds.add(sServerSocket.getFileDescriptor());
    peers.add(null);
    while (true) {
        StructPollfd[] pollFds = new StructPollfd[fds.size()];
        for (int i = 0; i < pollFds.length; ++i) {
            pollFds[i] = new StructPollfd();
            pollFds[i].fd = fds.get(i);
            pollFds[i].events = (short) POLLIN;
        }
        try {
            Os.poll(pollFds, -1);
        } catch (ErrnoException ex) {
            throw new RuntimeException("poll failed", ex);
        }
        for (int i = pollFds.length - 1; i >= 0; --i) {
            if ((pollFds[i].revents & POLLIN) == 0) {
                continue;
            }
            if (i == 0) {
                ZygoteConnection newPeer = acceptCommandPeer(abiList);
                peers.add(newPeer);
                fds.add(newPeer.getFileDesciptor());
            } else {
                boolean done = peers.get(i).runOnce();
                if (done) {
                    peers.remove(i);
                    fds.remove(i);
                }
            }
        }
    }
}
Also used : ErrnoException(android.system.ErrnoException) ArrayList(java.util.ArrayList) StructPollfd(android.system.StructPollfd) FileDescriptor(java.io.FileDescriptor)

Example 9 with StructPollfd

use of android.system.StructPollfd in project android_frameworks_base by ResurrectionRemix.

the class UsbMidiDevice method openLocked.

private boolean openLocked() {
    // FIXME - support devices with different number of input and output ports
    FileDescriptor[] fileDescriptors = nativeOpen(mAlsaCard, mAlsaDevice, mSubdeviceCount);
    if (fileDescriptors == null) {
        Log.e(TAG, "nativeOpen failed");
        return false;
    }
    mFileDescriptors = fileDescriptors;
    int inputCount = fileDescriptors.length;
    // last file descriptor returned from nativeOpen() is only used for unblocking Os.poll()
    // in our input thread
    int outputCount = fileDescriptors.length - 1;
    mPollFDs = new StructPollfd[inputCount];
    mInputStreams = new FileInputStream[inputCount];
    for (int i = 0; i < inputCount; i++) {
        FileDescriptor fd = fileDescriptors[i];
        StructPollfd pollfd = new StructPollfd();
        pollfd.fd = fd;
        pollfd.events = (short) OsConstants.POLLIN;
        mPollFDs[i] = pollfd;
        mInputStreams[i] = new FileInputStream(fd);
    }
    mOutputStreams = new FileOutputStream[outputCount];
    mEventSchedulers = new MidiEventScheduler[outputCount];
    for (int i = 0; i < outputCount; i++) {
        mOutputStreams[i] = new FileOutputStream(fileDescriptors[i]);
        MidiEventScheduler scheduler = new MidiEventScheduler();
        mEventSchedulers[i] = scheduler;
        mInputPortReceivers[i].setReceiver(scheduler.getReceiver());
    }
    final MidiReceiver[] outputReceivers = mServer.getOutputPortReceivers();
    // Create input thread which will read from all input ports
    new Thread("UsbMidiDevice input thread") {

        @Override
        public void run() {
            byte[] buffer = new byte[BUFFER_SIZE];
            try {
                while (true) {
                    // Record time of event immediately after waking.
                    long timestamp = System.nanoTime();
                    synchronized (mLock) {
                        if (!mIsOpen)
                            break;
                        // look for a readable FileDescriptor
                        for (int index = 0; index < mPollFDs.length; index++) {
                            StructPollfd pfd = mPollFDs[index];
                            if ((pfd.revents & (OsConstants.POLLERR | OsConstants.POLLHUP)) != 0) {
                                break;
                            } else if ((pfd.revents & OsConstants.POLLIN) != 0) {
                                // clear readable flag
                                pfd.revents = 0;
                                if (index == mInputStreams.length - 1) {
                                    // last file descriptor is used only for unblocking Os.poll()
                                    break;
                                }
                                int count = mInputStreams[index].read(buffer);
                                outputReceivers[index].send(buffer, 0, count, timestamp);
                            }
                        }
                    }
                    // wait until we have a readable port or we are signalled to close
                    Os.poll(mPollFDs, -1);
                }
            } catch (IOException e) {
                Log.d(TAG, "reader thread exiting");
            } catch (ErrnoException e) {
                Log.d(TAG, "reader thread exiting");
            }
            Log.d(TAG, "input thread exit");
        }
    }.start();
    // Create output thread for each output port
    for (int port = 0; port < outputCount; port++) {
        final MidiEventScheduler eventSchedulerF = mEventSchedulers[port];
        final FileOutputStream outputStreamF = mOutputStreams[port];
        final int portF = port;
        new Thread("UsbMidiDevice output thread " + port) {

            @Override
            public void run() {
                while (true) {
                    MidiEvent event;
                    try {
                        event = (MidiEvent) eventSchedulerF.waitNextEvent();
                    } catch (InterruptedException e) {
                        // try again
                        continue;
                    }
                    if (event == null) {
                        break;
                    }
                    try {
                        outputStreamF.write(event.data, 0, event.count);
                    } catch (IOException e) {
                        Log.e(TAG, "write failed for port " + portF);
                    }
                    eventSchedulerF.addEventToPool(event);
                }
                Log.d(TAG, "output thread exit");
            }
        }.start();
    }
    mIsOpen = true;
    return true;
}
Also used : MidiEvent(com.android.internal.midi.MidiEventScheduler.MidiEvent) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) MidiEventScheduler(com.android.internal.midi.MidiEventScheduler) ErrnoException(android.system.ErrnoException) MidiReceiver(android.media.midi.MidiReceiver) FileOutputStream(java.io.FileOutputStream) StructPollfd(android.system.StructPollfd)

Aggregations

ErrnoException (android.system.ErrnoException)9 StructPollfd (android.system.StructPollfd)9 FileDescriptor (java.io.FileDescriptor)9 ArrayList (java.util.ArrayList)5 MidiReceiver (android.media.midi.MidiReceiver)4 MidiEventScheduler (com.android.internal.midi.MidiEventScheduler)4 MidiEvent (com.android.internal.midi.MidiEventScheduler.MidiEvent)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4