2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
10 static const bigtime_t kWorkerTimeout
= 1000000;
11 // One second until a worker thread quits without a job
13 static const int32 kWorkerCountPerCPU
= 3;
15 static int32 sWorkerCount
;
18 Worker::Worker(JobQueue
& queue
)
34 fThread
= spawn_thread(&Worker::_Process
, Name(), B_NORMAL_PRIORITY
,
39 status_t status
= resume_thread(fThread
);
41 atomic_add(&sWorkerCount
, 1);
52 status_t status
= fJobQueue
.Pop(Timeout(), false, &job
);
58 // TODO: proper error reporting on failed job!
59 debug_printf("Launching %s failed: %s\n", job
->Title().String(),
67 Worker::Timeout() const
69 return kWorkerTimeout
;
81 Worker::Run(BJob
* job
)
88 Worker::_Process(void* _self
)
90 Worker
* self
= (Worker
*)_self
;
91 status_t status
= self
->Process();
101 MainWorker::MainWorker(JobQueue
& queue
)
104 fMaxWorkerCount(kWorkerCountPerCPU
)
106 // TODO: keep track of workers, and quit them on destruction
108 if (get_system_info(&info
) == B_OK
)
109 fMaxWorkerCount
= info
.cpu_count
* kWorkerCountPerCPU
;
114 MainWorker::Timeout() const
116 return B_INFINITE_TIMEOUT
;
121 MainWorker::Name() const
123 return "main worker";
128 MainWorker::Run(BJob
* job
)
130 int32 count
= atomic_get(&sWorkerCount
);
132 size_t jobCount
= fJobQueue
.CountJobs();
133 if (jobCount
> INT_MAX
)
136 if ((int32
)jobCount
> count
&& count
< fMaxWorkerCount
) {
137 Worker
* worker
= new Worker(fJobQueue
);
141 return Worker::Run(job
);