File: IORef.hs

package info (click to toggle)
bali-phy 4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,392 kB
  • sloc: cpp: 120,442; xml: 13,966; haskell: 9,975; python: 2,936; yacc: 1,328; perl: 1,169; lex: 912; sh: 343; makefile: 26
file content (48 lines) | stat: -rw-r--r-- 1,368 bytes parent folder | download | duplicates (2)
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
{-# LANGUAGE NoImplicitPrelude #-}
module Data.IORef (IORef,
                   newIORef,
                   readIORef,
                   writeIORef,
                   modifyIORef,
                   modifyIORef',
                   atomicModifyIORef,
                   atomicModifyIORef',
                   atomicWriteIORef
) where

import Compiler.IO
import Compiler.Prim

data IORef a

foreign import bpcall "Prelude:" newIORef :: a -> IO (IORef a)

foreign import bpcall "Prelude:" readIORef :: IORef a -> IO a
-- We need to use the builtin here in order to avoid inlining IORefs.
-- IORefs can change value, which breaks the assumptions of inlining.

foreign import bpcall "Prelude:" writeIORef :: IORef a -> a -> IO ()

foreign import bpcall "Prelude:" modifyIORef :: IORef a -> (a -> a) -> IO ()

foreign import bpcall "Prelude:modifyIORefStrict" modifyIORef' :: IORef a -> (a -> a) -> IO ()

atomicModifyIORef :: IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef r f = do
  x <- readIORef r
  let (x',y) = f x
  writeIORef r x'
  return y

atomicModifyIORef' :: IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' r f = do
  x <- readIORef r
  let (x',y) = f x
  writeIORef r x'
  x' `seq` y `seq` return y

foreign import bpcall "Prelude:writeIORef" atomicWriteIORef :: IORef a -> a -> IO ()

{-
mkWeakIORef :: IORef a -> IO () -> IO (Weak (IORef a))
-}