blob: 67ce866d18d61704dde985d04a379b7e5888e409 [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
tommi@webrtc.org361981f2015-03-19 14:44:18 +000019#include "webrtc/base/scoped_ptr.h"
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000020#include "webrtc/common_types.h"
21#include "webrtc/typedefs.h"
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000022
niklase@google.com470e71d2011-07-07 08:21:25 +000023namespace webrtc {
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000024
phoglund@webrtc.orgc63f7882011-10-24 13:20:09 +000025// Callback function that the spawned thread will enter once spawned.
26// A return value of false is interpreted as that the function has no
27// more work to do and that the thread can be released.
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000028typedef bool(*ThreadRunFunction)(void*);
niklase@google.com470e71d2011-07-07 08:21:25 +000029
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000030enum ThreadPriority {
31 kLowPriority = 1,
32 kNormalPriority = 2,
33 kHighPriority = 3,
34 kHighestPriority = 4,
35 kRealtimePriority = 5
niklase@google.com470e71d2011-07-07 08:21:25 +000036};
37
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000038// Represents a simple worker thread. The implementation must be assumed
39// to be single threaded, meaning that all methods of the class, must be
40// called from the same thread, including instantiation.
tommi@webrtc.org361981f2015-03-19 14:44:18 +000041// TODO(tommi): There's no need for this to be a virtual interface since there's
42// only ever a single implementation of it.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000043class ThreadWrapper {
44 public:
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000045 virtual ~ThreadWrapper() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000046
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000047 // Factory method. Constructor disabled.
48 //
49 // func Pointer to a, by user, specified callback function.
50 // obj Object associated with the thread. Passed in the callback
51 // function.
52 // prio Thread priority. May require root/admin rights.
53 // thread_name NULL terminated thread name, will be visable in the Windows
54 // debugger.
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000055 // TODO(tommi): Remove the priority argument and provide a setter instead.
56 // TODO(tommi): Make thread_name non-optional (i.e. no default value).
tommi@webrtc.org361981f2015-03-19 14:44:18 +000057 static rtc::scoped_ptr<ThreadWrapper> CreateThread(ThreadRunFunction func,
58 void* obj, ThreadPriority prio = kNormalPriority,
59 const char* thread_name = 0);
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000060
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000061 // Get the current thread's thread ID.
62 // NOTE: This is a static method. It returns the id of the calling thread,
63 // *not* the id of the worker thread that a ThreadWrapper instance represents.
64 // TODO(tommi): Move outside of the ThreadWrapper class to avoid confusion.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000065 static uint32_t GetThreadId();
niklase@google.com470e71d2011-07-07 08:21:25 +000066
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000067 // Tries to spawns a thread and returns true if that was successful.
68 // Additionally, it tries to set thread priority according to the priority
69 // from when CreateThread was called. However, failure to set priority will
70 // not result in a false return value.
pbos@webrtc.org86639732015-03-13 00:06:21 +000071 virtual bool Start() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000072
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000073 // Stops the spawned thread and waits for it to be reclaimed with a timeout
74 // of two seconds. Will return false if the thread was not reclaimed.
75 // Multiple tries to Stop are allowed (e.g. to wait longer than 2 seconds).
76 // It's ok to call Stop() even if the spawned thread has been reclaimed.
77 virtual bool Stop() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000078};
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000079
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000080} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000081
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000082#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_