{"action":"created","issue":{"url":"https://api.github.com/repos/sandstorm-io/capnproto/issues/320","repository_url":"https://api.github.com/repos/sandstorm-io/capnproto","labels_url":"https://api.github.com/repos/sandstorm-io/capnproto/issues/320/labels{/name}","comments_url":"https://api.github.com/repos/sandstorm-io/capnproto/issues/320/comments","events_url":"https://api.github.com/repos/sandstorm-io/capnproto/issues/320/events","html_url":"https://github.com/sandstorm-io/capnproto/issues/320","id":151849152,"number":320,"title":"How to post / dispatch handler into EventLoop?","user":{"login":"katreniak","id":3446570,"avatar_url":"https://avatars.githubusercontent.com/u/3446570?v=3","gravatar_id":"","url":"https://api.github.com/users/katreniak","html_url":"https://github.com/katreniak","followers_url":"https://api.github.com/users/katreniak/followers","following_url":"https://api.github.com/users/katreniak/following{/other_user}","gists_url":"https://api.github.com/users/katreniak/gists{/gist_id}","starred_url":"https://api.github.com/users/katreniak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/katreniak/subscriptions","organizations_url":"https://api.github.com/users/katreniak/orgs","repos_url":"https://api.github.com/users/katreniak/repos","events_url":"https://api.github.com/users/katreniak/events{/privacy}","received_events_url":"https://api.github.com/users/katreniak/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-04-29T11:52:07Z","updated_at":"2016-04-30T00:27:26Z","closed_at":null,"body":"I am looking for a way how to post handler from other threads and have in it processed inside kj::EventLoop thread. Semantically equivalent to `asio::io_service::post(f)` and / or `asio::io_service::dispatch(f)` in `asio`.\r\n\r\nI failed to find such concept in `kj`. Am I just bad at searching?\r\n\r\nI can imagine to implement this on top of `kj::LowLevelAsyncIoProvider`. Create pipe, wrap read fd by `kj::LowLevelAsyncIoProvider::wrapInputFd` and use write fd from other threads. Method `post` moves the handler to heap allocated location and writes heap address to write. Asynchronous read loop in kj EvenLoop reads pointers to handlers, invokes them and releases the heap memory.\r\n\r\n@kentonv, are you interested in such functionality upstream? Hints for better approach? Hints for api?\r\n"},"comment":{"url":"https://api.github.com/repos/sandstorm-io/capnproto/issues/comments/215914037","html_url":"https://github.com/sandstorm-io/capnproto/issues/320#issuecomment-215914037","issue_url":"https://api.github.com/repos/sandstorm-io/capnproto/issues/320","id":215914037,"user":{"login":"kentonv","id":4001805,"avatar_url":"https://avatars.githubusercontent.com/u/4001805?v=3","gravatar_id":"","url":"https://api.github.com/users/kentonv","html_url":"https://github.com/kentonv","followers_url":"https://api.github.com/users/kentonv/followers","following_url":"https://api.github.com/users/kentonv/following{/other_user}","gists_url":"https://api.github.com/users/kentonv/gists{/gist_id}","starred_url":"https://api.github.com/users/kentonv/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kentonv/subscriptions","organizations_url":"https://api.github.com/users/kentonv/orgs","repos_url":"https://api.github.com/users/kentonv/repos","events_url":"https://api.github.com/users/kentonv/events{/privacy}","received_events_url":"https://api.github.com/users/kentonv/received_events","type":"User","site_admin":false},"created_at":"2016-04-30T00:27:25Z","updated_at":"2016-04-30T00:27:25Z","body":"Yeah, currently there is no explicit support for this, but you can use a pipe as you describe.\r\n\r\nI do expect to add explicit support at some point. This is why `EventPort` has the `wake()` method. I imagine we would have some separate thread-safe (mutex-protected or lock-free) event queue in each `EventLoop` and then cross-thread events would involve queuing an event there and then calling `wake()` on the `EventPort`. This causes `wait()` or `poll()` in the main thread to return true which hints to the thread that it should check the thread-safe queue (which it would otherwise skip to avoid synchronization overhead).\r\n\r\nI haven't quite figured out what the application-facing interface should be here, and I think this will require some design work.\r\n\r\nEventually I want it to be easy to define Cap'n Proto objects tied to threads and then make cross-thread calls the same way you'd make RPC calls.\r\n\r\nIn any case, I don't think the long-term API is going to be pipe-based; we can do something that performs much better than that. But I'd suggest that for now it probably makes sense for you to stick with pipes rather than try to design this complicated KJ feature..."}}