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 | |
| 11 | #ifndef WEBRTC_MODULES_UTILITY_INCLUDE_PROCESS_THREAD_H_ |
| 12 | #define WEBRTC_MODULES_UTILITY_INCLUDE_PROCESS_THREAD_H_ |
| 13 | |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 16 | #include "webrtc/typedefs.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | class Module; |
| 20 | |
tommi | 6414551 | 2016-05-28 11:00:30 -0700 | [diff] [blame^] | 21 | class ProcessTask { |
| 22 | public: |
| 23 | ProcessTask() {} |
| 24 | virtual ~ProcessTask() {} |
| 25 | |
| 26 | virtual void Run() = 0; |
| 27 | }; |
| 28 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 29 | class ProcessThread { |
| 30 | public: |
| 31 | virtual ~ProcessThread(); |
| 32 | |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 33 | static std::unique_ptr<ProcessThread> Create(const char* thread_name); |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 34 | |
| 35 | // Starts the worker thread. Must be called from the construction thread. |
| 36 | virtual void Start() = 0; |
| 37 | |
| 38 | // Stops the worker thread. Must be called from the construction thread. |
| 39 | virtual void Stop() = 0; |
| 40 | |
| 41 | // Wakes the thread up to give a module a chance to do processing right |
| 42 | // away. This causes the worker thread to wake up and requery the specified |
| 43 | // module for when it should be called back. (Typically the module should |
| 44 | // return 0 from TimeUntilNextProcess on the worker thread at that point). |
| 45 | // Can be called on any thread. |
| 46 | virtual void WakeUp(Module* module) = 0; |
| 47 | |
| 48 | // Queues a task object to run on the worker thread. Ownership of the |
| 49 | // task object is transferred to the ProcessThread and the object will |
| 50 | // either be deleted after running on the worker thread, or on the |
| 51 | // construction thread of the ProcessThread instance, if the task did not |
| 52 | // get a chance to run (e.g. posting the task while shutting down or when |
| 53 | // the thread never runs). |
tommi | 6414551 | 2016-05-28 11:00:30 -0700 | [diff] [blame^] | 54 | virtual void PostTask(std::unique_ptr<ProcessTask> task) = 0; |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 55 | |
| 56 | // Adds a module that will start to receive callbacks on the worker thread. |
| 57 | // Can be called from any thread. |
| 58 | virtual void RegisterModule(Module* module) = 0; |
| 59 | |
| 60 | // Removes a previously registered module. |
| 61 | // Can be called from any thread. |
| 62 | virtual void DeRegisterModule(Module* module) = 0; |
| 63 | }; |
| 64 | |
| 65 | } // namespace webrtc |
| 66 | |
| 67 | #endif // WEBRTC_MODULES_UTILITY_INCLUDE_PROCESS_THREAD_H_ |