1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/******************************************************************************
* $Id: threads.c,v 1.2 2007/01/13 22:36:24 gareuselesinge Exp $
* This file is part of liberopops (http://liberopops.sf.net) *
* This file is distributed under the terms of GNU GPL license. *
******************************************************************************/
/******************************************************************************
* File description:
* some functions to handle a thread pool
* Notes:
* uses static data structures, NOT thread safe (should be??)
* Authors:
* Enrico Tassi <gareuselesinge@users.sourceforge.net>
******************************************************************************/
#include <stdlib.h>
#include "threads.h"
#include "beos_compatibility.h"
#include "log.h"
#define LOG_ZONE "POPSERVER"
#define HIDDEN static
/*****************************************************************************/
HIDDEN int max_threads;
#define UNUSED 0
#define USED 1
#define WILL_DIE 2
/*** local data types *********************************************************/
struct thread_t
{
pthread_t *pth;
pthread_attr_t *att;
int active;
};
/*** static variables *********************************************************/
HIDDEN struct thread_t *thread_register;
/*** exported functions *******************************************************/
/********************************
* Inits all threads data structures
*
*/
void thread_init(int n)
{
int i;
//no free for this since it is done only one time
thread_register=calloc(n,sizeof(struct thread_t));
max_threads = n;
for (i=0;i < max_threads;i++)
{
thread_register[i].active=UNUSED;
thread_register[i].pth=NULL;
thread_register[i].att=NULL;
}
}
/******************************
* t announces that will die
*
*/
void thread_die(pthread_t t)
{
int i;
for (i=0;i < max_threads;i++)
{
if ((thread_register[i].pth != NULL) && (pthread_equal(t,*thread_register[i].pth)))
{
DBG("thread %d will die\n",i);
thread_register[i].active=WILL_DIE;
break;
}
}
}
/******************************
* clean all will-die threads
*
*/
void thread_clean()
{
int i;
for (i=0;i < max_threads;i++)
{
if(thread_register[i].active == WILL_DIE)
{
pthread_join(*thread_register[i].pth,NULL);
pthread_attr_destroy(thread_register[i].att);
free(thread_register[i].pth);
free(thread_register[i].att);
thread_register[i].pth=NULL;
thread_register[i].att=NULL;
thread_register[i].active=UNUSED;
DBG("cleaning thread %d\n",i);
}
}
}
/*******************************
* gets a free handler
*
*/
void thread_get_free(pthread_t** t,pthread_attr_t** a)
{
int i;
*t=NULL;
*a=NULL;
for (i=0;i < max_threads;i++)
{
if(thread_register[i].active == UNUSED)
{
thread_register[i].pth=malloc(sizeof(pthread_t));
thread_register[i].att=malloc(sizeof(pthread_attr_t));
*t=thread_register[i].pth;
*a=thread_register[i].att;
pthread_attr_init(*a);
thread_register[i].active=USED;
MALLOC_CHECK(*t);
MALLOC_CHECK(*a);
break;
}
}
}
/******************************************************************
* if called thread_get_free, but the thread has not been created,
* the structure can be freed on the fly
*
*/
void thread_notborn(pthread_t *t)
{
int i;
for (i=0;i < max_threads;i++)
{
if(t == thread_register[i].pth)
{
pthread_attr_destroy(thread_register[i].att);
free(thread_register[i].pth);
free(thread_register[i].att);
thread_register[i].pth=NULL;
thread_register[i].att=NULL;
thread_register[i].active=UNUSED;
DBG("aborting thread %d\n",i);
break;
}
}
}
|