blob: 856a1c3e341c5ae76a4eff6a5f58cd016c4ca660 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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// System independant wrapper for spawning threads
12// Note: the spawned thread will loop over the callback function until stopped.
13// Note: The callback function is expected to return every 2 seconds or more
14// often.
15
16#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_
17#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_
18
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000019#include "webrtc/common_types.h"
20#include "webrtc/typedefs.h"
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000021
niklase@google.com470e71d2011-07-07 08:21:25 +000022namespace webrtc {
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000023
phoglund@webrtc.orgc63f7882011-10-24 13:20:09 +000024// Callback function that the spawned thread will enter once spawned.
25// A return value of false is interpreted as that the function has no
26// more work to do and that the thread can be released.
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000027typedef bool(*ThreadRunFunction)(void*);
niklase@google.com470e71d2011-07-07 08:21:25 +000028
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000029enum ThreadPriority {
30 kLowPriority = 1,
31 kNormalPriority = 2,
32 kHighPriority = 3,
33 kHighestPriority = 4,
34 kRealtimePriority = 5
niklase@google.com470e71d2011-07-07 08:21:25 +000035};
36
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000037// Represents a simple worker thread. The implementation must be assumed
38// to be single threaded, meaning that all methods of the class, must be
39// called from the same thread, including instantiation.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000040class ThreadWrapper {
41 public:
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000042 virtual ~ThreadWrapper() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000043
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000044 // Factory method. Constructor disabled.
45 //
46 // func Pointer to a, by user, specified callback function.
47 // obj Object associated with the thread. Passed in the callback
48 // function.
49 // prio Thread priority. May require root/admin rights.
50 // thread_name NULL terminated thread name, will be visable in the Windows
51 // debugger.
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000052 // TODO(tommi): Remove the priority argument and provide a setter instead.
53 // TODO(tommi): Make thread_name non-optional (i.e. no default value).
henrike@webrtc.orga3e6bec2013-01-18 16:39:21 +000054 static ThreadWrapper* CreateThread(ThreadRunFunction func,
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000055 void* obj,
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000056 ThreadPriority prio = kNormalPriority,
57 const char* thread_name = 0);
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000058
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000059 // Get the current thread's thread ID.
60 // NOTE: This is a static method. It returns the id of the calling thread,
61 // *not* the id of the worker thread that a ThreadWrapper instance represents.
62 // TODO(tommi): Move outside of the ThreadWrapper class to avoid confusion.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000063 static uint32_t GetThreadId();
niklase@google.com470e71d2011-07-07 08:21:25 +000064
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000065 // Tries to spawns a thread and returns true if that was successful.
66 // Additionally, it tries to set thread priority according to the priority
67 // from when CreateThread was called. However, failure to set priority will
68 // not result in a false return value.
pbos@webrtc.org86639732015-03-13 00:06:21 +000069 virtual bool Start() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000070
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000071 // Stops the spawned thread and waits for it to be reclaimed with a timeout
72 // of two seconds. Will return false if the thread was not reclaimed.
73 // Multiple tries to Stop are allowed (e.g. to wait longer than 2 seconds).
74 // It's ok to call Stop() even if the spawned thread has been reclaimed.
75 virtual bool Stop() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000076};
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000077
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000078} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000079
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000080#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_