thread-mg-share.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "tests.h"
  2. #include "machine.h"
  3. #include "thread.h"
  4. #include "map.h"
  5. #include "debug.h"
  6. int test__thread_mg_share(void)
  7. {
  8. struct machines machines;
  9. struct machine *machine;
  10. /* thread group */
  11. struct thread *leader;
  12. struct thread *t1, *t2, *t3;
  13. struct map_groups *mg;
  14. /* other process */
  15. struct thread *other, *other_leader;
  16. struct map_groups *other_mg;
  17. /*
  18. * This test create 2 processes abstractions (struct thread)
  19. * with several threads and checks they properly share and
  20. * maintain map groups info (struct map_groups).
  21. *
  22. * thread group (pid: 0, tids: 0, 1, 2, 3)
  23. * other group (pid: 4, tids: 4, 5)
  24. */
  25. machines__init(&machines);
  26. machine = &machines.host;
  27. /* create process with 4 threads */
  28. leader = machine__findnew_thread(machine, 0, 0);
  29. t1 = machine__findnew_thread(machine, 0, 1);
  30. t2 = machine__findnew_thread(machine, 0, 2);
  31. t3 = machine__findnew_thread(machine, 0, 3);
  32. /* and create 1 separated process, without thread leader */
  33. other = machine__findnew_thread(machine, 4, 5);
  34. TEST_ASSERT_VAL("failed to create threads",
  35. leader && t1 && t2 && t3 && other);
  36. mg = leader->mg;
  37. TEST_ASSERT_VAL("wrong refcnt", mg->refcnt == 4);
  38. /* test the map groups pointer is shared */
  39. TEST_ASSERT_VAL("map groups don't match", mg == t1->mg);
  40. TEST_ASSERT_VAL("map groups don't match", mg == t2->mg);
  41. TEST_ASSERT_VAL("map groups don't match", mg == t3->mg);
  42. /*
  43. * Verify the other leader was created by previous call.
  44. * It should have shared map groups with no change in
  45. * refcnt.
  46. */
  47. other_leader = machine__find_thread(machine, 4, 4);
  48. TEST_ASSERT_VAL("failed to find other leader", other_leader);
  49. other_mg = other->mg;
  50. TEST_ASSERT_VAL("wrong refcnt", other_mg->refcnt == 2);
  51. TEST_ASSERT_VAL("map groups don't match", other_mg == other_leader->mg);
  52. /* release thread group */
  53. thread__delete(leader);
  54. TEST_ASSERT_VAL("wrong refcnt", mg->refcnt == 3);
  55. thread__delete(t1);
  56. TEST_ASSERT_VAL("wrong refcnt", mg->refcnt == 2);
  57. thread__delete(t2);
  58. TEST_ASSERT_VAL("wrong refcnt", mg->refcnt == 1);
  59. thread__delete(t3);
  60. /* release other group */
  61. thread__delete(other_leader);
  62. TEST_ASSERT_VAL("wrong refcnt", other_mg->refcnt == 1);
  63. thread__delete(other);
  64. /*
  65. * Cannot call machine__delete_threads(machine) now,
  66. * because we've already released all the threads.
  67. */
  68. machines__exit(&machines);
  69. return 0;
  70. }