1 #Region "Microsoft.VisualBasic::35f8fcd9c401d2e908d9b917cc3fee14, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\Threads\ThreadQueue.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class ThreadQueue
35     
36     '         Properties: MultiThreadSupport
37     
38     '         Constructor: (+1 OverloadsSub New
39     '         Sub: AddToQueue, (+2 Overloads) Dispose, exeQueue, WaitQueue
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports System.Threading
47
48 Namespace Parallel
49
50     ''' <summary>
51     ''' 任务线程队列
52     ''' </summary>
53     Public Class ThreadQueue : Implements IDisposable
54
55         ''' <summary>
56         ''' Writer Thread ☺
57         ''' </summary>
58         Dim MyThread As New Thread(AddressOf exeQueue)
59
60         ''' <summary>
61         ''' If TRUE, the Writing process will be separated from the main thread.
62         ''' </summary>
63         Public Property MultiThreadSupport As Boolean = True
64
65         ''' <summary>
66         ''' Just my queue
67         ''' </summary>
68         ReadOnly Queue As New Queue(Of Action)(6666)
69
70         ''' <summary>
71         ''' Is thread running?
72         ''' hum
73         ''' </summary>
74         Dim QSolverRunning As Boolean = False
75
76         Dim waitForExit As Boolean = False
77
78         ''' <summary>
79         ''' lock
80         ''' </summary>
81         Dim dummy As New Object()
82
83         Sub New()
84             QSolverRunning = False
85         End Sub
86
87         ''' <summary>
88         ''' Add an Action to the queue.
89         ''' </summary>
90         ''' <param name="A">()=>{ .. }</param>
91         Public Sub AddToQueue(A As Action)
92             SyncLock Queue
93                 Call Queue.Enqueue(A)
94             End SyncLock
95
96             If MultiThreadSupport Then ' 只需要将任务添加到队列之中就行了
97                 SyncLock dummy
98                     If Not MyThread.IsAlive Then
99                         QSolverRunning = False
100                         MyThread = New Thread(AddressOf exeQueue)
101
102                         MyThread.Name = "xConsole · Multi-Thread Writer"
103                         MyThread.Start()
104                     End If
105                 End SyncLock
106             Else
107                 exeQueue()  ' 等待线程任务的执行完毕
108             End If
109         End Sub
110
111         ''' <summary>
112         ''' Wait for all thread queue job done.(Needed if you are using multiThreaded queue)
113         ''' </summary>
114         Public Sub WaitQueue()
115             While QSolverRunning = True
116                 Thread.Sleep(10)
117             End While
118         End Sub
119
120         ''' <summary>
121         ''' Execute the queue list
122         ''' </summary>
123         Private Sub exeQueue()
124             QSolverRunning = True
125
126             While Queue IsNot Nothing AndAlso Queue.Count > 0
127                 Call Thread.MemoryBarrier()
128
129                 While True
130                     Dim a As Action
131
132                     SyncLock Queue
133                         If Queue.Count = 0 Then
134                             Exit While
135                         Else
136                             a = Queue.Dequeue()
137                         End If
138                     End SyncLock
139
140                     If a IsNot Nothing Then
141                         a.Invoke()
142                     End If
143                 End While
144             End While
145
146             QSolverRunning = False
147         End Sub
148
149 #Region "IDisposable Support"
150         Private disposedValue As Boolean ' 要检测冗余调用
151
152         ' IDisposable
153         Protected Overridable Sub Dispose(disposing As Boolean)
154             If Not disposedValue Then
155                 If disposing Then
156                     ' TODO: 释放托管状态(托管对象)。
157                     Call WaitQueue()
158                     waitForExit = True
159                 End If
160
161                 ' TODO: 释放未托管资源(未托管对象)并在以下内容中替代 Finalize()。
162                 ' TODO: 将大型字段设置为 null。
163             End If
164             disposedValue = True
165         End Sub
166
167         ' TODO: 仅当以上 Dispose(disposing As Boolean)拥有用于释放未托管资源的代码时才替代 Finalize()。
168         'Protected Overrides Sub Finalize()
169         '    ' 请勿更改此代码。将清理代码放入以上 Dispose(disposing As Boolean)中。
170         '    Dispose(False)
171         '    MyBase.Finalize()
172         'End Sub
173
174         ' Visual Basic 添加此代码以正确实现可释放模式。
175         Public Sub Dispose() Implements IDisposable.Dispose
176             ' 请勿更改此代码。将清理代码放入以上 Dispose(disposing As Boolean)中。
177             Dispose(True)
178             ' TODO: 如果在以上内容中替代了 Finalize(),则取消注释以下行。
179             ' GC.SuppressFinalize(Me)
180         End Sub
181 #End Region
182     End Class
183 End Namespace