RAUL
0.7.0
|
00001 /* This file is part of Raul. 00002 * Copyright (C) 2007-2009 David Robillard <http://drobilla.net> 00003 * 00004 * Raul is free software; you can redistribute it and/or modify it under the 00005 * terms of the GNU General Public License as published by the Free Software 00006 * Foundation; either version 2 of the License, or (at your option) any later 00007 * version. 00008 * 00009 * Raul is distributed in the hope that it will be useful, but WITHOUT ANY 00010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00011 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. 00012 * 00013 * You should have received a copy of the GNU General Public License along 00014 * with this program; if not, write to the Free Software Foundation, Inc., 00015 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00016 */ 00017 00018 #ifndef RAUL_THREAD_HPP 00019 #define RAUL_THREAD_HPP 00020 00021 #include <set> 00022 #include <string> 00023 #include <iostream> 00024 #include <pthread.h> 00025 #include <boost/utility.hpp> 00026 00027 namespace Raul { 00028 00029 00039 class Thread : boost::noncopyable 00040 { 00041 public: 00042 virtual ~Thread() { 00043 stop(); 00044 } 00045 00046 static Thread* create(const std::string& name="") 00047 { return new Thread(name); } 00048 00050 static Thread* create_for_this_thread(const std::string& name="") 00051 { return new Thread(pthread_self(), name); } 00052 00053 static Thread& get(); 00054 00055 virtual void start(); 00056 virtual void stop(); 00057 00058 void set_scheduling(int policy, unsigned int priority); 00059 00060 const std::string& name() const { return _name; } 00061 void set_name(const std::string& name) { _name = name; } 00062 00063 bool is_context(unsigned context) const { return _contexts.find(context) != _contexts.end(); } 00064 void set_context(unsigned context) { _contexts.insert(context); } 00065 00066 protected: 00067 Thread(const std::string& name=""); 00068 Thread(pthread_t thread, const std::string& name=""); 00069 00078 virtual void _run() {} 00079 00080 bool _exit_flag; 00081 00082 private: 00083 static void* _static_run(void* me); 00084 00086 static void thread_key_alloc() { 00087 pthread_key_create(&_thread_key, NULL); 00088 } 00089 00090 /* Key for the thread-specific buffer */ 00091 static pthread_key_t _thread_key; 00092 00093 /* Once-only initialisation of the key */ 00094 static pthread_once_t _thread_key_once; 00095 00096 std::set<unsigned> _contexts; 00097 std::string _name; 00098 bool _pthread_exists; 00099 bool _own_thread; 00100 pthread_t _pthread; 00101 }; 00102 00103 00104 } // namespace Raul 00105 00106 #endif // RAUL_THREAD_HPP