tsd.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "test/jemalloc_test.h"
  2. #define THREAD_DATA 0x72b65c10
  3. typedef unsigned int data_t;
  4. static bool data_cleanup_executed;
  5. malloc_tsd_types(data_, data_t)
  6. malloc_tsd_protos(, data_, data_t)
  7. void
  8. data_cleanup(void *arg)
  9. {
  10. data_t *data = (data_t *)arg;
  11. if (!data_cleanup_executed) {
  12. assert_x_eq(*data, THREAD_DATA,
  13. "Argument passed into cleanup function should match tsd "
  14. "value");
  15. }
  16. data_cleanup_executed = true;
  17. /*
  18. * Allocate during cleanup for two rounds, in order to assure that
  19. * jemalloc's internal tsd reinitialization happens.
  20. */
  21. switch (*data) {
  22. case THREAD_DATA:
  23. *data = 1;
  24. data_tsd_set(data);
  25. break;
  26. case 1:
  27. *data = 2;
  28. data_tsd_set(data);
  29. break;
  30. case 2:
  31. return;
  32. default:
  33. not_reached();
  34. }
  35. {
  36. void *p = mallocx(1, 0);
  37. assert_ptr_not_null(p, "Unexpeced mallocx() failure");
  38. dallocx(p, 0);
  39. }
  40. }
  41. malloc_tsd_externs(data_, data_t)
  42. #define DATA_INIT 0x12345678
  43. malloc_tsd_data(, data_, data_t, DATA_INIT)
  44. malloc_tsd_funcs(, data_, data_t, DATA_INIT, data_cleanup)
  45. static void *
  46. thd_start(void *arg)
  47. {
  48. data_t d = (data_t)(uintptr_t)arg;
  49. void *p;
  50. assert_x_eq(*data_tsd_get(), DATA_INIT,
  51. "Initial tsd get should return initialization value");
  52. p = malloc(1);
  53. assert_ptr_not_null(p, "Unexpected malloc() failure");
  54. data_tsd_set(&d);
  55. assert_x_eq(*data_tsd_get(), d,
  56. "After tsd set, tsd get should return value that was set");
  57. d = 0;
  58. assert_x_eq(*data_tsd_get(), (data_t)(uintptr_t)arg,
  59. "Resetting local data should have no effect on tsd");
  60. free(p);
  61. return (NULL);
  62. }
  63. TEST_BEGIN(test_tsd_main_thread)
  64. {
  65. thd_start((void *) 0xa5f3e329);
  66. }
  67. TEST_END
  68. TEST_BEGIN(test_tsd_sub_thread)
  69. {
  70. thd_t thd;
  71. data_cleanup_executed = false;
  72. thd_create(&thd, thd_start, (void *)THREAD_DATA);
  73. thd_join(thd, NULL);
  74. assert_true(data_cleanup_executed,
  75. "Cleanup function should have executed");
  76. }
  77. TEST_END
  78. int
  79. main(void)
  80. {
  81. data_tsd_boot();
  82. return (test(
  83. test_tsd_main_thread,
  84. test_tsd_sub_thread));
  85. }