dispatch_group.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. #include <dispatch/dispatch.h>
  21. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  22. #include <unistd.h>
  23. #endif
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <assert.h>
  28. #ifdef __APPLE__
  29. #include <libkern/OSAtomic.h>
  30. #endif
  31. #include <math.h>
  32. #include <bsdtests.h>
  33. #include "dispatch_test.h"
  34. #ifndef NSEC_PER_SEC
  35. #define NSEC_PER_SEC 1000000000
  36. #endif
  37. #if TARGET_OS_EMBEDDED
  38. #define LOOP_COUNT 50000
  39. #else
  40. #define LOOP_COUNT 200000
  41. #endif
  42. static void test_group_notify(void*);
  43. static dispatch_group_t
  44. create_group(size_t count, unsigned int delay)
  45. {
  46. size_t i;
  47. dispatch_group_t group = dispatch_group_create();
  48. for (i = 0; i < count; ++i) {
  49. dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
  50. assert(queue);
  51. dispatch_group_async(group, queue, ^{
  52. if (delay) {
  53. fprintf(stderr, "sleeping...\n");
  54. sleep(delay);
  55. fprintf(stderr, "done.\n");
  56. }
  57. });
  58. dispatch_release(queue);
  59. }
  60. return group;
  61. }
  62. static void
  63. test_group(void *ctxt __attribute__((unused)))
  64. {
  65. long res;
  66. dispatch_group_t group;
  67. group = create_group(100, 0);
  68. test_ptr_notnull("dispatch_group_async", group);
  69. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  70. // should be OK to re-use a group
  71. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{});
  72. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  73. dispatch_release(group);
  74. group = NULL;
  75. group = create_group(3, 7);
  76. test_ptr_notnull("dispatch_group_async", group);
  77. res = dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC));
  78. test_long("dispatch_group_wait", !res, 0);
  79. // retry after timeout (this time succeed)
  80. res = dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC));
  81. test_long("dispatch_group_wait", res, 0);
  82. dispatch_release(group);
  83. group = NULL;
  84. group = create_group(100, 0);
  85. test_ptr_notnull("dispatch_group_async", group);
  86. dispatch_group_notify(group, dispatch_get_main_queue(), ^{
  87. dispatch_test_current("Notification Received", dispatch_get_main_queue());
  88. dispatch_async_f(dispatch_get_main_queue(), NULL, test_group_notify);
  89. });
  90. dispatch_release(group);
  91. group = NULL;
  92. }
  93. static long completed;
  94. static void
  95. test_group_notify2(long cycle, dispatch_group_t tested)
  96. {
  97. static dispatch_queue_t rq, nq;
  98. static dispatch_once_t once;
  99. dispatch_once(&once, ^{
  100. rq = dispatch_queue_create("release", 0);
  101. dispatch_suspend(rq);
  102. nq = dispatch_queue_create("notify", 0);
  103. });
  104. dispatch_resume(rq);
  105. // n=4 works great for a 4CPU Mac Pro, this might work for a wider range of
  106. // systems.
  107. #if HAVE_ARC4RANDOM
  108. const int n = 1 + arc4random() % 8;
  109. #else
  110. const int n = 1 + random() % 8;
  111. #endif
  112. dispatch_group_t group = dispatch_group_create();
  113. dispatch_queue_t qa[n];
  114. dispatch_group_enter(group);
  115. for (int i = 0; i < n; i++) {
  116. char buf[48];
  117. sprintf(buf, "T%ld-%d", cycle, i);
  118. qa[i] = dispatch_queue_create(buf, 0);
  119. }
  120. __block float eh = 0;
  121. for (int i = 0; i < n; i++) {
  122. dispatch_queue_t q = qa[i];
  123. dispatch_group_async(group, q, ^{
  124. // Seems to trigger a little more reliably with some work being
  125. // done in this block
  126. assert(cycle && "cycle must be non-zero");
  127. eh = (float)sin(M_1_PI / cycle);
  128. });
  129. }
  130. dispatch_group_leave(group);
  131. dispatch_group_notify(group, nq, ^{
  132. completed = cycle;
  133. dispatch_group_leave(tested);
  134. });
  135. // Releasing qa's queues here seems to avoid the race, so we are arranging
  136. // for the current iteration's queues to be released on the next iteration.
  137. dispatch_sync(rq, ^{});
  138. dispatch_suspend(rq);
  139. for (int i = 0; i < n; i++) {
  140. dispatch_queue_t q = qa[i];
  141. dispatch_async(rq, ^{ dispatch_release(q); });
  142. }
  143. dispatch_release(group);
  144. }
  145. static void
  146. test_group_notify(void *ctxt __attribute__((unused)))
  147. {
  148. // <rdar://problem/11445820>
  149. dispatch_group_t tested = dispatch_group_create();
  150. test_ptr_notnull("dispatch_group_create", tested);
  151. long i;
  152. for (i = 1; i <= LOOP_COUNT; i++) {
  153. if (!(i % (LOOP_COUNT/10))) {
  154. fprintf(stderr, "#%ld\n", i);
  155. }
  156. dispatch_group_enter(tested);
  157. test_group_notify2(i, tested);
  158. if (dispatch_group_wait(tested, dispatch_time(DISPATCH_TIME_NOW,
  159. NSEC_PER_SEC))) {
  160. break;
  161. }
  162. }
  163. test_long("dispatch_group_notify", i - 1, LOOP_COUNT);
  164. test_stop();
  165. }
  166. int
  167. main(void)
  168. {
  169. dispatch_test_start("Dispatch Group");
  170. dispatch_async_f(dispatch_get_main_queue(), NULL, test_group);
  171. dispatch_main();
  172. return 0;
  173. }