Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* RCThread.h */ |
michael@0 | 7 | |
michael@0 | 8 | #if defined(_RCTHREAD_H) |
michael@0 | 9 | #else |
michael@0 | 10 | #define _RCTHREAD_H |
michael@0 | 11 | |
michael@0 | 12 | #include "rcbase.h" |
michael@0 | 13 | |
michael@0 | 14 | #include <prthread.h> |
michael@0 | 15 | |
michael@0 | 16 | class RCInterval; |
michael@0 | 17 | |
michael@0 | 18 | class PR_IMPLEMENT(RCThreadPrivateData) |
michael@0 | 19 | { |
michael@0 | 20 | public: |
michael@0 | 21 | RCThreadPrivateData(); |
michael@0 | 22 | RCThreadPrivateData(const RCThreadPrivateData&); |
michael@0 | 23 | |
michael@0 | 24 | virtual ~RCThreadPrivateData(); |
michael@0 | 25 | |
michael@0 | 26 | virtual void Release() = 0; |
michael@0 | 27 | |
michael@0 | 28 | }; /* RCThreadPrivateData */ |
michael@0 | 29 | |
michael@0 | 30 | class PR_IMPLEMENT(RCThread): public RCBase |
michael@0 | 31 | { |
michael@0 | 32 | public: |
michael@0 | 33 | |
michael@0 | 34 | typedef enum |
michael@0 | 35 | { |
michael@0 | 36 | local = PR_LOCAL_THREAD, global = PR_GLOBAL_THREAD |
michael@0 | 37 | } Scope; |
michael@0 | 38 | |
michael@0 | 39 | typedef enum |
michael@0 | 40 | { |
michael@0 | 41 | joinable = PR_JOINABLE_THREAD, unjoinable = PR_UNJOINABLE_THREAD |
michael@0 | 42 | } State; |
michael@0 | 43 | |
michael@0 | 44 | typedef enum |
michael@0 | 45 | { |
michael@0 | 46 | first = PR_PRIORITY_FIRST, |
michael@0 | 47 | low = PR_PRIORITY_LOW, |
michael@0 | 48 | normal = PR_PRIORITY_NORMAL, |
michael@0 | 49 | high = PR_PRIORITY_HIGH, |
michael@0 | 50 | urgent = PR_PRIORITY_URGENT, |
michael@0 | 51 | last = PR_PRIORITY_LAST |
michael@0 | 52 | } Priority; |
michael@0 | 53 | |
michael@0 | 54 | /* |
michael@0 | 55 | * Create a new thread, providing scope and joinability state. |
michael@0 | 56 | */ |
michael@0 | 57 | RCThread(Scope scope, State state, PRUint32 stackSize=0); |
michael@0 | 58 | |
michael@0 | 59 | /* |
michael@0 | 60 | * New threads are created in a suspended state. It must be 'started" |
michael@0 | 61 | * before it begins execution in the class' defined 'RootFunction()'. |
michael@0 | 62 | */ |
michael@0 | 63 | virtual PRStatus Start(); |
michael@0 | 64 | |
michael@0 | 65 | /* |
michael@0 | 66 | * If a thread is created joinable, then the thread's object exists |
michael@0 | 67 | * until join is called. The thread that calls join will block until |
michael@0 | 68 | * the target thread returns from it's root function. |
michael@0 | 69 | */ |
michael@0 | 70 | virtual PRStatus Join(); |
michael@0 | 71 | |
michael@0 | 72 | /* |
michael@0 | 73 | * The priority of a newly created thread is the same as the creator. |
michael@0 | 74 | * The priority may be changed either by the new thread itself, by |
michael@0 | 75 | * the creator or any other arbitrary thread. |
michael@0 | 76 | */ |
michael@0 | 77 | virtual void SetPriority(Priority newPriority); |
michael@0 | 78 | |
michael@0 | 79 | |
michael@0 | 80 | /* |
michael@0 | 81 | * Interrupt another thread, causing it to stop what it |
michael@0 | 82 | * is doing and return with a well known error code. |
michael@0 | 83 | */ |
michael@0 | 84 | virtual PRStatus Interrupt(); |
michael@0 | 85 | |
michael@0 | 86 | /* |
michael@0 | 87 | * And in case a thread was interrupted and didn't get a chance |
michael@0 | 88 | * to have the notification delivered, a way to cancel the pending |
michael@0 | 89 | * status. |
michael@0 | 90 | */ |
michael@0 | 91 | static void ClearInterrupt(); |
michael@0 | 92 | |
michael@0 | 93 | /* |
michael@0 | 94 | * Methods to discover the attributes of an existing thread. |
michael@0 | 95 | */ |
michael@0 | 96 | static PRThread *Self(); |
michael@0 | 97 | Scope GetScope() const; |
michael@0 | 98 | State GetState() const; |
michael@0 | 99 | Priority GetPriority() const; |
michael@0 | 100 | |
michael@0 | 101 | /* |
michael@0 | 102 | * Thread private data |
michael@0 | 103 | */ |
michael@0 | 104 | static PRStatus NewPrivateIndex(PRUintn* index); |
michael@0 | 105 | |
michael@0 | 106 | /* |
michael@0 | 107 | * Getting it - if you want to modify, make a copy |
michael@0 | 108 | */ |
michael@0 | 109 | static RCThreadPrivateData* GetPrivateData(PRUintn index); |
michael@0 | 110 | |
michael@0 | 111 | /* |
michael@0 | 112 | * Setting it to <empty> - deletes existing data |
michael@0 | 113 | */ |
michael@0 | 114 | static PRStatus SetPrivateData(PRUintn index); |
michael@0 | 115 | |
michael@0 | 116 | /* |
michael@0 | 117 | * Setting it - runtime will make a copy, freeing old iff necessary |
michael@0 | 118 | */ |
michael@0 | 119 | static PRStatus SetPrivateData(PRUintn index, RCThreadPrivateData* data); |
michael@0 | 120 | |
michael@0 | 121 | /* |
michael@0 | 122 | * Scheduling control |
michael@0 | 123 | */ |
michael@0 | 124 | static PRStatus Sleep(const RCInterval& ticks); |
michael@0 | 125 | |
michael@0 | 126 | friend void nas_Root(void*); |
michael@0 | 127 | friend class RCPrimordialThread; |
michael@0 | 128 | protected: |
michael@0 | 129 | |
michael@0 | 130 | /* |
michael@0 | 131 | * The instantiator of a class must not call the destructor. The base |
michael@0 | 132 | * implementation of Join will, and if the thread is created unjoinable, |
michael@0 | 133 | * then the code that called the RootFunction will call the desctructor. |
michael@0 | 134 | */ |
michael@0 | 135 | virtual ~RCThread(); |
michael@0 | 136 | |
michael@0 | 137 | private: |
michael@0 | 138 | |
michael@0 | 139 | /* |
michael@0 | 140 | * This is where a newly created thread begins execution. Returning |
michael@0 | 141 | * from this function is equivalent to terminating the thread. |
michael@0 | 142 | */ |
michael@0 | 143 | virtual void RootFunction() = 0; |
michael@0 | 144 | |
michael@0 | 145 | PRThread *identity; |
michael@0 | 146 | |
michael@0 | 147 | /* Threads are unstarted until started - pretty startling */ |
michael@0 | 148 | enum {ex_unstarted, ex_started} execution; |
michael@0 | 149 | |
michael@0 | 150 | /* There is no public default constructor or copy constructor */ |
michael@0 | 151 | RCThread(); |
michael@0 | 152 | RCThread(const RCThread&); |
michael@0 | 153 | |
michael@0 | 154 | /* And there is no assignment operator */ |
michael@0 | 155 | void operator=(const RCThread&); |
michael@0 | 156 | |
michael@0 | 157 | public: |
michael@0 | 158 | static RCPrimordialThread *WrapPrimordialThread(); |
michael@0 | 159 | |
michael@0 | 160 | }; |
michael@0 | 161 | |
michael@0 | 162 | /* |
michael@0 | 163 | ** class RCPrimordialThread |
michael@0 | 164 | */ |
michael@0 | 165 | class PR_IMPLEMENT(RCPrimordialThread): public RCThread |
michael@0 | 166 | { |
michael@0 | 167 | public: |
michael@0 | 168 | /* |
michael@0 | 169 | ** The primordial thread can (optionally) wait for all created |
michael@0 | 170 | ** threads to terminate before allowing the process to exit. |
michael@0 | 171 | ** Not calling Cleanup() before returning from main() will cause |
michael@0 | 172 | ** the immediate termination of the entire process, including |
michael@0 | 173 | ** any running threads. |
michael@0 | 174 | */ |
michael@0 | 175 | static PRStatus Cleanup(); |
michael@0 | 176 | |
michael@0 | 177 | /* |
michael@0 | 178 | ** Only the primordial thread is allowed to adjust the number of |
michael@0 | 179 | ** virtual processors of the runtime. It's a lame security thing. |
michael@0 | 180 | */ |
michael@0 | 181 | static PRStatus SetVirtualProcessors(PRIntn count=10); |
michael@0 | 182 | |
michael@0 | 183 | friend class RCThread; |
michael@0 | 184 | private: |
michael@0 | 185 | /* |
michael@0 | 186 | ** None other than the runtime can create of destruct |
michael@0 | 187 | ** a primordial thread. It is fabricated by the runtime |
michael@0 | 188 | ** to wrap the thread that initiated the application. |
michael@0 | 189 | */ |
michael@0 | 190 | RCPrimordialThread(); |
michael@0 | 191 | ~RCPrimordialThread(); |
michael@0 | 192 | void RootFunction(); |
michael@0 | 193 | }; /* RCPrimordialThread */ |
michael@0 | 194 | |
michael@0 | 195 | #endif /* defined(_RCTHREAD_H) */ |