Module: LiveData::ThreadWatch

Defined in:
lib/live_data/thread_watch.rb

Constant Summary collapse

Lock =
Mutex.new
WaitTime =
15
UserThreads =
{}
UserData =
{}

Class Method Summary collapse

Class Method Details

.clear(user_id) ⇒ Object



61
62
63
64
65
66
# File 'lib/live_data/thread_watch.rb', line 61

def self.clear( user_id )
 data = get_user_data( user_id )
 Lock.synchronize{
  data.clear
 }
end

.get_user_data(user_id) ⇒ Object



68
69
70
71
72
# File 'lib/live_data/thread_watch.rb', line 68

def self.get_user_data( user_id )
 Lock.synchronize {
  UserData[user_id] ||= [] 
 }
end

.read(user_id, wait_time = WaitTime) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/live_data/thread_watch.rb', line 39

def self.read( user_id, wait_time = WaitTime  )
 data = get_user_data( user_id )
 obj  = nil
 Lock.synchronize{
  obj = data.shift
 }
 return obj if obj
 wait( user_id, wait_time )
 Lock.synchronize{
  obj = data.shift
 }
 return obj
end

.wait(user_id, time = WaitTime, user_threads = UserThreads) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/live_data/thread_watch.rb', line 8

def self.wait( user_id, time = WaitTime, user_threads = UserThreads )
   already_use = false
   Lock.synchronize {
      if( user_threads[user_id] )
         already_use = true
      else
         user_threads[user_id] = Thread.current
      end
   }
   if( already_use )
      return nil
   end
   used_time = sleep( time )
   Lock.synchronize {
      user_threads.delete( user_id )
   }
   return used_time
end

.wakeup(user_id, user_threads = UserThreads) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/live_data/thread_watch.rb', line 27

def self.wakeup( user_id , user_threads = UserThreads )
   Lock.synchronize {
      uthread = user_threads[user_id]
      if( uthread )
         uthread.wakeup
         return true
      else
         return false
      end
   }
end

.write(user_id, obj) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/live_data/thread_watch.rb', line 53

def self.write( user_id, obj )
 data = get_user_data( user_id )
 Lock.synchronize {
  data.push( obj )
 }
 wakeup( user_id )
end