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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
#define PDL_CORE /* For certain ifdefs */
#ifndef WIN32
#define USE_MMAP
#else
#undef USE_MMAP
#endif
#include "pdlcore.h"
/* Singly linked list */
/* Note that this zeroes ->next!) */
void pdl__magic_add(pdl *it,pdl_magic *mag)
{
pdl_magic **foo = &(it->magic);
while(*foo) {
foo = &((*foo)->next);
}
(*foo) = mag;
mag->next = NULL;
}
void pdl__magic_rm(pdl *it,pdl_magic *mag)
{
pdl_magic **foo = &(it->magic);
while(*foo) {
if(*foo == mag) {
*foo = (*foo)->next;
}
foo = &((*foo)->next);
}
die("PDL:Magic not found: Internal error\n");
}
/* Test for undestroyability */
int pdl__magic_isundestroyable(pdl *it)
{
pdl_magic **foo = &(it->magic);
while(*foo) {
if((*foo)->what & PDL_MAGIC_UNDESTROYABLE) {return 1;}
foo = &((*foo)->next);
}
return 0;
}
/* Call magics */
void *pdl__call_magic(pdl *it,int which)
{
void *ret = NULL;
pdl_magic **foo = &(it->magic);
while(*foo) {
if((*foo)->what & which) {
if((*foo)->what & PDL_MAGIC_DELAYED)
pdl_add_delayed_magic(*foo);
else
ret = (void *)((*foo)->vtable->cast(*foo));
/* Cast spell */
}
foo = &((*foo)->next);
}
return ret;
}
/* XXX FINDS ONLY FIRST */
pdl_magic *pdl__find_magic(pdl *it, int which)
{
pdl_magic **foo = &(it->magic);
while(*foo) {
if((*foo)->what & which) {
return *foo;
}
foo = &((*foo)->next);
}
return NULL;
}
pdl_magic *pdl__print_magic(pdl *it)
{
pdl_magic **foo = &(it->magic);
while(*foo) {
printf("Magic %d\ttype: ",*foo);
if((*foo)->what & PDL_MAGIC_MARKCHANGED)
printf("PDL_MAGIC_MARKCHANGED");
else if ((*foo)->what & PDL_MAGIC_MUTATEDPARENT)
printf("PDL_MAGIC_MUTATEDPARENT");
else if ((*foo)->what & PDL_MAGIC_THREADING)
printf("PDL_MAGIC_THREADING");
else
printf("UNKNOWN");
if ((*foo)->what & (PDL_MAGIC_DELAYED|PDL_MAGIC_UNDESTROYABLE))
{
printf(" qualifier(s): ");
if ((*foo)->what & PDL_MAGIC_DELAYED)
printf(" PDL_MAGIC_DELAYED");
if ((*foo)->what & PDL_MAGIC_UNDESTROYABLE)
printf(" PDL_MAGIC_UNDESTROYABLE");
}
printf("\n");
foo = &((*foo)->next);
}
return NULL;
}
int pdl__ismagic(pdl *it)
{
return (it->magic != 0);
}
static pdl_magic **delayed=NULL;
static int ndelayed = 0;
void pdl_add_delayed_magic(pdl_magic *mag) {
delayed = realloc(delayed,sizeof(*delayed)*++ndelayed);
delayed[ndelayed-1] = mag;
}
void pdl_run_delayed_magic() {
int i;
pdl_magic **oldd = delayed; /* In case someone makes new delayed stuff */
int nold = ndelayed;
delayed = NULL;
ndelayed = 0;
for(i=0; i<nold; i++) {
oldd[i]->vtable->cast(oldd[i]);
}
free(oldd);
}
/****************
*
* ->bind - magic
*/
void *svmagic_cast(pdl_magic *mag)
{
pdl_magic_perlfunc *magp = (pdl_magic_perlfunc *)mag;
dSP;
PUSHMARK(sp);
perl_call_sv(magp->sv, G_DISCARD | G_NOARGS);
return NULL;
}
static pdl_magic_vtable svmagic_vtable = {
svmagic_cast,
NULL
};
pdl_magic *pdl_add_svmagic(pdl *it,SV *func)
{
AV *av;
pdl_magic_perlfunc *ptr = malloc(sizeof(pdl_magic_perlfunc));
ptr->what = PDL_MAGIC_MARKCHANGED | PDL_MAGIC_DELAYED;
ptr->vtable = &svmagic_vtable;
ptr->sv = newSVsv(func);
ptr->pdl = it;
ptr->next = NULL;
pdl__magic_add(it,(pdl_magic *)ptr);
if(it->state & PDL_ANYCHANGED)
pdl_add_delayed_magic((pdl_magic *)ptr);
/* In order to have our SV destroyed in time for the interpreter, */
/* XXX Work this out not to memleak */
av = perl_get_av("PDL::disposable_svmagics",TRUE);
av_push(av,ptr->sv);
return (pdl_magic *)ptr;
}
/****************
*
* ->bind - magic
*/
pdl_trans *pdl_find_mutatedtrans(pdl *it)
{
if(!it->magic) return 0;
return pdl__call_magic(it,PDL_MAGIC_MUTATEDPARENT);
}
static void *fammutmagic_cast(pdl_magic *mag)
{
pdl_magic_fammut *magp = (pdl_magic_fammut *)mag;
return magp->ftr;
}
struct pdl_magic_vtable familymutmagic_vtable = {
fammutmagic_cast,
NULL
};
pdl_magic *pdl_add_fammutmagic(pdl *it,pdl_trans *ft)
{
pdl_magic_fammut *ptr = malloc(sizeof(pdl_magic_fammut));
ptr->what = PDL_MAGIC_MUTATEDPARENT;
ptr->vtable = &familymutmagic_vtable;
ptr->ftr = ft;
ptr->pdl = it;
ptr->next = NULL;
pdl__magic_add(it,(pdl_magic *)ptr);
return (pdl_magic *)ptr;
}
#ifdef PDL_PTHREAD
/**************
*
* pthreads
*
*/
#define TVERB 0
typedef struct ptarg {
pdl_magic_pthread *mag;
void (*func)(pdl_trans *);
pdl_trans *t;
int no;
} ptarg;
static void *pthread_perform(void *vp) {
struct ptarg *p = (ptarg *)vp;
if(TVERB) printf("STARTING THREAD %d (%d)\n",p->no, pthread_self());
pthread_setspecific(p->mag->key,(void *)&(p->no));
(p->func)(p->t);
if(TVERB) printf("ENDING THREAD %d (%d)\n",p->no, pthread_self());
return NULL;
}
int pdl_magic_thread_nthreads(pdl *it,int *nthdim) {
pdl_magic_pthread *ptr = (pdl_magic_pthread *)pdl__find_magic(it,
PDL_MAGIC_THREADING);
if(!ptr) return 0;
*nthdim = ptr->nthdim;
return ptr->nthreads;
}
int pdl_magic_get_thread(pdl *it) { /* XXX -> only one thread can handle pdl at once */
pdl_magic_pthread *ptr;
int *p;
ptr = (pdl_magic_pthread *)pdl__find_magic(it,
PDL_MAGIC_THREADING);
if(!ptr) {die("Invalid pdl_magic_get_thread!");}
p = (int*)pthread_getspecific(ptr->key);
if(!p) {
die("Invalid pdl_magic_get_thread specific!!!!");
}
return *p;
}
void pdl_magic_thread_cast(pdl *it,void (*func)(pdl_trans *),pdl_trans *t) {
pdl_magic_pthread *ptr; pthread_t *tp; ptarg *tparg;
int i;
ptr = (pdl_magic_pthread *)pdl__find_magic(it,
PDL_MAGIC_THREADING);
if(!ptr) {die("Invalid pdl_magic_thread_cast!");}
tp = malloc(sizeof(pthread_t) * ptr->nthreads);
tparg = malloc(sizeof(*tparg) * ptr->nthreads);
pthread_key_create(&(ptr->key),NULL);
if(TVERB) printf("CREATING THREADS, ME: %d, key: %d\n",pthread_self(),
ptr->key);
for(i=0; i<ptr->nthreads; i++) {
tparg[i].mag = ptr;
tparg[i].func = func;
tparg[i].t = t;
tparg[i].no = i;
pthread_create(tp+i, NULL, pthread_perform, tparg+i);
}
if(TVERB) printf("JOINING THREADS, ME: %d, key: %d\n",pthread_self(),
ptr->key);
for(i=0; i<ptr->nthreads; i++) {
pthread_join(tp[i], NULL);
}
if(TVERB) printf("FINISHED THREADS, ME: %d, key: %d\n",pthread_self(),
ptr->key);
pthread_key_delete((ptr->key));
}
void pdl_add_threading_magic(pdl *it,int nthdim,int nthreads)
{
pdl_magic_pthread *ptr = malloc(sizeof(pdl_magic_pthread));
ptr->what = PDL_MAGIC_THREADING;
ptr->vtable = NULL;
ptr->next = NULL;
ptr->nthdim = nthdim;
ptr->nthreads = nthreads;
pdl__magic_add(it,(pdl_magic *)ptr);
}
#else
/* Dummy versions */
void pdl_add_threading_magic(pdl *it,int nthdim,int nthreads) {}
int pdl_magic_get_thread(pdl *it) {return 0;}
void pdl_magic_thread_cast(pdl *it,void (*func)(pdl_trans *),pdl_trans *t) {}
int pdl_magic_thread_nthreads(pdl *it,int *nthdim) {return 0;}
#endif
/***************************
*
* Delete magic
*
*/
void pdl_delete_mmapped_data(pdl *p, int param)
{
if(!p) {return;}
if(!p->data) {return;}
#ifdef USE_MMAP
munmap(p->data, param);
#else
croak("internal error: trying to delete mmaped data on unsupported platform");
#endif
p->data = 0;
}
static void *delete_mmapped_cast(pdl_magic *mag)
{
pdl_magic_deletedata *magp = (pdl_magic_deletedata *)mag;
magp->func(magp->pdl, magp->param);
return NULL;
}
struct pdl_magic_vtable deletedatamagic_vtable = {
delete_mmapped_cast,
NULL
};
void pdl_add_deletedata_magic(pdl *it, void (*func)(pdl *, int param), int param)
{
pdl_magic_deletedata *ptr = malloc(sizeof(pdl_magic_deletedata));
ptr->what = PDL_MAGIC_DELETEDATA;
ptr->vtable = &deletedatamagic_vtable;
ptr->pdl = it;
ptr->func = func;
ptr->param = param;
pdl__magic_add(it, (pdl_magic *)ptr);
}
|