blob: 529138de42ae935dbf8aac12171ede735f37dbbe [file] [log] [blame]
perkj@google.comef04cf42011-09-02 09:47:28 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
perkj@google.comef04cf42011-09-02 09:47:28 +00003 *
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 MODULES_INTERFACE_MODULE_H_
12#define MODULES_INTERFACE_MODULE_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
pbos@webrtc.org57eb8582013-11-11 10:20:27 +000014#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
perkj@google.comef04cf42011-09-02 09:47:28 +000016namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000017
tommi@webrtc.org3985f012015-02-27 13:36:34 +000018class ProcessThread;
19
perkj@google.comef04cf42011-09-02 09:47:28 +000020class Module {
21 public:
tommi@webrtc.org41617152015-01-29 12:12:49 +000022 // Returns the number of milliseconds until the module wants a worker
perkj@google.comef04cf42011-09-02 09:47:28 +000023 // thread to call Process.
tommi@webrtc.org41617152015-01-29 12:12:49 +000024 // This method is called on the same worker thread as Process will
25 // be called on.
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000026 // TODO(tommi): Almost all implementations of this function, need to know
27 // the current tick count. Consider passing it as an argument. It could
28 // also improve the accuracy of when the next callback occurs since the
29 // thread that calls Process() will also have it's tick count reference
30 // which might not match with what the implementations use.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000031 virtual int64_t TimeUntilNextProcess() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000032
perkj@google.comef04cf42011-09-02 09:47:28 +000033 // Process any pending tasks such as timeouts.
tommi@webrtc.org41617152015-01-29 12:12:49 +000034 // Called on a worker thread.
perkj@google.comef04cf42011-09-02 09:47:28 +000035 virtual int32_t Process() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000036
tommi@webrtc.org3985f012015-02-27 13:36:34 +000037 // This method is called when the module is attached to a *running* process
38 // thread or detached from one. In the case of detaching, |process_thread|
39 // will be nullptr.
40 //
41 // This method will be called in the following cases:
42 //
43 // * Non-null process_thread:
44 // * ProcessThread::RegisterModule() is called while the thread is running.
45 // * ProcessThread::Start() is called and RegisterModule has previously
46 // been called. The thread will be started immediately after notifying
47 // all modules.
48 //
49 // * Null process_thread:
50 // * ProcessThread::DeRegisterModule() is called while the thread is
51 // running.
52 // * ProcessThread::Stop() was called and the thread has been stopped.
53 //
54 // NOTE: This method is not called from the worker thread itself, but from
55 // the thread that registers/deregisters the module or calls Start/Stop.
56 virtual void ProcessThreadAttached(ProcessThread* process_thread) {}
57
perkj@google.comef04cf42011-09-02 09:47:28 +000058 protected:
59 virtual ~Module() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000060};
61
tommi@webrtc.org41617152015-01-29 12:12:49 +000062// Reference counted version of the Module interface.
perkj@google.comef04cf42011-09-02 09:47:28 +000063class RefCountedModule : public Module {
64 public:
65 // Increase the reference count by one.
66 // Returns the incremented reference count.
tommi@webrtc.org41617152015-01-29 12:12:49 +000067 virtual int32_t AddRef() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000068
perkj@google.comef04cf42011-09-02 09:47:28 +000069 // Decrease the reference count by one.
70 // Returns the decreased reference count.
71 // Returns 0 if the last reference was just released.
tommi@webrtc.org41617152015-01-29 12:12:49 +000072 // When the reference count reaches 0 the object will self-destruct.
73 virtual int32_t Release() = 0;
perkj@google.comef04cf42011-09-02 09:47:28 +000074
75 protected:
Karl Wiberg2519c452015-04-07 16:12:57 +020076 ~RefCountedModule() override = default;
perkj@google.comef04cf42011-09-02 09:47:28 +000077};
78
79} // namespace webrtc
80
81#endif // MODULES_INTERFACE_MODULE_H_