summaryrefslogtreecommitdiffstats
path: root/lib/evendoors/twirl.rb
blob: 87422206f959b2b39d4fe8a5632a1c0a61d54a82 (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
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-

#
module EvenDoors
    #
    class Twirl
        #
        @debug = false
        @pool = {}          # per particle class free list
        @sys_fifo = []      # system particles fifo list
        @app_fifo = []      # application particles fifo list
        #
        #
        class << self
            #
            attr_accessor :debug
            #
            def release_p p
                # hope there is no circular loop
                while p2=p.merged_shift
                    release_p p2
                end
                ( @pool[p.class] ||= [] ) << p
            end
            #
            def require_p p_kls
                l = @pool[p_kls]
                return p_kls.new if l.nil?
                p = l.pop
                return p_kls.new if p.nil?
                p.reset!
                p
            end
            #
            def send_p p
                @app_fifo << p
            end
            #
            def send_sys_p p
                @sys_fifo << p
            end
            #
            def twirl!
                while @sys_fifo.length>0 or @app_fifo.length>0
                    while @sys_fifo.length>0
                        p = @sys_fifo.shift
                        p.door.process_sys_p p
                    end
                    while @app_fifo.length>0
                        p = @app_fifo.shift
                        p.door.process_p p
                    end
                end
            end
            #
        end
        #
    end
    #
end
#
# EOF