-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStorage.hs
More file actions
224 lines (202 loc) · 8.58 KB
/
Storage.hs
File metadata and controls
224 lines (202 loc) · 8.58 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnliftedFFITypes #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
-- | A perilous implementation of thread-local storage for Haskell.
-- This module uses a fair amount of GHC internals to enable performing
-- lookups of context for any threads that are alive. Caution should be
-- taken for consumers of this module to not retain ThreadId references
-- indefinitely, as that could delay cleanup of thread-local state.
--
-- Thread-local contexts have the following semantics:
--
-- - A value 'attach'ed to a 'ThreadId' will remain alive at least as long
-- as the 'ThreadId'.
-- - A value may be detached from a 'ThreadId' via 'detach' by the
-- library consumer without detriment.
-- - No guarantees are made about when a value will be garbage-collected
-- once all references to 'ThreadId' have been dropped. However, this simply
-- means in practice that any unused contexts will cleaned up upon the next
-- garbage collection and may not be actively freed when the program exits.
--
-- Note that this implementation of context sharing is
-- mildly expensive for the garbage collector, hard to reason about without deep
-- knowledge of the code you are instrumenting, and has limited guarantees of behavior
-- across GHC versions due to internals usage.
module Control.Concurrent.Thread.Storage
(
-- * Create a 'ThreadStorageMap'
ThreadStorageMap
, newThreadStorageMap
-- * Retrieve values from a 'ThreadStorageMap'
, lookup
, lookupOnThread
-- * Update values in a 'ThreadStorageMap'
, update
, updateOnThread
-- * Associate values with a thread in a 'ThreadStorageMap'
, attach
, attachOnThread
-- * Remove values from a thread in a 'ThreadStorageMap'
, detach
, detachFromThread
-- * Update values for a thread in a 'ThreadStorageMap'
, adjust
, adjustOnThread
-- * Monitoring utilities
, storedItems
-- * Thread ID manipulation
, getThreadId
#if MIN_VERSION_base(4,18,0)
, purgeDeadThreads
#endif
) where
import Control.Concurrent
import Control.Concurrent.Thread.Finalizers
import Control.Monad ( when, void, forM_ )
import Control.Monad.IO.Class
import Data.Maybe (isNothing, isJust)
import Data.Word (Word64)
import GHC.Base (Addr#)
import GHC.IO (IO(..), mask_)
import GHC.Int
#if MIN_VERSION_base(4,18,0)
import GHC.Conc (listThreads)
#endif
import GHC.Conc.Sync ( ThreadId(..) )
import GHC.Prim
import qualified Data.IntMap.Strict as I
import qualified Data.IntSet as IS
import Foreign.C.Types
import Prelude hiding (lookup)
import GHC.Exts (unsafeCoerce#)
foreign import ccall unsafe "rts_getThreadId" c_getThreadId :: Addr# -> CULLong
numStripes :: Word
numStripes = 32
getThreadId :: ThreadId -> Word
getThreadId (ThreadId tid#) = fromIntegral (c_getThreadId (unsafeCoerce# tid#))
stripeHash :: Word -> Int
stripeHash = fromIntegral . (`mod` numStripes)
readStripe :: ThreadStorageMap a -> ThreadId -> IO (I.IntMap a)
readStripe (ThreadStorageMap arr#) t = IO $ \s -> readArray# arr# tid# s
where
(I# tid#) = stripeHash $ getThreadId t
atomicModifyStripe :: ThreadStorageMap a -> Word -> (I.IntMap a -> (I.IntMap a, b)) -> IO b
atomicModifyStripe (ThreadStorageMap arr#) tid f = IO $ \s -> go s
where
(I# stripe#) = fromIntegral $ stripeHash tid
go s = case readArray# arr# stripe# s of
(# s1, intMap #) ->
let (updatedIntMap, result) = f intMap
in case casArray# arr# stripe# intMap updatedIntMap s1 of
(# s2, outcome, old #) -> case outcome of
0# -> updatedIntMap `seq` (# s2, result #)
1# -> go s2
_ -> error "Got impossible result in atomicModifyStripe"
-- | A storage mechanism for values of a type. This structure retains items
-- on per-(green)thread basis, which can be useful in rare cases.
data ThreadStorageMap a = ThreadStorageMap
(MutableArray# RealWorld (I.IntMap a))
-- | Create a new thread storage map. The map is striped by thread
-- into 32 sections in order to reduce contention.
newThreadStorageMap
:: MonadIO m => m (ThreadStorageMap a)
newThreadStorageMap = liftIO $ IO $ \s -> case newArray# numStripes# mempty s of
(# s1, ma #) -> (# s1, ThreadStorageMap ma #)
where
(I# numStripes#) = fromIntegral numStripes
-- | Retrieve a value if it exists for the current thread
lookup :: MonadIO m => ThreadStorageMap a -> m (Maybe a)
lookup tsm = liftIO $ do
tid <- myThreadId
lookupOnThread tsm tid
-- | Retrieve a value if it exists for the specified thread
lookupOnThread :: MonadIO m => ThreadStorageMap a -> ThreadId -> m (Maybe a)
lookupOnThread tsm tid = liftIO $ do
m <- readStripe tsm tid
pure $ I.lookup threadAsInt m
where
threadAsInt = fromIntegral $ getThreadId tid
-- | Associate the provided value with the current thread.
--
-- Returns the previous value if it was set.
attach :: MonadIO m => ThreadStorageMap a -> a -> m (Maybe a)
attach tsm x = liftIO $ do
tid <- myThreadId
attachOnThread tsm tid x
-- | Associate the provided value with the specified thread. This replaces
-- any values already associated with the 'ThreadId'.
attachOnThread :: MonadIO m => ThreadStorageMap a -> ThreadId -> a -> m (Maybe a)
attachOnThread tsm tid ctxt =
updateOnThread tsm tid (\prev -> (Just ctxt, prev))
-- | Disassociate the associated value from the current thread, returning it if it exists.
detach :: MonadIO m => ThreadStorageMap a -> m (Maybe a)
detach tsm = liftIO $ do
tid <- myThreadId
detachFromThread tsm tid
-- | Disassociate the associated value from the specified thread, returning it if it exists.
detachFromThread :: MonadIO m => ThreadStorageMap a -> ThreadId -> m (Maybe a)
detachFromThread tsm tid = liftIO $ do
let threadAsInt = getThreadId tid
updateOnThread tsm tid (\prev -> (Nothing, prev))
-- | The most general function in this library. Update a 'ThreadStorageMap' on a given thread,
-- with the ability to add or remove values and return some sort of result.
updateOnThread :: MonadIO m => ThreadStorageMap a -> ThreadId -> (Maybe a -> (Maybe a, b)) -> m b
updateOnThread tsm tid f = liftIO $ mask_ $ do
-- ^ We mask here in order to ensure that the finalizer will always be created
(isNewThreadEntry, result) <- atomicModifyStripe tsm threadAsWord $ \m ->
let (resultWithNewThreadDetection, m') =
I.alterF
(\x -> case f x of
(!x', !y) -> ((isNothing x && isJust x', y), x')
)
(fromIntegral threadAsWord)
m
in (m', resultWithNewThreadDetection)
when isNewThreadEntry $ do
addThreadFinalizer tid $ cleanUp tsm threadAsWord
pure result
where
threadAsWord = getThreadId tid
update :: MonadIO m => ThreadStorageMap a -> (Maybe a -> (Maybe a, b)) -> m b
update tsm f = liftIO $ do
tid <- myThreadId
updateOnThread tsm tid f
-- | Update the associated value for the current thread if it is attached.
adjust :: MonadIO m => ThreadStorageMap a -> (a -> a) -> m ()
adjust tsm f = liftIO $ do
tid <- myThreadId
adjustOnThread tsm tid f
-- | Update the associated value for the specified thread if it is attached.
adjustOnThread :: MonadIO m => ThreadStorageMap a -> ThreadId -> (a -> a) -> m ()
adjustOnThread tsm tid f = liftIO $ do
atomicModifyStripe tsm threadAsWord $ \m -> (I.adjust f (fromIntegral threadAsWord) m, ())
where
threadAsWord = getThreadId tid
-- Remove this context for thread from the map on finalization
cleanUp :: ThreadStorageMap a -> Word -> IO ()
cleanUp tsm tid = do
atomicModifyStripe tsm tid $ \m ->
(I.delete (fromIntegral tid) m, ())
-- | List thread ids with live entries in the 'ThreadStorageMap'.
--
-- This is useful for monitoring purposes to verify that there
-- are no memory leaks retaining threads and thus preventing
-- items from being freed from a 'ThreadStorageMap'
storedItems :: ThreadStorageMap a -> IO [(Int, a)]
storedItems tsm = do
stripes <- mapM (stripeByIndex tsm) [0..(fromIntegral numStripes - 1)]
pure $ concatMap I.toList stripes
where
stripeByIndex :: ThreadStorageMap a -> Int -> IO (I.IntMap a)
stripeByIndex (ThreadStorageMap arr#) (I# i#) = IO $ \s -> readArray# arr# i# s
#if MIN_VERSION_base(4,18,0)
-- | This should generally not be needed, but may be used to remove values prior to GC-triggered finalizers being run from the 'ThreadStorageMap' for threads that have exited.
purgeDeadThreads :: MonadIO m => ThreadStorageMap a -> m ()
purgeDeadThreads tsm = liftIO $ do
tids <- listThreads
let threadSet = IS.fromList $ map (fromIntegral . getThreadId) tids
forM_ [0..(numStripes - 1)] $ \stripe ->
atomicModifyStripe tsm stripe $ \im -> (I.restrictKeys im threadSet, ())
#endif