sync_stress_parallelism.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * sync stress test: parallelism
  3. * Copyright 2015-2016 Collabora Ltd.
  4. *
  5. * Based on the implementation from the Android Open Source Project,
  6. *
  7. * Copyright 2012 Google, Inc
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include <pthread.h>
  28. #include "sync.h"
  29. #include "sw_sync.h"
  30. #include "synctest.h"
  31. static struct {
  32. int iterations;
  33. int timeline;
  34. int counter;
  35. } test_data_two_threads;
  36. static int test_stress_two_threads_shared_timeline_thread(void *d)
  37. {
  38. int thread_id = (long)d;
  39. int timeline = test_data_two_threads.timeline;
  40. int iterations = test_data_two_threads.iterations;
  41. int fence, valid, ret, i;
  42. for (i = 0; i < iterations; i++) {
  43. fence = sw_sync_fence_create(timeline, "fence",
  44. i * 2 + thread_id);
  45. valid = sw_sync_fence_is_valid(fence);
  46. ASSERT(valid, "Failure allocating fence\n");
  47. /* Wait on the prior thread to complete */
  48. ret = sync_wait(fence, -1);
  49. ASSERT(ret > 0, "Problem occurred on prior thread\n");
  50. /*
  51. * Confirm the previous thread's writes are visible
  52. * and then increment
  53. */
  54. ASSERT(test_data_two_threads.counter == i * 2 + thread_id,
  55. "Counter got damaged!\n");
  56. test_data_two_threads.counter++;
  57. /* Kick off the other thread */
  58. ret = sw_sync_timeline_inc(timeline, 1);
  59. ASSERT(ret == 0, "Advancing timeline failed\n");
  60. sw_sync_fence_destroy(fence);
  61. }
  62. return 0;
  63. }
  64. int test_stress_two_threads_shared_timeline(void)
  65. {
  66. pthread_t a, b;
  67. int valid;
  68. int timeline = sw_sync_timeline_create();
  69. valid = sw_sync_timeline_is_valid(timeline);
  70. ASSERT(valid, "Failure allocating timeline\n");
  71. test_data_two_threads.iterations = 1 << 16;
  72. test_data_two_threads.counter = 0;
  73. test_data_two_threads.timeline = timeline;
  74. /*
  75. * Use a single timeline to synchronize two threads
  76. * hammmering on the same counter.
  77. */
  78. pthread_create(&a, NULL, (void *(*)(void *))
  79. test_stress_two_threads_shared_timeline_thread,
  80. (void *)0);
  81. pthread_create(&b, NULL, (void *(*)(void *))
  82. test_stress_two_threads_shared_timeline_thread,
  83. (void *)1);
  84. pthread_join(a, NULL);
  85. pthread_join(b, NULL);
  86. /* make sure the threads did not trample on one another */
  87. ASSERT(test_data_two_threads.counter ==
  88. test_data_two_threads.iterations * 2,
  89. "Counter has unexpected value\n");
  90. sw_sync_timeline_destroy(timeline);
  91. return 0;
  92. }