Bläddra i källkod

Use brl.threadpool with NG.

Brucey 3 år sedan
förälder
incheckning
6629736a57
7 ändrade filer med 368 tillägg och 251 borttagningar
  1. 8 1
      CHANGELOG
  2. 1 1
      bmk_config.bmx
  3. 11 249
      bmk_ng.bmx
  4. 33 0
      bmk_ng_utils.bmx
  5. 244 0
      bmk_pm_legacy.bmx
  6. 64 0
      bmk_pm_ng.bmx
  7. 7 0
      bmk_proc_man.bmx

+ 8 - 1
CHANGELOG

@@ -1,4 +1,11 @@
-## [3.46] - 2021-03-01
+## [3.47] - 2021-12-14
+### Fixed
+ - Less optimisations with -gdb option.
+ - .mm files use CPP_OPTS.
+### Changed
+ - Use BRL.ThreadPool with NG.
+
+## [3.46] - 2021-01-03
 ### Fixed
  - Linkage for incbin in included sources.
 

+ 1 - 1
bmk_config.bmx

@@ -10,7 +10,7 @@ Import brl.map
 
 Import "stringbuffer_core.bmx"
 
-Const BMK_VERSION:String = "3.46"
+Const BMK_VERSION:String = "3.47"
 
 Const ALL_SRC_EXTS$="bmx;i;c;m;h;cpp;cxx;mm;hpp;hxx;s;cc;asm;S"
 

+ 11 - 249
bmk_ng.bmx

@@ -9,6 +9,7 @@ Import Pub.FreeProcess
 
 ?threaded
 Import BRL.Threads
+Import "bmk_proc_man.bmx"
 ?
 ?Not win32
 Import "waitpid.c"
@@ -1449,56 +1450,21 @@ Type TBMKCommand
 
 End Type
 
-?threaded
-Type TProcessManager
-
-	Field pool:TThreadPool
-	
-	Field cpuCount:Int
-	
-	Field threads:TList = New TList
-	
-	Method New()
-		cpuCount = GetCoreCount()
-		
-		pool = TThreadPool.Create(Max(1, cpuCount - 1), cpuCount * 6)
-		
-	End Method
-
-	Method CheckTasks()
-		While pool.count() = pool.Size()
-			Delay 5
-		Wend
-	End Method
-	
-	Method WaitForTasks()
-		While pool.Count() Or pool.Running()
-			Delay 5
-		Wend
-	End Method
-	
-	Method DoSystem(cmd:String, src:String, obj:String, supp:String)
-		CheckTasks()
-
-		pool.AddTask(TProcessTask._DoTasks, New TProcessTask.Create(cmd, src, obj, supp))
-
-	End Method
-
-	Method AddTask:Int(func:Object(data:Object), data:Object)
-		CheckTasks()
-
-		pool.AddTask(func, data)
-	End Method
-	
-End Type
-
 ?Not win32
 Extern
 	Function bmx_system:Int(cmd:Byte Ptr)
 End Extern
 ?
 
-Type TProcessTask
+Type TProcessTaskFactoryImpl Extends TProcessTaskFactory
+	Method Create:TProcessTask( cmd:String, src:String, obj:String, supp:String )
+		Return new TProcessTaskImpl.Create(cmd, src, obj, supp)
+	End Method
+End Type
+
+new TProcessTaskFactoryImpl
+
+Type TProcessTaskImpl Extends TProcessTask
 
 	Field command:String
 	Field source:String
@@ -1513,10 +1479,6 @@ Type TProcessTask
 		Self.supp = supp
 		Return Self
 	End Method
-
-	Function _DoTasks:Object(data:Object)
-		Return TProcessTask(data).DoTasks()
-	End Function
 	
 	Method DoTasks:Object()
 		Local res:Int
@@ -1556,204 +1518,4 @@ End Type
 
 ?threaded
 Global processManager:TProcessManager = New TProcessManager
