These are chat archives for vu3rdd/functorrent

2nd
Aug 2015
Jaseem Abid
@jaseemabid
Aug 02 2015 02:15
@dfordivam See Don Steward's answer IORef vs MVar.
Generally, in Haskell, use MVars or TVars (STM-based mutable cells), over STRef or IORef.
IORef is not thread safe.
Also, the performance penalty might not be a concern for us. Its in the order of nano seconds.
Here is a trivial benchmark https://gist.github.com/chemist/5853246
For purely consistency sake, I think we should stick to MVar. Else, we will have to explain to the next person why some particular fields are IORef when we use MVar everywhere else
Jaseem Abid
@jaseemabid
Aug 02 2015 02:53
@dfordivam What issues do you see with async exceptions? Why not use it for thread management?
Jaseem Abid
@jaseemabid
Aug 02 2015 03:12
We are doing that in a bunch of places anyway. For example, the server thread just kills the listener thread. We could extrapolate this everywhere. See the following code. Its already on master
serverThreadMain :: ServerThread -> IO ()
serverThreadMain st = serverInit st >>= serverMainLoop >>= serverExit
 where serverExit st1 = do
         tid <- takeMVar (listenThread st1)
         killThread tid
         putStrLn "Exiting server-thread"
Jaseem Abid
@jaseemabid
Aug 02 2015 03:45
Also, server thread's mainLoop is a tight loop without CPU. Massive waste of CPU cycles. control thread is a loop with a sleep. @vu3rdd @dfordivam Should I just use STM? Best way to poll for changes
Jaseem Abid
@jaseemabid
Aug 02 2015 03:53
@vu3rdd @dfordivam Any comments on this tool? http://nhmood.github.io/watson-ruby/
Ramakrishnan Muthukrishnan
@vu3rdd
Aug 02 2015 03:57
@jaseemabid on the use of STM, I am yet to read that code, so I don't have an opinion at this point.
Jaseem Abid
@jaseemabid
Aug 02 2015 03:58
The main app puts something into an MVar. Control thread polls with a sleep. Server thread polls with no sleep. Either dont poll or poll with STM's retry. Thats it.
Ramakrishnan Muthukrishnan
@vu3rdd
Aug 02 2015 03:59
Tried to wrap the peerstate record inside a state monad, but ended up removing it. It only complicates things.
Haskell records sucks so much. One thing I hate in Haskell is records.
Jaseem Abid
@jaseemabid
Aug 02 2015 03:59
I wrapped everything in brackets. Removed Stop action. Using killThread everywhere consistently
Ramakrishnan Muthukrishnan
@vu3rdd
Aug 02 2015 04:00
@jaseemabid that sounds nice, perhaps let us go down that path then.
Jaseem Abid
@jaseemabid
Aug 02 2015 04:00
Other than the fact that you cannot have 2 records with the same field in the same module, is there anything else wrong with it?
@dfordivam had some issues with using async exceptions (which is what killThread) but some of his code used that anyway. I'll raise a PR. Let him have a look at it and comment
startTorrentConc :: Log -> Metainfo -> IO ()
startTorrentConc _ m = do
  -- Handle user-interrupt
  interrupt <- newEmptyMVar
  _ <- installHandler sigINT (Catch $ putMVar interrupt sigINT) Nothing
  _ <- installHandler sigTERM (Catch $ putMVar interrupt sigTERM) Nothing

  -- Fork Control-Thread(s)
  (ct, ctid) <- initControlThread m

  -- Fork Server-Thread
  (_, stid) <- initServerThread [(m, ct)]

  -- Wait For user-interrupt
  _ <- takeMVar interrupt

  -- Exit gracefully
  killThread stid
  killThread ctid

  -- [review] - Why do we need a yield here?
  yield

  -- [review] - Why do we need a sleep here?
  threadDelay $ 4*1000*1000
This is how it looks now
Jaseem Abid
@jaseemabid
Aug 02 2015 04:10
So we don't have to wait for stop messages anymore. The parent kills the child thread.
And the child in its cleanup will kill its thread
And so on. Brackets everywhere
Ramakrishnan Muthukrishnan
@vu3rdd
Aug 02 2015 04:15
@jaseemabid yes, there is a reason why there are many different lens libraries. What you described is just one problem. The namespace pollution.
composing with records is so hard.
Jaseem Abid
@jaseemabid
Aug 02 2015 04:17
Agreed
Inconsistencies like controlTAction :: IORef ControlThreadAction and serverTAction :: MVar ServerThreadAction needs to be sorted out. I'm fixing that.
Ramakrishnan Muthukrishnan
@vu3rdd
Aug 02 2015 04:27
I am looking for some example programs/libraries to read, which use records + state monad. If any of you spot some, please let me know.
Jaseem Abid
@jaseemabid
Aug 02 2015 04:39
Yes.
I'm trying to get the threading bits to a clean state
Jaseem Abid
@jaseemabid
Aug 02 2015 06:27
type ¯_(ツ)_/¯ = IO ()
Jaseem Abid
@jaseemabid
Aug 02 2015 06:43
Tons of code duplication . stopDownload and stopPeerThread looks the same to me
Do we really need peer thread mocking?
Jaseem Abid
@jaseemabid
Aug 02 2015 09:14

@vu3rdd @dfordivam See the PR bangalore-haskell-user-group/functorrent#40
Build passed https://travis-ci.org/jaseemabid/functorrent/builds/73753845

Some changes are controversial, so give your best eye.

Jaseem Abid
@jaseemabid
Aug 02 2015 09:47
See the commit message. Changes are explained there