libadblockplus
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
AsyncExecutor.h
1 /*
2  * This file is part of Adblock Plus <https://adblockplus.org/>,
3  * Copyright (C) 2006-present eyeo GmbH
4  *
5  * Adblock Plus is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 3 as
7  * published by the Free Software Foundation.
8  *
9  * Adblock Plus is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #pragma once
18 #include "ActiveObject.h"
19 
20 namespace AdblockPlus
21 {
27  {
28  // This class not only serializes access to the list of threads but also
29  // ensures that internals of std::thread are valid in another (collecting)
30  // thread. The latter is about sentries protecting A and B because
31  // otherwise it can happen that a worker thread has already finished the
32  // call, passed info to the collector thread and the collector is trying
33  // to get information from iterator (B) but the assignment (A) has not
34  // happened yet.
35  class SyncThreads
36  {
37  typedef std::list<std::thread> Threads;
38  public:
39  typedef Threads::iterator iterator;
40  void SpawnThread(std::function<void(iterator)>&& task);
41  std::thread TakeOut(iterator pos);
42  void WaitUtilEmpty();
43  protected:
44  Threads collection;
45  std::mutex mutex;
46  std::condition_variable conditionVar;
47  };
48  public:
53 
60  void Dispatch(const std::function<void()>& call);
61  private:
62  SyncThreads threads;
63  ActiveObject threadCollector;
64  };
65 
73  {
74  public:
81  : executor(new AsyncExecutor())
82  {
83  }
84 
91  void Dispatch(const std::function<void()>& call)
92  {
93  std::lock_guard<std::mutex> lock(asyncExecutorMutex);
94  if (!executor)
95  return;
96  executor->Dispatch(call);
97  }
98 
103  void Invalidate()
104  {
105  std::unique_ptr<AsyncExecutor> tmp;
106  {
107  std::lock_guard<std::mutex> lock(asyncExecutorMutex);
108  tmp = move(executor);
109  }
110  }
111  private:
112  std::mutex asyncExecutorMutex;
113  std::unique_ptr<AsyncExecutor> executor;
114  };
115 }
~AsyncExecutor()
Destructor, it waits for finishing of all already dispatched tasks.
This class provides the same interface as AsyncExecutor but allows in a thread-safe manner invalidate...
Definition: AsyncExecutor.h:72
void Dispatch(const std::function< void()> &call)
Creates a new thread in which the call will be executed.
void Invalidate()
Destroys internally held AsyncExecutor, any subsequent calls of Dispatch have no effect.
Definition: AsyncExecutor.h:103
OptionalAsyncExecutor()
Contructor.
Definition: AsyncExecutor.h:80
void Dispatch(const std::function< void()> &call)
Creates a new thread in which the call will be executed.
Definition: AsyncExecutor.h:91
Main namespace of libadblockplus.
Definition: ActiveObject.h:23
The implementation of active object pattern, simply put it sequentially executes posted callable obje...
Definition: ActiveObject.h:30
Spawns a new thread for each task and waits for finishing of all spawned threads in the destructor...
Definition: AsyncExecutor.h:26