-
-Rem
-bbdoc: A thread pool.
-End Rem
-Type TThreadPool
-
-	Field _threads:TThread[]
-	Field _queue:TThreadPoolTask[]
-	
-	Field _lock:TMutex
-	Field _waitVar:TCondVar
-	
-	Field _count:Int
-	Field _head:Int
-	Field _tail:Int
-	Field _running:Int
-	
-	Field _shutdown:Int
-
-	Rem
-	bbdoc: Creates a new thread pool of @threadCount threads and a queue size of @queueSize.
-	End Rem
-	Function Create:TThreadPool(threadCount:Int, queueSize:Int)
-		Local pool:TThreadPool = New TThreadPool
-		pool._threads = New TThread[threadCount]
-		pool._queue = New TThreadPoolTask[queueSize]
-		
-		pool._lock = TMutex.Create()
-		pool._waitVar = TCondVar.Create()
-		
-		For Local i:Int = 0 Until threadCount
-			pool._threads[i] = TThread.Create(_ThreadPoolThread, pool)
-		Next
-		
-		' cache tasks
-		For Local i:Int = 0 Until queueSize
-			pool._queue[i] = New TThreadPoolTask
-		Next
-		
-		Return pool
-	End Function
-	
-	Rem
-	bbdoc: Returns the number of tasks in the queue.
-	End Rem
-	Method Count:Int()
-		Return _count
-	End Method
-	
-	Rem
-	bbdoc: Returns the size of the queue.
-	End Rem
-	Method Size:Int()
-		Return _queue.length
-	End Method
-	
-	Rem
-	bbdoc: Returns the number of busy/running threads.
-	End Rem
-	Method Running:Int()
-		Return _running
-	End Method
-	
-	Rem
-	bbdoc: Adds a task to the queue.
-	End Rem
-	Method AddTask:Int(func:Object(data:Object), data:Object)
-	
-		Local result:Int = True
-	
-		_lock.Lock()
-		
-		Local slot:Int = _tail + 1
-		If slot = _queue.length Then
-			slot = 0
-		End If
-		
-		While True
-		
-			If _count = _queue.length Then
-				result = False
-				Exit
-			End If
-		
-			If _shutdown Then
-				result = False
-				Exit
-			End If
-			
-			_queue[_tail].func = func
-			_queue[_tail].data = data
-			_tail = slot
-			_count :+ 1
-			
-			_waitVar.Broadcast()
-			
-			Exit
-		Wend
-		
-		_lock.Unlock()
-		
-		Return result
-	End Method
-	
-	Rem
-	bbdoc: Shutdown the pool.
-	about: If @immediately is False, the queue will be processed to the end.
-	End Rem
-	Method Shutdown(immediately:Int = False)
-		_lock.Lock()
-		
-		While True
-		
-			If _shutdown Then
-				Return
-			End If
-			
-			If immediately Then
-				_shutdown = 2
-			Else
-				_shutdown = 1
-			End If
-		
-			_waitVar.Broadcast()
-			_lock.Unlock()
-			
-			For Local i:Int = 0 Until _threads.length
-				_threads[i].Wait()
-			Next
-		
-			Exit
-		Wend
-		
-		_lock.Lock()
-		_lock.Close()
-		_waitVar.Close()
-		
-	End Method
-	
-	Function _ThreadPoolThread:Object(data:Object)
-		Local pool:TThreadPool = TThreadPool(data)
-		
-		While True
-		
-			pool._lock.Lock()
-			
-			While pool._count = 0 And Not pool._shutdown
-				pool._waitVar.Wait(pool._lock)
-				Delay 5
-			Wend
-			
-			If pool._shutdown And pool._count = 0 Then
-				' time to finish
-				Exit
-			End If
-			
-			Local task:TThreadPoolTask = pool._queue[pool._head]
-			
-			Local func:Object(data:Object) = task.func
-			Local data:Object = task.data
-			
-			pool._head :+ 1
-			
-			If pool._head = pool._queue.length Then
-				pool._head = 0
-			End If
-			
-			' less queue
-			pool._count :- 1
-			' more running threads
-			pool._running :+ 1
-			
-			pool._lock.Unlock()
-			
-			' perform a task
-			func(data)
-			
-			pool._lock.Lock()
-			pool._running :- 1
-			pool._lock.Unlock()
-		Wend
-		
-		pool._lock.Unlock()
-		
-	End Function
-	
-	Method Delete()
-		Shutdown()
-	End Method
-	
-End Type
-
-Type TThreadPoolTask
-
-	Field func:Object(data:Object)
-	Field data:Object
-	
-End Type
-
-?
-
+?

+ 33 - 0
bmk_ng_utils.bmx

@@ -190,3 +190,36 @@ Type TFileUtils
 	End Method
 	
 End Type
