/usr/share/doc/libghc-stm-doc/html/src/Control-Concurrent-STM-TChan.html is in libghc-stm-doc 2.4.4.1-1.
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 | <?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/Concurrent/STM/TChan.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>{-# OPTIONS_GHC -fno-warn-name-shadowing #-}</span>
<a name="line-2"></a><span class='hs-comment'>{-# LANGUAGE CPP, DeriveDataTypeable #-}</span>
<a name="line-3"></a>
<a name="line-4"></a><span class='hs-cpp'>#if __GLASGOW_HASKELL__ >= 701</span>
<a name="line-5"></a><span class='hs-comment'>{-# LANGUAGE Trustworthy #-}</span>
<a name="line-6"></a><span class='hs-cpp'>#endif</span>
<a name="line-7"></a>
<a name="line-8"></a><span class='hs-comment'>-----------------------------------------------------------------------------</span>
<a name="line-9"></a><span class='hs-comment'>-- |</span>
<a name="line-10"></a><span class='hs-comment'>-- Module : Control.Concurrent.STM.TChan</span>
<a name="line-11"></a><span class='hs-comment'>-- Copyright : (c) The University of Glasgow 2004</span>
<a name="line-12"></a><span class='hs-comment'>-- License : BSD-style (see the file libraries/base/LICENSE)</span>
<a name="line-13"></a><span class='hs-comment'>--</span>
<a name="line-14"></a><span class='hs-comment'>-- Maintainer : libraries@haskell.org</span>
<a name="line-15"></a><span class='hs-comment'>-- Stability : experimental</span>
<a name="line-16"></a><span class='hs-comment'>-- Portability : non-portable (requires STM)</span>
<a name="line-17"></a><span class='hs-comment'>--</span>
<a name="line-18"></a><span class='hs-comment'>-- TChan: Transactional channels</span>
<a name="line-19"></a><span class='hs-comment'>-- (GHC only)</span>
<a name="line-20"></a><span class='hs-comment'>--</span>
<a name="line-21"></a><span class='hs-comment'>-----------------------------------------------------------------------------</span>
<a name="line-22"></a>
<a name="line-23"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Concurrent</span><span class='hs-varop'>.</span><span class='hs-conid'>STM</span><span class='hs-varop'>.</span><span class='hs-conid'>TChan</span> <span class='hs-layout'>(</span>
<a name="line-24"></a><span class='hs-cpp'>#ifdef __GLASGOW_HASKELL__</span>
<a name="line-25"></a> <span class='hs-comment'>-- * TChans</span>
<a name="line-26"></a> <span class='hs-conid'>TChan</span><span class='hs-layout'>,</span>
<a name="line-27"></a>
<a name="line-28"></a> <span class='hs-comment'>-- ** Construction</span>
<a name="line-29"></a> <span class='hs-varid'>newTChan</span><span class='hs-layout'>,</span>
<a name="line-30"></a> <span class='hs-varid'>newTChanIO</span><span class='hs-layout'>,</span>
<a name="line-31"></a> <span class='hs-varid'>newBroadcastTChan</span><span class='hs-layout'>,</span>
<a name="line-32"></a> <span class='hs-varid'>newBroadcastTChanIO</span><span class='hs-layout'>,</span>
<a name="line-33"></a> <span class='hs-varid'>dupTChan</span><span class='hs-layout'>,</span>
<a name="line-34"></a> <span class='hs-varid'>cloneTChan</span><span class='hs-layout'>,</span>
<a name="line-35"></a>
<a name="line-36"></a> <span class='hs-comment'>-- ** Reading and writing</span>
<a name="line-37"></a> <span class='hs-varid'>readTChan</span><span class='hs-layout'>,</span>
<a name="line-38"></a> <span class='hs-varid'>tryReadTChan</span><span class='hs-layout'>,</span>
<a name="line-39"></a> <span class='hs-varid'>peekTChan</span><span class='hs-layout'>,</span>
<a name="line-40"></a> <span class='hs-varid'>tryPeekTChan</span><span class='hs-layout'>,</span>
<a name="line-41"></a> <span class='hs-varid'>writeTChan</span><span class='hs-layout'>,</span>
<a name="line-42"></a> <span class='hs-varid'>unGetTChan</span><span class='hs-layout'>,</span>
<a name="line-43"></a> <span class='hs-varid'>isEmptyTChan</span>
<a name="line-44"></a><span class='hs-cpp'>#endif</span>
<a name="line-45"></a> <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-46"></a>
<a name="line-47"></a><span class='hs-cpp'>#ifdef __GLASGOW_HASKELL__</span>
<a name="line-48"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>GHC</span><span class='hs-varop'>.</span><span class='hs-conid'>Conc</span>
<a name="line-49"></a>
<a name="line-50"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Typeable</span> <span class='hs-layout'>(</span><span class='hs-conid'>Typeable</span><span class='hs-layout'>)</span>
<a name="line-51"></a>
<a name="line-52"></a><span class='hs-cpp'>#define _UPK_(x) {-# UNPACK #-} !(x)</span>
<a name="line-53"></a>
<a name="line-54"></a><a name="TChan"></a><span class='hs-comment'>-- | 'TChan' is an abstract type representing an unbounded FIFO channel.</span>
<a name="line-55"></a><a name="TChan"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>TChan</span> <span class='hs-sel'>_UPK_</span><span class='hs-layout'>(</span><span class='hs-conid'>TVar</span> <span class='hs-layout'>(</span><span class='hs-conid'>TVarList</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-56"></a> <span class='hs-sel'>_UPK_</span><span class='hs-layout'>(</span><span class='hs-conid'>TVar</span> <span class='hs-layout'>(</span><span class='hs-conid'>TVarList</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-57"></a> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span> <span class='hs-conid'>Typeable</span><span class='hs-layout'>)</span>
<a name="line-58"></a>
<a name="line-59"></a><a name="TVarList"></a><span class='hs-keyword'>type</span> <span class='hs-conid'>TVarList</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>TVar</span> <span class='hs-layout'>(</span><span class='hs-conid'>TList</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-60"></a><a name="TList"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>TList</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>TNil</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>TCons</span> <span class='hs-varid'>a</span> <span class='hs-sel'>_UPK_</span><span class='hs-layout'>(</span><span class='hs-conid'>TVarList</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-61"></a>
<a name="line-62"></a><a name="newTChan"></a><span class='hs-comment'>-- |Build and return a new instance of 'TChan'</span>
<a name="line-63"></a><span class='hs-definition'>newTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>STM</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-64"></a><span class='hs-definition'>newTChan</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-65"></a> <span class='hs-varid'>hole</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVar</span> <span class='hs-conid'>TNil</span>
<a name="line-66"></a> <span class='hs-varid'>read</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVar</span> <span class='hs-varid'>hole</span>
<a name="line-67"></a> <span class='hs-varid'>write</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVar</span> <span class='hs-varid'>hole</span>
<a name="line-68"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-varid'>write</span><span class='hs-layout'>)</span>
<a name="line-69"></a>
<a name="line-70"></a><a name="newTChanIO"></a><span class='hs-comment'>-- |@IO@ version of 'newTChan'. This is useful for creating top-level</span>
<a name="line-71"></a><span class='hs-comment'>-- 'TChan's using 'System.IO.Unsafe.unsafePerformIO', because using</span>
<a name="line-72"></a><span class='hs-comment'>-- 'atomically' inside 'System.IO.Unsafe.unsafePerformIO' isn't</span>
<a name="line-73"></a><span class='hs-comment'>-- possible.</span>
<a name="line-74"></a><span class='hs-definition'>newTChanIO</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>IO</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-75"></a><span class='hs-definition'>newTChanIO</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-76"></a> <span class='hs-varid'>hole</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVarIO</span> <span class='hs-conid'>TNil</span>
<a name="line-77"></a> <span class='hs-varid'>read</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVarIO</span> <span class='hs-varid'>hole</span>
<a name="line-78"></a> <span class='hs-varid'>write</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVarIO</span> <span class='hs-varid'>hole</span>
<a name="line-79"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-varid'>write</span><span class='hs-layout'>)</span>
<a name="line-80"></a>
<a name="line-81"></a><a name="newBroadcastTChan"></a><span class='hs-comment'>-- | Create a write-only 'TChan'. More precisely, 'readTChan' will 'retry'</span>
<a name="line-82"></a><span class='hs-comment'>-- even after items have been written to the channel. The only way to read</span>
<a name="line-83"></a><span class='hs-comment'>-- a broadcast channel is to duplicate it with 'dupTChan'.</span>
<a name="line-84"></a><span class='hs-comment'>--</span>
<a name="line-85"></a><span class='hs-comment'>-- Consider a server that broadcasts messages to clients:</span>
<a name="line-86"></a><span class='hs-comment'>--</span>
<a name="line-87"></a><span class='hs-comment'>-- >serve :: TChan Message -> Client -> IO loop</span>
<a name="line-88"></a><span class='hs-comment'>-- >serve broadcastChan client = do</span>
<a name="line-89"></a><span class='hs-comment'>-- > myChan <- dupTChan broadcastChan</span>
<a name="line-90"></a><span class='hs-comment'>-- > forever $ do</span>
<a name="line-91"></a><span class='hs-comment'>-- > message <- readTChan myChan</span>
<a name="line-92"></a><span class='hs-comment'>-- > send client message</span>
<a name="line-93"></a><span class='hs-comment'>--</span>
<a name="line-94"></a><span class='hs-comment'>-- The problem with using 'newTChan' to create the broadcast channel is that if</span>
<a name="line-95"></a><span class='hs-comment'>-- it is only written to and never read, items will pile up in memory. By</span>
<a name="line-96"></a><span class='hs-comment'>-- using 'newBroadcastTChan' to create the broadcast channel, items can be</span>
<a name="line-97"></a><span class='hs-comment'>-- garbage collected after clients have seen them.</span>
<a name="line-98"></a><span class='hs-comment'>--</span>
<a name="line-99"></a><span class='hs-comment'>-- @since 2.4</span>
<a name="line-100"></a><span class='hs-definition'>newBroadcastTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>STM</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-101"></a><span class='hs-definition'>newBroadcastTChan</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-102"></a> <span class='hs-varid'>write_hole</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVar</span> <span class='hs-conid'>TNil</span>
<a name="line-103"></a> <span class='hs-varid'>read</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVar</span> <span class='hs-layout'>(</span><span class='hs-varid'>error</span> <span class='hs-str'>"reading from a TChan created by newBroadcastTChan; use dupTChan first"</span><span class='hs-layout'>)</span>
<a name="line-104"></a> <span class='hs-varid'>write</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVar</span> <span class='hs-varid'>write_hole</span>
<a name="line-105"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-varid'>write</span><span class='hs-layout'>)</span>
<a name="line-106"></a>
<a name="line-107"></a><a name="newBroadcastTChanIO"></a><span class='hs-comment'>-- | @IO@ version of 'newBroadcastTChan'.</span>
<a name="line-108"></a><span class='hs-comment'>--</span>
<a name="line-109"></a><span class='hs-comment'>-- @since 2.4</span>
<a name="line-110"></a><span class='hs-definition'>newBroadcastTChanIO</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>IO</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-111"></a><span class='hs-definition'>newBroadcastTChanIO</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-112"></a> <span class='hs-varid'>write_hole</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVarIO</span> <span class='hs-conid'>TNil</span>
<a name="line-113"></a> <span class='hs-varid'>read</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVarIO</span> <span class='hs-layout'>(</span><span class='hs-varid'>error</span> <span class='hs-str'>"reading from a TChan created by newBroadcastTChanIO; use dupTChan first"</span><span class='hs-layout'>)</span>
<a name="line-114"></a> <span class='hs-varid'>write</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVarIO</span> <span class='hs-varid'>write_hole</span>
<a name="line-115"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-varid'>write</span><span class='hs-layout'>)</span>
<a name="line-116"></a>
<a name="line-117"></a><a name="writeTChan"></a><span class='hs-comment'>-- |Write a value to a 'TChan'.</span>
<a name="line-118"></a><span class='hs-definition'>writeTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>STM</span> <span class='hs-conid'>()</span>
<a name="line-119"></a><span class='hs-definition'>writeTChan</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-sel'>_read</span> <span class='hs-varid'>write</span><span class='hs-layout'>)</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-120"></a> <span class='hs-varid'>listend</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>write</span> <span class='hs-comment'>-- listend == TVar pointing to TNil</span>
<a name="line-121"></a> <span class='hs-varid'>new_listend</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVar</span> <span class='hs-conid'>TNil</span>
<a name="line-122"></a> <span class='hs-varid'>writeTVar</span> <span class='hs-varid'>listend</span> <span class='hs-layout'>(</span><span class='hs-conid'>TCons</span> <span class='hs-varid'>a</span> <span class='hs-varid'>new_listend</span><span class='hs-layout'>)</span>
<a name="line-123"></a> <span class='hs-varid'>writeTVar</span> <span class='hs-varid'>write</span> <span class='hs-varid'>new_listend</span>
<a name="line-124"></a>
<a name="line-125"></a><a name="readTChan"></a><span class='hs-comment'>-- |Read the next value from the 'TChan'.</span>
<a name="line-126"></a><span class='hs-definition'>readTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>STM</span> <span class='hs-varid'>a</span>
<a name="line-127"></a><span class='hs-definition'>readTChan</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-sel'>_write</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-128"></a> <span class='hs-varid'>listhead</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>read</span>
<a name="line-129"></a> <span class='hs-varid'>head</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>listhead</span>
<a name="line-130"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>head</span> <span class='hs-keyword'>of</span>
<a name="line-131"></a> <span class='hs-conid'>TNil</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>retry</span>
<a name="line-132"></a> <span class='hs-conid'>TCons</span> <span class='hs-varid'>a</span> <span class='hs-varid'>tail</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-133"></a> <span class='hs-varid'>writeTVar</span> <span class='hs-varid'>read</span> <span class='hs-varid'>tail</span>
<a name="line-134"></a> <span class='hs-varid'>return</span> <span class='hs-varid'>a</span>
<a name="line-135"></a>
<a name="line-136"></a><a name="tryReadTChan"></a><span class='hs-comment'>-- | A version of 'readTChan' which does not retry. Instead it</span>
<a name="line-137"></a><span class='hs-comment'>-- returns @Nothing@ if no value is available.</span>
<a name="line-138"></a><span class='hs-definition'>tryReadTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>STM</span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-139"></a><span class='hs-definition'>tryReadTChan</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-sel'>_write</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-140"></a> <span class='hs-varid'>listhead</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>read</span>
<a name="line-141"></a> <span class='hs-varid'>head</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>listhead</span>
<a name="line-142"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>head</span> <span class='hs-keyword'>of</span>
<a name="line-143"></a> <span class='hs-conid'>TNil</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>return</span> <span class='hs-conid'>Nothing</span>
<a name="line-144"></a> <span class='hs-conid'>TCons</span> <span class='hs-varid'>a</span> <span class='hs-varid'>tl</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-145"></a> <span class='hs-varid'>writeTVar</span> <span class='hs-varid'>read</span> <span class='hs-varid'>tl</span>
<a name="line-146"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-147"></a>
<a name="line-148"></a><a name="peekTChan"></a><span class='hs-comment'>-- | Get the next value from the @TChan@ without removing it,</span>
<a name="line-149"></a><span class='hs-comment'>-- retrying if the channel is empty.</span>
<a name="line-150"></a><span class='hs-definition'>peekTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>STM</span> <span class='hs-varid'>a</span>
<a name="line-151"></a><span class='hs-definition'>peekTChan</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-sel'>_write</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-152"></a> <span class='hs-varid'>listhead</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>read</span>
<a name="line-153"></a> <span class='hs-varid'>head</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>listhead</span>
<a name="line-154"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>head</span> <span class='hs-keyword'>of</span>
<a name="line-155"></a> <span class='hs-conid'>TNil</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>retry</span>
<a name="line-156"></a> <span class='hs-conid'>TCons</span> <span class='hs-varid'>a</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>return</span> <span class='hs-varid'>a</span>
<a name="line-157"></a>
<a name="line-158"></a><a name="tryPeekTChan"></a><span class='hs-comment'>-- | A version of 'peekTChan' which does not retry. Instead it</span>
<a name="line-159"></a><span class='hs-comment'>-- returns @Nothing@ if no value is available.</span>
<a name="line-160"></a><span class='hs-definition'>tryPeekTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>STM</span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-161"></a><span class='hs-definition'>tryPeekTChan</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-sel'>_write</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-162"></a> <span class='hs-varid'>listhead</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>read</span>
<a name="line-163"></a> <span class='hs-varid'>head</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>listhead</span>
<a name="line-164"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>head</span> <span class='hs-keyword'>of</span>
<a name="line-165"></a> <span class='hs-conid'>TNil</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>return</span> <span class='hs-conid'>Nothing</span>
<a name="line-166"></a> <span class='hs-conid'>TCons</span> <span class='hs-varid'>a</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-167"></a>
<a name="line-168"></a><a name="dupTChan"></a><span class='hs-comment'>-- |Duplicate a 'TChan': the duplicate channel begins empty, but data written to</span>
<a name="line-169"></a><span class='hs-comment'>-- either channel from then on will be available from both. Hence this creates</span>
<a name="line-170"></a><span class='hs-comment'>-- a kind of broadcast channel, where data written by anyone is seen by</span>
<a name="line-171"></a><span class='hs-comment'>-- everyone else.</span>
<a name="line-172"></a><span class='hs-definition'>dupTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>STM</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-173"></a><span class='hs-definition'>dupTChan</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-sel'>_read</span> <span class='hs-varid'>write</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-174"></a> <span class='hs-varid'>hole</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>write</span>
<a name="line-175"></a> <span class='hs-varid'>new_read</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVar</span> <span class='hs-varid'>hole</span>
<a name="line-176"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>new_read</span> <span class='hs-varid'>write</span><span class='hs-layout'>)</span>
<a name="line-177"></a>
<a name="line-178"></a><a name="unGetTChan"></a><span class='hs-comment'>-- |Put a data item back onto a channel, where it will be the next item read.</span>
<a name="line-179"></a><span class='hs-definition'>unGetTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>STM</span> <span class='hs-conid'>()</span>
<a name="line-180"></a><span class='hs-definition'>unGetTChan</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-sel'>_write</span><span class='hs-layout'>)</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-181"></a> <span class='hs-varid'>listhead</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>read</span>
<a name="line-182"></a> <span class='hs-varid'>newhead</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVar</span> <span class='hs-layout'>(</span><span class='hs-conid'>TCons</span> <span class='hs-varid'>a</span> <span class='hs-varid'>listhead</span><span class='hs-layout'>)</span>
<a name="line-183"></a> <span class='hs-varid'>writeTVar</span> <span class='hs-varid'>read</span> <span class='hs-varid'>newhead</span>
<a name="line-184"></a>
<a name="line-185"></a><a name="isEmptyTChan"></a><span class='hs-comment'>-- |Returns 'True' if the supplied 'TChan' is empty.</span>
<a name="line-186"></a><span class='hs-definition'>isEmptyTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>STM</span> <span class='hs-conid'>Bool</span>
<a name="line-187"></a><span class='hs-definition'>isEmptyTChan</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-sel'>_write</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-188"></a> <span class='hs-varid'>listhead</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>read</span>
<a name="line-189"></a> <span class='hs-varid'>head</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>listhead</span>
<a name="line-190"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>head</span> <span class='hs-keyword'>of</span>
<a name="line-191"></a> <span class='hs-conid'>TNil</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>return</span> <span class='hs-conid'>True</span>
<a name="line-192"></a> <span class='hs-conid'>TCons</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>return</span> <span class='hs-conid'>False</span>
<a name="line-193"></a>
<a name="line-194"></a><a name="cloneTChan"></a><span class='hs-comment'>-- |Clone a 'TChan': similar to dupTChan, but the cloned channel starts with the</span>
<a name="line-195"></a><span class='hs-comment'>-- same content available as the original channel.</span>
<a name="line-196"></a><span class='hs-comment'>--</span>
<a name="line-197"></a><span class='hs-comment'>-- @since 2.4</span>
<a name="line-198"></a><span class='hs-definition'>cloneTChan</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>STM</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-199"></a><span class='hs-definition'>cloneTChan</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>read</span> <span class='hs-varid'>write</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-200"></a> <span class='hs-varid'>readpos</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>readTVar</span> <span class='hs-varid'>read</span>
<a name="line-201"></a> <span class='hs-varid'>new_read</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>newTVar</span> <span class='hs-varid'>readpos</span>
<a name="line-202"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>TChan</span> <span class='hs-varid'>new_read</span> <span class='hs-varid'>write</span><span class='hs-layout'>)</span>
<a name="line-203"></a><span class='hs-cpp'>#endif</span>
</pre></body>
</html>
|