Class: Rbgo::Channel::NonBufferChan

Inherits:
Object
  • Object
show all
Includes:
Chan
Defined in:
lib/rbgo/select_chan.rb

Overview

NonBufferChan

Instance Method Summary collapse

Methods included from Chan

new

Constructor Details

#initializeNonBufferChan

Returns a new instance of NonBufferChan.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rbgo/select_chan.rb', line 88

def initialize
  self.enq_mutex             = Mutex.new
  self.deq_mutex             = Mutex.new
  self.enq_cond              = ConditionVariable.new
  self.deq_cond              = ConditionVariable.new
  self.resource_array        = []
  self.close_flag            = false
  self.have_enq_waiting_flag = false
  self.have_deq_waiting_flag = false

  @readable_observers = Set.new
  @readable_observers.extend(MonitorMixin)
  @writable_observers = Set.new
  @writable_observers.extend(MonitorMixin)
end

Instance Method Details

#closeObject



191
192
193
194
195
196
197
198
# File 'lib/rbgo/select_chan.rb', line 191

def close
  self.close_flag = true
  enq_cond.broadcast
  deq_cond.broadcast
  notify_readable_observers
  notify_writable_observers
  self
end

#closed?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/rbgo/select_chan.rb', line 200

def closed?
  close_flag
end

#pop(nonblock = false) ⇒ Object Also known as: deq, shift



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
# File 'lib/rbgo/select_chan.rb', line 158

def pop(nonblock = false)
  resource = nil
  if closed?
    return [nil, false]
  end

  if nonblock
    raise ThreadError.new unless deq_mutex.try_lock
  else
    deq_mutex.lock
  end

  begin
    if nonblock
      raise ThreadError.new unless have_enq_waiting_flag
    end

    while resource_array.empty? && !closed?
      self.have_deq_waiting_flag = true
      notify_writable_observers
      enq_cond.wait(deq_mutex)
    end
    resource = resource_array.first
    resource_array.clear
    self.have_deq_waiting_flag = false
    deq_cond.signal
  ensure
    deq_mutex.unlock
  end

  [resource, !closed?]
end

#push(obj, nonblock = false) ⇒ Object Also known as: <<, enq



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
# File 'lib/rbgo/select_chan.rb', line 104

def push(obj, nonblock = false)
  if closed?
    raise ClosedQueueError.new
  end

  if nonblock
    raise ThreadError.new unless enq_mutex.try_lock
  else
    enq_mutex.lock
  end

  begin
    if nonblock
      raise ThreadError.new unless have_deq_waiting_flag
    end

    begin
      if closed?
        raise ClosedQueueError.new
      else
        deq_mutex.synchronize do
          resource_array[0] = obj
          enq_cond.signal
          until resource_array.empty? || closed?
            self.have_enq_waiting_flag = true

            begin
              Thread.new do
                deq_mutex.synchronize do
                  # no op
                end
                notify_readable_observers
              end
            rescue Exception => ex
              STDERR.puts ex
              sleep 1
              retry
            end

            deq_cond.wait(deq_mutex)
          end
          raise ClosedQueueError.new if closed?
        end
      end
    ensure
      self.have_enq_waiting_flag = false
    end
  ensure
    enq_mutex.unlock
  end

  self
end