Skip to content

Commit 50becbe

Browse files
committed
remove duplicate OnBlocked data structure and add three-argument callbacks
1 parent af69794 commit 50becbe

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

GHCJS/Concurrent.hs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
-}
2727

2828
module GHCJS.Concurrent ( isThreadSynchronous
29-
, isContinueAsync
30-
, OnBlock (..)
29+
, isThreadContinueAsync
30+
, OnBlocked(..)
3131
, WouldBlockException(..)
3232
, synchronously
3333
) where
@@ -47,9 +47,17 @@ import Data.Typeable
4747

4848
import Unsafe.Coerce
4949

50-
data OnBlock = ContinueAsync
51-
| ThrowWouldBlock
52-
deriving (Data, Typeable, Enum, Show, Eq, Ord)
50+
{- |
51+
The runtime tries to run synchronous threads to completion. Sometimes it's
52+
not possible to continue running a thread, for example when the thread
53+
tries to take an empty 'MVar'. The runtime can then either throw a
54+
'WouldBlockException', aborting the blocking action, or continue the
55+
thread asynchronously.
56+
-}
57+
58+
data OnBlocked = ContinueAsync -- ^ continue the thread asynchronously if blocked
59+
| ThrowWouldBlock -- ^ throw 'WouldBlockException' if blocked
60+
deriving (Data, Typeable, Enum, Show, Eq, Ord)
5361

5462
{- |
5563
Runs the action synchronously, which means that the thread will not
@@ -80,8 +88,8 @@ isThreadSynchronous = fmap (`testBit` 0) . syncThreadState
8088
Returns whether the 'ThreadId' will continue running async. Always
8189
returns 'True' when the thread is not synchronous.
8290
-}
83-
isContinueAsync :: ThreadId -> IO Bool
84-
isContinueAsync = fmap (`testBit` 1) . syncThreadState
91+
isThreadContinueAsync :: ThreadId -> IO Bool
92+
isThreadContinueAsync = fmap (`testBit` 1) . syncThreadState
8593

8694
syncThreadState :: ThreadId-> IO Int
8795
syncThreadState (ThreadId tid) = js_syncThreadState tid

GHCJS/Foreign/Callback.hs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ module GHCJS.Foreign.Callback
77
, asyncCallback
88
, asyncCallback1
99
, asyncCallback2
10+
, asyncCallback3
1011
, syncCallback
1112
, syncCallback1
1213
, syncCallback2
14+
, syncCallback3
1315
) where
1416

17+
import GHCJS.Concurrent
1518
import GHCJS.Marshal
1619
import GHCJS.Marshal.Pure
1720
import GHCJS.Foreign.Callback.Internal
@@ -24,17 +27,6 @@ import Data.Typeable
2427

2528
import Unsafe.Coerce
2629

27-
{- |
28-
The runtime tries to run synchronous threads to completion. Sometimes it's
29-
not possible to continue running a thread, for example when the thread
30-
tries to take an empty 'MVar'. The runtime can then either throw a
31-
'WouldBlockException', aborting the blocking action, or continue the
32-
thread asynchronously.
33-
-}
34-
data OnBlocked = ContinueAsync -- ^ continue the thread asynchronously if blocked
35-
| ThrowWouldBlock -- ^ throw 'WouldBlockException' if blocked
36-
deriving (Show, Eq, Enum, Typeable)
37-
3830
{- |
3931
When you create a callback, the Haskell runtime stores a reference to
4032
the exported IO action or function. This means that all data referenced by the
@@ -84,6 +76,19 @@ syncCallback2 :: OnBlocked -- ^ what to do when th
8476
-> IO (Callback (JSVal -> JSVal -> IO ())) -- ^ the callback
8577
syncCallback2 onBlocked x = js_syncCallbackApply (onBlocked == ContinueAsync) 2 (unsafeCoerce x)
8678

79+
{- | Make a callback (JavaScript function) that runs the supplied IO function in a synchronous
80+
thread when called. The callback takes three arguments that it passes as JSVal values to
81+
the Haskell function.
82+
83+
Call 'releaseCallback' when done with the callback, freeing data referenced
84+
by the function.
85+
-}
86+
syncCallback3 :: OnBlocked -- ^ what to do when the thread blocks
87+
-> (JSVal -> JSVal -> JSVal -> IO ()) -- ^ the Haskell function
88+
-> IO (Callback (JSVal -> JSVal -> JSVal -> IO ())) -- ^ the callback
89+
syncCallback3 onBlocked x = js_syncCallbackApply (onBlocked == ContinueAsync) 3 (unsafeCoerce x)
90+
91+
8792

8893
{- | Make a callback (JavaScript function) that runs the supplied IO action in an asynchronous
8994
thread when called.
@@ -103,6 +108,10 @@ asyncCallback2 :: (JSVal -> JSVal -> IO ()) -- ^ the Haskell function
103108
-> IO (Callback (JSVal -> JSVal -> IO ())) -- ^ the callback
104109
asyncCallback2 x = js_asyncCallbackApply 2 (unsafeCoerce x)
105110

111+
asyncCallback3 :: (JSVal -> JSVal -> JSVal -> IO ()) -- ^ the Haskell function that the callback calls
112+
-> IO (Callback (JSVal -> JSVal -> JSVal -> IO ())) -- ^ the callback
113+
asyncCallback3 x = js_asyncCallbackApply 3 (unsafeCoerce x)
114+
106115
-- ----------------------------------------------------------------------------
107116

108117
foreign import javascript unsafe "h$makeCallback(h$runSync, [$1], $2)"

0 commit comments

Comments
 (0)