1 /***************************************************************************
2 * Copyright (C) 2009-2011 by Stefan Fuhrmann *
3 * stefanfuhrmann@alice-dsl.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include "JobScheduler.h"
29 // queue size management
31 void CJobScheduler::CQueue::Grow (size_t newSize
)
33 TJob
* newData
= new TJob
[newSize
];
35 size_t count
= size();
37 memmove (newData
, first
, count
* sizeof (TJob
[1]));
41 end
= newData
+ newSize
;
43 last
= newData
+ count
;
46 void CJobScheduler::CQueue::AutoGrow()
48 size_t count
= size();
50 if (count
* 2 <= capacity())
52 memmove (data
, first
, count
* sizeof (TJob
[1]));
58 Grow (capacity() * 2);
62 // remove one entry from \ref starving container.
63 // Return NULL, if container was empty
65 CJobScheduler
* CJobScheduler::CThreadPool::SelectStarving()
67 CCriticalSectionLock
lock (mutex
);
68 if (starving
.empty() || pool
.empty())
71 std::vector
<CJobScheduler
*>::iterator begin
= starving
.begin();
73 CJobScheduler
* scheduler
= *begin
;
74 starving
.erase (begin
);
79 // create empty thread pool
81 CJobScheduler::CThreadPool::CThreadPool()
90 CJobScheduler::CThreadPool::~CThreadPool()
97 CJobScheduler::CThreadPool
& CJobScheduler::CThreadPool::GetInstance()
99 static CThreadPool instance
;
105 CJobScheduler::SThreadInfo
* CJobScheduler::CThreadPool::TryAlloc()
107 CCriticalSectionLock
lock (mutex
);
110 if (yetToCreate
== 0)
113 // lazy thread creation
115 SThreadInfo
* info
= new SThreadInfo
;
117 info
->thread
= new CThread (&ThreadFunc
, info
, true);
125 CJobScheduler::SThreadInfo
* thread
= pool
.back();
132 void CJobScheduler::CThreadPool::Release (SThreadInfo
* thread
)
135 // put back into pool, unless its capacity has been exceeded
137 CCriticalSectionLock
lock (mutex
);
138 if (pool
.size() + --allocCount
< maxCount
)
140 pool
.push_back (thread
);
144 if (starving
.empty())
149 // pool overflow -> terminate thread
151 delete thread
->thread
;
156 // notify starved schedulers that there may be some threads to alloc
158 for ( CJobScheduler
* scheduler
= SelectStarving()
160 ; scheduler
= SelectStarving())
162 scheduler
->ThreadAvailable();
166 // set max. number of concurrent threads
168 void CJobScheduler::CThreadPool::SetThreadCount (size_t count
)
170 CCriticalSectionLock
lock (mutex
);
173 if (pool
.size() + allocCount
< maxCount
)
174 yetToCreate
= maxCount
- pool
.size() + allocCount
;
176 while ((pool
.size() + allocCount
> maxCount
) && !pool
.empty())
178 SThreadInfo
* info
= pool
.back();
186 size_t CJobScheduler::CThreadPool::GetThreadCount() const
191 // manage starving schedulers
193 void CJobScheduler::CThreadPool::AddStarving (CJobScheduler
* scheduler
)
195 CCriticalSectionLock
lock (mutex
);
196 starving
.push_back (scheduler
);
199 bool CJobScheduler::CThreadPool::RemoveStarving (CJobScheduler
* scheduler
)
201 CCriticalSectionLock
lock (mutex
);
203 typedef std::vector
<CJobScheduler
*>::iterator TI
;
204 TI begin
= starving
.begin();
205 TI end
= starving
.end();
207 TI newEnd
= std::remove_copy (begin
, end
, begin
, scheduler
);
211 starving
.erase (newEnd
, end
);
215 // job execution helpers
217 CJobScheduler::TJob
CJobScheduler::AssignJob (SThreadInfo
* info
)
219 CCriticalSectionLock
lock (mutex
);
221 // wake up threads that waited for some work to finish
223 if (waitingThreads
> 0)
226 // suspend this thread if there is no work left
228 if (queue
.empty() || (threads
.stopCount
!= 0))
232 info
->thread
->Suspend();
234 // remove from "running" list and put it back either to
235 // "suspended" pool or global shared pool
237 bool terminateThread
= false;
238 for (size_t i
= 0, count
= threads
.running
.size(); i
< count
; ++i
)
239 if (threads
.running
[i
] == info
)
241 threads
.running
[i
] = threads
.running
[count
-1];
242 threads
.running
.pop_back();
244 // still in debt of shared pool?
246 if (threads
.fromShared
> 0)
248 // if we are actually idle, we aren't starving anymore
252 // put back to global pool
255 CThreadPool::GetInstance().Release (info
);
257 --threads
.fromShared
;
259 else if (aggressiveThreading
)
261 // don't keep private idle threads
264 ++threads
.yetToCreate
;
265 terminateThread
= true;
272 threads
.suspended
.push_back (info
);
273 ++threads
.suspendedCount
;
276 // signal empty queue, if necessary
278 ++threads
.unusedCount
;
279 if (--threads
.runningCount
== 0)
285 return TJob ((IJob
*)NULL
, terminateThread
);
293 // try to get a thread from the shared pool.
294 // register as "starving" if that failed
296 bool CJobScheduler::AllocateSharedThread()
298 if (!threads
.starved
)
300 SThreadInfo
* info
= CThreadPool::GetInstance().TryAlloc();
303 ++threads
.fromShared
;
304 --threads
.unusedCount
;
305 ++threads
.runningCount
;
307 threads
.running
.push_back (info
);
309 info
->thread
->Resume();
315 threads
.starved
= true;
316 CThreadPool::GetInstance().AddStarving (this);
323 bool CJobScheduler::AllocateThread()
325 if (threads
.suspendedCount
> 0)
327 // recycle suspended, private thread
329 SThreadInfo
* info
= threads
.suspended
.back();
330 threads
.suspended
.pop_back();
332 --threads
.suspendedCount
;
333 --threads
.unusedCount
;
334 ++threads
.runningCount
;
336 threads
.running
.push_back (info
);
337 info
->thread
->Resume();
339 else if (threads
.yetToCreate
> 0)
341 // time to start a new private thread
343 --threads
.yetToCreate
;
345 --threads
.unusedCount
;
346 ++threads
.runningCount
;
348 SThreadInfo
* info
= new SThreadInfo
;
350 info
->thread
= new CThread (&ThreadFunc
, info
, true);
351 threads
.running
.push_back (info
);
353 info
->thread
->Resume();
357 // try to allocate a shared thread
359 if (threads
.fromShared
< threads
.maxFromShared
)
360 return AllocateSharedThread();
368 // unregister from "starving" list.
369 // This may race with CThreadPool::Release -> must loop here.
371 void CJobScheduler::StopStarvation()
373 while (threads
.starved
)
374 if (CThreadPool::GetInstance().RemoveStarving (this))
376 threads
.starved
= false;
381 // worker thread function
383 bool CJobScheduler::ThreadFunc (void* arg
)
385 SThreadInfo
* info
= reinterpret_cast<SThreadInfo
*>(arg
);
387 TJob job
= info
->owner
->AssignJob (info
);
388 if (job
.first
!= NULL
)
392 job
.first
->Execute();
394 // it is no longer referenced by the scheduler
396 job
.first
->OnUnSchedule (info
->owner
);
398 // is it our responsibility to clean up this job?
409 // maybe, auto-delete thread object
415 // Create & remove threads
417 CJobScheduler::CJobScheduler
419 , size_t sharedThreads
420 , bool aggressiveThreading
423 , aggressiveThreading (aggressiveThreading
)
425 threads
.runningCount
= 0;
426 threads
.suspendedCount
= 0;
428 threads
.fromShared
= 0;
429 threads
.maxFromShared
= sharedThreads
;
431 threads
.unusedCount
= threadCount
+ sharedThreads
;
432 threads
.yetToCreate
= threadCount
;
434 threads
.starved
= false;
435 threads
.stopCount
= 0;
437 queue
.set_fifo (fifo
);
439 // auto-initialize shared threads
441 if (GetSharedThreadCount() == 0)
445 CJobScheduler::~CJobScheduler(void)
450 assert (threads
.running
.empty());
451 assert (threads
.fromShared
== 0);
453 for (size_t i
= 0, count
= threads
.suspended
.size(); i
< count
; ++i
)
455 SThreadInfo
* info
= threads
.suspended
[i
];
463 CJobScheduler
* CJobScheduler::GetDefault()
465 static CJobScheduler
instance (0, SIZE_MAX
);
472 void CJobScheduler::Schedule (IJob
* job
, bool transferOwnership
)
474 TJob
toAdd (job
, transferOwnership
);
475 job
->OnSchedule (this);
477 CCriticalSectionLock
lock (mutex
);
483 if (threads
.stopCount
!= 0)
486 bool addThread
= aggressiveThreading
487 || ( (queue
.size() > 2 * threads
.runningCount
)
488 && (threads
.unusedCount
> 0));
494 // notification that a new thread may be available
496 void CJobScheduler::ThreadAvailable()
498 CCriticalSectionLock
lock (mutex
);
499 threads
.starved
= false;
501 while ( (threads
.stopCount
!= 0)
502 && (queue
.size() > 2 * threads
.runningCount
)
503 && (threads
.suspendedCount
== 0)
504 && (threads
.yetToCreate
== 0)
505 && (threads
.fromShared
< threads
.maxFromShared
))
507 if (!AllocateSharedThread())
512 // wait for all current and follow-up jobs to terminate
514 void CJobScheduler::WaitForEmptyQueue()
516 WaitForEmptyQueueOrTimeout(INFINITE
);
519 bool CJobScheduler::WaitForEmptyQueueOrTimeout(DWORD milliSeconds
)
524 CCriticalSectionLock
lock (mutex
);
526 // if the scheduler has been stopped, we need to remove
527 // the waiting jobs manually
529 if (threads
.stopCount
!= 0)
530 while (!queue
.empty())
532 const TJob
& job
= queue
.pop();
534 job
.first
->OnUnSchedule(this);
539 // empty queue and no jobs still being processed?
541 if ((threads
.runningCount
== 0) && queue
.empty())
544 // we will be woken up as soon as both containers are empty
549 if (!emptyEvent
.WaitForEndOrTimeout(milliSeconds
))
555 // wait for some jobs to be finished.
557 void CJobScheduler::WaitForSomeJobs()
560 CCriticalSectionLock
lock (mutex
);
561 if ( static_cast<size_t>(InterlockedIncrement (&waitingThreads
))
562 < threads
.runningCount
)
564 // there are enough running job threads left
565 // -> wait for one of them to run idle *or*
566 // for too many of them to enter this method
568 threadIsIdle
.Reset();
572 // number of waiting jobs nor equal to or greater than
573 // the number of running job threads
577 InterlockedDecrement (&waitingThreads
);
583 threadIsIdle
.WaitFor();
584 InterlockedDecrement (&waitingThreads
);
587 // Returns the number of jobs waiting for execution.
589 size_t CJobScheduler::GetQueueDepth() const
591 CCriticalSectionLock
lock (mutex
);
595 // Returns the number of threads that currently execute
596 // jobs for this scheduler
598 size_t CJobScheduler::GetRunningThreadCount() const
600 CCriticalSectionLock
lock (mutex
);
601 return threads
.runningCount
;
604 // remove waiting entries from the queue until their
605 // number drops to or below the given watermark.
607 std::vector
<IJob
*> CJobScheduler::RemoveJobFromQueue
611 std::vector
<IJob
*> removed
;
614 CCriticalSectionLock
lock (mutex
);
615 if (queue
.size() > watermark
)
617 size_t toRemove
= queue
.size() - watermark
;
618 removed
.reserve (toRemove
);
620 // temporarily change the queue extraction strategy
621 // such that we remove jobs from the requested end
622 // (fifo -> oldest are at front, otherwise they are
625 bool fifo
= queue
.get_fifo();
626 queue
.set_fifo (oldest
== fifo
);
630 for (size_t i
= 0; i
< toRemove
; ++i
)
632 IJob
* job
= queue
.pop().first
;
633 job
->OnUnSchedule (this);
635 removed
.push_back (job
);
638 // restore job execution order
640 queue
.set_fifo (fifo
);
647 long CJobScheduler::Stop()
649 CCriticalSectionLock
lock (mutex
);
650 return ++threads
.stopCount
;
653 long CJobScheduler::Resume()
655 CCriticalSectionLock
lock (mutex
);
656 assert (threads
.stopCount
> 0);
658 if (--threads
.stopCount
== 0)
660 while ( ( (queue
.size() > threads
.runningCount
)
661 && aggressiveThreading
)
662 || ( (queue
.size() > 4 * threads
.runningCount
)
663 && (threads
.unusedCount
> 0)))
665 if (!AllocateThread())
670 return threads
.stopCount
;
675 // set max. number of concurrent threads
677 void CJobScheduler::SetSharedThreadCount (size_t count
)
679 CThreadPool::GetInstance().SetThreadCount(count
);
682 size_t CJobScheduler::GetSharedThreadCount()
684 return CThreadPool::GetInstance().GetThreadCount();
687 void CJobScheduler::UseAllCPUs()
689 SetSharedThreadCount (GetHWThreadCount());
692 size_t CJobScheduler::GetHWThreadCount()
698 size_t sysNumProcs
= si
.dwNumberOfProcessors
;
700 size_t sysNumProcs
= sysconf (_SC_NPROCESSORS_CONF
);