载入中...
搜索中...
未找到
CrowdThread.cpp
浏览该文件的文档.
2#include <exception>
3#include <vector>
5#include "thread/JobSystem.h"
6
8namespace {
9Result<void> failed(const char* message) {
11}
12class JobScheduler final : public eve::crowd::CrowdWorkScheduler {
13public:
14 explicit JobScheduler(std::weak_ptr<eve::thread::JobSystem> jobs) : jobs_(std::move(jobs)) {}
15 Result<void> execute(int partitions, const std::function<void(int)>& body) override try {
16 if (partitions < 0 || !body) return failed("Invalid Crowd partition dispatch");
17 auto owner = jobs_.lock();
18 if (!owner || !owner->isRunning()) return failed("Crowd JobSystem provider expired or stopped");
19 if (partitions == 0) return Result<void>::success();
20 // JobSystem's join does not propagate child failure: record exceptions in partition-owned slots.
21 std::vector<std::exception_ptr> errors(std::size_t(partitions), nullptr);
22 std::unique_ptr<eve::thread::Job> job(owner->parallelFor(
23 0, partitions,
24 [&](int first, int last) {
25 for (int index = first; index < last; ++index) {
26 try {
27 body(index);
28 } catch (...) {
29 errors[std::size_t(index)] = std::current_exception();
30 }
31 }
32 },
33 1));
34 // This freshly submitted join cannot wait on itself, the documented wait exception condition.
35 job->wait();
36 if (job->hasFailed()) return failed("Crowd scheduler join failed");
37 for (const auto& error : errors)
38 if (error) return failed("Crowd partition body failed after join");
39 return Result<void>::success();
40 } catch (const std::exception& error) {
41 return failed(error.what());
42 } catch (...) {
43 return failed("Crowd scheduler dispatch failed");
44 }
45
46private:
47 std::weak_ptr<eve::thread::JobSystem> jobs_;
48};
49} // namespace
51 std::weak_ptr<eve::thread::JobSystem> jobs) try {
53 std::make_shared<JobScheduler>(std::move(jobs)));
54} catch (const std::exception& error) {
56 Diagnostic::error(DiagnosticCode::Failed, error.what(), "crowd.scheduler"));
57}
58} // namespace eve::crowd_thread
std::string message
std::int32_t first
std::shared_ptr< OceanGpuSpectrum > owner
std::string error
Definition Package.cpp:174
std::string body
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Synchronous scheduler for fixed logical partitions, independent of worker identity....
Result< std::shared_ptr< eve::crowd::CrowdWorkScheduler > > makeJobSystemCrowdScheduler(std::weak_ptr< eve::thread::JobSystem > jobs)
Create a synchronous Crowd scheduler observing a genuinely shared JobSystem owner.