Sha256: def4365206801907c314256db6859c5dac5a446783971e1f5c2c01d65028941b

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

#--
# SyncArray
#
# Copyright (c) 2004-2005 Navel, all rights reserved.
#
# Ruby License
#
# This module is free software. You may use, modify, and/or redistribute this
# software under the same terms as Ruby.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.
#
# ==========================================================================
#  Revision History
# ==========================================================================
#  2005.08.07 trans  Ported to Mega Modules
# ==========================================================================
#++

#:title: SyncArray
#
# A thread-safe array. We use a sync object instead of a 
# mutex, because it is re-entrant. An exclusive lock is 
# needed when writing, a shared lock IS NEEDED when reading.
#
# == Usage
#
#   TODO
#
# == Author(s)
#
# * George Moschovitis  <gm@navel.gr>

require 'sync'

class SyncArray < Array

  attr :sync

  #
  # gmosx: delegator is not yet used.
  #
  def initialize(delegator = nil)
    @sync = ::Sync.new()
    super()
  end

  def << (value)
    return @sync.synchronize(Sync::SH) { super }
  end

  def delete_if(&block) 
    return @sync.synchronize(Sync::SH) { super }
  end

  def [](key)
    return @sync.synchronize(Sync::SH) { super }
  end

  def []=(key, value)
    return @sync.synchronize(Sync::EX) { super }
  end

  def delete(key)
    return @sync.synchronize(Sync::EX) { super }
  end

  def clear
    @sync.synchronize(Sync::EX) {	super }
  end

  def size
    return @sync.synchronize(Sync::SH) { super }
  end

  def shift
    return @sync.synchronize(::Sync::EX) { super }
  end

  def unshift(el)
    return @sync.synchronize(::Sync::EX) { super }
  end

end

#--
# Funniest thing, remove the :: in front of Sync.new in the initialize
# routine and uncomment the alias line below and watch the stack blow chunks.
# I'm leaving this comment here as a reminder.
### Array::Sync = SyncArray
#++

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mega-0.3.1 lib/mega/syncarray.rb