Class: VirtFS::ContextManager

Inherits:
Object
  • Object
show all
Defined in:
lib/virtfs/context_manager.rb

Overview

Central/Global VirtFS context manager. Provides a singleton context management interface for use in VirtFS.

Contexts are recorded here on a per-thread-group basis

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread_group) ⇒ ContextManager

Returns a new instance of ContextManager.



69
70
71
72
73
# File 'lib/virtfs/context_manager.rb', line 69

def initialize(thread_group)
  @thread_group  = thread_group
  @context_mutex = Mutex.new
  reset
end

Instance Attribute Details

#thread_groupObject (readonly)

Returns the value of attribute thread_group.



7
8
9
# File 'lib/virtfs/context_manager.rb', line 7

def thread_group
  @thread_group
end

Class Method Details

.contextObject



28
29
30
# File 'lib/virtfs/context_manager.rb', line 28

def self.context
  current.current_context
end

.context!Object



32
33
34
# File 'lib/virtfs/context_manager.rb', line 32

def self.context!
  current!.current_context
end

.currentObject



16
17
18
19
20
# File 'lib/virtfs/context_manager.rb', line 16

def self.current
  @context_manager_mutex.synchronize do
    @context_managers[my_thread_group] ||= ContextManager.new(my_thread_group)
  end
end

.current!Object



22
23
24
25
26
# File 'lib/virtfs/context_manager.rb', line 22

def self.current!
  @context_manager_mutex.synchronize do
    @context_managers[my_thread_group] || raise(VirtFS::NoContextError.new)
  end
end

.manager_for(tgroup) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
# File 'lib/virtfs/context_manager.rb', line 42

def self.manager_for(tgroup)
  raise ArgumentError, "value must be a ThreadGroup object" unless tgroup.is_a?(ThreadGroup)
  @context_manager_mutex.synchronize do
    @context_managers[tgroup]
  end
end

.managersObject



36
37
38
39
40
# File 'lib/virtfs/context_manager.rb', line 36

def self.managers
  @context_manager_mutex.synchronize do
    @context_managers.dup
  end
end

.my_thread_groupObject



12
13
14
# File 'lib/virtfs/context_manager.rb', line 12

def self.my_thread_group
  Thread.current.group || ThreadGroup::Default
end

.new_manager_for(tgroup) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
# File 'lib/virtfs/context_manager.rb', line 49

def self.new_manager_for(tgroup)
  raise ArgumentError, "value must be a ThreadGroup object" unless tgroup.is_a?(ThreadGroup)
  @context_manager_mutex.synchronize do
    @context_managers[tgroup] = ContextManager.new(tgroup)
  end
end

.remove_manager_for(tgroup) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
# File 'lib/virtfs/context_manager.rb', line 56

def self.remove_manager_for(tgroup)
  raise ArgumentError, "value must be a ThreadGroup object" unless tgroup.is_a?(ThreadGroup)
  @context_manager_mutex.synchronize do
    @context_managers.delete(tgroup)
  end
end

.reset_allObject



63
64
65
66
67
# File 'lib/virtfs/context_manager.rb', line 63

def self.reset_all
  @context_manager_mutex.synchronize do
    @context_managers = {}
  end
end

Instance Method Details

#[](key) ⇒ Object



75
76
77
78
79
# File 'lib/virtfs/context_manager.rb', line 75

def [](key)
  @context_mutex.synchronize do
    @contexts[key]
  end
end

#[]=(key, context) ⇒ Object

Change context without saving current context state.

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/virtfs/context_manager.rb', line 84

def []=(key, context)
  raise ArgumentError, "Context must be a VirtFS::Context object" if context && !context.is_a?(Context)
  raise ArgumentError, "Cannot change the default context" if key == :default
  @context_mutex.synchronize do
    if context.nil?
      ctx = @contexts.delete(key)
      ctx.key = nil
      return ctx
    end
    raise "Context for given key already exists" if @contexts[key]
    context.key = key
    @contexts[key] = context
  end
end

#activate!(key) ⇒ Object

Save the current context state and change context.



108
109
110
111
112
113
114
115
116
# File 'lib/virtfs/context_manager.rb', line 108

def activate!(key)
  @context_mutex.synchronize do
    raise "Context already activated" if @saved_context
    raise "Context for given key doesn't exist" unless (ctx = @contexts[key])
    @saved_context = @current_context
    @current_context = ctx
    @saved_context # returns the pre-activation context.
  end
end

#activated?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/virtfs/context_manager.rb', line 99

def activated?
  @context_mutex.synchronize do
    !@saved_context.nil?
  end
end

#current_contextObject



131
132
133
134
135
# File 'lib/virtfs/context_manager.rb', line 131

def current_context
  @context_mutex.synchronize do
    @current_context
  end
end

#current_context=(key) ⇒ Object



137
138
139
140
141
142
# File 'lib/virtfs/context_manager.rb', line 137

def current_context=(key)
  @context_mutex.synchronize do
    raise "Context for given key doesn't exist" unless (ctx = @contexts[key])
    @current_context = ctx
  end
end

#deactivate!Object

Restore the context state saved by activate!



121
122
123
124
125
126
127
128
129
# File 'lib/virtfs/context_manager.rb', line 121

def deactivate!
  @context_mutex.synchronize do
    raise "Context not activated" unless @saved_context
    ret = @current_context
    @current_context = @saved_context
    @saved_context = nil
    ret # returns the pre-deactivated context.
  end
end

#resetObject



144
145
146
147
148
149
150
151
# File 'lib/virtfs/context_manager.rb', line 144

def reset
  @context_mutex.synchronize do
    @contexts           = {}
    @saved_context      = nil
    @contexts[:default] = Context.new
    @current_context    = @contexts[:default]
  end
end

#with(key) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/virtfs/context_manager.rb', line 153

def with(key)
  activate!(key)
  begin
    yield
  ensure
    deactivate!
  end
end

#withoutObject



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/virtfs/context_manager.rb', line 162

def without
  if !activated?
    yield
  else
    begin
      saved_context = deactivate!
      yield
    ensure
      activate!(saved_context.key)
    end
  end
end