Skip to content

Commit

Permalink
Expose mysql_thread_end as endThread
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-rouse committed Oct 30, 2016
1 parent a1346fc commit 5266930
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Database/MySQL/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module Database.MySQL.Base
-- * Concurrency
, initLibrary
, initThread
, endThread
) where

import Control.Applicative ((<$>), (<*>))
Expand Down Expand Up @@ -576,6 +577,20 @@ initThread = do
else throw $ ConnectionError "initThread" (-1)
"mysql_thread_init failed"

-- | Call @mysql_thread_end@
--
-- This is needed at thread exit to avoid a memory leak, except when using
-- a non-debug build of at least version 5.7.9 of the MySQL library.
-- See <https://dev.mysql.com/doc/refman/5.7/en/mysql-thread-end.html>.
-- However, the threads in question are the /OS threads/, so calling this
-- function is unlikely to be important except when using large numbers of
-- bound threads (see "Control.Concurrent"). Haskell threads - those created
-- with 'forkIO' and friends - share a small number of OS threads, so in those
-- it is hard to call this function safely, and little benefit in doing so (see
-- <https://ro-che.info/articles/2015-04-17-safe-concurrent-mysql-haskell>).
endThread :: IO ()
endThread = mysql_thread_end

withRes :: String -> Result -> (Ptr MYSQL_RES -> IO a) -> IO a
withRes func res act = do
valid <- readIORef (resValid res)
Expand Down
4 changes: 4 additions & 0 deletions Database/MySQL/Base/C.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ module Database.MySQL.Base.C
-- * Concurrency
, mysql_library_init
, mysql_thread_init
, mysql_thread_end
) where

#include "mysql_signals.h"
Expand Down Expand Up @@ -289,3 +290,6 @@ foreign import ccall safe "mysql.h mysql_server_init" mysql_library_init

foreign import ccall safe "mysql.h" mysql_thread_init
:: IO MyBool

foreign import ccall safe "mysql.h" mysql_thread_end
:: IO ()

0 comments on commit 5266930

Please sign in to comment.