reSIProcate/rutil  9694
ThreadIf.hxx
Go to the documentation of this file.
00001 #if !defined(RESIP_THREADIF_HXX)
00002 #define RESIP_THREADIF_HXX
00003 
00004 #include "rutil/Socket.hxx"
00005 
00006 #ifdef WIN32
00007 #  include <BaseTsd.h>
00008 #  include <winbase.h>
00009 #  include <map>
00010 #else
00011 #  include <pthread.h>
00012 #endif
00013 
00014 #include "rutil/Mutex.hxx"
00015 #include "rutil/Condition.hxx"
00016 
00017 namespace resip
00018 {
00019 
00044 class ThreadIf
00045 {
00046    public:
00047       ThreadIf();
00048       virtual ~ThreadIf();
00049 
00050       // runs the code in thread() .  Returns immediately
00051       virtual void run();
00052 
00053       // joins to the thread running thread()
00054       void join();
00055 
00056       // guarantees resources consumed by thread are released when thread terminates
00057       // after this join can no-longer be used
00058       void detach();
00059 
00060       // request the thread running thread() to return, by setting  mShutdown
00061       virtual void shutdown();
00062 
00063       //waits for waitMs, or stops waiting and returns true if shutdown was
00064       //called
00065       virtual bool waitForShutdown(int ms) const;
00066 
00067       // returns true if the thread has been asked to shutdown or not running
00068       bool isShutdown() const;
00069 
00070 #ifdef WIN32
00071       typedef DWORD Id;
00072 #else
00073       typedef pthread_t Id;
00074 #endif
00075       static Id selfId();
00076 
00077 #ifdef WIN32
00078       typedef DWORD TlsKey;
00079 #else
00080       typedef pthread_key_t TlsKey;
00081 #endif
00082       typedef void TlsDestructor(void*);
00083 
00085       static int tlsKeyCreate(TlsKey &key, TlsDestructor *destructor);
00087       static int tlsKeyDelete(TlsKey key);
00089       static int tlsSetValue(TlsKey key, const void *val);
00091       static void *tlsGetValue(TlsKey key);
00092 
00093 
00094       /* thread is a virtual method.  Users should derive and define
00095         thread() such that it returns when isShutdown() is true.
00096       */
00097       virtual void thread() = 0;
00098 
00099    protected:
00100 #ifdef WIN32
00101       HANDLE mThread;
00102       typedef std::map<DWORD,TlsDestructor *> TlsDestructorMap;
00103    public:
00105       static void tlsDestroyAll();
00106    protected:
00108       static TlsDestructorMap *mTlsDestructors;
00110       static Mutex *mTlsDestructorsMutex;
00111       friend class TlsDestructorInitializer;
00112 #endif
00113       Id mId;
00114 
00115       bool mShutdown;
00116       mutable Mutex mShutdownMutex;
00117       mutable Condition mShutdownCondition;
00118 
00119    private:
00120       // Suppress copying
00121       ThreadIf(const ThreadIf &);
00122       const ThreadIf & operator=(const ThreadIf &);
00123 };
00124 
00125 #ifdef WIN32
00126 
00127 class TlsDestructorInitializer {
00128 public:
00129    TlsDestructorInitializer();
00130    ~TlsDestructorInitializer();
00131 protected:
00132    static unsigned int mInstanceCounter;
00133 };
00134 static TlsDestructorInitializer _staticTlsInit;
00135 #endif
00136 
00137 }
00138 
00139 #endif
00140 
00141 /* ====================================================================
00142  * The Vovida Software License, Version 1.0
00143  *
00144  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00145  *
00146  * Redistribution and use in source and binary forms, with or without
00147  * modification, are permitted provided that the following conditions
00148  * are met:
00149  *
00150  * 1. Redistributions of source code must retain the above copyright
00151  *    notice, this list of conditions and the following disclaimer.
00152  *
00153  * 2. Redistributions in binary form must reproduce the above copyright
00154  *    notice, this list of conditions and the following disclaimer in
00155  *    the documentation and/or other materials provided with the
00156  *    distribution.
00157  *
00158  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00159  *    and "Vovida Open Communication Application Library (VOCAL)" must
00160  *    not be used to endorse or promote products derived from this
00161  *    software without prior written permission. For written
00162  *    permission, please contact vocal@vovida.org.
00163  *
00164  * 4. Products derived from this software may not be called "VOCAL", nor
00165  *    may "VOCAL" appear in their name, without prior written
00166  *    permission of Vovida Networks, Inc.
00167  *
00168  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00169  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00170  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00171  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00172  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00173  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00174  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00175  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00176  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00177  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00178  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00179  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00180  * DAMAGE.
00181  *
00182  * ====================================================================
00183  *
00184  * This software consists of voluntary contributions made by Vovida
00185  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00186  * Inc.  For more information on Vovida Networks, Inc., please see
00187  * <http://www.vovida.org/>.
00188  *
00189  */