• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

Thread.hpp

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 <iostream>
00022 #include <set>
00023 #include <string>
00024 
00025 #include <pthread.h>
00026 
00027 #include <boost/utility.hpp>
00028 
00029 namespace Raul {
00030 
00031 
00041 class Thread : boost::noncopyable
00042 {
00043 public:
00044     virtual ~Thread() {
00045         stop();
00046     }
00047 
00048     static Thread* create(const std::string& name="")
00049         { return new Thread(name); }
00050 
00052     static Thread* create_for_this_thread(const std::string& name="")
00053         { return new Thread(pthread_self(), name); }
00054 
00055     static Thread& get();
00056 
00057     virtual void start();
00058     virtual void stop();
00059 
00060     virtual void join();
00061 
00062     void set_scheduling(int policy, unsigned int priority);
00063 
00064     const std::string& name() const { return _name; }
00065     void set_name(const std::string& name) { _name = name; }
00066 
00067     bool is_context(unsigned context) const { return _contexts.find(context) != _contexts.end(); }
00068     void set_context(unsigned context) { _contexts.insert(context); }
00069 
00070 protected:
00071     explicit Thread(const std::string& name="");
00072     Thread(pthread_t thread, const std::string& name="");
00073 
00082     virtual void _run() {}
00083 
00084     bool _exit_flag;
00085 
00086 private:
00087     static void* _static_run(void* me);
00088 
00090     static void thread_key_alloc() {
00091         pthread_key_create(&_thread_key, NULL);
00092     }
00093 
00094     /* Key for the thread-specific buffer */
00095     static pthread_key_t _thread_key;
00096 
00097     /* Once-only initialisation of the key */
00098     static pthread_once_t _thread_key_once;
00099 
00100     std::set<unsigned> _contexts;
00101     std::string        _name;
00102     bool               _pthread_exists;
00103     bool               _own_thread;
00104     pthread_t          _pthread;
00105 };
00106 
00107 
00108 } // namespace Raul
00109 
00110 #endif // RAUL_THREAD_HPP

Generated on Tue Jan 11 2011 18:26:17 for RAUL by  doxygen 1.7.1