public SocketBase CreateSocket(ZmqSocketType type) { SocketBase s = null; lock (slot_sync) { if (m_starting) { m_starting = false; // Initialise the array of mailboxes. Additional three slots are for // zmq_term thread and reaper thread. int ios; int mazmq; lock (m_optSync) { mazmq = m_maxSockets; ios = m_ioThreadCount; } m_slotCount = mazmq + ios + 2; m_slots = new Mailbox[m_slotCount]; //alloc_Debug.Assert(slots); // Initialise the infrastructure for zmq_term thread. m_slots[TermTid] = term_mailbox; // Create the reaper thread. m_reaper = new Reaper(this, ReaperTid); //alloc_Debug.Assert(reaper); m_slots[ReaperTid] = m_reaper.Mailbox; m_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_Debug.Assert(io_thread); m_ioThreads.Add(ioThread); m_slots[i] = ioThread.Mailbox; ioThread.Start(); } // In the unused part of the slot array, create a list of empty slots. for (int i = (int)m_slotCount - 1; i >= (int)ios + 2; i--) { empty_slots.AddToBack(i); m_slots[i] = null; } } // Once zmq_term() was called, we can't create new sockets. if (m_terminating) { ZError.ErrorNumber = ErrorNumber.ETERM; return null; } // If max_sockets limit was reached, return error. if (empty_slots.Count == 0) { ZError.ErrorNumber = ErrorNumber.EMFILE; return null; } // Choose a slot for the socket. int slot = empty_slots.RemoveFromBack(); // Generate new unique socket ID. int sid = s_maxSocketId.IncrementAndGet(); // Create the socket and register its mailbox. s = SocketBase.Create(type, this, slot, sid); if (s == null) { empty_slots.AddToBack(slot); return null; } sockets.Add(s); m_slots[slot] = s.Mailbox; //LOG.debug("NEW Slot [" + slot + "] " + s); } return s; }
public Ctx() { m_tag = 0xabadcafe; m_starting = true; m_terminating = false; m_reaper = null; m_slotCount = 0; m_slots = null; m_maxSockets = ZMQ.ZmqMaxSocketsDflt; m_ioThreadCount = ZMQ.ZmqIOThreadsDflt; slot_sync = new object(); endpoints_sync = new object(); m_optSync = new object(); term_mailbox = new Mailbox("terminater"); empty_slots = new Deque<int>(); m_ioThreads = new List<IOThread>(); sockets = new List<SocketBase>(); endpoints = new Dictionary<String, Endpoint>(); }