thread-mg-share.c 2.3 KB

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