aboutsummaryrefslogtreecommitdiff
path: root/src/keeper.lua
blob: 2c38c0b76f7e90285b3e1fe6b7d3dd44a14d92fd (plain)
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
--
-- KEEPER.LUA
--
-- Keeper state logic
--
-- This code is read in for each "keeper state", which are the hidden, inter-
-- mediate data stores used by Lanes inter-state communication objects.
--
-- Author: Asko Kauppi <akauppi@gmail.com>
--
--[[
===============================================================================

Copyright (C) 2008-10 Asko Kauppi <akauppi@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

===============================================================================
]]--

-- unique key instead of 'nil' in queues
--
assert( nil_sentinel )

-- We only need to have base and table libraries (and io for debugging)
--
local table_remove= assert( table.remove )
local table_concat= assert( table.concat )

--[[
local function WR(...)
    if io then 
        io.stderr:write( table_concat({...},'\t').."\n" ) 
    end
end

local function DEBUG(title,ud,key)
    assert( title and ud and key )

    local data,incoming,_= tables(ud)

    local s= tostring(data[key])
    for _,v in ipairs( incoming[key] or {} ) do
        s= s..", "..tostring(v)
    end
    WR( "*** "..title.." ("..tostring(key).."): ", s )
end
--]]

-----
-- Actual data store
--
-- { [linda_deep_ud]= { key= val [, ...] }
--      ...
-- }
--
local _data= {}

-----
-- Entries queued for use when the existing 'data[ud][key]' entry is consumed.
--
-- { [linda_deep_ud]= { key= { val [, ... } [, ...] }
--      ...
-- }
--
local _incoming= {}

-----
-- Length limits (if any) for queues
--
-- 0:   don't queue values at all; ':send()' waits if the slot is not vacant
-- N:   allow N values to be queued (slot itself + N-1); wait if full
-- nil: no limits, '_incoming' may grow endlessly
--
local _limits= {}

-----
-- data_tbl, incoming_tbl, limits_tbl = tables( linda_deep_ud )
--
-- Gives appropriate tables for a certain Linda (creates them if needed)
--
local function tables( ud )
    -- tables are created either all or nothing
    --
    if not _data[ud] then
        _data[ud]= {}
        _incoming[ud]= {}
        _limits[ud]= {}
    end
    return _data[ud], _incoming[ud], _limits[ud]
end

-----
-- bool= send( linda_deep_ud, key, ... )
--
-- Send new data (1..N) to 'key' slot. This send is atomic; all the values
-- end up one after each other (this is why having possibility for sending
-- multiple values in one call is deemed important).
--
-- If the queue has a limit, values are sent only if all of them fit in.
--
-- Returns: 'true' if all the values were placed
--          'false' if sending would exceed the queue limit (wait & retry)
--
function send( ud, key, ... )

    local data,incoming,limits= tables(ud)

    local n= select('#',...)
    if n==0 then return true end    -- nothing to send

    -- Initialize queue for all keys that have been used with ':send()'
    --
    if incoming[key]==nil then
        incoming[key]= {}
    end

    local len= data[key] and 1+#incoming[key] or 0
    local m= limits[key]

    if m and len+n > m then
        return false    -- would exceed the limit; try again later
    end

    for i=1,n do
        local val= select(i,...)

        -- 'nil' in the data replaced by sentinel
        if val==nil then
            val= nil_sentinel
        end

        if len==0 then
            data[key]= val
            len= 1
        else
            incoming[key][len]= val
            len= len+1
        end
    end
    return true
end


-----
-- [val, key]= receive( linda_deep_ud, key [, ...] )
--
-- Read any of the given keys, consuming the data found. Keys are read in
-- order.
--
function receive( ud, ... )

    local data,incoming,_= tables(ud)

    for i=1,select('#',...) do
        local key= select(i,...)
        local val= data[key]

        if val~=nil then
            if incoming[key] and incoming[key][1]~=nil then
                -- pop [1] from 'incoming[key]' into the actual slot
                data[key]= table_remove( incoming[key], 1 )
            else
                data[key]= nil  -- empty the slot
            end
            if val==nil_sentinel then
                val= nil
            end
            return val, key
        end
    end
    --return nil
end


-----
-- = limit( linda_deep_ud, key, uint )
--
function limit( ud, key, n )

    local _,_,limits= tables(ud)

    limits[key]= n
end


-----
-- void= set( linda_deep_ud, key, [val] )
--
function set( ud, key, val )

    local data,incoming,_= tables(ud)

    -- Setting a key to 'nil' really clears it; only queing uses sentinels.
    --
    data[key]= val
    incoming[key]= nil
end


-----
-- [val]= get( linda_deep_ud, key )
--
function get( ud, key )

    local data,_,_= tables(ud)

    local val= data[key]
    if val==nil_sentinel then
        val= nil
    end
    return val
end


-----
-- void= clear( linda_deep_ud )
--
-- Clear the data structures used for a Linda (at its destructor)
--
function clear( ud )

    _data[ud]= nil
    _incoming[ud]= nil
    _limits[ud]= nil
end