+
+Private
+
+Global factories:TProcessTaskFactory
+
+Public
+
+Type TProcessTask
+
+	Function _DoTasks:Object(data:Object)
+		Return TProcessTask(data).DoTasks()
+	End Function
+
+	Method DoTasks:Object() abstract
+End Type
+
+Type TProcessTaskFactory
+
+	Field _succ:TProcessTaskFactory
+	
+	Method New()
+		_succ=factories
+		factories=Self
+	End Method
+	
+	Method Create:TProcessTask( cmd:String, src:String, obj:String, supp:String ) Abstract
+	
+End Type
+
+Function CreateProcessTask:TProcessTask(cmd:String, src:String, obj:String, supp:String)
+	Local factory:TProcessTaskFactory=factories
+	Return factory.Create(cmd, src, obj, supp)
+End Function

+ 244 - 0
bmk_pm_legacy.bmx

@@ -0,0 +1,244 @@
+SuperStrict
+
+Import "bmk_ng_utils.bmx"
+
+Type TProcessManager
+
+	Field pool:TThreadPool
+	
+	Field cpuCount:Int
+	
+	Field threads:TList = New TList
+	
+	Method New()
+		cpuCount = GetCoreCount()
+		
+		pool = TThreadPool.Create(Max(1, cpuCount - 1), cpuCount * 6)
+		
+	End Method
+
+	Method CheckTasks()
+		While pool.count() = pool.Size()
+			Delay 5
+		Wend
+	End Method
+	
+	Method WaitForTasks()
+		While pool.Count() Or pool.Running()
+			Delay 5
+		Wend
+	End Method
+	
+	Method DoSystem(cmd:String, src:String, obj:String, supp:String)
+		CheckTasks()
+
+		pool.AddTask(TProcessTask._DoTasks, CreateProcessTask(cmd, src, obj, supp))
+
+	End Method
+
+	Method AddTask:Int(func:Object(data:Object), data:Object)
+		CheckTasks()
+
+		pool.AddTask(func, data)
+	End Method
+	
+End Type
+
+Rem
+bbdoc: A thread pool.
+End Rem
+Type TThreadPool
+
+	Field _threads:TThread[]
+	Field _queue:TThreadPoolTask[]
+	
+	Field _lock:TMutex
+	Field _waitVar:TCondVar
+	
+	Field _count:Int
+	Field _head:Int
+	Field _tail:Int
+	Field _running:Int
+	
+	Field _shutdown:Int
+
+	Rem
+	bbdoc: Creates a new thread pool of @threadCount threads and a queue size of @queueSize.
+	End Rem
+	Function Create:TThreadPool(threadCount:Int, queueSize:Int)
+		Local pool:TThreadPool = New TThreadPool
+		pool._threads = New TThread[threadCount]
+		pool._queue = New TThreadPoolTask[queueSize]
+		
+		pool._lock = TMutex.Create()
+		pool._waitVar = TCondVar.Create()
+		
+		For Local i:Int = 0 Until threadCount
+			pool._threads[i] = TThread.Create(_ThreadPoolThread, pool)
+		Next
+		
+		' cache tasks
+		For Local i:Int = 0 Until queueSize
+			pool._queue[i] = New TThreadPoolTask
+		Next
+		
+		Return pool
+	End Function
+	
+	Rem
+	bbdoc: Returns the number of tasks in the queue.
+	End Rem
+	Method Count:Int()
+		Return _count
+	End Method
+	
+	Rem
+	bbdoc: Returns the size of the queue.
+	End Rem
+	Method Size:Int()
+		Return _queue.length
+	End Method
+	
+	Rem
+	bbdoc: Returns the number of busy/running threads.
+	End Rem
+	Method Running:Int()
+		Return _running
+	End Method
+	
+	Rem
+	bbdoc: Adds a task to the queue.
+	End Rem
+	Method AddTask:Int(func:Object(data:Object), data:Object)
+	
+		Local result:Int = True
+	
+		_lock.Lock()
+		
+		Local slot:Int = _tail + 1
+		If slot = _queue.length Then
+			slot = 0
+		End If
+		
+		While True
+		
+			If _count = _queue.length Then
+				result = False
+				Exit
+			End If
+		
+			If _shutdown Then
+				result = False
+				Exit
+			End If
+			
+			_queue[_tail].func = func
+			_queue[_tail].data = data
+			_tail = slot
+			_count :+ 1
+			
+			_waitVar.Broadcast()
+			
+			Exit
+		Wend
+		
+		_lock.Unlock()
+		
+		Return result
+	End Method
+	
+	Rem
+	bbdoc: Shutdown the pool.
+	about: If @immediately is False, the queue will be processed to the end.
+	End Rem
+	Method Shutdown(immediately:Int = False)
+		_lock.Lock()
+		
+		While True
+		
+			If _shutdown Then
+				Return
+			End If
+			
+			If immediately Then
+				_shutdown = 2
+			Else
+				_shutdown = 1
+			End If
+		
+			_waitVar.Broadcast()
+			_lock.Unlock()
+			
+			For Local i:Int = 0 Until _threads.length
+				_threads[i].Wait()
+			Next
+		
+			Exit
+		Wend
+		
+		_lock.Lock()
+		_lock.Close()
+		_waitVar.Close()
+		
+	End Method
+	
+	Function _ThreadPoolThread:Object(data:Object)
+		Local pool:TThreadPool = TThreadPool(data)
+		
+		While True
+		
+			pool._lock.Lock()
+			
+			While pool._count = 0 And Not pool._shutdown
+				pool._waitVar.Wait(pool._lock)
+				Delay 5
+			Wend
+			
+			If pool._shutdown And pool._count = 0 Then
+				' time to finish
+				Exit
+			End If
+			
+			Local task:TThreadPoolTask = pool._queue[pool._head]
+			
+			Local func:Object(data:Object) = task.func
+			Local data:Object = task.data
+			
+			pool._head :+ 1
+			
+			If pool._head = pool._queue.length Then
+				pool._head = 0
+			End If
+			
+			' less queue
+			pool._count :- 1
+			' more running threads
+			pool._running :+ 1
+			
+			pool._lock.Unlock()
+			
+			' perform a task
+			func(data)
+			
+			pool._lock.Lock()
+			pool._running :- 1
+			pool._lock.Unlock()
+		Wend
+		
+		pool._lock.Unlock()
+		
+	End Function
+	
+	Method Delete()
+		Shutdown()
+	End Method
+	
+End Type
+
+Type TThreadPoolTask
+
+	Field func:Object(data:Object)
+	Field data:Object
+	
+End Type
+

