Search in sources :

Example 1 with StructPollfd

use of android.system.StructPollfd in project platform_frameworks_base by android.

the class ZygoteServer 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 Zygote.MethodAndArgsCaller in a child process when a main()
     * should be executed.
     */
void runSelectLoop(String abiList) throws Zygote.MethodAndArgsCaller {
    ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>();
    ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>();
    fds.add(mServerSocket.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(this);
                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 2 with StructPollfd

use of android.system.StructPollfd in project platform_frameworks_base by android.

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 inputStreamCount = fileDescriptors.length;
    // last file descriptor returned from nativeOpen() is only used for unblocking Os.poll()
    // in our input thread
    int outputStreamCount = fileDescriptors.length - 1;
    mPollFDs = new StructPollfd[inputStreamCount];
    mInputStreams = new FileInputStream[inputStreamCount];
    for (int i = 0; i < inputStreamCount; 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[outputStreamCount];
    mEventSchedulers = new MidiEventScheduler[outputStreamCount];
    for (int i = 0; i < outputStreamCount; 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 output ports of the physical device
    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 input port of the physical device
    for (int port = 0; port < outputStreamCount; 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 3 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 4 with StructPollfd

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

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 5 with StructPollfd

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

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)

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