thread_arena.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "test/jemalloc_test.h"
  2. #define NTHREADS 10
  3. void *
  4. thd_start(void *arg)
  5. {
  6. unsigned main_arena_ind = *(unsigned *)arg;
  7. void *p;
  8. unsigned arena_ind;
  9. size_t size;
  10. int err;
  11. p = malloc(1);
  12. assert_ptr_not_null(p, "Error in malloc()");
  13. free(p);
  14. size = sizeof(arena_ind);
  15. if ((err = mallctl("thread.arena", &arena_ind, &size, &main_arena_ind,
  16. sizeof(main_arena_ind)))) {
  17. char buf[BUFERROR_BUF];
  18. buferror(err, buf, sizeof(buf));
  19. test_fail("Error in mallctl(): %s", buf);
  20. }
  21. size = sizeof(arena_ind);
  22. if ((err = mallctl("thread.arena", &arena_ind, &size, NULL, 0))) {
  23. char buf[BUFERROR_BUF];
  24. buferror(err, buf, sizeof(buf));
  25. test_fail("Error in mallctl(): %s", buf);
  26. }
  27. assert_u_eq(arena_ind, main_arena_ind,
  28. "Arena index should be same as for main thread");
  29. return (NULL);
  30. }
  31. TEST_BEGIN(test_thread_arena)
  32. {
  33. void *p;
  34. unsigned arena_ind;
  35. size_t size;
  36. int err;
  37. thd_t thds[NTHREADS];
  38. unsigned i;
  39. p = malloc(1);
  40. assert_ptr_not_null(p, "Error in malloc()");
  41. size = sizeof(arena_ind);
  42. if ((err = mallctl("thread.arena", &arena_ind, &size, NULL, 0))) {
  43. char buf[BUFERROR_BUF];
  44. buferror(err, buf, sizeof(buf));
  45. test_fail("Error in mallctl(): %s", buf);
  46. }
  47. for (i = 0; i < NTHREADS; i++) {
  48. thd_create(&thds[i], thd_start,
  49. (void *)&arena_ind);
  50. }
  51. for (i = 0; i < NTHREADS; i++) {
  52. intptr_t join_ret;
  53. thd_join(thds[i], (void *)&join_ret);
  54. assert_zd_eq(join_ret, 0, "Unexpected thread join error");
  55. }
  56. }
  57. TEST_END
  58. int
  59. main(void)
  60. {
  61. return (test(
  62. test_thread_arena));
  63. }