Sirikata
libcore/include/sirikata/core/util/Thread.hpp
Go to the documentation of this file.
00001 /*  Sirikata Utilities -- Sirikata Synchronization Utilities
00002  *  Thread.hpp
00003  *
00004  *  Copyright (c) 2009, Ewen Cheslack-Postava
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 #ifndef _SIRIKATA_UTIL_THREAD_HPP_
00033 #define _SIRIKATA_UTIL_THREAD_HPP_
00034 
00035 #include "Platform.hpp"
00036 #include <boost/thread.hpp>
00037 
00038 #if SIRIKATA_PLATFORM != SIRIKATA_PLATFORM_WINDOWS
00039 #include <sys/time.h>
00040 #endif
00041 
00042 namespace Sirikata {
00043 
00049 class Thread {
00050 public:
00051     typedef std::tr1::function<void()> ThreadMainFunc;
00052 
00053     Thread() {
00054     }
00055     ~Thread() {
00056     }
00057 
00058     explicit Thread(String name, ThreadMainFunc f)
00059      : mImpl(
00060 #if SIRIKATA_PLATFORM != SIRIKATA_PLATFORM_WINDOWS
00061          std::tr1::bind(
00062              &Thread::initThread, this, name, f
00063              , Thread::getProfilerData()
00064          )
00065 #else
00066          std::tr1::bind(
00067              &Thread::initThread, this, name, f
00068          )
00069 #endif
00070        )
00071     {
00072     }
00073 
00074     boost::thread::id get_id() const {
00075         return mImpl.get_id();
00076     }
00077 
00078     bool joinable() const {
00079         return mImpl.joinable();
00080     }
00081     void join() {
00082         mImpl.join();
00083     }
00084     bool timed_join(const boost::system_time& wait_until) {
00085         return mImpl.timed_join(wait_until);
00086     }
00087 
00088     template<typename TimeDuration>
00089     bool timed_join(TimeDuration const& rel_time) {
00090         return mImpl.timed_join(rel_time);
00091     }
00092 
00093     void detach() {
00094         mImpl.detach();
00095     }
00096 
00097     static unsigned hardware_concurrency() {
00098         return boost::thread::hardware_concurrency();
00099     }
00100 
00101     void interrupt() {
00102         mImpl.interrupt();
00103     }
00104 
00105     bool interruption_requested() const {
00106         return mImpl.interruption_requested();
00107     }
00108 
00109     static void yield() {
00110         boost::thread::yield();
00111     }
00112 
00113     static void sleep(const boost::system_time& xt) {
00114         boost::thread::sleep(xt);
00115     }
00116 private:
00117 #if SIRIKATA_PLATFORM != SIRIKATA_PLATFORM_WINDOWS
00118     typedef struct itimerval ProfilerData;
00119     static ProfilerData getProfilerData() {
00120         ProfilerData prof_data;
00121         getitimer(ITIMER_PROF, &prof_data);
00122         return prof_data;
00123     }
00124 
00125     static void setProfilerData(ProfilerData pd) {
00126         setitimer(ITIMER_PROF, &pd, NULL);
00127     }
00128 
00129     void initThread(String name, ThreadMainFunc f, ProfilerData pd) {
00130         setProfilerData(pd);
00131         f();
00132     }
00133 #else
00134     void initThread(String name, ThreadMainFunc f) {
00135         f();
00136     }
00137 #endif
00138 
00139     Thread(Thread& rhs);
00140 
00141     boost::thread mImpl;
00142 }; // class Thread
00143 
00144 } // namespace Sirikata
00145 
00146 #endif //_SIRIKATA_UTIL_THREAD_HPP_