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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
/*
* Copyright (c) 2000 Charles Ying. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the same terms as perl itself.
*
* Please note that this code falls under a different license than the
* other code found in Sendmail::Milter.
*
*/
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <pthread.h>
#include "intpools.h"
/*
** INIT_INTERPRETERS -- initialize the interpreter pool
**
** Parameters:
** ipool -- interpreter pool
** max_interp -- the maximum limit on interpreters allowed.
** max_requests -- the maximum limit on requests perinterpreter.
**
** Returns:
** none.
**
** Side Effects:
** Sets up the global variables for the interpreter pool.
*/
void
init_interpreters(ipool, max_interp, max_requests)
intpool_t *ipool;
int max_interp;
int max_requests;
{
int error;
memset(ipool, 0, sizeof(intpool_t));
/* Initialize the mutex */
if ((error = pthread_mutex_init(&(ipool->ip_mutex), NULL)) != 0)
croak("intpool pthread_mutex_init failed: %d", error);
/* Initialize the condition variable */
if ((error = pthread_cond_init(&(ipool->ip_cond), NULL)) != 0)
croak("intpool pthread_cond_init() failed: %d", error);
/* Lock interpreter table */
if ((error = pthread_mutex_lock(&(ipool->ip_mutex))) != 0)
croak("intpool pthread_mutex_lock() failed: %d", error);
/* Critical section */
/* Initialize the max number of interpreters */
ipool->ip_max = max_interp;
ipool->ip_retire = max_requests;
/* Initialize the free table */
ipool->ip_freequeue = (AV*) newAV();
/* Set the number of busy interpreters to zero. */
ipool->ip_busycount = 0;
/* This is the global interpreter that thread wrappers will clone .*/
ipool->ip_parent = PERL_GET_CONTEXT;
/* End critical section */
/* Unlock interpreter table */
if ((error = pthread_mutex_unlock(&(ipool->ip_mutex))) != 0)
croak("intpool pthread_mutex_unlock() failed: %d", error);
}
/*
** ALLOC_INTERPRETER_CACHE -- Allocate memory for interpreter cache.
**
** Parameters:
** interp -- Interpreter to allocate cache for.
** size -- Size of cache to allocate.
**
** Returns:
** none.
**
** Warning:
** This routine is not thread-safe.
*/
void
alloc_interpreter_cache(interp_t *interp, size_t size)
{
if ((interp->cache = malloc(size)) == NULL)
croak("failed to allocate memory for interpreter cache.");
}
/*
** FREE_INTERPRETER_CACHE -- Free memory used by interpreter cache.
**
** Parameters:
** interp -- Interpreter to free cache for.
**
** Returns:
** none.
**
** Warning:
** This routine is not thread-safe.
*/
void
free_interpreter_cache(interp_t *interp)
{
free(interp->cache);
interp->cache = NULL;
}
/*
** CREATE_INTERPRETER -- create an interpreter from the parent.
**
** Parameters:
** ipool -- interpreter pool
**
** Returns:
** An interpreter context cloned off the parent.
**
** Warning:
** This routine is not thread-safe.
*/
interp_t *
create_interpreter(ipool)
intpool_t *ipool;
{
interp_t *new_interp;
/* Clone the reference interpreter and use that. */
new_interp = (interp_t *) malloc(sizeof(interp_t));
new_interp->perl = perl_clone(ipool->ip_parent, FALSE);
new_interp->requests = 1;
new_interp->cache = NULL;
{
/* Hack from modperl until Perl 5.6.1 */
dTHXa(new_interp->perl);
if (PL_scopestack_ix == 0)
{
/* ENTER could expand. A lot. */
ENTER;
}
}
/* Restore the parent interpreter after a perl_clone() */
PERL_SET_CONTEXT(ipool->ip_parent);
return new_interp;
}
/*
** CLEANUP_INTERPRETER -- destroy an interpreter
**
** Parameters:
** ipool -- interpreter pool
** del_interp - the interp_t to destroy.
**
** Returns:
** none.
**
** Warning:
** This routine is not thread-safe.
*/
void
cleanup_interpreter(ipool, del_interp)
intpool_t *ipool;
interp_t *del_interp;
{
perl_destruct(del_interp->perl);
perl_free(del_interp->perl);
free_interpreter_cache(del_interp);
free(del_interp);
}
/*
** LOCK_INTERPRETER -- lock and retrieve a perl interpreter
**
** Parameters:
** ipool -- interpreter pool
**
** Returns:
** An interpreter context out of the interpreter pool.
**
** Side Effects:
** The caller has exclusive rights to the interpreter
** until the caller unlocks the interpreter.
**
** Warning:
** This routine will block until a free interpreter
** is available.
**
** (A timeout might be implemented in the future)
*/
interp_t *
lock_interpreter(ipool)
intpool_t *ipool;
{
int error;
SV *sv_value;
interp_t *new_interp;
/* Lock interpreter table */
if ((error = pthread_mutex_lock(&(ipool->ip_mutex))) != 0)
croak("intpool pthread_mutex_lock() failed: %d", error);
/* Critical section */
/*
** Predicate: Any available interpreters? (Free or createable)
**
** ASSERT: ipool->ip_busycount always contains the number of
** interpreters that are locked in the system.
*/
while ( !((ipool->ip_max == 0) ||
(ipool->ip_busycount < ipool->ip_max)) )
{
/* No. */
/* P(): Lock on the condition variable. */
if ((error = pthread_cond_wait( &(ipool->ip_cond),
&(ipool->ip_mutex) )) != 0)
{
croak("cond_wait failed waiting for interpreter: %d",
error);
}
/* When we wake up again, we might get a new interpreter. */
}
/* Restore the parent interpreter context */
PERL_SET_CONTEXT(ipool->ip_parent);
/* Any free interpreters on the queue? */
if (av_len(ipool->ip_freequeue) != -1)
{
/* Reuse an old interpreter */
sv_value = av_shift(ipool->ip_freequeue);
new_interp = (interp_t *) SvIV(sv_value);
/* Decrement the reference count. */
(void) SvREFCNT_dec(sv_value);
/* Increase the number of requests. */
new_interp->requests++;
/* Increment the number of busy interpreters */
ipool->ip_busycount++;
}
else /* No, there aren't, but we can still create one. */
{
new_interp = create_interpreter(ipool);
/* Increment the number of busy interpreters */
ipool->ip_busycount++;
}
/* End critical section */
/* Restore the parent interpreter context. */
PERL_SET_CONTEXT(ipool->ip_parent);
/* Unlock interpreter table */
if ((error = pthread_mutex_unlock(&(ipool->ip_mutex))) != 0)
croak("intpool pthread_mutex_unlock() failed: %d", error);
return new_interp;
}
/*
** UNLOCK_INTERPRETER -- unlock a perl interpreter
**
** Parameters:
** ipool -- interpreter pool
** busy_interp -- the interpreter context to unlock.
**
** Returns:
** none.
**
** Side Effects:
** The interpreter is placed back in the interpreter pool
** and the caller should immediately discard its pointer
** to the interpreter.
*/
void
unlock_interpreter(ipool, busy_interp)
intpool_t *ipool;
interp_t *busy_interp;
{
int error;
/* Lock interpreter table */
if ((error = pthread_mutex_lock(&(ipool->ip_mutex))) != 0)
croak("intpool pthread_mutex_lock() failed: %d", error);
/* Critical section */
/* Restore the parent interpreter context. */
PERL_SET_CONTEXT(ipool->ip_parent);
/* ASSERT(ipool->ip_busycount > 0)
if (ipool->ip_busycount <= 0)
croak("internal error: busy_count reached zero unexpectedly.");
/* Decrement the number of busy interpreters */
ipool->ip_busycount--;
if ((ipool->ip_retire != 0) &&
(busy_interp->requests > ipool->ip_retire))
{
/* Interpreter is too old, recycle it. */
cleanup_interpreter(ipool, busy_interp);
busy_interp = create_interpreter(ipool);
}
/* Stick busy_interp in the free table */
(void) av_push(ipool->ip_freequeue, newSViv((IV) busy_interp));
/* V(): Signal a thread that a new interpreter is available. */
if ((error = pthread_cond_signal(&(ipool->ip_cond))) != 0)
{
croak("cond_signal failed to signal a free interpreter: %d",
error);
}
/* Restore the parent interpreter context. */
PERL_SET_CONTEXT(ipool->ip_parent);
/* End critical section */
/* Unlock interpreter table */
if ((error = pthread_mutex_unlock(&(ipool->ip_mutex))) != 0)
croak("intpool pthread_mutex_unlock() failed: %d", error);
}
/*
** CLEANUP_INTERPRETERS -- clean up the interpreter pool
**
** Parameters:
** ipool -- interpreter pool
**
** Returns:
** none.
**
** Side Effects:
** Shuts down and cleans up the interpreter pool.
**
** Warning:
** All interpreters should be unlocked before
** calling this routine.
*/
void
cleanup_interpreters(ipool)
intpool_t *ipool;
{
int error;
SV *sv_value;
interp_t *del_interp;
/* Lock interpreter table */
if ((error = pthread_mutex_lock(&(ipool->ip_mutex))) != 0)
croak("intpool pthread_mutex_lock() failed: %d", error);
/* Critical section */
/* Restore the original interpreter context. */
PERL_SET_CONTEXT(ipool->ip_parent);
/* At some point, we really should V() all of the waiting threads. */
while (av_len(ipool->ip_freequeue) != -1)
{
/* Reuse an old interpreter */
sv_value = av_shift(ipool->ip_freequeue);
del_interp = (interp_t *) SvIV(sv_value);
/* Decrement the reference count. */
(void) SvREFCNT_dec(sv_value);
cleanup_interpreter(ipool, del_interp);
}
av_undef(ipool->ip_freequeue);
ipool->ip_freequeue = NULL;
/* Restore the original interpreter context. */
PERL_SET_CONTEXT(ipool->ip_parent);
/* End critical section */
/* Unlock interpreter table */
if ((error = pthread_mutex_unlock(&(ipool->ip_mutex))) != 0)
croak("intpool pthread_mutex_unlock() failed: %d", error);
/* Destroy the condition variable */
if ((error = pthread_cond_destroy(&(ipool->ip_cond))) != 0)
croak("intpool pthread_cond_destroy() failed: %d", error);
/* Destroy the intpool mutex */
if ((error = pthread_mutex_destroy(&(ipool->ip_mutex))) != 0)
croak("intpool pthread_mutex_destroy() failed: %d", error);
}
/* ---+ Interpreter pools test code. -------------------------------------- */
typedef void *(*test_callback_ptr)(void *);
static intpool_t T_pool;
#define GLOBAL_TEST "Sendmail::Milter::Callbacks::_test_callback"
void
test_run_callback(pTHX_ SV *callback)
{
int error;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSViv((IV) aTHX)));
PUTBACK;
printf("test_wrapper: Analysing callback...\n");
if (SvROK(callback) && (SvTYPE(SvRV(callback)) == SVt_PVCV))
{
printf("test_wrapper: It's a code reference to: 0x%08x\n",
SvRV(callback));
}
if (SvPOK(callback))
{
int len;
printf("test_wrapper: pointer to string... string is '%s'\n",
SvPV(callback, len));
}
printf("test_wrapper: Calling callback 0x%08x from aTHX 0x%08x.\n",
callback, aTHX);
call_sv(callback, G_DISCARD);
SPAGAIN;
PUTBACK;
FREETMPS;
LEAVE;
}
void *
test_callback_wrapper(void *arg)
{
interp_t *interp;
SV *callback;
if ((interp = lock_interpreter(&T_pool)) == NULL)
croak("test_wrapper: could not lock a new perl interpreter.");
PERL_SET_CONTEXT(interp->perl);
callback = get_sv(GLOBAL_TEST, FALSE);
test_run_callback(aTHX_ callback);
unlock_interpreter(&T_pool, interp);
return NULL;
}
int
test_intpools(pTHX_ int max_interp, int max_requests, int i_max, int j_max,
SV* callback)
{
int i;
int j;
pthread_t thread_id;
SV *global_callback;
printf("test_wrapper: Original interpreter cloned: 0x%08x\n", aTHX);
init_interpreters(&T_pool, max_interp, max_requests);
global_callback = get_sv(GLOBAL_TEST, TRUE);
sv_setsv(global_callback, callback);
for (i = 0; i < i_max; i++)
{
for (j = 0; j < j_max; j++)
pthread_create(&thread_id, NULL,
(test_callback_ptr) test_callback_wrapper,
(void *)NULL);
pthread_join(thread_id, NULL);
}
cleanup_interpreters(&T_pool);
return 1;
}
|