+ 64 - 0
bmk_pm_ng.bmx

@@ -0,0 +1,64 @@
+SuperStrict
+
+Import BRL.ThreadPool
+Import "bmk_ng_utils.bmx"
+
+Type TProcessManager
+
+	Field pool:TThreadPoolExecutor
+	
+	Field cpuCount:Int
+	
+	Field threads:TList = New TList
+	
+	Method New()
+		cpuCount = GetCoreCount()
+		
+		pool = TThreadPoolExecutor.newFixedThreadPool(Max(1, cpuCount - 1))
+		
+	End Method
+
+	Method CheckTasks()
+		While pool.getActiveCount() = pool.maxThreads
+			Delay 5
+		Wend
+	End Method
+	
+	Method WaitForTasks()
+		While pool.getActiveCount() Or Not pool.IsQueueEmpty()
+			Delay 5
+		Wend
+	End Method
+	
+	Method DoSystem(cmd:String, src:String, obj:String, supp:String)
+		CheckTasks()
+
+		pool.execute(new TThreadPoolTask.Create(TProcessTask._DoTasks, CreateProcessTask(cmd, src, obj, supp)))
+
+	End Method
+
+	Method AddTask:Int(func:Object(data:Object), data:Object)
+		CheckTasks()
+
+		pool.execute(new TThreadPoolTask.Create(func, data))
+	End Method
+	
+End Type
+
+Type TThreadPoolTask Extends TRunnable
+
+	Field func:Object(data:Object)
+	Field data:Object
+
+	Method Create:TThreadPoolTask(func:Object(data:Object), data:Object)
+		Self.func = func
+		Self.data = data
+		Return self
+	End Method
+
+	Method run()
+		func(data)
+	End Method
+
+End Type
+

+ 7 - 0
bmk_proc_man.bmx

@@ -0,0 +1,7 @@
+SuperStrict
+
+?not bmxng
+Import "bmk_pm_legacy.bmx"
+?bmxng
+Import "bmk_pm_ng.bmx"
+?