/usr/share/doc/libghc-resourcet-doc/html/src/Control-Monad-Trans-Resource.html is in libghc-resourcet-doc 1.1.8-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
<title>Control/Monad/Trans/Resource.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>{-# LANGUAGE FlexibleContexts #-}</span>
<a name="line-2"></a><span class='hs-comment'>{-# LANGUAGE FlexibleInstances #-}</span>
<a name="line-3"></a><span class='hs-comment'>{-# LANGUAGE MultiParamTypeClasses #-}</span>
<a name="line-4"></a><span class='hs-comment'>{-# LANGUAGE UndecidableInstances #-}</span>
<a name="line-5"></a><span class='hs-comment'>{-# LANGUAGE TypeFamilies #-}</span>
<a name="line-6"></a><span class='hs-comment'>{-# LANGUAGE RankNTypes #-}</span>
<a name="line-7"></a><span class='hs-comment'>{-# LANGUAGE CPP #-}</span>
<a name="line-8"></a><span class='hs-comment'>{-# LANGUAGE DeriveDataTypeable #-}</span>
<a name="line-9"></a><span class='hs-cpp'>#if __GLASGOW_HASKELL__ >= 704</span>
<a name="line-10"></a><span class='hs-comment'>{-# LANGUAGE ConstraintKinds #-}</span>
<a name="line-11"></a><span class='hs-cpp'>#endif</span>
<a name="line-12"></a><span class='hs-comment'>{-# LANGUAGE Safe #-}</span>
<a name="line-13"></a><span class='hs-comment'>-- | Allocate resources which are guaranteed to be released.</span>
<a name="line-14"></a><span class='hs-comment'>--</span>
<a name="line-15"></a><span class='hs-comment'>-- For more information, see <https://www.fpcomplete.com/user/snoyberg/library-documentation/resourcet>.</span>
<a name="line-16"></a><span class='hs-comment'>--</span>
<a name="line-17"></a><span class='hs-comment'>-- One point to note: all register cleanup actions live in the @IO@ monad, not</span>
<a name="line-18"></a><span class='hs-comment'>-- the main monad. This allows both more efficient code, and for monads to be</span>
<a name="line-19"></a><span class='hs-comment'>-- transformed.</span>
<a name="line-20"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Trans</span><span class='hs-varop'>.</span><span class='hs-conid'>Resource</span>
<a name="line-21"></a> <span class='hs-layout'>(</span> <span class='hs-comment'>-- * Data types</span>
<a name="line-22"></a> <span class='hs-conid'>ResourceT</span>
<a name="line-23"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>ResIO</span>
<a name="line-24"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>ReleaseKey</span>
<a name="line-25"></a> <span class='hs-comment'>-- * Unwrap</span>
<a name="line-26"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>runResourceT</span>
<a name="line-27"></a> <span class='hs-comment'>-- * Special actions</span>
<a name="line-28"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>resourceForkIO</span>
<a name="line-29"></a> <span class='hs-comment'>-- * Monad transformation</span>
<a name="line-30"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>transResourceT</span>
<a name="line-31"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>joinResourceT</span>
<a name="line-32"></a> <span class='hs-comment'>-- * Registering/releasing</span>
<a name="line-33"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>allocate</span>
<a name="line-34"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>register</span>
<a name="line-35"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>release</span>
<a name="line-36"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>unprotect</span>
<a name="line-37"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>resourceMask</span>
<a name="line-38"></a> <span class='hs-comment'>-- * Type class/associated types</span>
<a name="line-39"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>MonadResource</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span>
<a name="line-40"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>MonadResourceBase</span>
<a name="line-41"></a> <span class='hs-comment'>-- ** Low-level</span>
<a name="line-42"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>InvalidAccess</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span>
<a name="line-43"></a> <span class='hs-comment'>-- * Re-exports</span>
<a name="line-44"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>MonadBaseControl</span>
<a name="line-45"></a> <span class='hs-comment'>-- * Internal state</span>
<a name="line-46"></a> <span class='hs-comment'>-- $internalState</span>
<a name="line-47"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>InternalState</span>
<a name="line-48"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>getInternalState</span>
<a name="line-49"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>runInternalState</span>
<a name="line-50"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>withInternalState</span>
<a name="line-51"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>createInternalState</span>
<a name="line-52"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>closeInternalState</span>
<a name="line-53"></a> <span class='hs-comment'>-- * Backwards compatibility</span>
<a name="line-54"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>ExceptionT</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span>
<a name="line-55"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>runExceptionT</span>
<a name="line-56"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>runExceptionT_</span>
<a name="line-57"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>runException</span>
<a name="line-58"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>runException_</span>
<a name="line-59"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>MonadThrow</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span>
<a name="line-60"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>monadThrow</span>
<a name="line-61"></a> <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-62"></a>
<a name="line-63"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>IntMap</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>IntMap</span>
<a name="line-64"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Exception</span> <span class='hs-layout'>(</span><span class='hs-conid'>SomeException</span><span class='hs-layout'>,</span> <span class='hs-varid'>throw</span><span class='hs-layout'>)</span>
<a name="line-65"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Trans</span><span class='hs-varop'>.</span><span class='hs-conid'>Control</span>
<a name="line-66"></a> <span class='hs-layout'>(</span> <span class='hs-conid'>MonadBaseControl</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-varid'>liftBaseDiscard</span><span class='hs-layout'>,</span> <span class='hs-varid'>control</span> <span class='hs-layout'>)</span>
<a name="line-67"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>IORef</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>I</span>
<a name="line-68"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Base</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadBase</span><span class='hs-layout'>,</span> <span class='hs-varid'>liftBase</span><span class='hs-layout'>)</span>
<a name="line-69"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Applicative</span> <span class='hs-layout'>(</span><span class='hs-conid'>Applicative</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-70"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>IO</span><span class='hs-varop'>.</span><span class='hs-conid'>Class</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadIO</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-71"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span> <span class='hs-layout'>(</span><span class='hs-varid'>liftM</span><span class='hs-layout'>)</span>
<a name="line-72"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Exception</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>E</span>
<a name="line-73"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Monoid</span> <span class='hs-layout'>(</span><span class='hs-conid'>Monoid</span><span class='hs-layout'>)</span>
<a name="line-74"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Exception</span><span class='hs-varop'>.</span><span class='hs-conid'>Lifted</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>L</span>
<a name="line-75"></a>
<a name="line-76"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Trans</span><span class='hs-varop'>.</span><span class='hs-conid'>Resource</span><span class='hs-varop'>.</span><span class='hs-conid'>Internal</span>
<a name="line-77"></a>
<a name="line-78"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Concurrent</span> <span class='hs-layout'>(</span><span class='hs-conid'>ThreadId</span><span class='hs-layout'>,</span> <span class='hs-varid'>forkIO</span><span class='hs-layout'>)</span>
<a name="line-79"></a>
<a name="line-80"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Functor</span><span class='hs-varop'>.</span><span class='hs-conid'>Identity</span> <span class='hs-layout'>(</span><span class='hs-conid'>Identity</span><span class='hs-layout'>,</span> <span class='hs-varid'>runIdentity</span><span class='hs-layout'>)</span>
<a name="line-81"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Catch</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadThrow</span><span class='hs-layout'>,</span> <span class='hs-varid'>throwM</span><span class='hs-layout'>)</span>
<a name="line-82"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Catch</span><span class='hs-varop'>.</span><span class='hs-conid'>Pure</span> <span class='hs-layout'>(</span><span class='hs-conid'>CatchT</span><span class='hs-layout'>,</span> <span class='hs-varid'>runCatchT</span><span class='hs-layout'>)</span>
<a name="line-83"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Acquire</span><span class='hs-varop'>.</span><span class='hs-conid'>Internal</span> <span class='hs-layout'>(</span><span class='hs-conid'>ReleaseType</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-84"></a>
<a name="line-85"></a>
<a name="line-86"></a>
<a name="line-87"></a><a name="register"></a><span class='hs-comment'>-- | Register some action that will be called precisely once, either when</span>
<a name="line-88"></a><span class='hs-comment'>-- 'runResourceT' is called, or when the 'ReleaseKey' is passed to 'release'.</span>
<a name="line-89"></a><span class='hs-comment'>--</span>
<a name="line-90"></a><span class='hs-comment'>-- Since 0.3.0</span>
<a name="line-91"></a><span class='hs-definition'>register</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadResource</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>()</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-conid'>ReleaseKey</span>
<a name="line-92"></a><span class='hs-definition'>register</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftResourceT</span> <span class='hs-varop'>.</span> <span class='hs-varid'>registerRIO</span>
<a name="line-93"></a>
<a name="line-94"></a><a name="release"></a><span class='hs-comment'>-- | Call a release action early, and deregister it from the list of cleanup</span>
<a name="line-95"></a><span class='hs-comment'>-- actions to be performed.</span>
<a name="line-96"></a><span class='hs-comment'>--</span>
<a name="line-97"></a><span class='hs-comment'>-- Since 0.3.0</span>
<a name="line-98"></a><span class='hs-definition'>release</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>ReleaseKey</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-conid'>()</span>
<a name="line-99"></a><span class='hs-definition'>release</span> <span class='hs-layout'>(</span><span class='hs-conid'>ReleaseKey</span> <span class='hs-varid'>istate</span> <span class='hs-varid'>rk</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftIO</span> <span class='hs-varop'>$</span> <span class='hs-varid'>release'</span> <span class='hs-varid'>istate</span> <span class='hs-varid'>rk</span>
<a name="line-100"></a> <span class='hs-layout'>(</span><span class='hs-varid'>maybe</span> <span class='hs-layout'>(</span><span class='hs-varid'>return</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span> <span class='hs-varid'>id</span><span class='hs-layout'>)</span>
<a name="line-101"></a>
<a name="line-102"></a><a name="unprotect"></a><span class='hs-comment'>-- | Unprotect resource from cleanup actions, this allowes you to send</span>
<a name="line-103"></a><span class='hs-comment'>-- resource into another resourcet process and reregister it there.</span>
<a name="line-104"></a><span class='hs-comment'>-- It returns an release action that should be run in order to clean</span>
<a name="line-105"></a><span class='hs-comment'>-- resource or Nothing in case if resource is already freed.</span>
<a name="line-106"></a><span class='hs-comment'>--</span>
<a name="line-107"></a><span class='hs-comment'>-- Since 0.4.5</span>
<a name="line-108"></a><span class='hs-definition'>unprotect</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>ReleaseKey</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-conid'>IO</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-109"></a><span class='hs-definition'>unprotect</span> <span class='hs-layout'>(</span><span class='hs-conid'>ReleaseKey</span> <span class='hs-varid'>istate</span> <span class='hs-varid'>rk</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftIO</span> <span class='hs-varop'>$</span> <span class='hs-varid'>release'</span> <span class='hs-varid'>istate</span> <span class='hs-varid'>rk</span> <span class='hs-varid'>return</span>
<a name="line-110"></a>
<a name="line-111"></a><a name="allocate"></a><span class='hs-comment'>-- | Perform some allocation, and automatically register a cleanup action.</span>
<a name="line-112"></a><span class='hs-comment'>--</span>
<a name="line-113"></a><span class='hs-comment'>-- This is almost identical to calling the allocation and then</span>
<a name="line-114"></a><span class='hs-comment'>-- @register@ing the release action, but this properly handles masking of</span>
<a name="line-115"></a><span class='hs-comment'>-- asynchronous exceptions.</span>
<a name="line-116"></a><span class='hs-comment'>--</span>
<a name="line-117"></a><span class='hs-comment'>-- Since 0.3.0</span>
<a name="line-118"></a><span class='hs-definition'>allocate</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadResource</span> <span class='hs-varid'>m</span>
<a name="line-119"></a> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span> <span class='hs-comment'>-- ^ allocate</span>
<a name="line-120"></a> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span> <span class='hs-comment'>-- ^ free resource</span>
<a name="line-121"></a> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-layout'>(</span><span class='hs-conid'>ReleaseKey</span><span class='hs-layout'>,</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-122"></a><span class='hs-definition'>allocate</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftResourceT</span> <span class='hs-varop'>.</span> <span class='hs-varid'>allocateRIO</span> <span class='hs-varid'>a</span>
<a name="line-123"></a>
<a name="line-124"></a><a name="resourceMask"></a><span class='hs-comment'>-- | Perform asynchronous exception masking.</span>
<a name="line-125"></a><span class='hs-comment'>--</span>
<a name="line-126"></a><span class='hs-comment'>-- This is more general then @Control.Exception.mask@, yet more efficient</span>
<a name="line-127"></a><span class='hs-comment'>-- than @Control.Exception.Lifted.mask@.</span>
<a name="line-128"></a><span class='hs-comment'>--</span>
<a name="line-129"></a><span class='hs-comment'>-- Since 0.3.0</span>
<a name="line-130"></a><span class='hs-definition'>resourceMask</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadResource</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-keyword'>forall</span> <span class='hs-varid'>a</span><span class='hs-varop'>.</span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>b</span>
<a name="line-131"></a><span class='hs-definition'>resourceMask</span> <span class='hs-varid'>r</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftResourceT</span> <span class='hs-layout'>(</span><span class='hs-varid'>resourceMaskRIO</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span>
<a name="line-132"></a>
<a name="line-133"></a><a name="allocateRIO"></a><span class='hs-definition'>allocateRIO</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-layout'>(</span><span class='hs-conid'>ReleaseKey</span><span class='hs-layout'>,</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-134"></a><span class='hs-definition'>allocateRIO</span> <span class='hs-varid'>acquire</span> <span class='hs-varid'>rel</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>ResourceT</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>istate</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>liftIO</span> <span class='hs-varop'>$</span> <span class='hs-conid'>E</span><span class='hs-varop'>.</span><span class='hs-varid'>mask</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>restore</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-135"></a> <span class='hs-varid'>a</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>restore</span> <span class='hs-varid'>acquire</span>
<a name="line-136"></a> <span class='hs-varid'>key</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>register'</span> <span class='hs-varid'>istate</span> <span class='hs-varop'>$</span> <span class='hs-varid'>rel</span> <span class='hs-varid'>a</span>
<a name="line-137"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>key</span><span class='hs-layout'>,</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-138"></a>
<a name="line-139"></a><a name="registerRIO"></a><span class='hs-definition'>registerRIO</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>IO</span> <span class='hs-conid'>()</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-conid'>ReleaseKey</span>
<a name="line-140"></a><span class='hs-definition'>registerRIO</span> <span class='hs-varid'>rel</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>ResourceT</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>istate</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>liftIO</span> <span class='hs-varop'>$</span> <span class='hs-varid'>register'</span> <span class='hs-varid'>istate</span> <span class='hs-varid'>rel</span>
<a name="line-141"></a>
<a name="line-142"></a><a name="resourceMaskRIO"></a><span class='hs-definition'>resourceMaskRIO</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-keyword'>forall</span> <span class='hs-varid'>a</span><span class='hs-varop'>.</span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>b</span>
<a name="line-143"></a><span class='hs-definition'>resourceMaskRIO</span> <span class='hs-varid'>f</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>ResourceT</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>istate</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>liftIO</span> <span class='hs-varop'>$</span> <span class='hs-conid'>E</span><span class='hs-varop'>.</span><span class='hs-varid'>mask</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>restore</span> <span class='hs-keyglyph'>-></span>
<a name="line-144"></a> <span class='hs-keyword'>let</span> <span class='hs-conid'>ResourceT</span> <span class='hs-varid'>f'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>f</span> <span class='hs-layout'>(</span><span class='hs-varid'>go</span> <span class='hs-varid'>restore</span><span class='hs-layout'>)</span>
<a name="line-145"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>f'</span> <span class='hs-varid'>istate</span>
<a name="line-146"></a> <span class='hs-keyword'>where</span>
<a name="line-147"></a> <span class='hs-varid'>go</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-keyword'>forall</span> <span class='hs-varid'>a</span><span class='hs-varop'>.</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-keyword'>forall</span> <span class='hs-varid'>a</span><span class='hs-varop'>.</span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-148"></a> <span class='hs-varid'>go</span> <span class='hs-varid'>r</span> <span class='hs-layout'>(</span><span class='hs-conid'>ResourceT</span> <span class='hs-varid'>g</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>ResourceT</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>i</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>r</span> <span class='hs-layout'>(</span><span class='hs-varid'>g</span> <span class='hs-varid'>i</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-149"></a>
<a name="line-150"></a>
<a name="line-151"></a>
<a name="line-152"></a><a name="release'"></a><span class='hs-definition'>release'</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>I</span><span class='hs-varop'>.</span><span class='hs-conid'>IORef</span> <span class='hs-conid'>ReleaseMap</span>
<a name="line-153"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Int</span>
<a name="line-154"></a> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-conid'>IO</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-155"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-varid'>a</span>
<a name="line-156"></a><span class='hs-definition'>release'</span> <span class='hs-varid'>istate</span> <span class='hs-varid'>key</span> <span class='hs-varid'>act</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>E</span><span class='hs-varop'>.</span><span class='hs-varid'>mask_</span> <span class='hs-varop'>$</span> <span class='hs-keyword'>do</span>
<a name="line-157"></a> <span class='hs-varid'>maction</span> <span class='hs-keyglyph'><-</span> <span class='hs-conid'>I</span><span class='hs-varop'>.</span><span class='hs-varid'>atomicModifyIORef</span> <span class='hs-varid'>istate</span> <span class='hs-varid'>lookupAction</span>
<a name="line-158"></a> <span class='hs-varid'>act</span> <span class='hs-varid'>maction</span>
<a name="line-159"></a> <span class='hs-keyword'>where</span>
<a name="line-160"></a> <span class='hs-varid'>lookupAction</span> <span class='hs-varid'>rm</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-conid'>ReleaseMap</span> <span class='hs-varid'>next</span> <span class='hs-varid'>rf</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-161"></a> <span class='hs-keyword'>case</span> <span class='hs-conid'>IntMap</span><span class='hs-varop'>.</span><span class='hs-varid'>lookup</span> <span class='hs-varid'>key</span> <span class='hs-varid'>m</span> <span class='hs-keyword'>of</span>
<a name="line-162"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>rm</span><span class='hs-layout'>,</span> <span class='hs-conid'>Nothing</span><span class='hs-layout'>)</span>
<a name="line-163"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>action</span> <span class='hs-keyglyph'>-></span>
<a name="line-164"></a> <span class='hs-layout'>(</span> <span class='hs-conid'>ReleaseMap</span> <span class='hs-varid'>next</span> <span class='hs-varid'>rf</span> <span class='hs-varop'>$</span> <span class='hs-conid'>IntMap</span><span class='hs-varop'>.</span><span class='hs-varid'>delete</span> <span class='hs-varid'>key</span> <span class='hs-varid'>m</span>
<a name="line-165"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>Just</span> <span class='hs-layout'>(</span><span class='hs-varid'>action</span> <span class='hs-conid'>ReleaseEarly</span><span class='hs-layout'>)</span>
<a name="line-166"></a> <span class='hs-layout'>)</span>
<a name="line-167"></a> <span class='hs-comment'>-- We tried to call release, but since the state is already closed, we</span>
<a name="line-168"></a> <span class='hs-comment'>-- can assume that the release action was already called. Previously,</span>
<a name="line-169"></a> <span class='hs-comment'>-- this threw an exception, though given that @release@ can be called</span>
<a name="line-170"></a> <span class='hs-comment'>-- from outside the context of a @ResourceT@ starting with version</span>
<a name="line-171"></a> <span class='hs-comment'>-- 0.4.4, it's no longer a library misuse or a library bug.</span>
<a name="line-172"></a> <span class='hs-varid'>lookupAction</span> <span class='hs-conid'>ReleaseMapClosed</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-conid'>ReleaseMapClosed</span><span class='hs-layout'>,</span> <span class='hs-conid'>Nothing</span><span class='hs-layout'>)</span>
<a name="line-173"></a>
<a name="line-174"></a>
<a name="line-175"></a>
<a name="line-176"></a><a name="runResourceT"></a><span class='hs-comment'>-- | Unwrap a 'ResourceT' transformer, and call all registered release actions.</span>
<a name="line-177"></a><span class='hs-comment'>--</span>
<a name="line-178"></a><span class='hs-comment'>-- Note that there is some reference counting involved due to 'resourceForkIO'.</span>
<a name="line-179"></a><span class='hs-comment'>-- If multiple threads are sharing the same collection of resources, only the</span>
<a name="line-180"></a><span class='hs-comment'>-- last call to @runResourceT@ will deallocate the resources.</span>
<a name="line-181"></a><span class='hs-comment'>--</span>
<a name="line-182"></a><span class='hs-comment'>-- Since 0.3.0</span>
<a name="line-183"></a><span class='hs-definition'>runResourceT</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadBaseControl</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>ResourceT</span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-184"></a><span class='hs-definition'>runResourceT</span> <span class='hs-layout'>(</span><span class='hs-conid'>ResourceT</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>control</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>run</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-185"></a> <span class='hs-varid'>istate</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>createInternalState</span>
<a name="line-186"></a> <span class='hs-conid'>E</span><span class='hs-varop'>.</span><span class='hs-varid'>mask</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>restore</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-187"></a> <span class='hs-varid'>res</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>restore</span> <span class='hs-layout'>(</span><span class='hs-varid'>run</span> <span class='hs-layout'>(</span><span class='hs-varid'>r</span> <span class='hs-varid'>istate</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varop'>`</span><span class='hs-conid'>E</span><span class='hs-varop'>.</span><span class='hs-varid'>onException</span><span class='hs-varop'>`</span>
<a name="line-188"></a> <span class='hs-varid'>stateCleanup</span> <span class='hs-conid'>ReleaseException</span> <span class='hs-varid'>istate</span>
<a name="line-189"></a> <span class='hs-varid'>stateCleanup</span> <span class='hs-conid'>ReleaseNormal</span> <span class='hs-varid'>istate</span>
<a name="line-190"></a> <span class='hs-varid'>return</span> <span class='hs-varid'>res</span>
<a name="line-191"></a>
<a name="line-192"></a><a name="bracket_"></a><span class='hs-definition'>bracket_</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadBaseControl</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>m</span>
<a name="line-193"></a> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>()</span> <span class='hs-comment'>-- ^ allocate</span>
<a name="line-194"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>()</span> <span class='hs-comment'>-- ^ normal cleanup</span>
<a name="line-195"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>()</span> <span class='hs-comment'>-- ^ exceptional cleanup</span>
<a name="line-196"></a> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-197"></a> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-198"></a><span class='hs-definition'>bracket_</span> <span class='hs-varid'>alloc</span> <span class='hs-varid'>cleanupNormal</span> <span class='hs-varid'>cleanupExc</span> <span class='hs-varid'>inside</span> <span class='hs-keyglyph'>=</span>
<a name="line-199"></a> <span class='hs-varid'>control</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>run</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>E</span><span class='hs-varop'>.</span><span class='hs-varid'>mask</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>restore</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-200"></a> <span class='hs-varid'>alloc</span>
<a name="line-201"></a> <span class='hs-varid'>res</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>restore</span> <span class='hs-layout'>(</span><span class='hs-varid'>run</span> <span class='hs-varid'>inside</span><span class='hs-layout'>)</span> <span class='hs-varop'>`</span><span class='hs-conid'>E</span><span class='hs-varop'>.</span><span class='hs-varid'>onException</span><span class='hs-varop'>`</span> <span class='hs-varid'>cleanupExc</span>
<a name="line-202"></a> <span class='hs-varid'>cleanupNormal</span>
<a name="line-203"></a> <span class='hs-varid'>return</span> <span class='hs-varid'>res</span>
<a name="line-204"></a>
<a name="line-205"></a><a name="finally"></a><span class='hs-definition'>finally</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadBaseControl</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>()</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-206"></a><span class='hs-definition'>finally</span> <span class='hs-varid'>action</span> <span class='hs-varid'>cleanup</span> <span class='hs-keyglyph'>=</span>
<a name="line-207"></a> <span class='hs-varid'>control</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>run</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>E</span><span class='hs-varop'>.</span><span class='hs-varid'>finally</span> <span class='hs-layout'>(</span><span class='hs-varid'>run</span> <span class='hs-varid'>action</span><span class='hs-layout'>)</span> <span class='hs-varid'>cleanup</span>
<a name="line-208"></a>
<a name="line-209"></a><a name="joinResourceT"></a><span class='hs-comment'>-- | This function mirrors @join@ at the transformer level: it will collapse</span>
<a name="line-210"></a><span class='hs-comment'>-- two levels of @ResourceT@ into a single @ResourceT@.</span>
<a name="line-211"></a><span class='hs-comment'>--</span>
<a name="line-212"></a><span class='hs-comment'>-- Since 0.4.6</span>
<a name="line-213"></a><span class='hs-definition'>joinResourceT</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ResourceT</span> <span class='hs-layout'>(</span><span class='hs-conid'>ResourceT</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varid'>a</span>
<a name="line-214"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-215"></a><span class='hs-definition'>joinResourceT</span> <span class='hs-layout'>(</span><span class='hs-conid'>ResourceT</span> <span class='hs-varid'>f</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>ResourceT</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>r</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>unResourceT</span> <span class='hs-layout'>(</span><span class='hs-varid'>f</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-varid'>r</span>
<a name="line-216"></a>
<a name="line-217"></a><a name="ExceptionT"></a><span class='hs-comment'>-- | For backwards compatibility.</span>
<a name="line-218"></a><a name="ExceptionT"></a><span class='hs-keyword'>type</span> <span class='hs-conid'>ExceptionT</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>CatchT</span>
<a name="line-219"></a>
<a name="line-220"></a><a name="runExceptionT"></a><span class='hs-comment'>-- | For backwards compatibility.</span>
<a name="line-221"></a><span class='hs-definition'>runExceptionT</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ExceptionT</span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-layout'>(</span><span class='hs-conid'>Either</span> <span class='hs-conid'>SomeException</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-222"></a><span class='hs-definition'>runExceptionT</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>runCatchT</span>
<a name="line-223"></a>
<a name="line-224"></a><a name="runExceptionT_"></a><span class='hs-comment'>-- | Same as 'runExceptionT', but immediately 'E.throw' any exception returned.</span>
<a name="line-225"></a><span class='hs-comment'>--</span>
<a name="line-226"></a><span class='hs-comment'>-- Since 0.3.0</span>
<a name="line-227"></a><span class='hs-definition'>runExceptionT_</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Monad</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>ExceptionT</span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-228"></a><span class='hs-definition'>runExceptionT_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftM</span> <span class='hs-layout'>(</span><span class='hs-varid'>either</span> <span class='hs-conid'>E</span><span class='hs-varop'>.</span><span class='hs-varid'>throw</span> <span class='hs-varid'>id</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>runExceptionT</span>
<a name="line-229"></a>
<a name="line-230"></a><a name="runException"></a><span class='hs-comment'>-- | Run an @ExceptionT Identity@ stack.</span>
<a name="line-231"></a><span class='hs-comment'>--</span>
<a name="line-232"></a><span class='hs-comment'>-- Since 0.4.2</span>
<a name="line-233"></a><span class='hs-definition'>runException</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ExceptionT</span> <span class='hs-conid'>Identity</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>SomeException</span> <span class='hs-varid'>a</span>
<a name="line-234"></a><span class='hs-definition'>runException</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>runIdentity</span> <span class='hs-varop'>.</span> <span class='hs-varid'>runExceptionT</span>
<a name="line-235"></a>
<a name="line-236"></a><a name="runException_"></a><span class='hs-comment'>-- | Run an @ExceptionT Identity@ stack, but immediately 'E.throw' any exception returned.</span>
<a name="line-237"></a><span class='hs-comment'>--</span>
<a name="line-238"></a><span class='hs-comment'>-- Since 0.4.2</span>
<a name="line-239"></a><span class='hs-definition'>runException_</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ExceptionT</span> <span class='hs-conid'>Identity</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span>
<a name="line-240"></a><span class='hs-definition'>runException_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>runIdentity</span> <span class='hs-varop'>.</span> <span class='hs-varid'>runExceptionT_</span>
<a name="line-241"></a>
<a name="line-242"></a><a name="resourceForkIO"></a><span class='hs-comment'>-- | Introduce a reference-counting scheme to allow a resource context to be</span>
<a name="line-243"></a><span class='hs-comment'>-- shared by multiple threads. Once the last thread exits, all remaining</span>
<a name="line-244"></a><span class='hs-comment'>-- resources will be released.</span>
<a name="line-245"></a><span class='hs-comment'>--</span>
<a name="line-246"></a><span class='hs-comment'>-- Note that abuse of this function will greatly delay the deallocation of</span>
<a name="line-247"></a><span class='hs-comment'>-- registered resources. This function should be used with care. A general</span>
<a name="line-248"></a><span class='hs-comment'>-- guideline:</span>
<a name="line-249"></a><span class='hs-comment'>--</span>
<a name="line-250"></a><span class='hs-comment'>-- If you are allocating a resource that should be shared by multiple threads,</span>
<a name="line-251"></a><span class='hs-comment'>-- and will be held for a long time, you should allocate it at the beginning of</span>
<a name="line-252"></a><span class='hs-comment'>-- a new @ResourceT@ block and then call @resourceForkIO@ from there.</span>
<a name="line-253"></a><span class='hs-comment'>--</span>
<a name="line-254"></a><span class='hs-comment'>-- Since 0.3.0</span>
<a name="line-255"></a><span class='hs-definition'>resourceForkIO</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadBaseControl</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>ResourceT</span> <span class='hs-varid'>m</span> <span class='hs-conid'>()</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-varid'>m</span> <span class='hs-conid'>ThreadId</span>
<a name="line-256"></a><span class='hs-definition'>resourceForkIO</span> <span class='hs-layout'>(</span><span class='hs-conid'>ResourceT</span> <span class='hs-varid'>f</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>ResourceT</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>r</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>L</span><span class='hs-varop'>.</span><span class='hs-varid'>mask</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>restore</span> <span class='hs-keyglyph'>-></span>
<a name="line-257"></a> <span class='hs-comment'>-- We need to make sure the counter is incremented before this call</span>
<a name="line-258"></a> <span class='hs-comment'>-- returns. Otherwise, the parent thread may call runResourceT before</span>
<a name="line-259"></a> <span class='hs-comment'>-- the child thread increments, and all resources will be freed</span>
<a name="line-260"></a> <span class='hs-comment'>-- before the child gets called.</span>
<a name="line-261"></a> <span class='hs-varid'>bracket_</span>
<a name="line-262"></a> <span class='hs-layout'>(</span><span class='hs-varid'>stateAlloc</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span>
<a name="line-263"></a> <span class='hs-layout'>(</span><span class='hs-varid'>return</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span>
<a name="line-264"></a> <span class='hs-layout'>(</span><span class='hs-varid'>return</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span>
<a name="line-265"></a> <span class='hs-layout'>(</span><span class='hs-varid'>liftBaseDiscard</span> <span class='hs-varid'>forkIO</span> <span class='hs-varop'>$</span> <span class='hs-varid'>bracket_</span>
<a name="line-266"></a> <span class='hs-layout'>(</span><span class='hs-varid'>return</span> <span class='hs-conid'>()</span><span class='hs-layout'>)</span>
<a name="line-267"></a> <span class='hs-layout'>(</span><span class='hs-varid'>stateCleanup</span> <span class='hs-conid'>ReleaseNormal</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span>
<a name="line-268"></a> <span class='hs-layout'>(</span><span class='hs-varid'>stateCleanup</span> <span class='hs-conid'>ReleaseException</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span>
<a name="line-269"></a> <span class='hs-layout'>(</span><span class='hs-varid'>restore</span> <span class='hs-varop'>$</span> <span class='hs-varid'>f</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-270"></a>
<a name="line-271"></a>
<a name="line-272"></a>
<a name="line-273"></a><span class='hs-comment'>-- | A @Monad@ which can be used as a base for a @ResourceT@.</span>
<a name="line-274"></a><span class='hs-comment'>--</span>
<a name="line-275"></a><span class='hs-comment'>-- A @ResourceT@ has some restrictions on its base monad:</span>
<a name="line-276"></a><span class='hs-comment'>--</span>
<a name="line-277"></a><span class='hs-comment'>-- * @runResourceT@ requires an instance of @MonadBaseControl IO@.</span>
<a name="line-278"></a><span class='hs-comment'>-- * @MonadResource@ requires an instance of @MonadThrow@, @MonadIO@, and @Applicative@.</span>
<a name="line-279"></a><span class='hs-comment'>--</span>
<a name="line-280"></a><span class='hs-comment'>-- While any instance of @MonadBaseControl IO@ should be an instance of the</span>
<a name="line-281"></a><span class='hs-comment'>-- other classes, this is not guaranteed by the type system (e.g., you may have</span>
<a name="line-282"></a><span class='hs-comment'>-- a transformer in your stack with does not implement @MonadThrow@). Ideally,</span>
<a name="line-283"></a><span class='hs-comment'>-- we would like to simply create an alias for the five type classes listed,</span>
<a name="line-284"></a><span class='hs-comment'>-- but this is not possible with GHC currently.</span>
<a name="line-285"></a><span class='hs-comment'>--</span>
<a name="line-286"></a><span class='hs-comment'>-- Instead, this typeclass acts as a proxy for the other five. Its only purpose</span>
<a name="line-287"></a><span class='hs-comment'>-- is to make your type signatures shorter.</span>
<a name="line-288"></a><span class='hs-comment'>--</span>
<a name="line-289"></a><span class='hs-comment'>-- Note that earlier versions of @conduit@ had a typeclass @ResourceIO@. This</span>
<a name="line-290"></a><span class='hs-comment'>-- fulfills much the same role.</span>
<a name="line-291"></a><span class='hs-comment'>--</span>
<a name="line-292"></a><span class='hs-comment'>-- Since 0.3.2</span>
<a name="line-293"></a><span class='hs-cpp'>#if __GLASGOW_HASKELL__ >= 704</span>
<a name="line-294"></a><a name="MonadResourceBase"></a><span class='hs-keyword'>type</span> <span class='hs-conid'>MonadResourceBase</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadBaseControl</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadThrow</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadBase</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>Applicative</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span>
<a name="line-295"></a><span class='hs-cpp'>#else</span>
<a name="line-296"></a><a name="MonadResourceBase"></a><span class='hs-keyword'>class</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadBaseControl</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadThrow</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>Applicative</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>MonadResourceBase</span> <span class='hs-varid'>m</span>
<a name="line-297"></a><a name="instance%20MonadResourceBase%20m"></a><span class='hs-keyword'>instance</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadBaseControl</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadThrow</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>Applicative</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>MonadResourceBase</span> <span class='hs-varid'>m</span>
<a name="line-298"></a><span class='hs-cpp'>#endif</span>
<a name="line-299"></a>
<a name="line-300"></a><span class='hs-comment'>-- $internalState</span>
<a name="line-301"></a><span class='hs-comment'>--</span>
<a name="line-302"></a><span class='hs-comment'>-- A @ResourceT@ internally is a modified @ReaderT@ monad transformer holding</span>
<a name="line-303"></a><span class='hs-comment'>-- onto a mutable reference to all of the release actions still remaining to be</span>
<a name="line-304"></a><span class='hs-comment'>-- performed. If you are building up a custom application monad, it may be more</span>
<a name="line-305"></a><span class='hs-comment'>-- efficient to embed this @ReaderT@ functionality directly in your own monad</span>
<a name="line-306"></a><span class='hs-comment'>-- instead of wrapping around @ResourceT@ itself. This section provides you the</span>
<a name="line-307"></a><span class='hs-comment'>-- means of doing so.</span>
<a name="line-308"></a>
<a name="line-309"></a><a name="createInternalState"></a><span class='hs-comment'>-- | Create a new internal state. This state must be closed with</span>
<a name="line-310"></a><span class='hs-comment'>-- @closeInternalState@. It is your responsibility to ensure exception safety.</span>
<a name="line-311"></a><span class='hs-comment'>-- Caveat emptor!</span>
<a name="line-312"></a><span class='hs-comment'>--</span>
<a name="line-313"></a><span class='hs-comment'>-- Since 0.4.9</span>
<a name="line-314"></a><span class='hs-definition'>createInternalState</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadBase</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>m</span> <span class='hs-conid'>InternalState</span>
<a name="line-315"></a><span class='hs-definition'>createInternalState</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftBase</span>
<a name="line-316"></a> <span class='hs-varop'>$</span> <span class='hs-conid'>I</span><span class='hs-varop'>.</span><span class='hs-varid'>newIORef</span>
<a name="line-317"></a> <span class='hs-varop'>$</span> <span class='hs-conid'>ReleaseMap</span> <span class='hs-varid'>maxBound</span> <span class='hs-layout'>(</span><span class='hs-varid'>minBound</span> <span class='hs-varop'>+</span> <span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-conid'>IntMap</span><span class='hs-varop'>.</span><span class='hs-varid'>empty</span>
<a name="line-318"></a>
<a name="line-319"></a><a name="closeInternalState"></a><span class='hs-comment'>-- | Close an internal state created by @createInternalState@.</span>
<a name="line-320"></a><span class='hs-comment'>--</span>
<a name="line-321"></a><span class='hs-comment'>-- Since 0.4.9</span>
<a name="line-322"></a><span class='hs-definition'>closeInternalState</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>MonadBase</span> <span class='hs-conid'>IO</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>InternalState</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-conid'>()</span>
<a name="line-323"></a><span class='hs-definition'>closeInternalState</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>liftBase</span> <span class='hs-varop'>.</span> <span class='hs-varid'>stateCleanup</span> <span class='hs-conid'>ReleaseNormal</span>
<a name="line-324"></a>
<a name="line-325"></a><a name="getInternalState"></a><span class='hs-comment'>-- | Get the internal state of the current @ResourceT@.</span>
<a name="line-326"></a><span class='hs-comment'>--</span>
<a name="line-327"></a><span class='hs-comment'>-- Since 0.4.6</span>
<a name="line-328"></a><span class='hs-definition'>getInternalState</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Monad</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>ResourceT</span> <span class='hs-varid'>m</span> <span class='hs-conid'>InternalState</span>
<a name="line-329"></a><span class='hs-definition'>getInternalState</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>ResourceT</span> <span class='hs-varid'>return</span>
<a name="line-330"></a>
<a name="line-331"></a><a name="InternalState"></a><span class='hs-comment'>-- | The internal state held by a @ResourceT@ transformer.</span>
<a name="line-332"></a><a name="InternalState"></a><span class='hs-comment'>--</span>
<a name="line-333"></a><a name="InternalState"></a><span class='hs-comment'>-- Since 0.4.6</span>
<a name="line-334"></a><a name="InternalState"></a><span class='hs-keyword'>type</span> <span class='hs-conid'>InternalState</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>I</span><span class='hs-varop'>.</span><span class='hs-conid'>IORef</span> <span class='hs-conid'>ReleaseMap</span>
<a name="line-335"></a>
<a name="line-336"></a><a name="runInternalState"></a><span class='hs-comment'>-- | Unwrap a @ResourceT@ using the given @InternalState@.</span>
<a name="line-337"></a><span class='hs-comment'>--</span>
<a name="line-338"></a><span class='hs-comment'>-- Since 0.4.6</span>
<a name="line-339"></a><span class='hs-definition'>runInternalState</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ResourceT</span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>InternalState</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-340"></a><span class='hs-definition'>runInternalState</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unResourceT</span>
<a name="line-341"></a>
<a name="line-342"></a><a name="withInternalState"></a><span class='hs-comment'>-- | Run an action in the underlying monad, providing it the @InternalState@.</span>
<a name="line-343"></a><span class='hs-comment'>--</span>
<a name="line-344"></a><span class='hs-comment'>-- Since 0.4.6</span>
<a name="line-345"></a><span class='hs-definition'>withInternalState</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>InternalState</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ResourceT</span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-346"></a><span class='hs-definition'>withInternalState</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>ResourceT</span>
<a name="line-347"></a>
<a name="line-348"></a><a name="monadThrow"></a><span class='hs-comment'>-- | Backwards compatibility</span>
<a name="line-349"></a><span class='hs-definition'>monadThrow</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span><span class='hs-varop'>.</span><span class='hs-conid'>Exception</span> <span class='hs-varid'>e</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadThrow</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>e</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-350"></a><span class='hs-definition'>monadThrow</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>throwM</span>
</pre></body>
</html>
|