| 1 | /* |
|---|---|
| 2 | * Copyright 2010-2017 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 | #include <aws/core/utils/threading/ThreadTask.h> |
| 17 | #include <aws/core/utils/threading/Executor.h> |
| 18 | |
| 19 | using namespace Aws::Utils; |
| 20 | using namespace Aws::Utils::Threading; |
| 21 | |
| 22 | ThreadTask::ThreadTask(PooledThreadExecutor& executor) : m_continue(true), m_executor(executor), m_thread(std::bind(&ThreadTask::MainTaskRunner, this)) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | ThreadTask::~ThreadTask() |
| 27 | { |
| 28 | StopProcessingWork(); |
| 29 | m_thread.join(); |
| 30 | } |
| 31 | |
| 32 | void ThreadTask::MainTaskRunner() |
| 33 | { |
| 34 | while (m_continue) |
| 35 | { |
| 36 | while (m_continue && m_executor.HasTasks()) |
| 37 | { |
| 38 | auto fn = m_executor.PopTask(); |
| 39 | if(fn) |
| 40 | { |
| 41 | (*fn)(); |
| 42 | Aws::Delete(fn); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if(m_continue) |
| 47 | { |
| 48 | m_executor.m_sync.WaitOne(); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void ThreadTask::StopProcessingWork() |
| 54 | { |
| 55 | m_continue = false; |
| 56 | } |
| 57 |