blob: edfc4bf52b6eefc5fa97242bf78df97e66272f31 [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
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000019#include "common_types.h"
20#include "typedefs.h"
21
niklase@google.com470e71d2011-07-07 08:21:25 +000022namespace webrtc {
23// Object that will be passed by the spawned thread when it enters the callback
24// function.
25#define ThreadObj void*
26
phoglund@webrtc.orgc63f7882011-10-24 13:20:09 +000027// Callback function that the spawned thread will enter once spawned.
28// A return value of false is interpreted as that the function has no
29// more work to do and that the thread can be released.
niklase@google.com470e71d2011-07-07 08:21:25 +000030typedef bool(*ThreadRunFunction)(ThreadObj);
31
32enum ThreadPriority
33{
34 kLowPriority = 1,
35 kNormalPriority = 2,
36 kHighPriority = 3,
37 kHighestPriority = 4,
38 kRealtimePriority = 5
39};
40
41class ThreadWrapper
42{
43public:
44 enum {kThreadMaxNameLength = 64};
45
46 virtual ~ThreadWrapper() {};
47
48 // Factory method. Constructor disabled.
49 //
50 // func Pointer to a, by user, specified callback function.
51 // obj Object associated with the thread. Passed in the callback
52 // function.
53 // prio Thread priority. May require root/admin rights.
54 // threadName NULL terminated thread name, will be visable in the Windows
55 // debugger.
56 static ThreadWrapper* CreateThread(ThreadRunFunction func = 0,
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000057 ThreadObj obj= 0,
58 ThreadPriority prio = kNormalPriority,
59 const char* threadName = 0);
60
61 // Get the current thread's kernel thread ID.
62 static uint32_t GetThreadId();
niklase@google.com470e71d2011-07-07 08:21:25 +000063
64 // Non blocking termination of the spawned thread. Note that it is not safe
65 // to delete this class until the spawned thread has been reclaimed.
66 virtual void SetNotAlive() = 0;
67
henrike@webrtc.org5ba44112012-10-05 14:36:54 +000068 // Tries to spawns a thread and returns true if that was successful.
69 // Additionally, it tries to set thread priority according to the priority
70 // from when CreateThread was called. However, failure to set priority will
71 // not result in a false return value.
72 // TODO(henrike): add a function for polling whether priority was set or
73 // not.
niklase@google.com470e71d2011-07-07 08:21:25 +000074 virtual bool Start(unsigned int& id) = 0;
75
76 // Sets the threads CPU affinity. CPUs are listed 0 - (number of CPUs - 1).
77 // The numbers in processorNumbers specify which CPUs are allowed to run the
78 // thread. processorNumbers should not contain any duplicates and elements
79 // should be lower than (number of CPUs - 1). amountOfProcessors should be
80 // equal to the number of processors listed in processorNumbers
81 virtual bool SetAffinity(const int* /*processorNumbers*/,
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000082 const unsigned int /*amountOfProcessors*/) {
83 return false;
84 }
niklase@google.com470e71d2011-07-07 08:21:25 +000085
86 // Stops the spawned thread and waits for it to be reclaimed with a timeout
87 // of two seconds. Will return false if the thread was not reclaimed.
88 // Multiple tries to Stop are allowed (e.g. to wait longer than 2 seconds).
89 // It's ok to call Stop() even if the spawned thread has been reclaimed.
90 virtual bool Stop() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000091};
92} // namespace webrtc
93
94#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_