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