AWS SDK for C++  0.14.3
AWS SDK for C++
Executor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <aws/core/Core_EXPORTS.h>
22 #include <functional>
23 #include <mutex>
24 #include <condition_variable>
25 
26 namespace Aws
27 {
28  namespace Utils
29  {
30  namespace Threading
31  {
32  class ThreadTask;
33 
39  {
40  public:
41  virtual ~Executor() = default;
42 
46  template<class Fn, class ... Args>
47  bool Submit(Fn&& fn, Args&& ... args)
48  {
49  return SubmitToThread(AWS_BUILD_TYPED_FUNCTION(std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...), void()));
50  }
51 
52 
53  protected:
57  virtual bool SubmitToThread(std::function<void()>&&) = 0;
58  };
59 
60 
65  {
66  public:
68  protected:
69  bool SubmitToThread(std::function<void()>&&) override;
70  };
71 
72  enum class OverflowPolicy
73  {
76  };
77 
82  {
83  public:
86 
92  PooledThreadExecutor& operator =(const PooledThreadExecutor&) = delete;
94  PooledThreadExecutor& operator =(PooledThreadExecutor&&) = delete;
95 
96  protected:
97  bool SubmitToThread(std::function<void()>&&) override;
98 
99  private:
101  std::mutex m_queueLock;
102  std::mutex m_syncPointLock;
103  std::condition_variable m_syncPoint;
104  Aws::Vector<ThreadTask*> m_threadTaskHandles;
105  size_t m_poolSize;
106  OverflowPolicy m_overflowPolicy;
107 
111  std::function<void()>* PopTask();
112  bool HasTasks();
113 
114  friend class ThreadTask;
115  };
116 
117 
118  } // namespace Threading
119  } // namespace Utils
120 } // namespace Aws
std::vector< T, Aws::Allocator< T > > Vector
Definition: AWSVector.h:27
bool Submit(Fn &&fn, Args &&...args)
Definition: Executor.h:47
std::queue< T, Deque< T > > Queue
Definition: AWSQueue.h:29
#define AWS_BUILD_TYPED_FUNCTION(func, type)
Definition: AWSFunction.h:80
#define AWS_CORE_API
Definition: Core_EXPORTS.h:35
JSON (JavaScript Object Notation).