2
0

tsd.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "test/jemalloc_test.h"
  2. #define THREAD_DATA 0x72b65c10
  3. typedef unsigned int data_t;
  4. static bool data_cleanup_executed;
  5. void
  6. data_cleanup(void *arg)
  7. {
  8. data_t *data = (data_t *)arg;
  9. assert_x_eq(*data, THREAD_DATA,
  10. "Argument passed into cleanup function should match tsd value");
  11. data_cleanup_executed = true;
  12. }
  13. malloc_tsd_protos(, data, data_t)
  14. malloc_tsd_externs(data, data_t)
  15. #define DATA_INIT 0x12345678
  16. malloc_tsd_data(, data, data_t, DATA_INIT)
  17. malloc_tsd_funcs(, data, data_t, DATA_INIT, data_cleanup)
  18. static void *
  19. thd_start(void *arg)
  20. {
  21. data_t d = (data_t)(uintptr_t)arg;
  22. assert_x_eq(*data_tsd_get(), DATA_INIT,
  23. "Initial tsd get should return initialization value");
  24. data_tsd_set(&d);
  25. assert_x_eq(*data_tsd_get(), d,
  26. "After tsd set, tsd get should return value that was set");
  27. d = 0;
  28. assert_x_eq(*data_tsd_get(), (data_t)(uintptr_t)arg,
  29. "Resetting local data should have no effect on tsd");
  30. return (NULL);
  31. }
  32. TEST_BEGIN(test_tsd_main_thread)
  33. {
  34. thd_start((void *) 0xa5f3e329);
  35. }
  36. TEST_END
  37. TEST_BEGIN(test_tsd_sub_thread)
  38. {
  39. thd_t thd;
  40. data_cleanup_executed = false;
  41. thd_create(&thd, thd_start, (void *)THREAD_DATA);
  42. thd_join(thd, NULL);
  43. assert_true(data_cleanup_executed,
  44. "Cleanup function should have executed");
  45. }
  46. TEST_END
  47. int
  48. main(void)
  49. {
  50. data_tsd_boot();
  51. return (test(
  52. test_tsd_main_thread,
  53. test_tsd_sub_thread));
  54. }