1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef _VOS_THREAD_HXX_ 25 #define _VOS_THREAD_HXX_ 26 27 # include <vos/types.hxx> 28 # include <vos/object.hxx> 29 # include <osl/thread.h> 30 # include <osl/conditn.h> 31 # include <vos/runnable.hxx> 32 33 #include <osl/time.h> 34 35 namespace vos 36 { 37 38 extern "C" typedef void ThreadWorkerFunction_impl(void *); 39 ThreadWorkerFunction_impl threadWorkerFunction_impl; 40 41 /** OThread is an objectoriented interface for threads. 42 This class should be the base class for all objects using threads. The 43 main working function is the run() method and should be overriden in the 44 derived class. To support soft termination of a thread, yield() should 45 be called in regular intervalls and the return value should be checked. 46 If yield returned False the run method should return. 47 48 @author Bernd Hofner 49 @version 1.0 50 */ 51 52 class OThread : public vos::IRunnable, 53 public vos::OObject 54 { 55 56 VOS_DECLARE_CLASSINFO(VOS_NAMESPACE(OThread, vos)); 57 58 oslCondition m_aCondition; 59 60 public: 61 /** priority of thread. 62 */ 63 enum TThreadPriority 64 { 65 TPriority_Highest = osl_Thread_PriorityHighest, 66 TPriority_AboveNormal = osl_Thread_PriorityAboveNormal, 67 TPriority_Normal = osl_Thread_PriorityNormal, 68 TPriority_BelowNormal = osl_Thread_PriorityBelowNormal, 69 TPriority_Lowest = osl_Thread_PriorityLowest, 70 TPriority_Unknown = osl_Thread_PriorityUnknown 71 }; 72 73 /** 74 */ 75 enum TThreadSleep 76 { 77 TSleep_Normal, 78 TSleep_Cancel, 79 TSleep_Pending, 80 TSleep_Active, 81 TSleep_Error, 82 TSleep_Unknown 83 }; 84 85 typedef oslThreadIdentifier TThreadIdentifier; 86 87 /// Constructor 88 OThread(); 89 90 /// Destructor kills thread if neccessary 91 virtual ~OThread(); 92 93 /** Create running instance of a thread. 94 @returns True if thread could be created. 95 */ 96 sal_Bool SAL_CALL create(); 97 98 /** Create suspended instance of a thread. 99 @returns True if thread could be created. 100 */ 101 sal_Bool SAL_CALL createSuspended(); 102 103 /// Suspend a runnng thread 104 void SAL_CALL suspend(); 105 106 /// Resume a suspended thread 107 void SAL_CALL resume(); 108 109 /** Tries to kill the thread. 110 will not block and might not succeed when run() won't heed isTerminationRequested(). 111 */ 112 virtual void SAL_CALL terminate(); 113 114 /// Kill thread hard and block until it is actually gone 115 virtual void SAL_CALL kill(); 116 117 /// Block till thread is terminated 118 void SAL_CALL join(); 119 120 /// Check if thread is running. 121 sal_Bool SAL_CALL isRunning(); 122 123 /** Change thread priority. 124 The valid priority levels are: 125 <ul> 126 <li>ThreadPriorityHighest, 127 <li>ThreadPriorityAboveNormal, 128 <li>ThreadPriorityNormal, 129 <li>ThreadPriorityBelowNormal, 130 <li>ThreadPriorityLowest, 131 </ul> 132 */ 133 void SAL_CALL setPriority(TThreadPriority Priority); 134 135 /** Query thread priority. 136 Valid return values are: 137 <ul> 138 <li>ThreadPriorityHighest, 139 <li>ThreadPriorityAboveNormal, 140 <li>ThreadPriorityNormal, 141 <li>ThreadPriorityBelowNormal, 142 <li>ThreadPriorityLowest, 143 <li>ThreadPriorityUnknown (returned if thread is killed) 144 </ul> 145 */ 146 TThreadPriority SAL_CALL getPriority(); 147 148 TThreadIdentifier SAL_CALL getIdentifier() const; 149 150 static TThreadIdentifier SAL_CALL getCurrentIdentifier(); 151 152 /** Let thread sleep a specified amout of time. 153 @param Delay specifies the number of time to sleep. 154 */ 155 TThreadSleep SAL_CALL sleep(const TimeValue& Delay); 156 157 /** Awake the sleeping thread. 158 @returns False if at least one of the handles is invalid 159 or the thread is not sleeping. 160 */ 161 sal_Bool SAL_CALL awake(); 162 163 /** Let current thread wait a specified amout of time. 164 @param Delay specifies the number of time 165 to wait. Note, if you need to interrupt the waiting operation 166 use sleep instead. 167 */ 168 static void SAL_CALL wait(const TimeValue& Delay); 169 170 /** Reschedules threads. 171 Call within your loop if you 172 want other threads offer some processing time. 173 This method is static, so it might be used by the 174 main-thread. 175 */ 176 static void SAL_CALL yield(); 177 178 protected: 179 180 /// Working method which should be overridden. 181 virtual void SAL_CALL run() = 0; 182 183 /** Checks if thread should terminate. 184 isTerminationRequested() will return True if someone called 185 terminate(). 186 @return True if thread should terminate, False if he can continue. 187 */ 188 virtual sal_Bool SAL_CALL schedule(); 189 190 /** Called when run() is done. 191 You might want to override it to do some cleanup. 192 */ 193 virtual void SAL_CALL onTerminated(); 194 195 protected: 196 oslThread m_hThread; 197 sal_Bool m_bTerminating; 198 199 friend void threadWorkerFunction_impl(void *); 200 }; 201 202 class OThreadData : public vos::OObject 203 { 204 VOS_DECLARE_CLASSINFO(VOS_NAMESPACE(OThreadData, vos)); 205 206 public: 207 /// Create a thread specific local data key 208 OThreadData( oslThreadKeyCallbackFunction = 0 ); 209 210 /// Destroy a thread specific local data key 211 virtual ~OThreadData(); 212 213 /** Set the data associated with the data key. 214 @returns True if operation was successfull 215 */ 216 sal_Bool SAL_CALL setData(void *pData); 217 218 /** Get the data associated with the data key. 219 @returns The data asscoitaed with the data key or 220 NULL if no data was set 221 */ 222 void* SAL_CALL getData(); 223 224 protected: 225 oslThreadKey m_hKey; 226 }; 227 228 } 229 230 #endif // _VOS_THREAD_HXX_ 231 232