partial code skeleton

This commit is contained in:
Michael Ploujnikov 2009-11-22 19:55:08 -05:00
parent 4bac65df72
commit 00eb4eda46
8 changed files with 535 additions and 5 deletions

View file

@ -26,6 +26,7 @@ liblumierabackend_la_SOURCES = \
$(liblumierabackend_la_srcdir)/mediaaccessfacade.cpp \
$(liblumierabackend_la_srcdir)/backend.c \
$(liblumierabackend_la_srcdir)/threads.c \
$(liblumierabackend_la_srcdir)/threadpool.c \
$(liblumierabackend_la_srcdir)/file.c \
$(liblumierabackend_la_srcdir)/filehandle.c \
$(liblumierabackend_la_srcdir)/filedescriptor.c \
@ -41,6 +42,7 @@ liblumierabackend_la_SOURCES = \
noinst_HEADERS += \
$(liblumierabackend_la_srcdir)/backend.h \
$(liblumierabackend_la_srcdir)/threads.h \
$(liblumierabackend_la_srcdir)/threadpool.h \
$(liblumierabackend_la_srcdir)/file.h \
$(liblumierabackend_la_srcdir)/filehandle.h \
$(liblumierabackend_la_srcdir)/filedescriptor.h \

243
src/backend/threadpool.c Normal file
View file

@ -0,0 +1,243 @@
/*
threadpool.c - Manage pools of threads
Copyright (C) Lumiera.org
2009, Michael Ploujnikov <ploujj@gmail.com>
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//
#include "include/logging.h"
#include "lib/safeclib.h"
//TODO: Lumiera header includes//
#include "backend/threadpool.h"
//TODO: internal/static forward declarations//
/**
* Find a suitable thread pool for the given thread type.
*/
static
LumieraThreadpool
lumiera_threadpool_findpool(enum lumiera_thread_class kind);
typedef struct lumiera_threadpoolmanager_struct lumiera_threadpoolmanager;
typedef lumiera_threadpoolmanager* LumieraThreadpoolmanager;
struct lumiera_threadpoolmanager_struct
{
llist pools;
};
/**
* Create the singleton threadpool manager
*/
static
void
lumiera_threadpoolmanager_new(void);
/**
* Delete/remove the singleton threadpool manager
*/
static
void
lumiera_threadpoolmanager_delete(void);
// singleton threadpool manager instance
static LumieraThreadpoolmanager lumiera_tpmanager;
typedef struct lumiera_threadpool_struct lumiera_threadpool;
typedef lumiera_threadpool* LumieraThreadpool;
enum lumiera_threadpool_type
{
// TODO: the following types need to be more thought out:
/** the default kind of threadpool **/
LUMIERA_DEFAULT_THREADPOOL,
/** a threadpool for special threads **/
LUMIERA_SPECIAL_THREADPOOL
};
struct lumiera_threadpool_struct
{
/* a list of threadpools for a threadpool manager */
llist node;
enum lumiera_threadpool_type type;
llist threads;
};
/**
* Create a thread pool.
*/
static
LumieraThreadpool
lumiera_threadpool_new(enum lumiera_threadpool_type type);
/**
* Delete/remove a threadpool.
*/
static
void
lumiera_threadpool_delete(LumieraThreadpool threadpool);
//TODO: System includes//
#include <pthread.h>
/**
* @file
*
*/
//NOBUG_DEFINE_FLAG_PARENT (threadpool, lumiera); /*TODO insert a suitable/better parent flag here */
//code goes here//
void* pool_thread_loop(void * arg)
{
(void) arg;
while (1)
{
;
}
return arg;
}
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 ...
}
LumieraThread
lumiera_thread_new (enum lumiera_thread_class kind,
LumieraReccondition finished,
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);
LumieraThread thread = lumiera_malloc (sizeof(*thread));
thread->finished = finished;
pthread_t dummy;
printf("creating a thread\n");
int error = pthread_create (&dummy, &attrs, &pool_thread_loop, thread);
if (error) return 0; /////TODO temporary addition by Ichthyo; probably we'll set lumiera_error?
return thread;
}
LumieraThreadpool
lumiera_threadpool_new(enum lumiera_threadpool_type type)
{
LumieraThreadpool self = lumiera_malloc (sizeof (*self));
self->type = type;
/* we start with no threads in the list */
llist_init (&self->threads);
return self;
}
LumieraThreadpoolmanager lumiera_tpmanager = NULL;
void
lumiera_threadpoolmanager_new()
{
REQUIRE (!lumiera_tpmanager, "Threadpool manager already initialized");
lumiera_tpmanager = lumiera_malloc (sizeof (lumiera_threadpoolmanager));
llist_init (&lumiera_tpmanager->pools);
// create a default threadpool
LumieraThreadpool default_tp = lumiera_threadpool_new(LUMIERA_DEFAULT_THREADPOOL);
llist_insert_head (&lumiera_tpmanager->pools, &default_tp->node);
};
LumieraThread
lumiera_threadpool_acquire_thread(enum lumiera_thread_class kind,
const char* purpose,
struct nobug_flag* flag)
{
LumieraThreadpool tp =
lumiera_threadpool_findpool(enum lumiera_thread_class kind);
if (!tp)
{
// ERROR: could not find a suitable threadpool or there are no threadpools
return (LumieraThreadpool)0;
}
// TODO: either get a thread from the pool or create a new one
}
LumieraThreadpool
lumiera_threadpool_findpool(enum lumiera_thread_class thread_kind)
{
if (!lumiera_tpmanager)
{
// create a threadpool manager if it doesn't exist yet
lumiera_threadpoolmanager_new();
}
if (!lumiera_tpmanager)
{
// ERROR: if the threadpool manager still doesn't exist, something went wrong
return (LumieraThreadpool)0;
}
else
{
// for now, ignore the thread all together and just return the first threadpool
(void) thread_kind;
// BUG: what if there are no pools in the list???
// where/when should I check for that case?
return (LumieraThreadpool)(llist_head (&lumiera_tpmanager->pools));
}
}
void
lumiera_threadpoolmanager_new(void)
{
REQUIRE (!lumiera_tpmanager, "Threadpool manager already initialized");
lumiera_tpmanager = lumiera_malloc (sizeof (lumiera_tpmanager));
llist_init (&lumiera_tpmanager->pools);
// start with just one default threadpool in the list
LumieraThreadpool tp =
lumiera_threadpool_new(LUMIERA_DEFAULT_THREADPOOL);
llist_insert_head (&lumiera_tp->pools, &tp->node);
}
/*
// Local Variables:
// mode: C
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
*/

