sync-event 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/sync/event.rb +52 -0
- metadata +45 -0
    
        data/lib/sync/event.rb
    ADDED
    
    | @@ -0,0 +1,52 @@ | |
| 1 | 
            +
            require 'thread'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class Hash
         | 
| 4 | 
            +
              def hmap(&block)
         | 
| 5 | 
            +
                new_hash = {}
         | 
| 6 | 
            +
                self.keys.each do |key|
         | 
| 7 | 
            +
                  hash = block.call(key, self[key])
         | 
| 8 | 
            +
                  new_hash[hash.keys.first] = hash[hash.keys.first]
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
                new_hash
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
            end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            module Sync
         | 
| 15 | 
            +
              class Event
         | 
| 16 | 
            +
                VERSION = '0.0.1'
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                # Get class verion
         | 
| 19 | 
            +
                def self.version
         | 
| 20 | 
            +
                  VERSION
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                def initialize
         | 
| 24 | 
            +
                  @lock    = Mutex.new
         | 
| 25 | 
            +
                  @cond    = ConditionVariable.new
         | 
| 26 | 
            +
                  @count   = 0
         | 
| 27 | 
            +
                  @threads = {}
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                # Set event
         | 
| 31 | 
            +
                def set
         | 
| 32 | 
            +
                  @lock.synchronize do
         | 
| 33 | 
            +
                    @count  += 1
         | 
| 34 | 
            +
                    @threads = @threads.hmap do |k,v|
         | 
| 35 | 
            +
                      { k => v+1 }
         | 
| 36 | 
            +
                    end
         | 
| 37 | 
            +
                    @cond.broadcast       
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                # Wait event
         | 
| 42 | 
            +
                def wait
         | 
| 43 | 
            +
                  @lock.synchronize do
         | 
| 44 | 
            +
                    current = Thread.current
         | 
| 45 | 
            +
                    @threads[current] = @count unless @threads[current]
         | 
| 46 | 
            +
                    @cond.wait(@lock) if @threads[current]==0
         | 
| 47 | 
            +
                    @threads[current] -= 1       
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
              end
         | 
| 51 | 
            +
            end
         | 
| 52 | 
            +
             | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,45 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: sync-event
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.1
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors:
         | 
| 8 | 
            +
            - Promix17
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2015-10-06 00:00:00.000000000 Z
         | 
| 13 | 
            +
            dependencies: []
         | 
| 14 | 
            +
            description: This library implements event synchronisation between threads
         | 
| 15 | 
            +
            email: promix17@yandex.ru
         | 
| 16 | 
            +
            executables: []
         | 
| 17 | 
            +
            extensions: []
         | 
| 18 | 
            +
            extra_rdoc_files: []
         | 
| 19 | 
            +
            files:
         | 
| 20 | 
            +
            - ./lib/sync/event.rb
         | 
| 21 | 
            +
            homepage: https://github.com/promix17/sync-event
         | 
| 22 | 
            +
            licenses: []
         | 
| 23 | 
            +
            post_install_message: 
         | 
| 24 | 
            +
            rdoc_options: []
         | 
| 25 | 
            +
            require_paths:
         | 
| 26 | 
            +
            - lib
         | 
| 27 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 28 | 
            +
              none: false
         | 
| 29 | 
            +
              requirements:
         | 
| 30 | 
            +
              - - ! '>='
         | 
| 31 | 
            +
                - !ruby/object:Gem::Version
         | 
| 32 | 
            +
                  version: '0'
         | 
| 33 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 34 | 
            +
              none: false
         | 
| 35 | 
            +
              requirements:
         | 
| 36 | 
            +
              - - ! '>='
         | 
| 37 | 
            +
                - !ruby/object:Gem::Version
         | 
| 38 | 
            +
                  version: '0'
         | 
| 39 | 
            +
            requirements: []
         | 
| 40 | 
            +
            rubyforge_project: 
         | 
| 41 | 
            +
            rubygems_version: 1.8.23
         | 
| 42 | 
            +
            signing_key: 
         | 
| 43 | 
            +
            specification_version: 3
         | 
| 44 | 
            +
            summary: This library implements event synchronisation between threads
         | 
| 45 | 
            +
            test_files: []
         |