dispatch_overcommit.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #ifdef __linux__
  21. // for asprintf
  22. #define _GNU_SOURCE 1
  23. #endif
  24. #include <dispatch/dispatch.h>
  25. #include <dispatch/private.h>
  26. #include <stdio.h>
  27. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  28. #include <unistd.h>
  29. #endif
  30. #include <stdlib.h>
  31. #include <assert.h>
  32. #ifdef __APPLE__
  33. #include <libkern/OSAtomic.h>
  34. #endif
  35. #include <bsdtests.h>
  36. #include "dispatch_test.h"
  37. int32_t count = 0;
  38. const int32_t final = 32;
  39. int
  40. main(void)
  41. {
  42. dispatch_test_start("Dispatch Overcommit");
  43. int i;
  44. for (i = 0; i < final; ++i) {
  45. char* name;
  46. (void)asprintf(&name, "test.overcommit.%d", i);
  47. dispatch_queue_t queue = dispatch_queue_create(name, NULL);
  48. test_ptr_notnull("dispatch_queue_create", queue);
  49. free(name);
  50. dispatch_set_target_queue(queue, dispatch_get_global_queue(0, DISPATCH_QUEUE_OVERCOMMIT));
  51. dispatch_async(queue, ^{
  52. OSAtomicIncrement32(&count);
  53. if (count == final) {
  54. test_long("count", count, final);
  55. test_stop();
  56. } else {
  57. while (1); // spin
  58. }
  59. });
  60. }
  61. dispatch_main();
  62. return 0;
  63. }