dispatch_cf_main.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2010-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. #include <stdio.h>
  22. #include <CoreFoundation/CoreFoundation.h>
  23. #ifdef __APPLE__
  24. #include <libkern/OSAtomic.h>
  25. #endif
  26. #include <bsdtests.h>
  27. #include "dispatch_test.h"
  28. const int32_t final = 10;
  29. static volatile int32_t count;
  30. static void
  31. work(void* ctxt __attribute__((unused)))
  32. {
  33. int32_t c = OSAtomicIncrement32(&count);
  34. if (c < final-1) {
  35. dispatch_async_f(dispatch_get_main_queue(), NULL, work);
  36. CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopDefaultMode, ^{
  37. fprintf(stderr, "CFRunLoopPerformBlock %d\n", c);
  38. test_long_less_than("CFRunLoopPerformBlock", count, final);
  39. });
  40. }
  41. }
  42. int
  43. main(void)
  44. {
  45. dispatch_test_start("Dispatch CF main queue"); // <rdar://problem/7760523>
  46. dispatch_async_f(dispatch_get_main_queue(), NULL, work);
  47. dispatch_async_f(dispatch_get_main_queue(), NULL, work);
  48. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
  49. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  50. test_long("done", count, final);
  51. test_stop();
  52. });
  53. CFRunLoopRun();
  54. return 0;
  55. }