blob: 53a2c0a738ec113d8098b02c74dcc29b0257c4c8 [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
niklase@google.com470e71d2011-07-07 08:21:25 +000024// Object that will be passed by the spawned thread when it enters the callback
25// function.
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000026// TODO(tommi): Remove this define.
niklase@google.com470e71d2011-07-07 08:21:25 +000027#define ThreadObj void*
28
phoglund@webrtc.orgc63f7882011-10-24 13:20:09 +000029// Callback function that the spawned thread will enter once spawned.
30// A return value of false is interpreted as that the function has no
31// more work to do and that the thread can be released.
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000032typedef bool(*ThreadRunFunction)(void*);
niklase@google.com470e71d2011-07-07 08:21:25 +000033
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000034enum ThreadPriority {
35 kLowPriority = 1,
36 kNormalPriority = 2,
37 kHighPriority = 3,
38 kHighestPriority = 4,
39 kRealtimePriority = 5
niklase@google.com470e71d2011-07-07 08:21:25 +000040};
41
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000042// Represents a simple worker thread. The implementation must be assumed
43// to be single threaded, meaning that all methods of the class, must be
44// called from the same thread, including instantiation.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000045class ThreadWrapper {
46 public:
47 enum {kThreadMaxNameLength = 64};
niklase@google.com470e71d2011-07-07 08:21:25 +000048
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000049 virtual ~ThreadWrapper() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000050
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000051 // Factory method. Constructor disabled.
52 //
53 // func Pointer to a, by user, specified callback function.
54 // obj Object associated with the thread. Passed in the callback
55 // function.
56 // prio Thread priority. May require root/admin rights.
57 // thread_name NULL terminated thread name, will be visable in the Windows
58 // debugger.
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000059 // TODO(tommi): Remove the priority argument and provide a setter instead.
60 // TODO(tommi): Make thread_name non-optional (i.e. no default value).
henrike@webrtc.orga3e6bec2013-01-18 16:39:21 +000061 static ThreadWrapper* CreateThread(ThreadRunFunction func,
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000062 void* obj,
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000063 ThreadPriority prio = kNormalPriority,
64 const char* thread_name = 0);
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000065
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000066 // Get the current thread's thread ID.
67 // NOTE: This is a static method. It returns the id of the calling thread,
68 // *not* the id of the worker thread that a ThreadWrapper instance represents.
69 // TODO(tommi): Move outside of the ThreadWrapper class to avoid confusion.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000070 static uint32_t GetThreadId();
niklase@google.com470e71d2011-07-07 08:21:25 +000071
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000072 // Tries to spawns a thread and returns true if that was successful.
73 // Additionally, it tries to set thread priority according to the priority
74 // from when CreateThread was called. However, failure to set priority will
75 // not result in a false return value.
tommi@webrtc.orgaef07792015-01-30 15:06:10 +000076 // TODO(tommi): Remove the id parameter.
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000077 virtual bool Start(unsigned int& id) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000078
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000079 // Stops the spawned thread and waits for it to be reclaimed with a timeout
80 // of two seconds. Will return false if the thread was not reclaimed.
81 // Multiple tries to Stop are allowed (e.g. to wait longer than 2 seconds).
82 // It's ok to call Stop() even if the spawned thread has been reclaimed.
83 virtual bool Stop() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000084};
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000085
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000086} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000087
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000088#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_