Make UEFI boot-platform build again
[haiku.git] / src / servers / launch / Worker.cpp
blobf7797d4d86b5f109c57a12ac70df640ff9463b8c
1 /*
2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "Worker.h"
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)
20 fThread(-1),
21 fJobQueue(queue)
26 Worker::~Worker()
31 status_t
32 Worker::Init()
34 fThread = spawn_thread(&Worker::_Process, Name(), B_NORMAL_PRIORITY,
35 this);
36 if (fThread < 0)
37 return fThread;
39 status_t status = resume_thread(fThread);
40 if (status == B_OK)
41 atomic_add(&sWorkerCount, 1);
43 return status;
47 status_t
48 Worker::Process()
50 while (true) {
51 BJob* job;
52 status_t status = fJobQueue.Pop(Timeout(), false, &job);
53 if (status != B_OK)
54 return status;
56 status = Run(job);
57 if (status != B_OK) {
58 // TODO: proper error reporting on failed job!
59 debug_printf("Launching %s failed: %s\n", job->Title().String(),
60 strerror(status));
66 bigtime_t
67 Worker::Timeout() const
69 return kWorkerTimeout;
73 const char*
74 Worker::Name() const
76 return "worker";
80 status_t
81 Worker::Run(BJob* job)
83 return job->Run();
87 /*static*/ status_t
88 Worker::_Process(void* _self)
90 Worker* self = (Worker*)_self;
91 status_t status = self->Process();
92 delete self;
94 return status;
98 // #pragma mark -
101 MainWorker::MainWorker(JobQueue& queue)
103 Worker(queue),
104 fMaxWorkerCount(kWorkerCountPerCPU)
106 // TODO: keep track of workers, and quit them on destruction
107 system_info info;
108 if (get_system_info(&info) == B_OK)
109 fMaxWorkerCount = info.cpu_count * kWorkerCountPerCPU;
113 bigtime_t
114 MainWorker::Timeout() const
116 return B_INFINITE_TIMEOUT;
120 const char*
121 MainWorker::Name() const
123 return "main worker";
127 status_t
128 MainWorker::Run(BJob* job)
130 int32 count = atomic_get(&sWorkerCount);
132 size_t jobCount = fJobQueue.CountJobs();
133 if (jobCount > INT_MAX)
134 jobCount = INT_MAX;
136 if ((int32)jobCount > count && count < fMaxWorkerCount) {
137 Worker* worker = new Worker(fJobQueue);
138 worker->Init();
141 return Worker::Run(job);