Make sure I / O calculations are performed on a specific thread

I need to make sure that some actions are performed on a specific OS thread. I wrote an API where this thread starts the loop, listening TQueue, and executes the given commands. On the user side, the API has an opaque value, which is actually just a new type in this queue.

One of the problems is that I really need to embed arbitrary actions (type IO a), but I believe that I cannot directly exchange messages of this type. So, I currently have something like this (pseudocode):

makeSafe :: RubyInterpreter -> IO a -> IO (Either RubyError a)
makeSafe (RubyInterpreter q) a = do
    mv <- newEmptyTMVarIO
    -- embedded is of type IO (), letting me send this in my queue
    let embedded = handleErrors a >>= atomically . putTMVar mv
    atomically (writeTQueue q (SomeMessage embedded))
    atomically (readTMVar mv)

(for more details this applies to hruby )

edit - explanations:

  • IO a , .
  • , API, , makeSafe -, , .
  • , , , / .
+4

All Articles