diff options
| author | Benoit Germain <bnt.germain@gmail.com> | 2013-10-01 15:57:54 +0200 |
|---|---|---|
| committer | Benoit Germain <bnt.germain@gmail.com> | 2013-10-01 15:57:54 +0200 |
| commit | 8f550701c7181c4d6352674b502c864d52e848ab (patch) | |
| tree | 9f599d6979ab9d7bd312ac77b108899c3545fe52 /src | |
| parent | 4112d16b53f13cb48a3b6ed229891624f81c1fd2 (diff) | |
| download | lanes-8f550701c7181c4d6352674b502c864d52e848ab.tar.gz lanes-8f550701c7181c4d6352674b502c864d52e848ab.tar.bz2 lanes-8f550701c7181c4d6352674b502c864d52e848ab.zip | |
Delete lanes-keeper.lua
Diffstat (limited to 'src')
| -rw-r--r-- | src/lanes-keeper.lua | 304 |
1 files changed, 0 insertions, 304 deletions
diff --git a/src/lanes-keeper.lua b/src/lanes-keeper.lua deleted file mode 100644 index a03ef4b..0000000 --- a/src/lanes-keeper.lua +++ /dev/null | |||
| @@ -1,304 +0,0 @@ | |||
| 1 | -- | ||
| 2 | -- KEEPER.LUA | ||
| 3 | -- | ||
| 4 | -- Keeper state logic | ||
| 5 | -- DEPRECATED BY THE EQUIVALENT C IMPLEMENTATION, KEPT FOR REFERENCE ONLY | ||
| 6 | -- SHOULD NOT BE PART OF THE INSTALLATION ANYMORE | ||
| 7 | -- | ||
| 8 | -- This code is read in for each "keeper state", which are the hidden, inter- | ||
| 9 | -- mediate data stores used by Lanes inter-state communication objects. | ||
| 10 | -- | ||
| 11 | -- Author: Asko Kauppi <akauppi@gmail.com> | ||
| 12 | -- | ||
| 13 | --[[ | ||
| 14 | =============================================================================== | ||
| 15 | |||
| 16 | Copyright (C) 2008-10 Asko Kauppi <akauppi@gmail.com> | ||
| 17 | |||
| 18 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 19 | of this software and associated documentation files (the "Software"), to deal | ||
| 20 | in the Software without restriction, including without limitation the rights | ||
| 21 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 22 | copies of the Software, and to permit persons to whom the Software is | ||
| 23 | furnished to do so, subject to the following conditions: | ||
| 24 | |||
| 25 | The above copyright notice and this permission notice shall be included in | ||
| 26 | all copies or substantial portions of the Software. | ||
| 27 | |||
| 28 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 29 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 30 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 31 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 32 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 33 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 34 | THE SOFTWARE. | ||
| 35 | |||
| 36 | =============================================================================== | ||
| 37 | ]]-- | ||
| 38 | |||
| 39 | -- We only need to have base and table libraries (and io for debugging) | ||
| 40 | -- | ||
| 41 | local table_concat = assert( table.concat) | ||
| 42 | local table_insert = assert( table.insert) | ||
| 43 | local table_remove = assert( table.remove) | ||
| 44 | local select, unpack = assert( select), assert( unpack) | ||
| 45 | |||
| 46 | --[[ | ||
| 47 | local function WR(...) | ||
| 48 | if io then | ||
| 49 | io.stderr:write( table_concat({...},'\t').."\n" ) | ||
| 50 | end | ||
| 51 | end | ||
| 52 | |||
| 53 | local function DEBUG(title,ud,key) | ||
| 54 | assert( title and ud and key ) | ||
| 55 | |||
| 56 | local data,_= tables(ud) | ||
| 57 | |||
| 58 | local s= tostring(data[key]) | ||
| 59 | for _,v in ipairs( data[key] or {} ) do | ||
| 60 | s= s..", "..tostring(v) | ||
| 61 | end | ||
| 62 | WR( "*** "..title.." ("..tostring(key).."): ", s ) | ||
| 63 | end | ||
| 64 | --]] | ||
| 65 | |||
| 66 | ----- | ||
| 67 | -- FIFO for a key | ||
| 68 | -- | ||
| 69 | |||
| 70 | local fifo_new = function() | ||
| 71 | return { first = 1, count = 0} | ||
| 72 | end | ||
| 73 | |||
| 74 | local fifo_push = function( fifo, ...) | ||
| 75 | local first, count, added = fifo.first, fifo.count, select( '#', ...) | ||
| 76 | local start = first + count - 1 | ||
| 77 | for i = 1, added do | ||
| 78 | fifo[start + i] = select( i, ...) | ||
| 79 | end | ||
| 80 | fifo.count = count + added | ||
| 81 | end | ||
| 82 | |||
| 83 | local fifo_peek = function( fifo, count) | ||
| 84 | if count <= fifo.count then | ||
| 85 | local first = fifo.first | ||
| 86 | local last = first + count - 1 | ||
| 87 | return unpack( fifo, first, last) | ||
| 88 | end | ||
| 89 | end | ||
| 90 | |||
| 91 | local fifo_pop = function( fifo, count) | ||
| 92 | local first = fifo.first | ||
| 93 | local last = first + count - 1 | ||
| 94 | local out = { unpack( fifo, first, last)} | ||
| 95 | for i = first, last do | ||
| 96 | fifo[i] = nil | ||
| 97 | end | ||
| 98 | fifo.first = first + count | ||
| 99 | fifo.count = fifo.count - count | ||
| 100 | return unpack( out) | ||
| 101 | end | ||
| 102 | |||
| 103 | |||
| 104 | ----- | ||
| 105 | -- Actual data store | ||
| 106 | -- | ||
| 107 | -- { [linda_deep_ud]= { key= { val [, ... ] } [, ...] } | ||
| 108 | -- ... | ||
| 109 | -- } | ||
| 110 | -- | ||
| 111 | local _data= {} | ||
| 112 | |||
| 113 | ----- | ||
| 114 | -- Length limits (if any) for queues | ||
| 115 | -- | ||
| 116 | -- 0: don't queue values at all; ':send()' waits if the slot is not vacant | ||
| 117 | -- N: allow N values to be queued (slot itself + N-1); wait if full | ||
| 118 | -- nil: no limits, '_data' may grow endlessly | ||
| 119 | -- | ||
| 120 | local _limits= {} | ||
| 121 | |||
| 122 | ----- | ||
| 123 | -- data_tbl, limits_tbl = tables( linda_deep_ud ) | ||
| 124 | -- | ||
| 125 | -- Gives appropriate tables for a certain Linda (creates them if needed) | ||
| 126 | -- | ||
| 127 | local function tables( ud ) | ||
| 128 | -- tables are created either all or nothing | ||
| 129 | -- | ||
| 130 | if not _data[ud] then | ||
| 131 | _data[ud]= {} | ||
| 132 | _limits[ud]= {} | ||
| 133 | end | ||
| 134 | return _data[ud], _limits[ud] | ||
| 135 | end | ||
| 136 | |||
| 137 | ----- | ||
| 138 | -- bool= send( linda_deep_ud, key, ...) | ||
| 139 | -- | ||
| 140 | -- Send new data (1..N) to 'key' slot. This send is atomic; all the values | ||
| 141 | -- end up one after each other (this is why having possibility for sending | ||
| 142 | -- multiple values in one call is deemed important). | ||
| 143 | -- | ||
| 144 | -- If the queue has a limit, values are sent only if all of them fit in. | ||
| 145 | -- | ||
| 146 | -- Returns: 'true' if all the values were placed | ||
| 147 | -- 'false' if sending would exceed the queue limit (wait & retry) | ||
| 148 | -- | ||
| 149 | function send( ud, key, ...) | ||
| 150 | |||
| 151 | local data, limits = tables( ud) | ||
| 152 | |||
| 153 | local n = select( '#', ...) | ||
| 154 | |||
| 155 | -- Initialize queue for all keys that have been used with ':send()' | ||
| 156 | -- | ||
| 157 | if data[key] == nil then | ||
| 158 | data[key] = fifo_new() | ||
| 159 | end | ||
| 160 | local fifo = data[key] | ||
| 161 | |||
| 162 | local len = fifo.count | ||
| 163 | local m = limits[key] | ||
| 164 | |||
| 165 | if m and len+n > m then | ||
| 166 | return false -- would exceed the limit; try again later | ||
| 167 | end | ||
| 168 | |||
| 169 | fifo_push( fifo, ...) | ||
| 170 | return true | ||
| 171 | end | ||
| 172 | |||
| 173 | |||
| 174 | ----- | ||
| 175 | -- [val, key]= receive( linda_deep_ud, key [, ...] ) | ||
| 176 | -- | ||
| 177 | -- Read any of the given keys, consuming the data found. Keys are read in | ||
| 178 | -- order. | ||
| 179 | -- | ||
| 180 | function receive( ud, ...) | ||
| 181 | |||
| 182 | local data = tables( ud) | ||
| 183 | |||
| 184 | for i = 1, select( '#', ...) do | ||
| 185 | local key = select( i, ...) | ||
| 186 | local fifo = data[key] | ||
| 187 | if fifo and fifo.count > 0 then | ||
| 188 | local val = fifo_pop( fifo, 1) | ||
| 189 | if val ~= nil then | ||
| 190 | return key, val | ||
| 191 | end | ||
| 192 | end | ||
| 193 | end | ||
| 194 | end | ||
| 195 | |||
| 196 | |||
| 197 | ----- | ||
| 198 | -- [val1, ... valCOUNT]= receive_batched( linda_deep_ud, key , min_COUNT, max_COUNT) | ||
| 199 | -- | ||
| 200 | -- Read a single key, consuming the data found. | ||
| 201 | -- | ||
| 202 | receive_batched = function( ud, key, min_count, max_count) | ||
| 203 | if min_count > 0 then | ||
| 204 | local fifo = tables( ud)[key] | ||
| 205 | if fifo then | ||
| 206 | local fifo_count = fifo.count | ||
| 207 | if fifo_count >= min_count then | ||
| 208 | max_count = max_count or min_count | ||
| 209 | max_count = (max_count > fifo_count) and fifo_count or max_count | ||
| 210 | return key, fifo_pop( fifo, max_count) | ||
| 211 | end | ||
| 212 | end | ||
| 213 | end | ||
| 214 | end | ||
| 215 | |||
| 216 | |||
| 217 | ----- | ||
| 218 | -- = limit( linda_deep_ud, key, uint ) | ||
| 219 | -- | ||
| 220 | function limit( ud, key, n) | ||
| 221 | |||
| 222 | local _, limits = tables( ud) | ||
| 223 | |||
| 224 | limits[key] = n | ||
| 225 | end | ||
| 226 | |||
| 227 | |||
| 228 | ----- | ||
| 229 | -- void= set( linda_deep_ud, key, [val] ) | ||
| 230 | -- | ||
| 231 | function set( ud, key, val) | ||
| 232 | |||
| 233 | local data, _ = tables( ud) | ||
| 234 | |||
| 235 | -- Setting a key to 'nil' really clears it; only queing uses sentinels. | ||
| 236 | -- | ||
| 237 | if val ~= nil then | ||
| 238 | local fifo = fifo_new() | ||
| 239 | fifo_push( fifo, val) | ||
| 240 | data[key] = fifo | ||
| 241 | else | ||
| 242 | data[key] = nil | ||
| 243 | end | ||
| 244 | end | ||
| 245 | |||
| 246 | |||
| 247 | ----- | ||
| 248 | -- [val]= get( linda_deep_ud, key ) | ||
| 249 | -- | ||
| 250 | function get( ud, key) | ||
| 251 | local data, _ = tables( ud) | ||
| 252 | local fifo = data[key] | ||
| 253 | return fifo and fifo_peek( fifo, 1) | ||
| 254 | end | ||
| 255 | |||
| 256 | |||
| 257 | ----- | ||
| 258 | -- [val]= count( linda_deep_ud, ...) | ||
| 259 | -- | ||
| 260 | -- 3 modes of operation | ||
| 261 | -- linda:count() -> returns a table of key/count pairs | ||
| 262 | -- linda:count(key) returns the number of items waiting in the key | ||
| 263 | -- linda:count(key,...) -> returns a table telling, for each key, the number of items | ||
| 264 | function count( ud, ...) | ||
| 265 | local data, _ = tables( ud) | ||
| 266 | local n = select( '#', ...) | ||
| 267 | if n == 0 then | ||
| 268 | local out | ||
| 269 | for key, _ in pairs( data) do | ||
| 270 | local fifo = data[key] | ||
| 271 | local count = fifo and fifo.count or 0 | ||
| 272 | out = out or {} | ||
| 273 | out[key] = count | ||
| 274 | found = true | ||
| 275 | end | ||
| 276 | return out | ||
| 277 | elseif n == 1 then | ||
| 278 | local key = ... | ||
| 279 | local fifo = data[key] | ||
| 280 | return fifo and fifo.count or nil | ||
| 281 | else -- more than 1 key | ||
| 282 | local out | ||
| 283 | for i = 1, n do | ||
| 284 | local key = select( i, ...) | ||
| 285 | local fifo = data[key] | ||
| 286 | local count = fifo and fifo.count or nil | ||
| 287 | out = out or {} | ||
| 288 | out[key] = count | ||
| 289 | end | ||
| 290 | return out | ||
| 291 | end | ||
| 292 | end | ||
| 293 | |||
| 294 | |||
| 295 | ----- | ||
| 296 | -- void= clear( linda_deep_ud) | ||
| 297 | -- | ||
| 298 | -- Clear the data structures used for a Linda (at its destructor) | ||
| 299 | -- | ||
| 300 | function clear( ud) | ||
| 301 | |||
| 302 | _data[ud]= nil | ||
| 303 | _limits[ud]= nil | ||
| 304 | end | ||
