AsyncTaskRunnerPool.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
10 #include <Bentley/Tasks/Tasks.h>
13 
15 
16 /*--------------------------------------------------------------------------------------+
17 * @bsiclass Bentley Systems
18 +---------------+---------------+---------------+---------------+---------------+------*/
19 struct AsyncTaskRunnerPool : public ITaskScheduler, public std::enable_shared_from_this<AsyncTaskRunnerPool>
20  {
21  private:
22  friend struct PoolTaskScheduler;
23 
24  private:
25  struct NotifyingTaskScheduler : TaskScheduler
26  {
27  private:
28  std::weak_ptr<AsyncTaskRunnerPool> m_poolPtr;
29 
30  protected:
31  void virtual _OnEmpty () override
32  {
33  if (auto pool = m_poolPtr.lock ())
34  {
35  pool->OnSchedulerEmpty ();
36  }
37  };
38 
39  public:
40  NotifyingTaskScheduler (std::shared_ptr<AsyncTaskRunnerPool> pool) : m_poolPtr (pool) {}
41 
42  virtual ~NotifyingTaskScheduler () { }
43  };
44 
45  private:
46  bool m_initializeFlag;
47  BeMutex m_mutex;
48 
49  protected:
52  bool m_started;
53 
54  bvector<std::shared_ptr<ITaskRunner>> m_runners;
55  std::shared_ptr<IAsyncTaskRunnerFactory> m_runnerFactory;
56 
57  std::shared_ptr<ITaskScheduler> m_taskScheduler;
58 
59  std::shared_ptr<ITaskScheduler> m_defaultWorkerThreadPool;
60 
61  std::shared_ptr<AsyncTaskRunnerPool> m_thisPtr;
63 
64  protected:
65  BENTLEYDLL_EXPORT void Start ();
66  BENTLEYDLL_EXPORT void Stop ();
67 
68  BENTLEYDLL_EXPORT void OnBeforeTaskPushed ();
69  BENTLEYDLL_EXPORT void OnAfterTaskPushed ();
70 
71  BENTLEYDLL_EXPORT void OnSchedulerEmpty ();
72 
73  public:
75  (
76  int runnerCount = 1,
77  Utf8CP name = nullptr,
78  std::shared_ptr<IAsyncTaskRunnerFactory> runnerFactory = nullptr
79  );
80 
82 
83  // -----------------------------------------------------------------------------------------------------
84  // ITaskScheduler Implementation
85 
86 
87  BENTLEYDLL_EXPORT virtual void Push (std::shared_ptr<AsyncTask> task, AsyncTask::Priority priority = AsyncTask::Priority::Inherited) override;
88 
90  BENTLEYDLL_EXPORT virtual void Push (std::shared_ptr<AsyncTask> task, std::shared_ptr<AsyncTask> parentTask, AsyncTask::Priority priority = AsyncTask::Priority::Inherited) override;
91 
92  BENTLEYDLL_EXPORT virtual std::shared_ptr<AsyncTask> WaitAndPop () override;
93 
94  BENTLEYDLL_EXPORT virtual std::shared_ptr<AsyncTask> TryPop () override;
95 
96  BENTLEYDLL_EXPORT virtual int GetQueueTaskCount () const override;
97 
98  BENTLEYDLL_EXPORT virtual bool HasRunningTasks () const override;
99 
100  BENTLEYDLL_EXPORT virtual AsyncTaskPtr<void> OnEmpty () const override;
101  };
102 
Contains a UTF-8 encoded string.
Definition: WString.h:275
#define BENTLEYDLL_EXPORT
Definition: Bentley.h:249
Utf8String m_name
Definition: AsyncTaskRunnerPool.h:51
BeMutex m_thisPtrMutex
Definition: AsyncTaskRunnerPool.h:62
bvector< std::shared_ptr< ITaskRunner > > m_runners
Definition: AsyncTaskRunnerPool.h:54
int m_runnerCount
Definition: AsyncTaskRunnerPool.h:50
std::shared_ptr< AsyncTaskRunnerPool > m_thisPtr
Definition: AsyncTaskRunnerPool.h:61
std::shared_ptr< ITaskScheduler > m_taskScheduler
Definition: AsyncTaskRunnerPool.h:57
Curve's start point.
#define BEGIN_BENTLEY_TASKS_NAMESPACE
Definition: Tasks.h:13
Priority
Definition: AsyncTask.h:49
A synchronization primitive that can be used to protect shared data from being simultaneously accesse...
Definition: BeThread.h:57
Definition: TaskScheduler.h:21
std::shared_ptr< IAsyncTaskRunnerFactory > m_runnerFactory
Definition: AsyncTaskRunnerPool.h:55
Definition: AsyncTaskRunnerPool.h:19
Utf8Char const * Utf8CP
Definition: Bentley.h:229
std::shared_ptr< PackagedAsyncTask< T >> AsyncTaskPtr
Definition: AsyncTask.h:282
#define END_BENTLEY_TASKS_NAMESPACE
Definition: Tasks.h:14
Definition: TaskScheduler.h:77
bool m_started
Definition: AsyncTaskRunnerPool.h:52
std::shared_ptr< ITaskScheduler > m_defaultWorkerThreadPool
Definition: AsyncTaskRunnerPool.h:59

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.