2008-12-23 08:44:30 +01:00
|
|
|
/*
|
|
|
|
|
threads.c - Manage threads
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Christian Thaeter <ct@pipapo.org>
|
|
|
|
|
|
|
|
|
|
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; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//TODO: Support library includes//
|
|
|
|
|
|
2009-01-28 04:56:01 +01:00
|
|
|
#include "include/logging.h"
|
2009-11-23 22:40:31 +01:00
|
|
|
#include "lib/mutex.h"
|
|
|
|
|
#include "lib/safeclib.h"
|
|
|
|
|
|
2008-12-23 08:44:30 +01:00
|
|
|
|
|
|
|
|
//TODO: Lumiera header includes//
|
2009-01-16 02:18:58 +01:00
|
|
|
#include "threads.h"
|
2008-12-23 08:44:30 +01:00
|
|
|
|
|
|
|
|
//TODO: internal/static forward declarations//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO: System includes//
|
2008-12-29 01:50:38 +01:00
|
|
|
#include <pthread.h>
|
2008-12-23 08:44:30 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2009-11-25 04:25:00 +01:00
|
|
|
NOBUG_DEFINE_FLAG_PARENT (threads, threads_dbg); /*TODO insert a suitable/better parent flag here */
|
2008-12-23 08:44:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//code goes here//
|
|
|
|
|
|
2008-12-29 01:50:38 +01:00
|
|
|
|
|
|
|
|
struct lumiera_thread_mockup
|
|
|
|
|
{
|
|
|
|
|
void (*fn)(void*);
|
|
|
|
|
void* arg;
|
2009-01-28 04:56:01 +01:00
|
|
|
LumieraReccondition finished;
|
2008-12-29 01:50:38 +01:00
|
|
|
};
|
|
|
|
|
|
2009-11-24 01:47:20 +01:00
|
|
|
static void* thread_loop (void* arg)
|
|
|
|
|
{
|
|
|
|
|
return arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
2008-12-29 01:50:38 +01:00
|
|
|
static void* pthread_runner (void* thread)
|
|
|
|
|
{
|
2009-01-28 04:56:01 +01:00
|
|
|
pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
|
|
|
|
|
|
2008-12-29 01:50:38 +01:00
|
|
|
struct lumiera_thread_mockup* starter = (struct lumiera_thread_mockup*) thread;
|
2009-06-19 00:45:08 +02:00
|
|
|
LumieraReccondition thread_end_notification = starter->finished;
|
2009-01-28 04:56:01 +01:00
|
|
|
|
2008-12-29 01:50:38 +01:00
|
|
|
starter->fn (starter->arg);
|
2009-01-28 04:56:01 +01:00
|
|
|
|
2009-06-19 00:45:08 +02:00
|
|
|
if (!thread_end_notification)
|
|
|
|
|
return NULL; // no signalling of thread termination desired
|
|
|
|
|
|
|
|
|
|
LUMIERA_RECCONDITION_SECTION(cond_sync, thread_end_notification)
|
2009-01-28 04:56:01 +01:00
|
|
|
LUMIERA_RECCONDITION_BROADCAST;
|
|
|
|
|
|
2008-12-29 01:50:38 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2009-11-24 01:47:20 +01:00
|
|
|
#endif
|
2008-12-29 01:50:38 +01:00
|
|
|
|
2009-01-28 04:56:01 +01:00
|
|
|
static pthread_once_t attr_once = PTHREAD_ONCE_INIT;
|
|
|
|
|
static pthread_attr_t attrs;
|
|
|
|
|
|
|
|
|
|
static void thread_attr_init (void)
|
|
|
|
|
{
|
|
|
|
|
pthread_attr_init (&attrs);
|
|
|
|
|
pthread_attr_setdetachstate (&attrs, PTHREAD_CREATE_DETACHED);
|
|
|
|
|
//cancel ...
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-23 01:55:08 +01:00
|
|
|
#if 0
|
2009-11-23 22:40:31 +01:00
|
|
|
// TODO: rewrite this using lumiera_threadpool_acquire()
|
2008-12-29 01:50:38 +01:00
|
|
|
LumieraThread
|
|
|
|
|
lumiera_thread_run (enum lumiera_thread_class kind,
|
|
|
|
|
void (*start_routine)(void *),
|
|
|
|
|
void * arg,
|
2009-01-28 04:56:01 +01:00
|
|
|
LumieraReccondition finished,
|
2008-12-29 01:50:38 +01:00
|
|
|
const char* purpose,
|
|
|
|
|
struct nobug_flag* flag)
|
|
|
|
|
{
|
|
|
|
|
(void) kind;
|
|
|
|
|
(void) purpose;
|
|
|
|
|
(void) flag;
|
|
|
|
|
|
|
|
|
|
if (attr_once == PTHREAD_ONCE_INIT)
|
|
|
|
|
pthread_once (&attr_once, thread_attr_init);
|
|
|
|
|
|
|
|
|
|
static struct lumiera_thread_mockup thread;
|
|
|
|
|
|
|
|
|
|
thread.fn = start_routine;
|
|
|
|
|
thread.arg = arg;
|
2009-01-28 04:56:01 +01:00
|
|
|
thread.finished = finished;
|
2008-12-29 01:50:38 +01:00
|
|
|
|
|
|
|
|
pthread_t dummy;
|
2009-01-02 08:01:51 +01:00
|
|
|
int error = pthread_create (&dummy, &attrs, pthread_runner, &thread);
|
2008-12-29 01:50:38 +01:00
|
|
|
|
2009-01-02 08:01:51 +01:00
|
|
|
if (error) return 0; /////TODO temporary addition by Ichthyo; probably we'll set lumiera_error?
|
2008-12-29 01:50:38 +01:00
|
|
|
return (LumieraThread) 1;
|
|
|
|
|
}
|
2009-11-23 01:55:08 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// TODO: new implementation, remove the above one
|
|
|
|
|
// maybe this shouldn't return LumieraThread at all
|
|
|
|
|
// when this is called it should have already been decided that the function
|
|
|
|
|
// shall run in parallel, as a thread
|
|
|
|
|
LumieraThread
|
|
|
|
|
lumiera_thread_run (enum lumiera_thread_class kind,
|
|
|
|
|
void (*function)(void *),
|
|
|
|
|
void * arg,
|
|
|
|
|
LumieraReccondition finished,
|
|
|
|
|
const char* purpose,
|
|
|
|
|
struct nobug_flag* flag)
|
|
|
|
|
{
|
|
|
|
|
(void)finished;
|
|
|
|
|
(void)function;
|
|
|
|
|
(void)arg;
|
|
|
|
|
// ask the threadpool for a thread (it might create a new one)
|
|
|
|
|
LumieraThread self = lumiera_threadpool_acquire_thread(kind, purpose, flag);
|
|
|
|
|
|
2009-11-23 22:40:31 +01:00
|
|
|
// TODO: set the function and data to be run
|
2009-11-23 01:55:08 +01:00
|
|
|
// lumiera_thread_set_func_data (self, start_routine, arg, purpose, flag);
|
|
|
|
|
|
2009-11-23 22:40:31 +01:00
|
|
|
// and let it really run (signal the condition var, the thread waits on it)
|
2009-11-23 01:55:08 +01:00
|
|
|
LUMIERA_RECCONDITION_SECTION(cond_sync, self->finished)
|
|
|
|
|
LUMIERA_RECCONDITION_SIGNAL;
|
|
|
|
|
|
|
|
|
|
// NOTE: example only, add solid error handling!
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-23 22:40:31 +01:00
|
|
|
/**
|
|
|
|
|
* Create a new thread structure with a matching pthread
|
|
|
|
|
*/
|
|
|
|
|
LumieraThread
|
|
|
|
|
lumiera_thread_new (enum lumiera_thread_class kind,
|
|
|
|
|
LumieraReccondition finished,
|
|
|
|
|
const char* purpose,
|
|
|
|
|
struct nobug_flag* flag)
|
|
|
|
|
{
|
2009-11-25 04:25:00 +01:00
|
|
|
// TODO: do something with these:
|
2009-11-23 22:40:31 +01:00
|
|
|
(void) purpose;
|
|
|
|
|
(void) flag;
|
|
|
|
|
|
|
|
|
|
if (attr_once == PTHREAD_ONCE_INIT)
|
|
|
|
|
pthread_once (&attr_once, thread_attr_init);
|
|
|
|
|
|
|
|
|
|
LumieraThread self = lumiera_malloc (sizeof (*self));
|
|
|
|
|
llist_init(&self->node);
|
|
|
|
|
self->finished = finished;
|
2009-11-25 04:25:00 +01:00
|
|
|
REQUIRE (kind < LUMIERA_THREADCLASS_COUNT, "invalid thread kind specified: %d", kind);
|
2009-11-23 22:51:18 +01:00
|
|
|
self->kind = kind;
|
2009-11-23 22:40:31 +01:00
|
|
|
self->state = LUMIERA_THREADSTATE_IDLE;
|
|
|
|
|
|
2009-11-25 04:25:00 +01:00
|
|
|
REQUIRE (&self->id);
|
|
|
|
|
//REQUIRE (&attrs);
|
|
|
|
|
//REQUIRE (&thread_loop);
|
|
|
|
|
REQUIRE (self);
|
2009-11-24 01:47:20 +01:00
|
|
|
int error = pthread_create (&self->id, &attrs, &thread_loop, self);
|
2009-11-23 01:55:08 +01:00
|
|
|
|
2009-11-23 22:40:31 +01:00
|
|
|
if (error)
|
|
|
|
|
{
|
|
|
|
|
free(self);
|
|
|
|
|
return 0; /////TODO temporary addition by Ichthyo; probably we'll set lumiera_error?
|
|
|
|
|
}
|
2008-12-23 08:44:30 +01:00
|
|
|
|
2009-11-23 22:40:31 +01:00
|
|
|
return self;
|
|
|
|
|
}
|
2008-12-23 08:44:30 +01:00
|
|
|
|
2009-11-25 04:25:00 +01:00
|
|
|
LumieraThread
|
|
|
|
|
lumiera_thread_destroy (LumieraThread self)
|
|
|
|
|
{
|
|
|
|
|
// TODO: stop the pthread
|
|
|
|
|
// TODO: empty the list/node?
|
|
|
|
|
//finished = NULL; // or free(finished)?
|
|
|
|
|
lumiera_reccondition_destroy (self->finished, &NOBUG_FLAG(threads));
|
|
|
|
|
//kind = 0;
|
|
|
|
|
//state = 0;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
lumiera_thread_delete (LumieraThread self)
|
|
|
|
|
{
|
|
|
|
|
lumiera_free (lumiera_thread_destroy (self));
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-23 08:44:30 +01:00
|
|
|
/*
|
|
|
|
|
// Local Variables:
|
|
|
|
|
// mode: C
|
|
|
|
|
// c-file-style: "gnu"
|
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
|
// End:
|
|
|
|
|
*/
|