| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include <esl_config.h> |
| 13 |
|
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
#ifdef HAVE_PTHREAD |
| 18 |
#include <pthread.h> |
| 19 |
#endif |
| 20 |
#ifdef HAVE_UNISTD_H |
| 21 |
#include <unistd.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "easel.h" |
| 25 |
#include "esl_random.h" |
| 26 |
|
| 27 |
#include "esl_stack.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
ESL_STACK * |
| 45 |
esl_stack_ICreate(void) |
| 46 |
{ |
| 47 |
ESL_STACK *ns = NULL; |
| 48 |
int status; |
| 49 |
|
| 50 |
ESL_ALLOC(ns, sizeof(ESL_STACK)); |
| 51 |
ns->nalloc = ESL_STACK_INITALLOC; |
| 52 |
ns->pdata = NULL; |
| 53 |
ns->cdata = NULL; |
| 54 |
ns->idata = NULL; |
| 55 |
ns->n = 0; |
| 56 |
#ifdef HAVE_PTHREAD |
| 57 |
ns->do_mutex = FALSE; |
| 58 |
ns->do_cond = FALSE; |
| 59 |
ns->mutex = NULL; |
| 60 |
ns->cond = NULL; |
| 61 |
#endif |
| 62 |
|
| 63 |
ESL_ALLOC(ns->idata, sizeof(int) * ns->nalloc); |
| 64 |
return ns; |
| 65 |
|
| 66 |
ERROR: |
| 67 |
esl_stack_Destroy(ns); |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
ESL_STACK * |
| 82 |
esl_stack_CCreate(void) |
| 83 |
{ |
| 84 |
ESL_STACK *cs = NULL; |
| 85 |
int status; |
| 86 |
|
| 87 |
ESL_ALLOC(cs, sizeof(ESL_STACK)); |
| 88 |
cs->nalloc = ESL_STACK_INITALLOC; |
| 89 |
cs->idata = NULL; |
| 90 |
cs->pdata = NULL; |
| 91 |
cs->cdata = NULL; |
| 92 |
cs->n = 0; |
| 93 |
#ifdef HAVE_PTHREAD |
| 94 |
cs->do_mutex = FALSE; |
| 95 |
cs->do_cond = FALSE; |
| 96 |
cs->mutex = NULL; |
| 97 |
cs->cond = NULL; |
| 98 |
#endif |
| 99 |
|
| 100 |
ESL_ALLOC(cs->cdata, sizeof(char) * cs->nalloc); |
| 101 |
return cs; |
| 102 |
|
| 103 |
ERROR: |
| 104 |
esl_stack_Destroy(cs); |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
ESL_STACK * |
| 119 |
esl_stack_PCreate(void) |
| 120 |
{ |
| 121 |
ESL_STACK *ps = NULL; |
| 122 |
int status; |
| 123 |
|
| 124 |
ESL_ALLOC(ps, sizeof(ESL_STACK)); |
| 125 |
ps->nalloc = ESL_STACK_INITALLOC; |
| 126 |
ps->idata = NULL; |
| 127 |
ps->cdata = NULL; |
| 128 |
ps->pdata = NULL; |
| 129 |
ps->n = 0; |
| 130 |
#ifdef HAVE_PTHREAD |
| 131 |
ps->do_mutex = FALSE; |
| 132 |
ps->do_cond = FALSE; |
| 133 |
ps->mutex = NULL; |
| 134 |
ps->cond = NULL; |
| 135 |
#endif |
| 136 |
|
| 137 |
ESL_ALLOC(ps->pdata, sizeof(void *) * ps->nalloc); |
| 138 |
return ps; |
| 139 |
|
| 140 |
ERROR: |
| 141 |
esl_stack_Destroy(ps); |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
int |
| 157 |
esl_stack_Reuse(ESL_STACK *s) |
| 158 |
{ |
| 159 |
s->n = 0; |
| 160 |
return eslOK; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
void |
| 170 |
esl_stack_Destroy(ESL_STACK *s) |
| 171 |
{ |
| 172 |
if (s) |
| 173 |
{ |
| 174 |
if (s->idata) free(s->idata); |
| 175 |
if (s->cdata) free(s->cdata); |
| 176 |
if (s->pdata) free(s->pdata); |
| 177 |
#ifdef HAVE_PTHREAD |
| 178 |
if (s->mutex) { pthread_mutex_destroy(s->mutex); free(s->mutex); } |
| 179 |
if (s->cond) { pthread_cond_destroy(s->cond); free(s->cond); } |
| 180 |
#endif |
| 181 |
free(s); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
int |
| 205 |
esl_stack_IPush(ESL_STACK *ns, int x) |
| 206 |
{ |
| 207 |
int *ptr = NULL; |
| 208 |
int status; |
| 209 |
|
| 210 |
#ifdef HAVE_PTHREAD |
| 211 |
if (ns->do_mutex) if (pthread_mutex_lock(ns->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_lock() failure"); |
| 212 |
#endif |
| 213 |
|
| 214 |
if (ns->n == ns->nalloc) { |
| 215 |
ESL_RALLOC(ns->idata, ptr, sizeof(int) * ns->nalloc * 2); |
| 216 |
ns->nalloc += ns->nalloc; |
| 217 |
} |
| 218 |
ns->idata[ns->n] = x; |
| 219 |
ns->n++; |
| 220 |
|
| 221 |
#ifdef HAVE_PTHREAD |
| 222 |
if (ns->do_cond) if (pthread_cond_signal(ns->cond) != 0) ESL_EXCEPTION(eslESYS, "pthread_cond_signal() failure"); |
| 223 |
if (ns->do_mutex) if (pthread_mutex_unlock(ns->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 224 |
#endif |
| 225 |
return eslOK; |
| 226 |
|
| 227 |
ERROR: |
| 228 |
#ifdef HAVE_PTHREAD |
| 229 |
if (ns->do_mutex) if (pthread_mutex_unlock(ns->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 230 |
#endif |
| 231 |
return status; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
int |
| 247 |
esl_stack_CPush(ESL_STACK *cs, char c) |
| 248 |
{ |
| 249 |
char *ptr = NULL; |
| 250 |
int status; |
| 251 |
|
| 252 |
#ifdef HAVE_PTHREAD |
| 253 |
if (cs->do_mutex) if (pthread_mutex_lock(cs->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_lock() failure"); |
| 254 |
#endif |
| 255 |
|
| 256 |
if (cs->n == cs->nalloc) { |
| 257 |
ESL_RALLOC(cs->cdata, ptr, sizeof(char) * cs->nalloc * 2); |
| 258 |
cs->nalloc += cs->nalloc; |
| 259 |
} |
| 260 |
cs->cdata[cs->n] = c; |
| 261 |
cs->n++; |
| 262 |
|
| 263 |
#ifdef HAVE_PTHREAD |
| 264 |
if (cs->do_cond) if (pthread_cond_signal(cs->cond) != 0) ESL_EXCEPTION(eslESYS, "pthread_cond_signal() failure"); |
| 265 |
if (cs->do_mutex) if (pthread_mutex_unlock(cs->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 266 |
#endif |
| 267 |
return eslOK; |
| 268 |
|
| 269 |
ERROR: |
| 270 |
#ifdef HAVE_PTHREAD |
| 271 |
if (cs->do_mutex) if (pthread_mutex_unlock(cs->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 272 |
#endif |
| 273 |
return status; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
int |
| 289 |
esl_stack_PPush(ESL_STACK *ps, void *p) |
| 290 |
{ |
| 291 |
void *ptr = NULL; |
| 292 |
int status; |
| 293 |
|
| 294 |
#ifdef HAVE_PTHREAD |
| 295 |
if (ps->do_mutex) if (pthread_mutex_lock(ps->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_lock() failure"); |
| 296 |
#endif |
| 297 |
|
| 298 |
if (ps->n == ps->nalloc) { |
| 299 |
ESL_RALLOC(ps->pdata, ptr, sizeof(void *) * ps->nalloc * 2); |
| 300 |
ps->nalloc += ps->nalloc; |
| 301 |
} |
| 302 |
ps->pdata[ps->n] = p; |
| 303 |
ps->n++; |
| 304 |
|
| 305 |
#ifdef HAVE_PTHREAD |
| 306 |
if (ps->do_cond) if (pthread_cond_signal(ps->cond) != 0) ESL_EXCEPTION(eslESYS, "pthread_cond_signal() failure"); |
| 307 |
if (ps->do_mutex) if (pthread_mutex_unlock(ps->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 308 |
#endif |
| 309 |
return eslOK; |
| 310 |
|
| 311 |
ERROR: |
| 312 |
#ifdef HAVE_PTHREAD |
| 313 |
if (ps->do_mutex) if (pthread_mutex_unlock(ps->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 314 |
#endif |
| 315 |
return status; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
int |
| 331 |
esl_stack_IPop(ESL_STACK *ns, int *ret_x) |
| 332 |
{ |
| 333 |
int status; |
| 334 |
#ifdef HAVE_PTHREAD |
| 335 |
if (ns->do_mutex) { if (pthread_mutex_lock(ns->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_lock() failure"); } |
| 336 |
while (ns->do_cond && ns->n == 0) { if (pthread_cond_wait(ns->cond, ns->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_cond_wait() failure"); } |
| 337 |
#endif |
| 338 |
|
| 339 |
if (ns->n == 0) |
| 340 |
{ |
| 341 |
*ret_x = 0; |
| 342 |
status = eslEOD; |
| 343 |
} |
| 344 |
else |
| 345 |
{ |
| 346 |
ns->n--; |
| 347 |
*ret_x = ns->idata[ns->n]; |
| 348 |
status = eslOK; |
| 349 |
} |
| 350 |
|
| 351 |
#ifdef HAVE_PTHREAD |
| 352 |
if (ns->do_mutex && pthread_mutex_unlock(ns->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 353 |
#endif |
| 354 |
return status; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
int |
| 370 |
esl_stack_CPop(ESL_STACK *cs, char *ret_c) |
| 371 |
{ |
| 372 |
int status; |
| 373 |
#ifdef HAVE_PTHREAD |
| 374 |
if (cs->do_mutex) { if (pthread_mutex_lock(cs->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_lock() failure"); } |
| 375 |
while (cs->do_cond && cs->n == 0) { if (pthread_cond_wait(cs->cond, cs->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_cond_wait() failure"); } |
| 376 |
#endif |
| 377 |
|
| 378 |
if (cs->n == 0) |
| 379 |
{ |
| 380 |
*ret_c = 0; |
| 381 |
status = eslEOD; |
| 382 |
} |
| 383 |
else |
| 384 |
{ |
| 385 |
cs->n--; |
| 386 |
*ret_c = cs->cdata[cs->n]; |
| 387 |
status = eslOK; |
| 388 |
} |
| 389 |
|
| 390 |
#ifdef HAVE_PTHREAD |
| 391 |
if (cs->do_mutex && pthread_mutex_unlock(cs->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 392 |
#endif |
| 393 |
return status; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
int |
| 409 |
esl_stack_PPop(ESL_STACK *ps, void **ret_p) |
| 410 |
{ |
| 411 |
int status; |
| 412 |
#ifdef HAVE_PTHREAD |
| 413 |
if (ps->do_mutex) { if (pthread_mutex_lock(ps->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_lock() failure"); } |
| 414 |
while (ps->do_cond && ps->n == 0) { if (pthread_cond_wait(ps->cond, ps->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_cond_wait() failure"); } |
| 415 |
#endif |
| 416 |
|
| 417 |
if (ps->n == 0) |
| 418 |
{ |
| 419 |
*ret_p = NULL; |
| 420 |
status = eslEOD; |
| 421 |
} |
| 422 |
else |
| 423 |
{ |
| 424 |
ps->n--; |
| 425 |
*ret_p = ps->pdata[ps->n]; |
| 426 |
status = eslOK; |
| 427 |
} |
| 428 |
|
| 429 |
#ifdef HAVE_PTHREAD |
| 430 |
if (ps->do_mutex && pthread_mutex_unlock(ps->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 431 |
#endif |
| 432 |
return status; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
int |
| 443 |
esl_stack_ObjectCount(ESL_STACK *s) |
| 444 |
{ |
| 445 |
return s->n; |
| 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 |
char * |
| 472 |
esl_stack_Convert2String(ESL_STACK *cs) |
| 473 |
{ |
| 474 |
char *s = NULL; |
| 475 |
void *tmp = NULL; |
| 476 |
int status; |
| 477 |
|
| 478 |
|
| 479 |
s = cs->cdata; |
| 480 |
cs->cdata = NULL; |
| 481 |
|
| 482 |
|
| 483 |
if (cs->n == cs->nalloc) |
| 484 |
ESL_RALLOC(s, tmp, sizeof(char) * (cs->nalloc +1)); |
| 485 |
s[cs->n] = '\0'; |
| 486 |
|
| 487 |
|
| 488 |
esl_stack_Destroy(cs); |
| 489 |
return s; |
| 490 |
|
| 491 |
ERROR: |
| 492 |
esl_stack_Destroy(cs); |
| 493 |
return NULL; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
int |
| 511 |
esl_stack_DiscardTopN(ESL_STACK *s, int n) |
| 512 |
{ |
| 513 |
#ifdef HAVE_PTHREAD |
| 514 |
if (s->do_mutex && pthread_mutex_lock(s->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_lock() failure"); |
| 515 |
#endif |
| 516 |
|
| 517 |
if (n <= s->n) s->n -= n; |
| 518 |
else s->n = 0; |
| 519 |
|
| 520 |
#ifdef HAVE_PTHREAD |
| 521 |
if (s->do_mutex && pthread_mutex_unlock(s->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 522 |
#endif |
| 523 |
return eslOK; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
int |
| 556 |
esl_stack_DiscardSelected(ESL_STACK *s, int (*discard_func)(void *, void *), void *param) |
| 557 |
{ |
| 558 |
int opos; |
| 559 |
int npos = 0; |
| 560 |
|
| 561 |
#ifdef HAVE_PTHREAD |
| 562 |
if (s->do_mutex && pthread_mutex_lock(s->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_lock() failure"); |
| 563 |
#endif |
| 564 |
|
| 565 |
if (s->idata) |
| 566 |
{ |
| 567 |
for (opos = 0, npos = 0 ; opos < s->n; opos++) |
| 568 |
if (! (*discard_func)(s->idata+opos, param)) |
| 569 |
s->idata[npos++] = s->idata[opos]; |
| 570 |
} |
| 571 |
else if (s->pdata) |
| 572 |
{ |
| 573 |
for (opos = 0, npos = 0 ; opos < s->n; opos++) |
| 574 |
if (! (*discard_func)(s->pdata[opos], param)) |
| 575 |
s->pdata[npos++] = s->pdata[opos]; |
| 576 |
} |
| 577 |
else if (s->cdata) |
| 578 |
{ |
| 579 |
for (opos = 0, npos = 0 ; opos < s->n; opos++) |
| 580 |
if (! (*discard_func)(s->cdata+opos, param)) |
| 581 |
s->cdata[npos++] = s->cdata[opos]; |
| 582 |
} |
| 583 |
s->n = npos; |
| 584 |
|
| 585 |
#ifdef HAVE_PTHREAD |
| 586 |
if (s->do_mutex && pthread_mutex_unlock(s->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 587 |
#endif |
| 588 |
return eslOK; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
int |
| 609 |
esl_stack_Shuffle(ESL_RANDOMNESS *r, ESL_STACK *s) |
| 610 |
{ |
| 611 |
int n = s->n; |
| 612 |
int w; |
| 613 |
|
| 614 |
#ifdef HAVE_PTHREAD |
| 615 |
if (s->do_mutex && pthread_mutex_lock(s->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_lock() failure"); |
| 616 |
#endif |
| 617 |
|
| 618 |
while (n > 1) { |
| 619 |
w = esl_rnd_Roll(r, n); |
| 620 |
if (s->idata != NULL) ESL_SWAP(s->idata[w], s->idata[n-1], int); |
| 621 |
else if (s->cdata != NULL) ESL_SWAP(s->cdata[w], s->cdata[n-1], char); |
| 622 |
else if (s->pdata != NULL) ESL_SWAP(s->pdata[w], s->pdata[n-1], void *); |
| 623 |
n--; |
| 624 |
} |
| 625 |
|
| 626 |
#ifdef HAVE_PTHREAD |
| 627 |
if (s->do_mutex && pthread_mutex_unlock(s->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 628 |
#endif |
| 629 |
return eslOK; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
#if defined HAVE_PTHREAD |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
int |
| 656 |
esl_stack_UseMutex(ESL_STACK *s) |
| 657 |
{ |
| 658 |
int pstatus; |
| 659 |
int status; |
| 660 |
|
| 661 |
ESL_ALLOC(s->mutex, sizeof(pthread_mutex_t)); |
| 662 |
if ((pstatus = pthread_mutex_init(s->mutex, NULL)) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_init failed with code %d\n", pstatus); |
| 663 |
s->do_mutex = TRUE; |
| 664 |
return eslOK; |
| 665 |
|
| 666 |
ERROR: |
| 667 |
if (s->mutex) free(s->mutex); |
| 668 |
s->mutex = NULL; |
| 669 |
s->do_mutex = FALSE; |
| 670 |
return status; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
int |
| 697 |
esl_stack_UseCond(ESL_STACK *s) |
| 698 |
{ |
| 699 |
int pstatus; |
| 700 |
int status; |
| 701 |
|
| 702 |
if (! s->do_mutex || ! s->mutex) ESL_EXCEPTION(eslEINVAL, "stack has no active mutex; can't call esl_stack_UseCond() on it"); |
| 703 |
|
| 704 |
ESL_ALLOC(s->cond, sizeof(pthread_cond_t)); |
| 705 |
if ((pstatus = pthread_cond_init(s->cond, NULL)) != 0) ESL_XEXCEPTION(eslESYS, "pthread_cond_init failed with code %d\n", pstatus); |
| 706 |
s->do_cond = TRUE; |
| 707 |
return eslOK; |
| 708 |
|
| 709 |
ERROR: |
| 710 |
if (s->cond) free(s->cond); |
| 711 |
s->cond = NULL; |
| 712 |
s->do_cond = FALSE; |
| 713 |
return status; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
int |
| 735 |
esl_stack_ReleaseCond(ESL_STACK *s) |
| 736 |
{ |
| 737 |
if (! s->do_mutex) ESL_EXCEPTION(eslESYS, "no mutex; esl_stack_ReleaseCond() call invalid"); |
| 738 |
if (! s->do_cond) ESL_EXCEPTION(eslESYS, "no conditional wait state is set"); |
| 739 |
|
| 740 |
if (pthread_mutex_lock(s->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_lock() failure"); |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
s->do_cond = FALSE; |
| 746 |
if (pthread_cond_broadcast(s->cond) != 0) ESL_EXCEPTION(eslESYS, "pthread_cond_broadcast() failure"); |
| 747 |
|
| 748 |
if (pthread_mutex_unlock(s->mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread_mutex_unlock() failure"); |
| 749 |
return eslOK; |
| 750 |
} |
| 751 |
#endif |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
#ifdef eslSTACK_TESTDRIVE |
| 760 |
|
| 761 |
#include "esl_random.h" |
| 762 |
|
| 763 |
static void |
| 764 |
utest_integer(void) |
| 765 |
{ |
| 766 |
char *msg = "integer stack basic unit test failed"; |
| 767 |
ESL_STACK *s = NULL; |
| 768 |
int n1 = ESL_STACK_INITALLOC*2+1; |
| 769 |
int n2 = 0; |
| 770 |
int i; |
| 771 |
int val; |
| 772 |
|
| 773 |
if ((s = esl_stack_ICreate()) == NULL) esl_fatal(msg); |
| 774 |
for (i = 0; i < n1; i++) if (esl_stack_IPush(s, i) != eslOK) esl_fatal(msg); |
| 775 |
while (esl_stack_IPop(s, &val) != eslEOD) n2++; |
| 776 |
if (n1 != n2) esl_fatal(msg); |
| 777 |
esl_stack_Reuse(s); |
| 778 |
|
| 779 |
|
| 780 |
for (i = 0; i < n1; i++) if (esl_stack_IPush(s, i) != eslOK) esl_fatal(msg); |
| 781 |
n2 = 0; |
| 782 |
while (esl_stack_ObjectCount(s)) { |
| 783 |
if (esl_stack_IPop(s, &val) != eslOK) esl_fatal(msg); |
| 784 |
n2++; |
| 785 |
} |
| 786 |
if (n1 != n2) esl_fatal(msg); |
| 787 |
esl_stack_Destroy(s); |
| 788 |
} |
| 789 |
|
| 790 |
static void |
| 791 |
utest_char(void) |
| 792 |
{ |
| 793 |
char *msg = "char stack basic unit test failed"; |
| 794 |
ESL_STACK *s = NULL; |
| 795 |
int n1 = ESL_STACK_INITALLOC*2+1; |
| 796 |
int n2 = 0; |
| 797 |
int i; |
| 798 |
char c; |
| 799 |
|
| 800 |
if ((s = esl_stack_CCreate()) == NULL) esl_fatal(msg); |
| 801 |
for (i = 0; i < n1; i++) if (esl_stack_CPush(s, 'X') != eslOK) esl_fatal(msg); |
| 802 |
while (esl_stack_CPop(s, &c) != eslEOD) { |
| 803 |
if (c != 'X') esl_fatal(msg); |
| 804 |
n2++; |
| 805 |
} |
| 806 |
if (n1 != n2) esl_fatal(msg); |
| 807 |
esl_stack_Reuse(s); |
| 808 |
|
| 809 |
|
| 810 |
for (i = 0; i < n1; i++) if (esl_stack_CPush(s, 'X') != eslOK) esl_fatal(msg); |
| 811 |
n2 = 0; |
| 812 |
while (esl_stack_ObjectCount(s)) { |
| 813 |
if (esl_stack_CPop(s, &c) != eslOK) esl_fatal(msg); |
| 814 |
n2++; |
| 815 |
} |
| 816 |
if (n1 != n2) esl_fatal(msg); |
| 817 |
esl_stack_Destroy(s); |
| 818 |
} |
| 819 |
|
| 820 |
static void |
| 821 |
utest_pointer(void) |
| 822 |
{ |
| 823 |
char *msg = "pointer stack basic unit test failed"; |
| 824 |
ESL_STACK *s = NULL; |
| 825 |
int n1 = ESL_STACK_INITALLOC*2+1; |
| 826 |
int n2 = 0; |
| 827 |
int i; |
| 828 |
void *p; |
| 829 |
|
| 830 |
if ((s = esl_stack_PCreate()) == NULL) esl_fatal(msg); |
| 831 |
for (i = 0; i < n1; i++) { |
| 832 |
p = malloc(sizeof(int) * 64); |
| 833 |
if (esl_stack_PPush(s, p) != eslOK) esl_fatal(msg); |
| 834 |
} |
| 835 |
while (esl_stack_PPop(s, &p) != eslEOD) { free(p); n2++; } |
| 836 |
if (n1 != n2) esl_fatal(msg); |
| 837 |
esl_stack_Reuse(s); |
| 838 |
|
| 839 |
|
| 840 |
for (i = 0; i < n1; i++) { |
| 841 |
p = malloc(sizeof(int) * 64); |
| 842 |
if (esl_stack_PPush(s, p) != eslOK) esl_fatal(msg); |
| 843 |
} |
| 844 |
n2 = 0; |
| 845 |
while (esl_stack_ObjectCount(s)) { |
| 846 |
if (esl_stack_PPop(s, &p) != eslOK) esl_fatal(msg); |
| 847 |
free(p); |
| 848 |
n2++; |
| 849 |
} |
| 850 |
if (n1 != n2) esl_fatal(msg); |
| 851 |
esl_stack_Destroy(s); |
| 852 |
} |
| 853 |
|
| 854 |
static void |
| 855 |
utest_convert2string(void) |
| 856 |
{ |
| 857 |
char *msg = "stack::convert2string unit test failed"; |
| 858 |
char *str = "ABCDEF"; |
| 859 |
ESL_STACK *s = NULL; |
| 860 |
int n = strlen(str); |
| 861 |
int i; |
| 862 |
char *result = NULL; |
| 863 |
|
| 864 |
if ((s = esl_stack_CCreate()) == NULL) esl_fatal(msg); |
| 865 |
for (i = 0; i < n; i++) if (esl_stack_CPush(s, str[i]) != eslOK) esl_fatal(msg); |
| 866 |
result = esl_stack_Convert2String(s); |
| 867 |
if (strcmp(result, str) != 0) esl_fatal(msg); |
| 868 |
free(result); |
| 869 |
} |
| 870 |
|
| 871 |
static void |
| 872 |
utest_shuffle(ESL_RANDOMNESS *r) |
| 873 |
{ |
| 874 |
char *msg = "stack shuffle unit test failed"; |
| 875 |
ESL_STACK *s = esl_stack_ICreate(); |
| 876 |
int n = ESL_STACK_INITALLOC*2+1; |
| 877 |
int *seen = malloc(sizeof(int) * n); |
| 878 |
int i; |
| 879 |
int val; |
| 880 |
|
| 881 |
for (i = 0; i < n; i++) esl_stack_IPush(s, i); |
| 882 |
esl_stack_Shuffle(r, s); |
| 883 |
|
| 884 |
for (i = 0; i < n; i++) seen[i] = 0; |
| 885 |
i = n-1; |
| 886 |
while (esl_stack_IPop(s, &val) != eslEOD) { |
| 887 |
seen[val]++; |
| 888 |
} |
| 889 |
for (i = 0; i < n; i++) if (seen[i] != 1) esl_fatal(msg); |
| 890 |
|
| 891 |
free(seen); |
| 892 |
esl_stack_Destroy(s); |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
static int |
| 897 |
discard_function(void *elemp, void *paramp) |
| 898 |
{ |
| 899 |
int elem = * (int *) elemp; |
| 900 |
int thresh = * (int *) paramp; |
| 901 |
return (elem > thresh) ? TRUE : FALSE; |
| 902 |
} |
| 903 |
|
| 904 |
static void |
| 905 |
utest_discard_selected(ESL_RANDOMNESS *r) |
| 906 |
{ |
| 907 |
char *msg = "stack: DiscardSelected() unit test failed"; |
| 908 |
ESL_STACK *ns = esl_stack_ICreate(); |
| 909 |
int n = 1000; |
| 910 |
int thresh = 42; |
| 911 |
int npass = 0; |
| 912 |
int val; |
| 913 |
int i; |
| 914 |
|
| 915 |
for (i = 0; i < n; i++) |
| 916 |
{ |
| 917 |
val = esl_rnd_Roll(r, 100) + 1; |
| 918 |
if (val <= thresh) npass++; |
| 919 |
esl_stack_IPush(ns, val); |
| 920 |
} |
| 921 |
|
| 922 |
if (esl_stack_DiscardSelected(ns, discard_function, &thresh) != eslOK) esl_fatal(msg); |
| 923 |
|
| 924 |
if (esl_stack_ObjectCount(ns) != npass) esl_fatal(msg); |
| 925 |
while (esl_stack_IPop(ns, &val) == eslOK) |
| 926 |
{ |
| 927 |
if (val > thresh) esl_fatal(msg); |
| 928 |
npass--; |
| 929 |
} |
| 930 |
if (npass != 0) esl_fatal(msg); |
| 931 |
esl_stack_Destroy(ns); |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
#ifdef HAVE_PTHREAD |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
struct threadtest_s { |
| 954 |
ESL_STACK *input; |
| 955 |
ESL_STACK *working; |
| 956 |
ESL_STACK *output; |
| 957 |
}; |
| 958 |
|
| 959 |
static void * |
| 960 |
pusher_thread(void *arg) |
| 961 |
{ |
| 962 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(0); |
| 963 |
struct threadtest_s *tt = (struct threadtest_s *) arg; |
| 964 |
int value; |
| 965 |
|
| 966 |
while ( esl_stack_IPop(tt->input, &value) == eslOK) |
| 967 |
{ |
| 968 |
usleep(esl_rnd_Roll(r, 100)+1); |
| 969 |
esl_stack_IPush(tt->working, value); |
| 970 |
} |
| 971 |
esl_randomness_Destroy(r); |
| 972 |
pthread_exit(NULL); |
| 973 |
} |
| 974 |
|
| 975 |
static void * |
| 976 |
popper_thread(void *arg) |
| 977 |
{ |
| 978 |
ESL_RANDOMNESS *r = esl_randomness_CreateFast(0); |
| 979 |
struct threadtest_s *tt = (struct threadtest_s *) arg; |
| 980 |
int value; |
| 981 |
|
| 982 |
while (esl_stack_IPop(tt->working, &value) == eslOK) |
| 983 |
{ |
| 984 |
usleep(esl_rnd_Roll(r, 100)+1); |
| 985 |
esl_stack_IPush(tt->output, value); |
| 986 |
} |
| 987 |
esl_randomness_Destroy(r); |
| 988 |
pthread_exit(NULL); |
| 989 |
} |
| 990 |
|
| 991 |
static void |
| 992 |
utest_interthread_comm(void) |
| 993 |
{ |
| 994 |
char *msg = "stack::interthread_comm unit test failed"; |
| 995 |
struct threadtest_s *tt = NULL; |
| 996 |
int njobs = 1000; |
| 997 |
int *ndone = NULL; |
| 998 |
pthread_t tid[4]; |
| 999 |
int i; |
| 1000 |
int value; |
| 1001 |
|
| 1002 |
ndone = malloc(sizeof(int) * njobs); |
| 1003 |
for (i = 0; i < njobs; i++) ndone[i] = 0; |
| 1004 |
|
| 1005 |
tt = malloc(sizeof(struct threadtest_s)); |
| 1006 |
tt->input = esl_stack_ICreate(); |
| 1007 |
tt->working = esl_stack_ICreate(); |
| 1008 |
tt->output = esl_stack_ICreate(); |
| 1009 |
|
| 1010 |
esl_stack_UseMutex(tt->input); |
| 1011 |
esl_stack_UseMutex(tt->output); |
| 1012 |
esl_stack_UseMutex(tt->working); |
| 1013 |
esl_stack_UseCond(tt->working); |
| 1014 |
|
| 1015 |
for (i = 0; i < njobs; i++) |
| 1016 |
esl_stack_IPush(tt->input, i); |
| 1017 |
|
| 1018 |
pthread_create(&(tid[0]), NULL, pusher_thread, tt); |
| 1019 |
pthread_create(&(tid[1]), NULL, pusher_thread, tt); |
| 1020 |
pthread_create(&(tid[2]), NULL, popper_thread, tt); |
| 1021 |
pthread_create(&(tid[3]), NULL, popper_thread, tt); |
| 1022 |
|
| 1023 |
pthread_join(tid[0], NULL); |
| 1024 |
pthread_join(tid[1], NULL); |
| 1025 |
|
| 1026 |
esl_stack_ReleaseCond(tt->working); |
| 1027 |
pthread_join(tid[2], NULL); |
| 1028 |
pthread_join(tid[3], NULL); |
| 1029 |
|
| 1030 |
while (esl_stack_IPop(tt->output, &value) == eslOK) |
| 1031 |
{ |
| 1032 |
if (value < 0 || value >= njobs) esl_fatal(msg); |
| 1033 |
ndone[value]++; |
| 1034 |
} |
| 1035 |
for (i = 0; i < njobs; i++) |
| 1036 |
if (ndone[i] != 1) esl_fatal(msg); |
| 1037 |
|
| 1038 |
free(ndone); |
| 1039 |
esl_stack_Destroy(tt->output); |
| 1040 |
esl_stack_Destroy(tt->working); |
| 1041 |
esl_stack_Destroy(tt->input); |
| 1042 |
free(tt); |
| 1043 |
return; |
| 1044 |
} |
| 1045 |
#endif |
| 1046 |
#endif |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
#ifdef eslSTACK_TESTDRIVE |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
#include "easel.h" |
| 1075 |
#include "esl_getopts.h" |
| 1076 |
#include "esl_random.h" |
| 1077 |
#include "esl_stack.h" |
| 1078 |
|
| 1079 |
static ESL_OPTIONS options[] = { |
| 1080 |
|
| 1081 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1082 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 1083 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1084 |
}; |
| 1085 |
static char usage[] = "[-options]"; |
| 1086 |
static char banner[] = "unit test driver for esl_stack module"; |
| 1087 |
|
| 1088 |
int |
| 1089 |
main(int argc, char **argv) |
| 1090 |
{ |
| 1091 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 1092 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 1093 |
|
| 1094 |
fprintf(stderr, "## %s\n", argv[0]); |
| 1095 |
fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 1096 |
|
| 1097 |
utest_integer(); |
| 1098 |
utest_char(); |
| 1099 |
utest_pointer(); |
| 1100 |
utest_convert2string(); |
| 1101 |
utest_shuffle(rng); |
| 1102 |
utest_discard_selected(rng); |
| 1103 |
|
| 1104 |
#ifdef HAVE_PTHREAD |
| 1105 |
utest_interthread_comm(); |
| 1106 |
#endif |
| 1107 |
|
| 1108 |
esl_randomness_Destroy(rng); |
| 1109 |
esl_getopts_Destroy(go); |
| 1110 |
|
| 1111 |
|
| 1112 |
fprintf(stderr, "# status = ok\n"); |
| 1113 |
return eslOK; |
| 1114 |
} |
| 1115 |
#endif |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
#ifdef eslSTACK_EXAMPLE |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
#include "easel.h" |
| 1130 |
#include "esl_stack.h" |
| 1131 |
|
| 1132 |
int |
| 1133 |
main(void) |
| 1134 |
{ |
| 1135 |
ESL_STACK *ns; |
| 1136 |
int x; |
| 1137 |
|
| 1138 |
ns = esl_stack_ICreate(); |
| 1139 |
esl_stack_IPush(ns, 42); |
| 1140 |
esl_stack_IPush(ns, 7); |
| 1141 |
esl_stack_IPush(ns, 3); |
| 1142 |
while (esl_stack_IPop(ns, &x) != eslEOD) |
| 1143 |
printf("%d\n", x); |
| 1144 |
esl_stack_Destroy(ns); |
| 1145 |
return 0; |
| 1146 |
} |
| 1147 |
|
| 1148 |
#endif |
| 1149 |
|
| 1150 |
|