public void start() throws Exception {
   if (reaper != null) {
     reaper.start();
   }
 }
Ejemplo n.º 2
0
  public SocketBase createSocket(int type) {
    SocketBase s = null;
    slotSync.lock();
    try {
      if (starting.compareAndSet(true, false)) {
        //  Initialize the array of mailboxes. Additional three slots are for
        //  zmq_term thread and reaper thread.
        int mazmq;
        int ios;
        optSync.lock();
        try {
          mazmq = maxSockets;
          ios = ioThreadCount;
        } finally {
          optSync.unlock();
        }
        slotCount = mazmq + ios + 2;
        slots = new Mailbox[slotCount];
        // alloc_assert (slots);

        //  Initialize the infrastructure for zmq_term thread.
        slots[TERM_TID] = termMailbox;

        //  Create the reaper thread.
        reaper = new Reaper(this, REAPER_TID);
        // alloc_assert (reaper);
        slots[REAPER_TID] = reaper.getMailbox();
        reaper.start();

        //  Create I/O thread objects and launch them.
        for (int i = 2; i != ios + 2; i++) {
          IOThread ioThread = new IOThread(this, i);
          // alloc_assert (io_thread);
          ioThreads.add(ioThread);
          slots[i] = ioThread.getMailbox();
          ioThread.start();
        }

        //  In the unused part of the slot array, create a list of empty slots.
        for (int i = (int) slotCount - 1; i >= (int) ios + 2; i--) {
          emptySlots.add(i);
          slots[i] = null;
        }
      }

      //  Once zmq_term() was called, we can't create new sockets.
      if (terminating) {
        throw new ZError.CtxTerminatedException();
      }

      //  If maxSockets limit was reached, return error.
      if (emptySlots.isEmpty()) {
        throw new IllegalStateException("EMFILE");
      }

      //  Choose a slot for the socket.
      int slot = emptySlots.pollLast();

      //  Generate new unique socket ID.
      int sid = maxSocketId.incrementAndGet();

      //  Create the socket and register its mailbox.
      s = SocketBase.create(type, this, slot, sid);
      if (s == null) {
        emptySlots.addLast(slot);
        return null;
      }
      sockets.add(s);
      slots[slot] = s.getMailbox();
    } finally {
      slotSync.unlock();
    }

    return s;
  }