2

The mio library for asynchronous I/O relies on the developer to provide instances of the Token type in order to correlate events that have happened back to the source, e.g. a particular TcpStream or Handler::Timeout.

As you can see from the implementation, Token is just a wrapper type around a usize. It's tempting to simply increment a counter each time a Token is needed, but it will eventually overflow.

What rules should I keep in mind as I go to generate Tokens to pass to the EventLoop? Some specific questions:

  • If I have two threads who each have their own EventLoop, can they both use Token=0 to listen for events on two different streams? (i.e. are Tokens bound to a particular EventLoop instance?)
  • Can I use Token=0 to simultaneously represent both a TcpStream and a pending Timeout, or are they both stored in the same collection of Tokens?
  • Is there any harm in jumping from 0 to 1,000,000? (e.g. Are they being stored in a data structure that's optimized for sequential numbers?)

Thanks!

zslayton
  • 51,416
  • 9
  • 35
  • 50

1 Answers1

2

The short version: mio doesn't actually do anything with the tokens except pass them back to you when you receive the corresponding event, so you can use whatever tokens you want as far as mio is concerned. To answer your questions individually:

If I have two threads who each have their own EventLoop, can they both use Token=0 to listen for events on two different streams? (i.e. are Tokens bound to a particular EventLoop instance?)

Sure, that's fine.

Can I use Token=0 to simultaneously represent both a TcpStream and a pending Timeout, or are they both stored in the same collection of Tokens?

mio doesn't have a collection of tokens. If you don't need unique tokens to identify things in your application code, you're free to use the same token in different places. (I'm a bit confused by this question though, since as far as I can tell, timeouts don't use mio Tokens at all)

Is there any harm in jumping from 0 to 1,000,000? (e.g. Are they being stored in a data structure that's optimized for sequential numbers?)

No. As I said above, mio doesn't care about the value of your tokens.

fjh
  • 12,121
  • 4
  • 46
  • 46
  • Ah, my mistake. The [timeout_ms](http://rustdoc.s3-website-us-east-1.amazonaws.com/mio/master/mio/struct.EventLoop.html#method.timeout_ms) method of EventLoop takes a parameter called 'token', but I didn't notice that it's not of type 'Token'. I'm surprised that the Tokens aren't being stored somewhere; does mio not pre-allocate Slabs for keeping track of the connections it maintains? How is it maintaining an association between the Token I provide and the OS notification that a Stream is readable? – zslayton Sep 13 '15 at 16:02
  • 1
    No, I don't think mio really tracks the connections at all. The tokens are passed to epoll/kqueue when registering a file descriptor and epoll/kqueue pass them back up with the events. I don't really know much about the internal data structures of epoll or kqueue, but I don't think they use the tokens to identify anything (looking at the man pages, mio tokens are just opaque 'user data' as far as the underlying APIs are concerned). – fjh Sep 13 '15 at 16:37