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 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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 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 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "testutil.h"
#include "apr_strings.h"
#include "apr_thread_proc.h"
#include "apr_errno.h"
#include "apr_general.h"
#include "apr_atomic.h"
#include "apr_time.h"
/* Use pthread_setconcurrency where it is available and not a nullop,
* i.e. platforms using M:N or M:1 thread models: */
#if APR_HAS_THREADS && \
((defined(SOLARIS2) && SOLARIS2 > 6) || defined(_AIX))
/* also HP-UX, IRIX? ... */
#define HAVE_PTHREAD_SETCONCURRENCY
#endif
#ifdef HAVE_PTHREAD_SETCONCURRENCY
#include <pthread.h>
#endif
static void test_init(abts_case *tc, void *data)
{
APR_ASSERT_SUCCESS(tc, "Could not initliaze atomics", apr_atomic_init(p));
}
static void test_set32(abts_case *tc, void *data)
{
apr_uint32_t y32;
apr_atomic_set32(&y32, 2);
ABTS_UINT_EQUAL(tc, 2, y32);
}
static void test_read32(abts_case *tc, void *data)
{
apr_uint32_t y32;
apr_atomic_set32(&y32, 2);
ABTS_UINT_EQUAL(tc, 2, apr_atomic_read32(&y32));
}
static void test_dec32(abts_case *tc, void *data)
{
apr_uint32_t y32;
int rv;
apr_atomic_set32(&y32, 2);
rv = apr_atomic_dec32(&y32);
ABTS_UINT_EQUAL(tc, 1, y32);
ABTS_ASSERT(tc, "atomic_dec returned zero when it shouldn't", rv != 0);
rv = apr_atomic_dec32(&y32);
ABTS_UINT_EQUAL(tc, 0, y32);
ABTS_ASSERT(tc, "atomic_dec didn't returned zero when it should", rv == 0);
}
static void test_xchg32(abts_case *tc, void *data)
{
apr_uint32_t oldval;
apr_uint32_t y32;
apr_atomic_set32(&y32, 100);
oldval = apr_atomic_xchg32(&y32, 50);
ABTS_UINT_EQUAL(tc, 100, oldval);
ABTS_UINT_EQUAL(tc, 50, y32);
}
static void test_xchgptr(abts_case *tc, void *data)
{
int a;
void *ref = "little piggy";
volatile void *target_ptr = ref;
void *old_ptr;
old_ptr = apr_atomic_xchgptr(&target_ptr, &a);
ABTS_PTR_EQUAL(tc, ref, old_ptr);
ABTS_PTR_EQUAL(tc, (void *)&a, (void *)target_ptr);
}
static void test_cas_equal(abts_case *tc, void *data)
{
apr_uint32_t casval = 0;
apr_uint32_t oldval;
oldval = apr_atomic_cas32(&casval, 12, 0);
ABTS_UINT_EQUAL(tc, 0, oldval);
ABTS_UINT_EQUAL(tc, 12, casval);
}
static void test_cas_equal_nonnull(abts_case *tc, void *data)
{
apr_uint32_t casval = 12;
apr_uint32_t oldval;
oldval = apr_atomic_cas32(&casval, 23, 12);
ABTS_UINT_EQUAL(tc, 12, oldval);
ABTS_UINT_EQUAL(tc, 23, casval);
}
static void test_cas_notequal(abts_case *tc, void *data)
{
apr_uint32_t casval = 12;
apr_uint32_t oldval;
oldval = apr_atomic_cas32(&casval, 23, 2);
ABTS_UINT_EQUAL(tc, 12, oldval);
ABTS_UINT_EQUAL(tc, 12, casval);
}
static void test_casptr_equal(abts_case *tc, void *data)
{
int a = 0;
volatile void *target_ptr = NULL;
void *old_ptr;
old_ptr = apr_atomic_casptr(&target_ptr, &a, NULL);
ABTS_PTR_EQUAL(tc, NULL, old_ptr);
ABTS_PTR_EQUAL(tc, (void *)&a, (void *)target_ptr);
}
static void test_casptr_equal_nonnull(abts_case *tc, void *data)
{
int a = 0, b = 0;
volatile void *target_ptr = &a;
void *old_ptr;
old_ptr = apr_atomic_casptr(&target_ptr, &b, &a);
ABTS_PTR_EQUAL(tc, (void *)&a, old_ptr);
ABTS_PTR_EQUAL(tc, (void *)&b, (void *)target_ptr);
}
static void test_casptr_notequal(abts_case *tc, void *data)
{
int a = 0, b = 0;
volatile void *target_ptr = &a;
void *old_ptr;
old_ptr = apr_atomic_casptr(&target_ptr, &a, &b);
ABTS_PTR_EQUAL(tc, (void *)&a, old_ptr);
ABTS_PTR_EQUAL(tc, (void *)&a, (void *)target_ptr);
}
static void test_add32(abts_case *tc, void *data)
{
apr_uint32_t oldval;
apr_uint32_t y32;
apr_atomic_set32(&y32, 23);
oldval = apr_atomic_add32(&y32, 4);
ABTS_UINT_EQUAL(tc, 23, oldval);
ABTS_UINT_EQUAL(tc, 27, y32);
}
static void test_add32_neg(abts_case *tc, void *data)
{
apr_uint32_t oldval;
apr_uint32_t y32;
apr_atomic_set32(&y32, 23);
oldval = apr_atomic_add32(&y32, -10);
ABTS_UINT_EQUAL(tc, 23, oldval);
ABTS_UINT_EQUAL(tc, 13, y32);
}
static void test_inc32(abts_case *tc, void *data)
{
apr_uint32_t oldval;
apr_uint32_t y32;
apr_atomic_set32(&y32, 23);
oldval = apr_atomic_inc32(&y32);
ABTS_UINT_EQUAL(tc, 23, oldval);
ABTS_UINT_EQUAL(tc, 24, y32);
}
static void test_set_add_inc_sub(abts_case *tc, void *data)
{
apr_uint32_t y32;
apr_atomic_set32(&y32, 0);
apr_atomic_add32(&y32, 20);
apr_atomic_inc32(&y32);
apr_atomic_sub32(&y32, 10);
ABTS_UINT_EQUAL(tc, 11, y32);
}
static void test_wrap_zero(abts_case *tc, void *data)
{
apr_uint32_t y32;
apr_uint32_t rv;
apr_uint32_t minus1 = (apr_uint32_t)-1;
char *str;
apr_atomic_set32(&y32, 0);
rv = apr_atomic_dec32(&y32);
ABTS_ASSERT(tc, "apr_atomic_dec32 on zero returned zero.", rv != 0);
str = apr_psprintf(p, "zero wrap failed: 0 - 1 = %d", y32);
ABTS_ASSERT(tc, str, y32 == minus1);
}
static void test_inc_neg1(abts_case *tc, void *data)
{
apr_uint32_t y32 = (apr_uint32_t)-1;
apr_uint32_t minus1 = (apr_uint32_t)-1;
apr_uint32_t rv;
char *str;
rv = apr_atomic_inc32(&y32);
ABTS_ASSERT(tc, "apr_atomic_inc32 didn't return the old value.", rv == minus1);
str = apr_psprintf(p, "zero wrap failed: -1 + 1 = %d", y32);
ABTS_ASSERT(tc, str, y32 == 0);
}
static void test_set64(abts_case *tc, void *data)
{
apr_uint64_t y64;
apr_atomic_set64(&y64, 2);
ABTS_ULLONG_EQUAL(tc, 2, y64);
}
static void test_read64(abts_case *tc, void *data)
{
apr_uint64_t y64;
apr_atomic_set64(&y64, 2);
ABTS_ULLONG_EQUAL(tc, 2, apr_atomic_read64(&y64));
}
static void test_dec64(abts_case *tc, void *data)
{
apr_uint64_t y64;
int rv;
apr_atomic_set64(&y64, 2);
rv = apr_atomic_dec64(&y64);
ABTS_ULLONG_EQUAL(tc, 1, y64);
ABTS_ASSERT(tc, "atomic_dec returned zero when it shouldn't", rv != 0);
rv = apr_atomic_dec64(&y64);
ABTS_ULLONG_EQUAL(tc, 0, y64);
ABTS_ASSERT(tc, "atomic_dec didn't returned zero when it should", rv == 0);
}
static void test_xchg64(abts_case *tc, void *data)
{
apr_uint64_t oldval;
apr_uint64_t y64;
apr_atomic_set64(&y64, 100);
oldval = apr_atomic_xchg64(&y64, 50);
ABTS_ULLONG_EQUAL(tc, 100, oldval);
ABTS_ULLONG_EQUAL(tc, 50, y64);
}
static void test_add64(abts_case *tc, void *data)
{
apr_uint64_t oldval;
apr_uint64_t y64;
apr_atomic_set64(&y64, 23);
oldval = apr_atomic_add64(&y64, 4);
ABTS_ULLONG_EQUAL(tc, 23, oldval);
ABTS_ULLONG_EQUAL(tc, 27, y64);
}
static void test_add64_neg(abts_case *tc, void *data)
{
apr_uint64_t oldval;
apr_uint64_t y64;
apr_atomic_set64(&y64, 23);
oldval = apr_atomic_add64(&y64, -10);
ABTS_ULLONG_EQUAL(tc, 23, oldval);
ABTS_ULLONG_EQUAL(tc, 13, y64);
}
static void test_inc64(abts_case *tc, void *data)
{
apr_uint64_t oldval;
apr_uint64_t y64;
apr_atomic_set64(&y64, 23);
oldval = apr_atomic_inc64(&y64);
ABTS_ULLONG_EQUAL(tc, 23, oldval);
ABTS_ULLONG_EQUAL(tc, 24, y64);
}
static void test_set_add_inc_sub64(abts_case *tc, void *data)
{
apr_uint64_t y64;
apr_atomic_set64(&y64, 0);
apr_atomic_add64(&y64, 20);
apr_atomic_inc64(&y64);
apr_atomic_sub64(&y64, 10);
ABTS_ULLONG_EQUAL(tc, 11, y64);
}
static void test_wrap_zero64(abts_case *tc, void *data)
{
apr_uint64_t y64;
apr_uint64_t rv;
apr_uint64_t minus1 = (apr_uint64_t)-1;
char *str;
apr_atomic_set64(&y64, 0);
rv = apr_atomic_dec64(&y64);
ABTS_ASSERT(tc, "apr_atomic_dec64 on zero returned zero.", rv != 0);
str = apr_psprintf(p, "zero wrap failed: 0 - 1 = %lu", y64);
ABTS_ASSERT(tc, str, y64 == minus1);
}
static void test_inc_neg164(abts_case *tc, void *data)
{
apr_uint64_t y64 = (apr_uint64_t)-1;
apr_uint64_t minus1 = (apr_uint64_t)-1;
apr_uint64_t rv;
char *str;
rv = apr_atomic_inc64(&y64);
ABTS_ASSERT(tc, "apr_atomic_inc64 didn't return the old value.", rv == minus1);
str = apr_psprintf(p, "zero wrap failed: -1 + 1 = %lu", y64);
ABTS_ASSERT(tc, str, y64 == 0);
}
#if APR_HAS_THREADS
void *APR_THREAD_FUNC thread_func_mutex(apr_thread_t *thd, void *data);
void *APR_THREAD_FUNC thread_func_mutex64(apr_thread_t *thd, void *data);
void *APR_THREAD_FUNC thread_func_atomic(apr_thread_t *thd, void *data);
void *APR_THREAD_FUNC thread_func_atomic64(apr_thread_t *thd, void *data);
apr_thread_mutex_t *thread_lock;
apr_thread_mutex_t *thread_lock64;
apr_uint32_t mutex_locks = 0;
apr_uint64_t mutex_locks64 = 0;
apr_uint32_t atomic_ops = 0;
apr_uint64_t atomic_ops64 = 0;
#ifndef CACHELINE_SIZE
#define CACHELINE_SIZE 64
#endif
struct {
char pad[CACHELINE_SIZE - 5];
apr_uint64_t ops64;
} atomic_pad __attribute__((aligned(CACHELINE_SIZE)));
apr_status_t exit_ret_val = 123; /* just some made up number to check on later */
#define NUM_THREADS 40
#define NUM_ITERATIONS 20000
void *APR_THREAD_FUNC thread_func_mutex(apr_thread_t *thd, void *data)
{
int i;
for (i = 0; i < NUM_ITERATIONS; i++) {
apr_thread_mutex_lock(thread_lock);
mutex_locks++;
apr_thread_mutex_unlock(thread_lock);
}
apr_thread_exit(thd, exit_ret_val);
return NULL;
}
void *APR_THREAD_FUNC thread_func_atomic(apr_thread_t *thd, void *data)
{
int i;
for (i = 0; i < NUM_ITERATIONS ; i++) {
apr_atomic_inc32(&atomic_ops);
apr_atomic_add32(&atomic_ops, 2);
apr_atomic_dec32(&atomic_ops);
apr_atomic_dec32(&atomic_ops);
}
apr_thread_exit(thd, exit_ret_val);
return NULL;
}
static void test_atomics_threaded(abts_case *tc, void *data)
{
apr_thread_t *t1[NUM_THREADS];
apr_thread_t *t2[NUM_THREADS];
apr_status_t rv;
int i;
#ifdef HAVE_PTHREAD_SETCONCURRENCY
pthread_setconcurrency(8);
#endif
rv = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, p);
APR_ASSERT_SUCCESS(tc, "Could not create lock", rv);
for (i = 0; i < NUM_THREADS; i++) {
apr_status_t r1, r2;
r1 = apr_thread_create(&t1[i], NULL, thread_func_mutex, NULL, p);
r2 = apr_thread_create(&t2[i], NULL, thread_func_atomic, NULL, p);
ABTS_ASSERT(tc, "Failed creating threads", !r1 && !r2);
}
for (i = 0; i < NUM_THREADS; i++) {
apr_status_t s1, s2;
apr_thread_join(&s1, t1[i]);
apr_thread_join(&s2, t2[i]);
ABTS_ASSERT(tc, "Invalid return value from thread_join",
s1 == exit_ret_val && s2 == exit_ret_val);
}
ABTS_UINT_EQUAL(tc, NUM_THREADS * NUM_ITERATIONS, mutex_locks);
ABTS_UINT_EQUAL(tc, NUM_THREADS * NUM_ITERATIONS,
apr_atomic_read32(&atomic_ops));
rv = apr_thread_mutex_destroy(thread_lock);
ABTS_ASSERT(tc, "Failed creating threads", rv == APR_SUCCESS);
}
#undef NUM_THREADS
#define NUM_THREADS 7
typedef struct tbox_t tbox_t;
typedef struct tbox_t64 tbox_t64;
struct tbox_t {
abts_case *tc;
apr_uint32_t *mem;
apr_uint32_t preval;
apr_uint32_t postval;
apr_uint32_t loop;
void (*func)(tbox_t *box);
};
static APR_INLINE void busyloop_read32(tbox_t *tbox)
{
apr_uint32_t val;
do {
val = apr_atomic_read32(tbox->mem);
if (val != tbox->preval)
apr_thread_yield();
else
break;
} while (1);
}
static void busyloop_set32(tbox_t *tbox)
{
do {
busyloop_read32(tbox);
apr_atomic_set32(tbox->mem, tbox->postval);
} while (--tbox->loop);
}
static void busyloop_add32(tbox_t *tbox)
{
apr_uint32_t val;
do {
busyloop_read32(tbox);
val = apr_atomic_add32(tbox->mem, tbox->postval);
apr_thread_mutex_lock(thread_lock);
ABTS_UINT_EQUAL(tbox->tc, val, tbox->preval);
apr_thread_mutex_unlock(thread_lock);
} while (--tbox->loop);
}
static void busyloop_sub32(tbox_t *tbox)
{
do {
busyloop_read32(tbox);
apr_atomic_sub32(tbox->mem, tbox->postval);
} while (--tbox->loop);
}
static void busyloop_inc32(tbox_t *tbox)
{
apr_uint32_t val;
do {
busyloop_read32(tbox);
val = apr_atomic_inc32(tbox->mem);
apr_thread_mutex_lock(thread_lock);
ABTS_UINT_EQUAL(tbox->tc, val, tbox->preval);
apr_thread_mutex_unlock(thread_lock);
} while (--tbox->loop);
}
static void busyloop_dec32(tbox_t *tbox)
{
apr_uint32_t val;
do {
busyloop_read32(tbox);
val = apr_atomic_dec32(tbox->mem);
apr_thread_mutex_lock(thread_lock);
ABTS_UINT_NEQUAL(tbox->tc, 0, val);
apr_thread_mutex_unlock(thread_lock);
} while (--tbox->loop);
}
static void busyloop_cas32(tbox_t *tbox)
{
apr_uint32_t val;
do {
do {
val = apr_atomic_cas32(tbox->mem, tbox->postval, tbox->preval);
if (val != tbox->preval)
apr_thread_yield();
else
break;
} while (1);
} while (--tbox->loop);
}
static void busyloop_xchg32(tbox_t *tbox)
{
apr_uint32_t val;
do {
busyloop_read32(tbox);
val = apr_atomic_xchg32(tbox->mem, tbox->postval);
apr_thread_mutex_lock(thread_lock);
ABTS_UINT_EQUAL(tbox->tc, val, tbox->preval);
apr_thread_mutex_unlock(thread_lock);
} while (--tbox->loop);
}
static void *APR_THREAD_FUNC thread_func_busyloop(apr_thread_t *thd, void *data)
{
tbox_t *tbox = data;
tbox->func(tbox);
apr_thread_exit(thd, 0);
return NULL;
}
static void test_atomics_busyloop_threaded(abts_case *tc, void *data)
{
unsigned int i;
apr_status_t rv;
apr_uint32_t count = 0;
tbox_t tbox[NUM_THREADS];
apr_thread_t *thread[NUM_THREADS];
rv = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, p);
APR_ASSERT_SUCCESS(tc, "Could not create lock", rv);
/* get ready */
for (i = 0; i < NUM_THREADS; i++) {
tbox[i].tc = tc;
tbox[i].mem = &count;
tbox[i].loop = 50;
}
tbox[0].preval = 98;
tbox[0].postval = 3891;
tbox[0].func = busyloop_add32;
tbox[1].preval = 3989;
tbox[1].postval = 1010;
tbox[1].func = busyloop_sub32;
tbox[2].preval = 2979;
tbox[2].postval = 0; /* not used */
tbox[2].func = busyloop_inc32;
tbox[3].preval = 2980;
tbox[3].postval = 16384;
tbox[3].func = busyloop_set32;
tbox[4].preval = 16384;
tbox[4].postval = 0; /* not used */
tbox[4].func = busyloop_dec32;
tbox[5].preval = 16383;
tbox[5].postval = 1048576;
tbox[5].func = busyloop_cas32;
tbox[6].preval = 1048576;
tbox[6].postval = 98; /* goto tbox[0] */
tbox[6].func = busyloop_xchg32;
/* get set */
for (i = 0; i < NUM_THREADS; i++) {
rv = apr_thread_create(&thread[i], NULL, thread_func_busyloop,
&tbox[i], p);
ABTS_ASSERT(tc, "Failed creating thread", rv == APR_SUCCESS);
}
/* go! */
apr_atomic_set32(tbox->mem, 98);
for (i = 0; i < NUM_THREADS; i++) {
apr_status_t retval;
rv = apr_thread_join(&retval, thread[i]);
ABTS_ASSERT(tc, "Thread join failed", rv == APR_SUCCESS);
ABTS_ASSERT(tc, "Invalid return value from thread_join", retval == 0);
}
ABTS_UINT_EQUAL(tbox->tc, 98, count);
rv = apr_thread_mutex_destroy(thread_lock);
ABTS_ASSERT(tc, "Failed creating threads", rv == APR_SUCCESS);
}
void *APR_THREAD_FUNC thread_func_mutex64(apr_thread_t *thd, void *data)
{
int i;
for (i = 0; i < NUM_ITERATIONS; i++) {
apr_thread_mutex_lock(thread_lock64);
mutex_locks64++;
apr_thread_mutex_unlock(thread_lock64);
}
apr_thread_exit(thd, exit_ret_val);
return NULL;
}
void *APR_THREAD_FUNC thread_func_atomic64(apr_thread_t *thd, void *data)
{
apr_uint64_t *ops64 = data;
int i;
for (i = 0; i < NUM_ITERATIONS ; i++) {
apr_atomic_inc64(ops64);
apr_atomic_add64(ops64, 2);
apr_atomic_dec64(ops64);
apr_atomic_dec64(ops64);
}
apr_thread_exit(thd, exit_ret_val);
return NULL;
}
static void test_atomics_threaded64(abts_case *tc, void *data)
{
apr_uint64_t *ops64 = data;
apr_thread_t *t1[NUM_THREADS];
apr_thread_t *t2[NUM_THREADS];
apr_status_t rv;
int i;
#ifdef HAVE_PTHREAD_SETCONCURRENCY
pthread_setconcurrency(8);
#endif
mutex_locks64 = 0;
apr_atomic_set64(ops64, 0);
rv = apr_thread_mutex_create(&thread_lock64, APR_THREAD_MUTEX_DEFAULT, p);
APR_ASSERT_SUCCESS(tc, "Could not create lock", rv);
for (i = 0; i < NUM_THREADS; i++) {
apr_status_t r1, r2;
r1 = apr_thread_create(&t1[i], NULL, thread_func_mutex64, NULL, p);
r2 = apr_thread_create(&t2[i], NULL, thread_func_atomic64, ops64, p);
ABTS_ASSERT(tc, "Failed creating threads", !r1 && !r2);
}
for (i = 0; i < NUM_THREADS; i++) {
apr_status_t s1, s2;
apr_thread_join(&s1, t1[i]);
apr_thread_join(&s2, t2[i]);
ABTS_ASSERT(tc, "Invalid return value from thread_join",
s1 == exit_ret_val && s2 == exit_ret_val);
}
ABTS_ULLONG_EQUAL(tc, NUM_THREADS * NUM_ITERATIONS, mutex_locks64);
ABTS_ULLONG_EQUAL(tc, NUM_THREADS * NUM_ITERATIONS,
apr_atomic_read64(ops64));
rv = apr_thread_mutex_destroy(thread_lock64);
ABTS_ASSERT(tc, "Failed creating threads", rv == APR_SUCCESS);
}
struct tbox_t64 {
abts_case *tc;
apr_uint64_t *mem;
apr_uint64_t preval;
apr_uint64_t postval;
apr_uint64_t loop;
void (*func)(tbox_t64 *box);
};
static APR_INLINE void busyloop_read64(tbox_t64 *tbox)
{
apr_uint64_t val;
do {
val = apr_atomic_read64(tbox->mem);
if (val != tbox->preval)
apr_thread_yield();
else
break;
} while (1);
}
static void busyloop_set64(tbox_t64 *tbox)
{
do {
busyloop_read64(tbox);
apr_atomic_set64(tbox->mem, tbox->postval);
} while (--tbox->loop);
}
static void busyloop_add64(tbox_t64 *tbox)
{
apr_uint64_t val;
do {
busyloop_read64(tbox);
val = apr_atomic_add64(tbox->mem, tbox->postval);
apr_thread_mutex_lock(thread_lock64);
ABTS_ULLONG_EQUAL(tbox->tc, val, tbox->preval);
apr_thread_mutex_unlock(thread_lock64);
} while (--tbox->loop);
}
static void busyloop_sub64(tbox_t64 *tbox)
{
do {
busyloop_read64(tbox);
apr_atomic_sub64(tbox->mem, tbox->postval);
} while (--tbox->loop);
}
static void busyloop_inc64(tbox_t64 *tbox)
{
apr_uint64_t val;
do {
busyloop_read64(tbox);
val = apr_atomic_inc64(tbox->mem);
apr_thread_mutex_lock(thread_lock64);
ABTS_ULLONG_EQUAL(tbox->tc, val, tbox->preval);
apr_thread_mutex_unlock(thread_lock64);
} while (--tbox->loop);
}
static void busyloop_dec64(tbox_t64 *tbox)
{
apr_uint64_t val;
do {
busyloop_read64(tbox);
val = apr_atomic_dec64(tbox->mem);
apr_thread_mutex_lock(thread_lock64);
ABTS_ULLONG_NEQUAL(tbox->tc, 0, val);
apr_thread_mutex_unlock(thread_lock64);
} while (--tbox->loop);
}
static void busyloop_cas64(tbox_t64 *tbox)
{
apr_uint64_t val;
do {
do {
val = apr_atomic_cas64(tbox->mem, tbox->postval, tbox->preval);
if (val != tbox->preval)
apr_thread_yield();
else
break;
} while (1);
} while (--tbox->loop);
}
static void busyloop_xchg64(tbox_t64 *tbox)
{
apr_uint64_t val;
do {
busyloop_read64(tbox);
val = apr_atomic_xchg64(tbox->mem, tbox->postval);
apr_thread_mutex_lock(thread_lock64);
ABTS_ULLONG_EQUAL(tbox->tc, val, tbox->preval);
apr_thread_mutex_unlock(thread_lock64);
} while (--tbox->loop);
}
static void *APR_THREAD_FUNC thread_func_busyloop64(apr_thread_t *thd, void *data)
{
tbox_t64 *tbox = data;
tbox->func(tbox);
apr_thread_exit(thd, 0);
return NULL;
}
static void test_atomics_busyloop_threaded64(abts_case *tc, void *data)
{
unsigned int i;
apr_status_t rv;
apr_uint64_t count = 0;
tbox_t64 tbox[NUM_THREADS];
apr_thread_t *thread[NUM_THREADS];
rv = apr_thread_mutex_create(&thread_lock64, APR_THREAD_MUTEX_DEFAULT, p);
APR_ASSERT_SUCCESS(tc, "Could not create lock", rv);
/* get ready */
for (i = 0; i < NUM_THREADS; i++) {
tbox[i].tc = tc;
tbox[i].mem = &count;
tbox[i].loop = 50;
}
tbox[0].preval = 98;
tbox[0].postval = 3891;
tbox[0].func = busyloop_add64;
tbox[1].preval = 3989;
tbox[1].postval = 1010;
tbox[1].func = busyloop_sub64;
tbox[2].preval = 2979;
tbox[2].postval = 0; /* not used */
tbox[2].func = busyloop_inc64;
tbox[3].preval = 2980;
tbox[3].postval = 16384;
tbox[3].func = busyloop_set64;
tbox[4].preval = 16384;
tbox[4].postval = 0; /* not used */
tbox[4].func = busyloop_dec64;
tbox[5].preval = 16383;
tbox[5].postval = 1048576;
tbox[5].func = busyloop_cas64;
tbox[6].preval = 1048576;
tbox[6].postval = 98; /* goto tbox[0] */
tbox[6].func = busyloop_xchg64;
/* get set */
for (i = 0; i < NUM_THREADS; i++) {
rv = apr_thread_create(&thread[i], NULL, thread_func_busyloop64,
&tbox[i], p);
ABTS_ASSERT(tc, "Failed creating thread", rv == APR_SUCCESS);
}
/* go! */
apr_atomic_set64(tbox->mem, 98);
for (i = 0; i < NUM_THREADS; i++) {
apr_status_t retval;
rv = apr_thread_join(&retval, thread[i]);
ABTS_ASSERT(tc, "Thread join failed", rv == APR_SUCCESS);
ABTS_ASSERT(tc, "Invalid return value from thread_join", retval == 0);
}
ABTS_ULLONG_EQUAL(tbox->tc, 98, count);
rv = apr_thread_mutex_destroy(thread_lock64);
ABTS_ASSERT(tc, "Failed creating threads", rv == APR_SUCCESS);
}
static void *APR_THREAD_FUNC test_func_set64(apr_thread_t *thd, void *data)
{
apr_uint64_t *ops64 = data;
int i;
for (i = 0; i < 1000 * 1000; i++) {
apr_atomic_set64(ops64, APR_UINT64_C(0x1111222233334444));
apr_atomic_set64(ops64, APR_UINT64_C(0x4444555566667777));
}
apr_thread_exit(thd, APR_SUCCESS);
return NULL;
}
static void test_atomics_threaded_setread64(abts_case *tc, void *data)
{
apr_uint64_t *ops64 = data;
apr_thread_t *thread;
apr_status_t retval;
int i;
apr_atomic_set64(ops64, APR_UINT64_C(0x1111222233334444));
apr_thread_create(&thread, NULL, test_func_set64, ops64, p);
for (i = 0; i < 1000 * 1000 * 2; i++) {
apr_uint64_t val = apr_atomic_read64(ops64);
if (val != APR_UINT64_C(0x1111222233334444) &&
val != APR_UINT64_C(0x4444555566667777))
{
ABTS_FAIL(tc, "Unexpected value");
break;
}
}
apr_thread_join(&retval, thread);
}
#endif /* !APR_HAS_THREADS */
abts_suite *testatomic(abts_suite *suite)
{
suite = ADD_SUITE(suite)
abts_run_test(suite, test_init, NULL);
abts_run_test(suite, test_set32, NULL);
abts_run_test(suite, test_read32, NULL);
abts_run_test(suite, test_dec32, NULL);
abts_run_test(suite, test_xchg32, NULL);
abts_run_test(suite, test_xchgptr, NULL);
abts_run_test(suite, test_cas_equal, NULL);
abts_run_test(suite, test_cas_equal_nonnull, NULL);
abts_run_test(suite, test_cas_notequal, NULL);
abts_run_test(suite, test_casptr_equal, NULL);
abts_run_test(suite, test_casptr_equal_nonnull, NULL);
abts_run_test(suite, test_casptr_notequal, NULL);
abts_run_test(suite, test_add32, NULL);
abts_run_test(suite, test_add32_neg, NULL);
abts_run_test(suite, test_inc32, NULL);
abts_run_test(suite, test_set_add_inc_sub, NULL);
abts_run_test(suite, test_wrap_zero, NULL);
abts_run_test(suite, test_inc_neg1, NULL);
abts_run_test(suite, test_set64, NULL);
abts_run_test(suite, test_read64, NULL);
abts_run_test(suite, test_dec64, NULL);
abts_run_test(suite, test_xchg64, NULL);
abts_run_test(suite, test_add64, NULL);
abts_run_test(suite, test_add64_neg, NULL);
abts_run_test(suite, test_inc64, NULL);
abts_run_test(suite, test_set_add_inc_sub64, NULL);
abts_run_test(suite, test_wrap_zero64, NULL);
abts_run_test(suite, test_inc_neg164, NULL);
#if APR_HAS_THREADS
abts_run_test(suite, test_atomics_threaded, NULL);
abts_run_test(suite, test_atomics_threaded64, &atomic_ops64);
abts_run_test(suite, test_atomics_threaded64, &atomic_pad.ops64);
abts_run_test(suite, test_atomics_busyloop_threaded, NULL);
abts_run_test(suite, test_atomics_busyloop_threaded64, NULL);
abts_run_test(suite, test_atomics_threaded_setread64, &atomic_ops64);
abts_run_test(suite, test_atomics_threaded_setread64, &atomic_pad.ops64);
#endif
return suite;
}
|