1 /******************************************************************************* 2 * Copyright (C) 2004-2008 Intel Corp. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * - Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * 10 * - Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * - Neither the name of Intel Corp. nor the names of its 15 * contributors may be used to endorse or promote products derived from this 16 * software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 *******************************************************************************/ 30 31 ////////////////////////////////////////////////////////////////////////// 32 // Thread.h 33 // 34 // This file contains an OS independent interface for thread manipulation 35 // A Thread class is defined for easy usage. 36 // 37 // Usage: 38 // 39 // Option 1: Construct an instance of the "Thread" class with an external 40 // callback function. When calling the "start" method, the thread will be 41 // started on the callback function 42 // 43 // Option 2: Subclass the "Thread" class and reimplement the virtual "run" 44 // method. When calling the "start" method, the thread will be started 45 // on the "run" function. 46 // 47 // Implementation overview: 48 // Calling the "start" method will start the new thread, which will call the 49 // "run" method. The default implementation of the "run" method will call 50 // the Callback function given in the constructor. 51 // 52 ////////////////////////////////////////////////////////////////////////// 53 #ifndef _LAD_THREAD_H 54 #define _LAD_THREAD_H 55 56 #ifndef WAIT_INFINITE 57 #define WAIT_INFINITE 0xffffffff 58 #endif 59 #ifndef NULL 60 #define NULL 0 61 #endif 62 63 typedef void (*CallbackFunction) (void *); 64 65 class OSThread; 66 67 class Thread 68 { 69 friend class OSThread; 70 71 public: 72 Thread(CallbackFunction func = NULL, void *param = NULL); 73 Thread(const Thread &rhs); 74 virtual ~Thread(); 75 76 // wait for the thread to complete; return true if the thread completed, 77 // false on timeout 78 bool wait(unsigned long msecs = WAIT_INFINITE) const; 79 // start the new thread, return true on success 80 bool start(); 81 // true if the thread is in running state 82 bool running() const; 83 // measure the time (in msecs) from thread start-time 84 long elapsedTime() const; 85 86 // return ID for the current thread 87 static unsigned long currentThread(); 88 // put the current thread to sleep 89 static void msleep(long msecs); 90 91 Thread &operator=(const Thread &rhs); 92 93 protected: 94 virtual void run(); 95 96 private: 97 CallbackFunction _func; 98 void *_param; 99 long _startTime; 100 OSThread *_osThread; 101 }; 102 103 #endif //_LAD_THREAD_H 104 105