1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_core_mt_Thread_hpp
38 #define anna_core_mt_Thread_hpp
43 #include <anna/core/RuntimeException.hpp>
44 #include <anna/core/mt/Mutex.hpp>
45 #include <anna/core/Allocator.hpp>
54 Recubrimiento para acceder a las funciones de threads de POSIX.
56 La funcionalidad de �ta clase slo estar�disponible en aplicaciones multithread aunque el
57 interfaz se mantiene constante de cualquier forma.
62 * Flags que puede ajuster el funcionamiento del thread.
66 enum { None = 0, Joinable = 1 };
72 Thread() : a_id((pthread_t) - 1), a_manager(NULL), a_flags(Flag::None) {;}
81 @return El identificador de este thread a nivel de sistema operativo.
83 pthread_t getId() const throw() { return a_id; }
86 * Establece los flags que configuran el comportamiento de este thread.
87 * \param flag Una combinación de los valores definidos por Thread::Flags::_v.
88 * \warning Los flags deben establecerse antes de invocar al método #start.
90 void setFlags(const int flags) throw() { a_flags = flags; }
93 * Devuelve el indicador que informa sobre si se podría aplicar el método #join sobre este thread
95 bool isJoinable() const throw() { return (a_flags & Flag::Joinable) != 0; }
98 Devuelve el estado de ejecucion de �te thread.
99 \return El estado de ejecucion de �te thread \em true si est�en ejecucion o \em false en otro
102 bool isRunning() const throw() { return (a_id != (pthread_t) - 1); }
106 Comienza la ejecucion de este threads siempre y cuando el thread no est�ejecutandose
109 Invoca al m�odo anna::Runnable::run del runnable recibido como par�etro.
111 @param runnable Instancia del objeto que va a ser ejecutado en el nuevo thread.
113 void start(Runnable& runnable) throw(RuntimeException);
116 Introduce un nuevo punto de cancelacin. Esto slo sera necesario en caso de que el proceso
117 que implementa nuestro thread no haga ninguna llamada al sistema.
119 void testCancel() throw() { pthread_testcancel(); }
122 Suspende la ejecución del thread que invoca a este método hasta que termine la ejecución
123 del thread sobre el que aplica.
131 cout << "Esperando a B." << endl;
133 cout << "B terminado." << endl;
137 catch (Exception& e) {
141 Con esto desde el thread C hemos lanzado los threads A y B; C quedar�bloqueado a la espera de que termine la ejecucion del
142 thread B y una vez hecho esto termina la ejecucion del thread A.
144 void join() throw(RuntimeException);
147 Devuelve una cadena con la informacin referente a �te thread.
148 \return Una cadena con la informacin referente a �te thread.
150 virtual std::string asString() const throw();
160 ThreadManager* a_manager;
163 Thread(const Thread& other);
165 static void* exec(void* arg);
167 friend class Allocator <Thread>;
168 friend class ThreadManager;