Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_UTILITY_INCLUDE_PROCESS_THREAD_H_ |
| 12 | #define MODULES_UTILITY_INCLUDE_PROCESS_THREAD_H_ |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 13 | |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 16 | #include "typedefs.h" // NOLINT(build/include) |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 17 | |
tommi | 435f98b | 2016-05-28 14:57:15 -0700 | [diff] [blame] | 18 | #if defined(WEBRTC_WIN) |
| 19 | // Due to a bug in the std::unique_ptr implementation that ships with MSVS, |
| 20 | // we need the full definition of QueuedTask, on Windows. |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/task_queue.h" |
tommi | 435f98b | 2016-05-28 14:57:15 -0700 | [diff] [blame] | 22 | #else |
| 23 | namespace rtc { |
| 24 | class QueuedTask; |
| 25 | } |
| 26 | #endif |
| 27 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 28 | namespace rtc { |
| 29 | class Location; |
| 30 | } |
| 31 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 32 | namespace webrtc { |
| 33 | class Module; |
| 34 | |
tommi | 435f98b | 2016-05-28 14:57:15 -0700 | [diff] [blame] | 35 | // TODO(tommi): ProcessThread probably doesn't need to be a virtual |
| 36 | // interface. There exists one override besides ProcessThreadImpl, |
| 37 | // MockProcessThread, but when looking at how it is used, it seems |
| 38 | // a nullptr might suffice (or simply an actual ProcessThread instance). |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 39 | class ProcessThread { |
| 40 | public: |
| 41 | virtual ~ProcessThread(); |
| 42 | |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 43 | static std::unique_ptr<ProcessThread> Create(const char* thread_name); |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 44 | |
| 45 | // Starts the worker thread. Must be called from the construction thread. |
| 46 | virtual void Start() = 0; |
| 47 | |
| 48 | // Stops the worker thread. Must be called from the construction thread. |
| 49 | virtual void Stop() = 0; |
| 50 | |
| 51 | // Wakes the thread up to give a module a chance to do processing right |
| 52 | // away. This causes the worker thread to wake up and requery the specified |
| 53 | // module for when it should be called back. (Typically the module should |
| 54 | // return 0 from TimeUntilNextProcess on the worker thread at that point). |
| 55 | // Can be called on any thread. |
| 56 | virtual void WakeUp(Module* module) = 0; |
| 57 | |
| 58 | // Queues a task object to run on the worker thread. Ownership of the |
| 59 | // task object is transferred to the ProcessThread and the object will |
| 60 | // either be deleted after running on the worker thread, or on the |
| 61 | // construction thread of the ProcessThread instance, if the task did not |
| 62 | // get a chance to run (e.g. posting the task while shutting down or when |
| 63 | // the thread never runs). |
tommi | 435f98b | 2016-05-28 14:57:15 -0700 | [diff] [blame] | 64 | virtual void PostTask(std::unique_ptr<rtc::QueuedTask> task) = 0; |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 65 | |
| 66 | // Adds a module that will start to receive callbacks on the worker thread. |
| 67 | // Can be called from any thread. |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 68 | virtual void RegisterModule(Module* module, const rtc::Location& from) = 0; |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 69 | |
| 70 | // Removes a previously registered module. |
| 71 | // Can be called from any thread. |
| 72 | virtual void DeRegisterModule(Module* module) = 0; |
| 73 | }; |
| 74 | |
| 75 | } // namespace webrtc |
| 76 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 77 | #endif // MODULES_UTILITY_INCLUDE_PROCESS_THREAD_H_ |