mirrored from https://gitlab.haskell.org/ghc/ghc.git
-
Notifications
You must be signed in to change notification settings - Fork 703
/
MVarList.hs
169 lines (148 loc) · 4.57 KB
/
MVarList.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE CPP
, ForeignFunctionInterface
, MagicHash
, UnboxedTuples
, ScopedTypeVariables
#-}
{-# OPTIONS_GHC -fno-warn-unused-imports -funbox-strict-fields #-}
-----------------------------------------------------------------------------
-- |
-- Module : LwConc.MVar
-- Copyright : (c) The University of Glasgow 2001
-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : libraries@haskell.org
-- Stability : experimental
-- Portability : non-portable (concurrency)
--
-- An implementation of MVar. This implementation is scheduler agnostic.
--
-----------------------------------------------------------------------------
module MVarList
(
MVar
, newMVar -- a -> IO (MVar a)
, newEmptyMVar -- IO (MVar a)
, putMVar -- MVar a -> a -> IO ()
, asyncPutMVar -- MVar a -> a -> PTM ()
, takeMVar -- MVar a -> IO a
, takeMVarWithHole -- MVar a -> IORef a -> IO a
) where
import LwConc.Substrate
import GHC.IORef
#include "profile.h"
-- data Queue a = Queue ![a] ![a]
--
-- _INL_(emptyQueue)
-- emptyQueue :: Queue a
-- emptyQueue = Queue [] []
--
-- _INL_(enque)
-- enque :: Queue a -> a -> Queue a
-- enque (Queue front back) e = Queue front $ e:back
--
-- _INL_(deque)
-- deque :: Queue a -> (Queue a, Maybe a)
-- deque (Queue !front !back) =
-- case front of
-- [] -> (case reverse back of
-- [] -> (emptyQueue, Nothing)
-- x:tl -> (Queue tl [], Just x))
-- x:tl -> (Queue tl back, Just x)
-- NOTE KC: Even a list seems to work just as well as a queue.
data Queue a = Queue [a]
_INL_(emptyQueue)
emptyQueue :: Queue a
emptyQueue = Queue []
_INL_(enque)
enque :: Queue a -> a -> Queue a
enque (Queue q) e = Queue $! e:q
_INL_(deque)
deque :: Queue a -> (Queue a, Maybe a)
deque (Queue q) =
case q of
[] -> (emptyQueue, Nothing)
x:tl -> (Queue tl, Just x)
newtype MVar a = MVar (PVar (MVPState a)) deriving (Eq)
data MVPState a = Full !a (Queue (a, PTM()))
| Empty (Queue (IORef a, PTM()))
_INL_(newMVar)
newMVar :: a -> IO (MVar a)
newMVar x = do
ref <- newPVarIO $! Full x emptyQueue
return $! MVar ref
_INL_(newEmptyMVar)
newEmptyMVar :: IO (MVar a)
newEmptyMVar = do
ref <- newPVarIO $! Empty emptyQueue
return $! MVar ref
_INL_(asyncPutMVar)
asyncPutMVar :: MVar a -> a -> PTM ()
asyncPutMVar (MVar ref) x = do
st <- readPVar ref
case st of
Empty ts ->
case deque ts of
(_, Nothing) -> do
writePVar ref $! Full x emptyQueue
(ts', Just (hole, wakeup)) -> do
unsafeIOToPTM $! writeIORef hole x
writePVar ref $! Empty ts'
wakeup
Full x' ts -> do
writePVar ref $ Full x' $ enque ts (x, return ())
_INL_(putMVarPTM)
putMVarPTM :: MVar a -> a -> ResumeToken -> PTM ()
putMVarPTM (MVar ref) x token = do
st <- readPVar ref
case st of
Empty ts -> do
case deque ts of
(_, Nothing) -> do
writePVar ref $! Full x emptyQueue
(ts', Just (hole, wakeup)) -> do
unsafeIOToPTM $! writeIORef hole x
writePVar ref $! Empty ts'
wakeup
Full x' ts -> do
blockAct <- getYieldControlAction
sc <- getSCont
unblockAct <- getScheduleSContAction
let !wakeup = unblockAct sc
writePVar ref $! Full x' $! enque ts (x, wakeup)
setSContSwitchReason sc $! BlockedInHaskell token
blockAct
_INL_(putMVar)
putMVar :: MVar a -> a -> ResumeToken -> IO ()
putMVar mv x token = atomically $ putMVarPTM mv x token
_INL_(takeMVarWithHole)
takeMVarWithHole :: MVar a -> IORef a -> ResumeToken -> IO a
takeMVarWithHole (MVar ref) hole token = do
atomically $ do
st <- readPVar ref
case st of
Empty ts -> do
blockAct <- getYieldControlAction
sc <- getSCont
unblockAct <- getScheduleSContAction
let !wakeup = unblockAct sc
writePVar ref $! Empty $! enque ts (hole, wakeup)
setSContSwitchReason sc $! BlockedInHaskell token
blockAct
Full x ts -> do
case deque ts of
(_, Nothing) -> do
writePVar ref $! Empty emptyQueue
unsafeIOToPTM $! writeIORef hole x
(ts', Just (x', wakeup)) -> do
writePVar ref $! Full x' ts'
unsafeIOToPTM $! writeIORef hole x
wakeup
readIORef hole
_INL_(takeMVar)
takeMVar :: MVar a -> ResumeToken -> IO a
takeMVar m token = do
hole <- newIORef undefined
takeMVarWithHole m hole token