2008-06-02 03:13:18 +02:00
|
|
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
/* Copyright (C) 2008 Jonathan Moore Liles */
|
|
|
|
|
/* */
|
|
|
|
|
/* This program is free software; you can redistribute it and/or modify it */
|
|
|
|
|
/* under the terms of the GNU General Public License as published by the */
|
|
|
|
|
/* Free Software Foundation; either version 2 of the License, or (at your */
|
|
|
|
|
/* option) any later version. */
|
|
|
|
|
/* */
|
|
|
|
|
/* This program is distributed in the hope that it will be useful, but WITHOUT */
|
|
|
|
|
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
|
|
|
|
|
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
|
|
|
|
|
/* more details. */
|
|
|
|
|
/* */
|
|
|
|
|
/* You should have received a copy of the GNU General Public License along */
|
|
|
|
|
/* with This program; see the file COPYING. If not,write to the Free Software */
|
|
|
|
|
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "Thread.H"
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pthread_key_t Thread::_current = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thread::Thread ( )
|
|
|
|
|
{
|
|
|
|
|
_thread = 0;
|
2015-02-15 01:13:48 +01:00
|
|
|
|
_running = false;
|
2008-06-02 03:13:18 +02:00
|
|
|
|
_name = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread::Thread ( const char *name )
|
|
|
|
|
{
|
|
|
|
|
_thread = 0;
|
2015-02-15 01:13:48 +01:00
|
|
|
|
_running = false;
|
2008-06-02 03:13:18 +02:00
|
|
|
|
_name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Thread::init ( void )
|
|
|
|
|
{
|
|
|
|
|
pthread_key_create( &_current, NULL );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Thread::is ( const char *name )
|
|
|
|
|
{
|
|
|
|
|
return ! strcmp( Thread::current()->name(), name );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** to be used by existing threads (that won't call clone()) */
|
|
|
|
|
void
|
|
|
|
|
Thread::set ( const char *name )
|
|
|
|
|
{
|
|
|
|
|
_thread = pthread_self();
|
|
|
|
|
_name = name;
|
2015-02-15 01:13:48 +01:00
|
|
|
|
_running = true;
|
2008-06-02 03:13:18 +02:00
|
|
|
|
|
|
|
|
|
pthread_setspecific( _current, (void*)this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread *
|
|
|
|
|
Thread::current ( void )
|
|
|
|
|
{
|
|
|
|
|
return (Thread*)pthread_getspecific( _current );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct thread_data
|
|
|
|
|
{
|
|
|
|
|
void *(*entry_point)(void *);
|
|
|
|
|
void *arg;
|
|
|
|
|
void *t;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
|
Thread::run_thread ( void *arg )
|
|
|
|
|
{
|
|
|
|
|
thread_data td = *(thread_data *)arg;
|
|
|
|
|
delete (thread_data*)arg;
|
|
|
|
|
|
|
|
|
|
pthread_setspecific( _current, td.t );
|
|
|
|
|
|
2015-02-15 01:13:48 +01:00
|
|
|
|
((Thread*)td.t)->_running = true;
|
|
|
|
|
|
2015-02-16 00:47:55 +01:00
|
|
|
|
void * r = td.entry_point( td.arg );
|
|
|
|
|
|
|
|
|
|
((Thread*)td.t)->_running = false;
|
|
|
|
|
|
|
|
|
|
return r;
|
2008-06-02 03:13:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
Thread::clone ( void *(*entry_point)(void *), void *arg )
|
|
|
|
|
{
|
|
|
|
|
assert( ! _thread );
|
|
|
|
|
|
|
|
|
|
thread_data *td = new thread_data;
|
|
|
|
|
td->entry_point = entry_point;
|
|
|
|
|
td->arg = arg;
|
|
|
|
|
td->t = this;
|
|
|
|
|
|
|
|
|
|
if ( pthread_create( &_thread, NULL, run_thread, td ) != 0 )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Thread::detach ( void )
|
|
|
|
|
{
|
|
|
|
|
pthread_detach( _thread );
|
|
|
|
|
_thread = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-19 08:00:48 +01:00
|
|
|
|
void
|
|
|
|
|
Thread::cancel ( void )
|
|
|
|
|
{
|
|
|
|
|
pthread_cancel( _thread );
|
|
|
|
|
_thread = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-02 03:13:18 +02:00
|
|
|
|
void
|
|
|
|
|
Thread::join ( void )
|
|
|
|
|
{
|
2015-03-24 02:01:39 +01:00
|
|
|
|
if ( _thread != 0 )
|
|
|
|
|
{
|
|
|
|
|
/* not joined yet, go ahead. */
|
|
|
|
|
pthread_join( _thread, NULL );
|
|
|
|
|
}
|
2008-06-02 03:13:18 +02:00
|
|
|
|
_thread = 0;
|
|
|
|
|
}
|
2012-06-16 03:53:40 +02:00
|
|
|
|
|
2015-02-15 00:45:12 +01:00
|
|
|
|
/* never call this unless some other thread will be calling 'join' on
|
|
|
|
|
* this one, otherwise, running() will return true even though the
|
|
|
|
|
* thread is dead */
|
2012-06-16 03:53:40 +02:00
|
|
|
|
void
|
|
|
|
|
Thread::exit ( void *retval )
|
|
|
|
|
{
|
2015-02-15 01:13:48 +01:00
|
|
|
|
_running = false;
|
2013-10-25 03:21:45 +02:00
|
|
|
|
pthread_exit( retval );
|
2012-06-16 03:53:40 +02:00
|
|
|
|
}
|