Class: TaskQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/avertasks/taskqueue.rb

Defined Under Namespace

Classes: ArgumentErr, TaskRunErr

Constant Summary collapse

@@errMsg =
[
    'ArgumentErr for func:{func} argument type: {type} is requirement'
]
@@run_times =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, full_count) ⇒ TaskQueue

Returns a new instance of TaskQueue.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/avertasks/taskqueue.rb', line 20

def initialize(id, full_count)
    # define for collect tasks
    @tasks = []
    @id = id
    @completed = 0
    @runing = false
    @waiting = true
    @task_mutex = Mutex.new
    @full_count = full_count
    @cankill = false
    @alldo = false
end

Instance Attribute Details

#cankillObject

Returns the value of attribute cankill.



9
10
11
# File 'lib/avertasks/taskqueue.rb', line 9

def cankill
  @cankill
end

#completedObject (readonly)

Returns the value of attribute completed.



5
6
7
# File 'lib/avertasks/taskqueue.rb', line 5

def completed
  @completed
end

#full_countObject

Returns the value of attribute full_count.



8
9
10
# File 'lib/avertasks/taskqueue.rb', line 8

def full_count
  @full_count
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/avertasks/taskqueue.rb', line 4

def id
  @id
end

#runingObject (readonly) Also known as: runing?

Returns the value of attribute runing.



6
7
8
# File 'lib/avertasks/taskqueue.rb', line 6

def runing
  @runing
end

#tasksObject (readonly)

Returns the value of attribute tasks.



3
4
5
# File 'lib/avertasks/taskqueue.rb', line 3

def tasks
  @tasks
end

#waitingObject (readonly) Also known as: waiting?

Returns the value of attribute waiting.



7
8
9
# File 'lib/avertasks/taskqueue.rb', line 7

def waiting
  @waiting
end

Class Method Details

.run_timesObject



81
82
83
# File 'lib/avertasks/taskqueue.rb', line 81

def run_times()
    return @@run_times
end

Instance Method Details

#hungry?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'lib/avertasks/taskqueue.rb', line 59

def hungry?()
    count = 0
    @task_mutex.synchronize do
        count = full_count - @tasks.size
    end
    return 0 if count <= 0
    return count
end

#none?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/avertasks/taskqueue.rb', line 67

def none?
    @task_mutex.synchronize do
        @tasks.none?    
    end
end

#push(task) ⇒ Object

Raises:



32
33
34
35
36
# File 'lib/avertasks/taskqueue.rb', line 32

def push(task)
    raise ArgumentErr.new(TaskQueue.errMsg[0].sub(/{func}/, 'TaskQueue.push').sub(/{type}/, 'Task')) unless task.instance_of? Task
    @task_mutex.synchronize { @tasks << task }
    self
end

#runObject

@waiting,@runing now is useless…



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/avertasks/taskqueue.rb', line 38

def run()
    @task_mutex.synchronize do
        if @tasks.none? then
            @waiting = true
            @runing = false
            @alldo = true
            return false
        else
            @waiting = false
            @runing = true
            @alldo = false
            # temp variable for this func
            @current_task = @tasks.pop
        end
    end
    # note that i have drop out the result of your proc
    @current_task.run if @current_task
    @completed += 1
    @@run_times += 1
    return true
end

#startObject



72
73
74
75
76
77
78
79
# File 'lib/avertasks/taskqueue.rb', line 72

def start()
    thr = Thread.new do
        while (!cankill || !@alldo) do
            Thread.pass unless self.run
        end
    end
    return thr
end