109
src/backend/threadpool.h Normal file
View file

@ -0,0 +1,109 @@
/*
threadpool.h - Manage pools of threads
Copyright (C) Lumiera.org
2009, Michael Ploujnikov <ploujj@gmail.com>
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.
*/
#ifndef LUMIERA_THREADPOOL_H
#define LUMIERA_THREADPOOL_H
//TODO: Support library includes//
#include "lib/reccondition.h"
#include "lib/llist.h"
//TODO: Forward declarations//
//TODO: Lumiera header includes//
#include "threads.h"
//TODO: System includes//
#include <nobug.h>
/**
* @file
*
*/
//TODO: declarations go here//
typedef struct lumiera_threadpool_struct lumiera_threadpool;
typedef lumiera_threadpool* LumieraThreadpool;
enum lumiera_threadpool_type
{
// TODO: the following types need to be more thought out:
/** the default kind of threadpool **/
LUMIERA_DEFAULT_THREADPOOL,
/** a threadpool for special threads **/
LUMIERA_SPECIAL_THREADPOOL
};
struct lumiera_threadpool_struct
{
/* a list of threadpools for a threadpool manager */
llist node;
enum lumiera_threadpool_type type;
llist threads;
};
/**
* Create a thread pool.
*/
LumieraThreadpool
lumiera_threadpool_new(enum lumiera_threadpool_type type);
/**
* Delete/remove a threadpool.
*/
void
lumiera_threadpool_delete(LumieraThreadpool threadpool);
/**
* Acquire a thread from a threadpool.
* This may either pick a thread from the list or create a new one when the list is empty.
* This function doesn't need to be accessible outside of the threadpool implementation.
*/
LumieraThread
lumiera_threadpool_acquire_thread(enum lumiera_thread_class kind,
const char* purpose,
struct nobug_flag* flag);
/**
* Release a thread
* This ends up putting a (parked/idle) thread back on the list of a threadpool.
* This function doesn't need to be accessible outside of the threadpool implementation.
*/
void
lumiera_threadpool_release_thread(LumieraThread thread);
#endif
/*
// Local Variables:
// mode: C
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
*/

View file

