forked from securitykiss-com/csp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsp.tcl
562 lines (493 loc) · 15.7 KB
/
csp.tcl
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# Copyrite (c) 2015 SecurityKISS Ltd (http://www.securitykiss.com)
#
# The MIT License (MIT)
#
# Yes, Mr patent attorney, you have nothing to do here. Find a decent job instead.
# Fight intellectual "property".
#
# 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.
package require Tcl 8.6
package provide csp 0.1.0
namespace eval csp {
# channel as a list/queue for buffered channel or
# venue container for rendez-vous channel (non-buffered size 0 channel)
variable Channel
array set Channel {}
# channel capacity (buffer size)
variable ChannelCap
array set ChannelCap {}
variable ChannelReadOnly
array set ChannelReadOnly {}
variable Routine
array set Routine {}
# counter/uid to produce unique Routine and Channel names
variable Uid 0
namespace export go channel select timer ticker tickernow range range! <- <-! -> ->> forward
namespace ensemble create
}
proc ::csp::ChannelProxy {ch operator val} {
if {$operator eq "close"} {
# delete channel command == channel closed
rename $ch ""
# let the CDrained purge the channel if empty
CDrained $ch
SetResume
return
}
CheckOperator $operator
if {[CReadOnly $ch]} {
error "Cannot send to read only channel $ch"
}
while {![CSendReady $ch]} {
CheckClosed $ch
Wait$operator
}
CAppend $ch $val
SetResume
# post-send extra logic for rendez-vous channels
if {![CBuffered $ch]} {
# wait again for container empty (once receiver collected the value)
while {![CEmpty $ch]} {
CheckClosed $ch
Wait$operator
}
}
return
}
proc ::csp::Wait<- {} {
yield
}
proc ::csp::Wait<-! {} {
vwait ::csp::resume
}
proc ::csp::IsOperator {op} {
return [expr {$op in {<- <-!}}]
}
proc ::csp::CheckOperator {op} {
if {![IsOperator $op]} {
error "Unrecognized operator $op. Should be <- or <-!"
}
if {[info coroutine] eq ""} {
if {$op eq "<-"} {
error "<- can only be used in a coroutine"
}
} else {
if {$op eq "<-!"} {
error "<-! should not be used in a coroutine"
}
}
}
# throw error if channel is closed
proc ::csp::CheckClosed {ch} {
if {[CClosed $ch]} {
error "Cannot send to the closed channel $ch"
}
}
# throw error if incorrect channel name
proc ::csp::CheckName {ch} {
if {![regexp {::csp::Channel#\d+} $ch]} {
error "Wrong channel name: $ch"
}
}
proc ::csp::CSendReady {ch} {
if {[CClosed $ch]} {
return 0
}
if {[CBuffered $ch]} {
return [expr {! [CFull $ch]}]
} else {
return [CEmpty $ch]
}
}
proc ::csp::CReceiveReady {ch} {
CheckName $ch
return [expr {![CEmpty $ch]}]
}
# remove the channel completely
proc ::csp::CPurge {ch} {
variable Channel
variable ChannelCap
variable ChannelReadOnly
CheckName $ch
catch {unset Channel($ch)}
catch {unset ChannelCap($ch)}
catch {unset ChannelReadOnly($ch)}
catch {rename $ch ""}
catch {SetResume}
}
# channel chlist ?cap?
# Create channel(s) (with internal name) and place that name in given var
# the default buffer size (capacity) is zero which means rendez-vous channel
proc ::csp::channel {chVars {cap 0}} {
variable Channel
variable ChannelCap
variable CTemplate
lmap chVar $chVars {
upvar $chVar ch
set ch [NewChannel]
# initialize channel as a list or do nothing if exists
set Channel($ch) {}
set ChannelCap($ch) $cap
# create channel object from the template
namespace eval ::csp [string map [list %CHANNEL% $ch] {
proc %CHANNEL% {operator {val ""}} {
::csp::ChannelProxy %CHANNEL% $operator $val
}
}]
}
}
# Sending will be possible only by using internal CAppend
proc ::csp::CMakeReadOnly {ch} {
variable ChannelReadOnly
set ChannelReadOnly($ch) 1
}
proc ::csp::CReadOnly {ch} {
variable ChannelReadOnly
return [info exists ChannelReadOnly($ch)]
}
# A channel is considered closed if no longer exists (but its name is correct)
proc ::csp::CClosed {ch} {
CheckName $ch
# if channel command no longer exists
return [expr {[info procs $ch] eq ""}]
}
# uconditionally append to the channel - internal only
proc ::csp::CAppend {ch val} {
variable Channel
lappend Channel($ch) $val
return
}
proc ::csp::CBuffered {ch} {
variable ChannelCap
return [expr {$ChannelCap($ch) != 0}]
}
proc ::csp::CEmpty {ch} {
variable Channel
CheckName $ch
if {[info exists Channel($ch)]} {
return [expr {[llength $Channel($ch)] == 0}]
} else {
return 1
}
}
proc ::csp::CFull {ch} {
variable Channel
variable ChannelCap
set clen [llength $Channel($ch)]
return [expr {$clen >= $ChannelCap($ch)}]
}
# return contents of the channel
proc ::csp::CGet {ch} {
variable Channel
return $Channel($ch)
}
# generate new routine name
proc ::csp::NewRoutine {} {
return ::csp::Routine#[incr ::csp::Uid]
}
proc ::csp::NewChannel {} {
return ::csp::Channel#[incr ::csp::Uid]
}
# invoke proc in a routine
# args should be proc name and arguments
proc ::csp::go {args} {
variable Routine
set rname [::csp::NewRoutine]
coroutine $rname {*}$args
set Routine($rname) 1
SetResume
return $rname
}
# schedule wake-up
proc ::csp::SetResume {} {
after idle {after 0 ::csp::Resume}
}
# notify routines to reevaluate resume conditions
# it may be improved by keeping track of the yielding routines (yield [info coroutine])
proc ::csp::Resume {} {
variable Routine
foreach r [array names Routine] {
if {[info commands $r] eq ""} {
# coroutine must have ended so remove it from the array
catch {unset Routine($r)}
} else {
# cannot run the already running coroutine - catch error when it happens
# this may regularly throw 'coroutine "::csp::Routine#N" is already running'
catch $r
}
}
set ::csp::resume 1
}
proc ::csp::CReceive {ch} {
variable Channel
set elem [lindex $Channel($ch) 0]
set Channel($ch) [lreplace $Channel($ch) 0 0]
return $elem
}
proc ::csp::CDrained {ch} {
CheckName $ch
# drained = empty and closed
set drained [expr {![CReceiveReady $ch] && [CClosed $ch]}]
if {$drained} {
# just in case purge every time
CPurge $ch
return 1
} else {
return 0
}
}
proc ::csp::ReceiveWith {ch operator} {
CheckOperator $operator
# check if ch contains elements, if so return element, yield otherwise
while {![CReceiveReady $ch]} {
# trying to receive from empty and closed channel should clean up the channel and throw error
if {[CDrained $ch]} {
error "Cannot receive from a drained (empty and closed) channel $ch"
}
Wait$operator
}
set elem [CReceive $ch]
SetResume
return $elem
}
# Receive from channel, wait if channel not ready, throw error if channel is drained
# Can be only used from coroutine
# Uses yield for wait
proc ::csp::<- {ch} {
return [ReceiveWith $ch <-]
}
# Receive from channel, wait if channel not ready, throw error if channel is drained
# Can be used from non-coroutine
# Uses vwait for wait. It means it creates nested event loops
# Not ready channel in nested vwait may block an upstream channel that became ready
# Use with care. Avoid if you can.
proc ::csp::<-! {ch} {
return [ReceiveWith $ch <-!]
}
# Create a channel and a callback handler being a coroutine which when called
# will send callback single argument to the newly created channel
# The library user should only receive from that channel
# It is designed for one-time callbacks i.e. the coroutine exits after first call.
# The channel will be automatically destroyed after receiving the first message.
# The limitation is that the callback must be a single- or zero- argument callback
# In case of no-argument callback empty string is sent to the channel
proc ::csp::-> {chVar} {
upvar $chVar ch
channel ch
CMakeReadOnly $ch
# this coroutine will not be registered in Routine array for unblocking calls
# it should be called from the userland callback
set routine [NewRoutine]
coroutine $routine OneTimeSender $ch
return $routine
}
# ch should be a newly created channel with exclusive rights for sending
proc ::csp::OneTimeSender {ch} {
# unconditionally push the callback arguments (returned by yield) to the channel
set arg [yield]
if {![CClosed $ch]} {
CAppend $ch $arg
$ch close
SetResume
}
}
# EXPERIMENTAL - works well with Tk sparse events like mouse clicks or key press buttons
# The problem is that in another typical use case i.e. reading from nonblocking file/socket chan
# the constant hammering with events from fileevent starves other coroutines
# and overflows the channel buffer here.
#
# Create a channel and a callback handler being a coroutine which when called
# will send callback single argument to the newly created channel
# The library user should only receive from that channel
# It is designed for multi-time callbacks i.e. the coroutine resumes after every callback.
# The limitation is that the callback must be a single- or zero- argument callback
# In case of no-argument callback empty string is sent to the channel
proc ::csp::->> {chVar {maxbuffer 1000}} {
upvar $chVar ch
channel ch $maxbuffer
CMakeReadOnly $ch
# this coroutine will not be registered in Routine array for unblocking calls
# it should be called from the userland callback
set routine [NewRoutine]
coroutine $routine MultiSender $ch
return $routine
}
proc ::csp::MultiSender {ch} {
while 1 {
set arg [yield]
# terminate the coroutine if channel not ready for send
if {![CSendReady $ch]} {
break
}
# push the callback arguments (returned by yield) to the channel
CAppend $ch $arg
SetResume
}
}
proc ::csp::Forward {from to} {
range msg $from {
# destination channel may be closed in the meantime so catch error and don't break the loop
# it is to clear the $from channels
catch {$to <- $msg}
}
}
proc ::csp::forward {froms to} {
foreach from $froms {
go ::csp::Forward $from $to
}
}
# The select command provides a way to handle multiple channels.
# It is a switch like statement where channels are evaluated for readiness.
# The select command makes the coroutine wait until at least one channel is ready.
# If multiple can proceed, select chooses pseudo-randomly.
# A default clause, if present, executes immediately if no channel is ready.
proc ::csp::select {a} {
set ready 0
# ensure that operator and channel are substituted only once
set substa {}
foreach {op ch body} $a {
if {$op eq "default"} {
lappend substa $op $ch $body
} else {
lappend substa [uplevel subst $op] [uplevel subst $ch] $body
}
}
while {$ready == 0} {
# (op ch body) triples ready for send/receive
set triples {}
set default 0
set defaultbody {}
set operator {}
foreach {op ch body} $substa {
if {[IsOperator $ch]} {
lassign [list $op $ch] ch op
if {$op ni $operator} {
lappend operator $op
}
if {[CSendReady $ch]} {
lappend triples s $ch $body
}
} elseif {[IsOperator $op]} {
if {$op ni $operator} {
lappend operator $op
}
if {[CReceiveReady $ch]} {
lappend triples r $ch $body
}
} elseif {$op eq "default"} {
set default 1
set defaultbody $ch
} else {
error "Wrong select arguments: $op $ch"
}
}
if {[llength $operator] == 0} {
error "<- or <-! operator required in select"
}
if {[llength $operator] > 1} {
error "<- and <-! should not be mixed in a single select"
}
CheckOperator $operator
set ready [expr {[llength $triples] / 3}]
if {$ready == 0} {
if {$default == 0} {
Wait$operator
} else {
return [uplevel $defaultbody]
}
}
}
if {$ready == 1} {
set triple $triples
} else {
set random [expr {round(floor(rand()*$ready))}]
set triple [lrange $triples [expr {$random * 3}] [expr {$random * 3 + 2}]]
}
lassign $triple op ch body
return [uplevel $body]
}
proc ::csp::timer {chVar interval} {
upvar $chVar ch
after $interval [-> ch] {[clock microseconds]}
return $ch
}
proc ::csp::TickerRoutine {ch interval closeafter} {
if {![CClosed $ch]} {
if {[regexp {#(\d+)} $closeafter _ ticksleft]} {
if {$ticksleft <= 0} {
$ch close
return
}
set closeafter #[incr ticksleft -1]
}
$ch <- [clock microseconds]
after $interval csp::go TickerRoutine $ch $interval $closeafter
}
}
# Return ticker channel. First tick in $interval ms
proc ::csp::ticker {chVar interval {closeafter 0}} {
return [ticker_generic $chVar $interval $interval $closeafter]
}
# Return ticker channel. First tick immediately.
proc ::csp::tickernow {chVar interval {closeafter 0}} {
return [ticker_generic $chVar 0 $interval $closeafter]
}
# Generic internal ticker function
proc ::csp::ticker_generic {chVar initial_interval interval closeafter} {
upvar 2 $chVar ch
csp::channel ch
if {$closeafter != 0 && [string is integer -strict $closeafter]} {
after $closeafter $ch close
}
after $initial_interval csp::go TickerRoutine $ch $interval $closeafter
return $ch
}
# receive from channel until closed in coroutine
proc ::csp::range {varName ch body} {
if {[info coroutine] eq ""} {
error "range can only be used in a coroutine"
}
uplevel [subst -nocommands {
while 1 {
try {
set $varName [<- $ch]
$body
} on error {out err} {
break
}
}
}]
}
# receive from channel until closed in main control flow
proc ::csp::range! {varName ch body} {
if {[info coroutine] ne ""} {
error "range! should not be used in a coroutine"
}
uplevel [subst -nocommands {
while 1 {
try {
set $varName [<-! $ch]
$body
} on error {out err} {
break
}
}
}]
}