public void stop() {
   if (reaper != null) {
     reaper.stop();
   }
   lock.lock();
   try {
     for (Iterator<Entry<Address, V>> i = conns.entrySet().iterator(); i.hasNext(); ) {
       Entry<Address, V> e = i.next();
       Util.close(e.getValue());
     }
     clear();
   } finally {
     lock.unlock();
   }
   conn_listeners.clear();
 }
Beispiel #2
0
  public void terminate() {
    tag = 0xdeadbeef;

    if (!starting.get()) {
      slotSync.lock();
      try {
        //  Check whether termination was already underway, but interrupted and now
        //  restarted.
        boolean restarted = terminating;
        terminating = true;

        //  First attempt to terminate the context.
        if (!restarted) {
          //  First send stop command to sockets so that any blocking calls
          //  can be interrupted. If there are no sockets we can ask reaper
          //  thread to stop.
          for (SocketBase socket : sockets) {
            socket.stop();
          }
          if (sockets.isEmpty()) {
            reaper.stop();
          }
        }
      } finally {
        slotSync.unlock();
      }
      //  Wait till reaper thread closes all the sockets.
      Command cmd = termMailbox.recv(-1);
      if (cmd == null) {
        throw new IllegalStateException();
      }
      assert (cmd.type() == Command.Type.DONE);
      slotSync.lock();
      try {
        assert (sockets.isEmpty());
      } finally {
        slotSync.unlock();
      }
    }

    //  Deallocate the resources.
    try {
      destroy();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
Beispiel #3
0
  public void destroySocket(SocketBase socket) {
    slotSync.lock();

    //  Free the associated thread slot.
    try {
      int tid = socket.getTid();
      emptySlots.add(tid);
      slots[tid] = null;

      //  Remove the socket from the list of sockets.
      sockets.remove(socket);

      //  If zmq_term() was already called and there are no more socket
      //  we can ask reaper thread to terminate.
      if (terminating && sockets.isEmpty()) {
        reaper.stop();
      }
    } finally {
      slotSync.unlock();
    }
  }