123456789_123456789_123456789_123456789_123456789_

Class: Thread::SizedQueue

Relationships & Source Files
Inherits: Object
Defined in: thread_sync.c,
thread_sync.c,
thread_sync.rb

Overview

This class represents queues of specified size capacity. The push operation may be blocked if the capacity is full.

See Queue for an example of how a SizedQueue works.

Class Method Summary

  • .new(max) constructor

    Creates a fixed-length queue with a maximum size of #max.

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(max)

Creates a fixed-length queue with a maximum size of #max.

[ GitHub ]

  
# File 'thread_sync.c', line 1207

static VALUE
rb_szqueue_initialize(VALUE self, VALUE vmax)
{
    long max;
    struct rb_szqueue *sq = szqueue_ptr(self);

    max = NUM2LONG(vmax);
    if (max <= 0) {
        rb_raise(rb_eArgError, "queue size must be positive");
    }

    RB_OBJ_WRITE(self, szqueue_list(sq), ary_buf_new());
    ccan_list_head_init(szqueue_waitq(sq));
    ccan_list_head_init(szqueue_pushq(sq));
    sq->max = max;

    return self;
}

Instance Attribute Details

#empty?Boolean (readonly)

Returns true if the queue is empty.

[ GitHub ]

  
# File 'thread_sync.c', line 1407

static VALUE
rb_szqueue_empty_p(VALUE self)
{
    struct rb_szqueue *sq = szqueue_ptr(self);

    return RBOOL(queue_length(self, &sq->q) == 0);
}

#max (rw)

Returns the maximum size of the queue.

[ GitHub ]

  
# File 'thread_sync.c', line 1257

static VALUE
rb_szqueue_max_get(VALUE self)
{
    return LONG2NUM(szqueue_ptr(self)->max);
}

#max=(number) (rw)

Sets the maximum size of the queue to the given number.

[ GitHub ]

  
# File 'thread_sync.c', line 1270

static VALUE
rb_szqueue_max_set(VALUE self, VALUE vmax)
{
    long max = NUM2LONG(vmax);
    long diff = 0;
    struct rb_szqueue *sq = szqueue_ptr(self);

    if (max <= 0) {
        rb_raise(rb_eArgError, "queue size must be positive");
    }
    if (max > sq->max) {
        diff = max - sq->max;
    }
    sq->max = max;
    sync_wakeup(szqueue_pushq(sq), diff);
    return vmax;
}

Instance Method Details

#<<(object, non_block = false, timeout: nil)

Alias for #push.

[ GitHub ]

  
# File 'thread_sync.rb', line 66

alias_method :<<, :push

#clear

Removes all objects from the queue.

[ GitHub ]

  
# File 'thread_sync.c', line 1359

static VALUE
rb_szqueue_clear(VALUE self)
{
    struct rb_szqueue *sq = szqueue_ptr(self);

    rb_ary_clear(check_array(self, sq->q.que));
    wakeup_all(szqueue_pushq(sq));
    return self;
}

#close

Similar to Queue#close.

The difference is behavior with waiting enqueuing threads.

If there are waiting enqueuing threads, they are interrupted by raising ClosedQueueError(‘queue closed’).

[ GitHub ]

  
# File 'thread_sync.c', line 1238

static VALUE
rb_szqueue_close(VALUE self)
{
    if (!queue_closed_p(self)) {
        struct rb_szqueue *sq = szqueue_ptr(self);

        FL_SET(self, QUEUE_CLOSED);
        wakeup_all(szqueue_waitq(sq));
        wakeup_all(szqueue_pushq(sq));
    }
    return self;
}

#deq(non_block = false, timeout: nil)

Alias for #pop.

[ GitHub ]

  
# File 'thread_sync.rb', line 42

alias_method :deq, :pop

#enq(object, non_block = false, timeout: nil)

Alias for #push.

[ GitHub ]

  
# File 'thread_sync.rb', line 65

alias_method :enq, :push

#length #size
Also known as: #size

Returns the length of the queue.

[ GitHub ]

  
# File 'thread_sync.c', line 1378

static VALUE
rb_szqueue_length(VALUE self)
{
    struct rb_szqueue *sq = szqueue_ptr(self);

    return LONG2NUM(queue_length(self, &sq->q));
}

#num_waiting

Returns the number of threads waiting on the queue.

[ GitHub ]

  
# File 'thread_sync.c', line 1392

static VALUE
rb_szqueue_num_waiting(VALUE self)
{
    struct rb_szqueue *sq = szqueue_ptr(self);

    return INT2NUM(sq->q.num_waiting + sq->num_waiting_push);
}

#pop(non_block = false, timeout: nil) Also known as: #deq, #shift

Retrieves data from the queue.

If the queue is empty, the calling thread is suspended until data is pushed onto the queue. If non_block is true, the thread isn’t suspended, and ::ThreadError is raised.

If timeout seconds have passed and no data is available nil is returned. If timeout is 0 it returns immediately.

[ GitHub ]

  
# File 'thread_sync.rb', line 36

def pop(non_block = false, timeout: nil)
  if non_block && timeout
    raise ArgumentError, "can't set a timeout if non_block is enabled"
  end
  Primitive.rb_szqueue_pop(non_block, timeout)
end

#push(object, non_block = false, timeout: nil) #enq(object, non_block = false, timeout: nil) #<<(object)
Also known as: #enq, #<<

Pushes object to the queue.

If there is no space left in the queue, waits until space becomes available, unless non_block is true. If non_block is true, the thread isn’t suspended, and ::ThreadError is raised.

If timeout seconds have passed and no space is available nil is returned. If timeout is 0 it returns immediately. Otherwise it returns self.

[ GitHub ]

  
# File 'thread_sync.rb', line 59

def push(object, non_block = false, timeout: nil)
  if non_block && timeout
    raise ArgumentError, "can't set a timeout if non_block is enabled"
  end
  Primitive.rb_szqueue_push(object, non_block, timeout)
end

#shift(non_block = false, timeout: nil)

Alias for #pop.

[ GitHub ]

  
# File 'thread_sync.rb', line 43

alias_method :shift, :pop

#length #size

Alias for #length.