From 0337b4adfdc34e53370f6fe95b4c55db5f13fe53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Mon, 1 Jun 2015 16:37:47 +0200 Subject: [PATCH] launch_daemon: Moved (Main)Worker to its own file. --- src/servers/launch/Jamfile | 1 + src/servers/launch/LaunchDaemon.cpp | 138 +--------------------------- src/servers/launch/Worker.cpp | 106 +++++++++++++++++++++ src/servers/launch/Worker.h | 49 ++++++++++ 4 files changed, 158 insertions(+), 136 deletions(-) create mode 100644 src/servers/launch/Worker.cpp create mode 100644 src/servers/launch/Worker.h diff --git a/src/servers/launch/Jamfile b/src/servers/launch/Jamfile index 0ba38ee892..e0fd5a1c42 100644 --- a/src/servers/launch/Jamfile +++ b/src/servers/launch/Jamfile @@ -6,6 +6,7 @@ UsePrivateSystemHeaders ; Server launch_daemon : LaunchDaemon.cpp + Worker.cpp # init jobs AbstractEmptyDirectoryJob.cpp diff --git a/src/servers/launch/LaunchDaemon.cpp b/src/servers/launch/LaunchDaemon.cpp index 56b6c3080d..27c8ec0ef3 100644 --- a/src/servers/launch/LaunchDaemon.cpp +++ b/src/servers/launch/LaunchDaemon.cpp @@ -21,16 +21,16 @@ #include #include -#include #include #include #include "InitRealTimeClockJob.h" #include "InitSharedMemoryDirectoryJob.h" #include "InitTemporaryDirectoryJob.h" +#include "Worker.h" -using namespace BPrivate; +using namespace ::BPrivate; using namespace BSupportKit; using BSupportKit::BPrivate::JobQueue; @@ -139,38 +139,6 @@ protected: }; -class Worker { -public: - Worker(JobQueue& queue); - virtual ~Worker(); - -protected: - virtual status_t Process(); - virtual bigtime_t Timeout() const; - virtual status_t Run(BJob* job); - -private: - static status_t _Process(void* self); - -protected: - thread_id fThread; - JobQueue& fJobQueue; -}; - - -class MainWorker : public Worker { -public: - MainWorker(JobQueue& queue); - -protected: - virtual bigtime_t Timeout() const; - virtual status_t Run(BJob* job); - -private: - int32 fCPUCount; -}; - - typedef std::map JobMap; @@ -211,12 +179,6 @@ private: }; -static const bigtime_t kWorkerTimeout = 1000000; - // One second until a worker thread quits without a job - -static int32 sWorkerCount; - - static const char* get_leaf(const char* signature) { @@ -534,102 +496,6 @@ Target::Execute() // #pragma mark - -Worker::Worker(JobQueue& queue) - : - fJobQueue(queue) -{ - fThread = spawn_thread(&Worker::_Process, "worker", B_NORMAL_PRIORITY, - this); - if (fThread >= 0 && resume_thread(fThread) == B_OK) - atomic_add(&sWorkerCount, 1); -} - - -Worker::~Worker() -{ -} - - -status_t -Worker::Process() -{ - while (true) { - BJob* job; - status_t status = fJobQueue.Pop(Timeout(), false, &job); - if (status != B_OK) - return status; - - Run(job); - // TODO: proper error reporting on failed job! - } -} - - -bigtime_t -Worker::Timeout() const -{ - return kWorkerTimeout; -} - - -status_t -Worker::Run(BJob* job) -{ - return job->Run(); -} - - -/*static*/ status_t -Worker::_Process(void* _self) -{ - Worker* self = (Worker*)_self; - status_t status = self->Process(); - delete self; - - return status; -} - - -// #pragma mark - - - -MainWorker::MainWorker(JobQueue& queue) - : - Worker(queue) -{ - // TODO: keep track of workers, and quit them on destruction - system_info info; - if (get_system_info(&info) == B_OK) - fCPUCount = info.cpu_count; -} - - -bigtime_t -MainWorker::Timeout() const -{ - return B_INFINITE_TIMEOUT; -} - - -status_t -MainWorker::Run(BJob* job) -{ - int32 count = atomic_get(&sWorkerCount); - - size_t jobCount = fJobQueue.CountJobs(); - if (jobCount > INT_MAX) - jobCount = INT_MAX; - - if ((int32)jobCount > count && count < fCPUCount) - new Worker(fJobQueue); - - return Worker::Run(job); -} - - -// #pragma mark - - - LaunchDaemon::LaunchDaemon(status_t& error) : BServer(kLaunchDaemonSignature, NULL, diff --git a/src/servers/launch/Worker.cpp b/src/servers/launch/Worker.cpp new file mode 100644 index 0000000000..c21d8fb195 --- /dev/null +++ b/src/servers/launch/Worker.cpp @@ -0,0 +1,106 @@ +/* + * Copyright 2015, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ + + +#include "Worker.h" + + +static const bigtime_t kWorkerTimeout = 1000000; + // One second until a worker thread quits without a job + +static int32 sWorkerCount; + + +Worker::Worker(JobQueue& queue) + : + fJobQueue(queue) +{ + fThread = spawn_thread(&Worker::_Process, "worker", B_NORMAL_PRIORITY, + this); + if (fThread >= 0 && resume_thread(fThread) == B_OK) + atomic_add(&sWorkerCount, 1); +} + + +Worker::~Worker() +{ +} + + +status_t +Worker::Process() +{ + while (true) { + BJob* job; + status_t status = fJobQueue.Pop(Timeout(), false, &job); + if (status != B_OK) + return status; + + Run(job); + // TODO: proper error reporting on failed job! + } +} + + +bigtime_t +Worker::Timeout() const +{ + return kWorkerTimeout; +} + + +status_t +Worker::Run(BJob* job) +{ + return job->Run(); +} + + +/*static*/ status_t +Worker::_Process(void* _self) +{ + Worker* self = (Worker*)_self; + status_t status = self->Process(); + delete self; + + return status; +} + + +// #pragma mark - + + +MainWorker::MainWorker(JobQueue& queue) + : + Worker(queue) +{ + // TODO: keep track of workers, and quit them on destruction + system_info info; + if (get_system_info(&info) == B_OK) + fCPUCount = info.cpu_count; +} + + +bigtime_t +MainWorker::Timeout() const +{ + return B_INFINITE_TIMEOUT; +} + + +status_t +MainWorker::Run(BJob* job) +{ + int32 count = atomic_get(&sWorkerCount); + + size_t jobCount = fJobQueue.CountJobs(); + if (jobCount > INT_MAX) + jobCount = INT_MAX; + + if ((int32)jobCount > count && count < fCPUCount) + new Worker(fJobQueue); + + return Worker::Run(job); +} diff --git a/src/servers/launch/Worker.h b/src/servers/launch/Worker.h new file mode 100644 index 0000000000..bcc3e28aa4 --- /dev/null +++ b/src/servers/launch/Worker.h @@ -0,0 +1,49 @@ +/* + * Copyright 2015, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ +#ifndef WORKER_H +#define WORKER_H + + +#include +#include + + +using namespace BSupportKit; +using BSupportKit::BPrivate::JobQueue; + + +class Worker { +public: + Worker(JobQueue& queue); + virtual ~Worker(); + +protected: + virtual status_t Process(); + virtual bigtime_t Timeout() const; + virtual status_t Run(BJob* job); + +private: + static status_t _Process(void* self); + +protected: + thread_id fThread; + JobQueue& fJobQueue; +}; + + +class MainWorker : public Worker { +public: + MainWorker(JobQueue& queue); + +protected: + virtual bigtime_t Timeout() const; + virtual status_t Run(BJob* job); + +private: + int32 fCPUCount; +}; + + +#endif // WORKER_H