@ -50,7 +50,7 @@ struct lumiera_thread_mockup
LumieraReccondition finished;
};
#if 0
static void* pthread_runner (void* thread)
{
pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
@ -79,8 +79,9 @@ static void thread_attr_init (void)
pthread_attr_setdetachstate (&attrs, PTHREAD_CREATE_DETACHED);
//cancel ...
}
#endif
#if 0
LumieraThread
lumiera_thread_run (enum lumiera_thread_class kind,
void (*start_routine)(void *),
@ -108,6 +109,39 @@ lumiera_thread_run (enum lumiera_thread_class kind,
if (error) return 0; /////TODO temporary addition by Ichthyo; probably we'll set lumiera_error?
return (LumieraThread) 1;
}
#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);
// set the function and data to be run
// lumiera_thread_set_func_data (self, start_routine, arg, purpose, flag);
// and let it really run (signal the condition var, it waits there)
LUMIERA_RECCONDITION_SECTION(cond_sync, self->finished)
LUMIERA_RECCONDITION_SIGNAL;
// NOTE: example only, add solid error handling!
return self;
}
/*

View file

@ -46,10 +46,12 @@
typedef struct lumiera_thread_struct lumiera_thread;
typedef lumiera_thread* LumieraThread;
/**
* Thread classes.
* We define some 'classes' of threads for different purposes to abstract
* priorities and other attributes.
** TODO: rename these to LUMIERA_THREADCLASS_*
*/
enum lumiera_thread_class
{
@ -74,6 +76,36 @@ enum lumiera_thread_class
LUMIERA_THREAD_OR_NOT = 1<<16
};
/**
* Thread state.
* These are the only states our threads can be in.
*/
typedef enum
{
LUMIERA_THREADSTATE_ERROR,
LUMIERA_THREADSTATE_IDLE,
LUMIERA_THREADSTATE_RUNNING
}
lumiera_thread_state;
#include "threadpool.h"
/**
* The actual thread data
*/
struct lumiera_thread_struct
{
llist node;
// the function and argument can be passed to the thread at creation time
// void (*function)(void*);
// void* arg;
LumieraReccondition finished;
enum lumiera_thread_class type;
lumiera_thread_state state;
};
/**
* Start a thread.
* Threads are implemented as procedures which take a void* and dont return anything.
@ -85,7 +117,7 @@ enum lumiera_thread_class
* * Threads shall not handle signals (all signals will be disabled for them) unless explicitly acknowledged
*
* @param kind class of the thread to start
* @param start_routine pointer to a function to execute in a thread (returning void, not void* as in pthreads)
* @param function pointer to a function to execute in a thread (returning void, not void* as in pthreads)
* @param arg generic pointer passed to the thread
* @param finished a condition variable to be broadcasted, if not NULL.
* The associated mutex should be locked at thread_run time already, else the signal can get lost.
@ -94,14 +126,13 @@ enum lumiera_thread_class
*/
LumieraThread
lumiera_thread_run (enum lumiera_thread_class kind,
void (*start_routine)(void *),
void (*function)(void *),
void * arg,
LumieraReccondition finished,
const char* purpose,
struct nobug_flag* flag);
#endif
/*
// Local Variables:

5
tests/50threadpool.tests Normal file
View file

@ -0,0 +1,5 @@
TESTING "Thread Pool" ./test-threadpool
TEST "Fake test" faketest <<END
out: this is a fake test
END

View file

@ -0,0 +1,53 @@
/*
test-threadpool.c - test thread pool creation and usage
Copyright (C) Lumiera.org
2009, Michael Ploujnikov <ploujj@gmail.com>
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.
*/
#include "tests/test.h"
#include "backend/threadpool.h"
#include <stdio.h>
#include <string.h>
TESTS_BEGIN
TEST ("faketest")
{
printf("this is a fake test\n");
/* create a threadpool manager,
it will automatically create all the threadpools */
lumiera_threadpoolmanager_new();
#if 0
/* create some jobs */
LumieraJob myjob = lumiera_job_new(functionpointer, parameters); // create a non timed (immediate job) */
/* find a suitable threadpool for a given job */
LumieraThreadpool somepool = lumiera_threadpool_findpool(myjob); //you want to find a pool which is suitable to run a given job by asking: "hey on which pool can I run this job?" */
/* pass the jobs to the thread pool: */
lumiera_threadpool_start_job(somepool, myjob);
#endif
/* wait for the job to finish */
/* retrive the job result from myjob */
}
TESTS_END

View file

@ -0,0 +1,53 @@
/*
test-threadpool.c - test thread pool creation and usage
Copyright (C) Lumiera.org
2009, Michael Ploujnikov <ploujj@gmail.com>
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.
*/
#include "tests/test.h"
#include "backend/threads.h"
#include <stdio.h>
#include <string.h>
TESTS_BEGIN
TEST ("faketest")
{
printf("this is a fake test\n");
/* create a threadpool manager,
it will automatically create all the threadpools */
lumiera_threadpoolmanager_new();
#if 0
/* create some jobs */
LumieraJob myjob = lumiera_job_new(functionpointer, parameters); // create a non timed (immediate job) */
/* find a suitable threadpool for a given job */
LumieraThreadpool somepool = lumiera_threadpool_findpool(myjob); //you want to find a pool which is suitable to run a given job by asking: "hey on which pool can I run this job?" */
/* pass the jobs to the thread pool: */
lumiera_threadpool_start_job(somepool, myjob);
#endif
/* wait for the job to finish */
/* retrive the job result from myjob */
}
TESTS_END