dispatch_timer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <stdlib.h>
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <dispatch/dispatch.h>
  25. #include <bsdtests.h>
  26. #include "dispatch_test.h"
  27. static bool finalized = false;
  28. static void
  29. test_fin(void *cxt)
  30. {
  31. test_ptr("finalizer ran", cxt, cxt);
  32. finalized = true;
  33. test_stop();
  34. }
  35. static void
  36. test_timer(void)
  37. {
  38. dispatch_test_start("Dispatch Source Timer");
  39. const int stop_at = 3;
  40. dispatch_queue_t main_q = dispatch_get_main_queue();
  41. //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());
  42. int64_t j;
  43. // create timers in two classes:
  44. // * ones that should trigger before the test ends
  45. // * ones that shouldn't trigger before the test ends
  46. for (j = 1; j <= 5; ++j)
  47. {
  48. dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
  49. test_ptr_notnull("dispatch_source_create", s);
  50. int64_t delta = (int64_t)((uint64_t)j * NSEC_PER_SEC + NSEC_PER_SEC / 10);
  51. dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, delta), DISPATCH_TIME_FOREVER, 0);
  52. dispatch_source_set_event_handler(s, ^{
  53. if (!finalized) {
  54. test_long_less_than("timer number", (long)j, stop_at);
  55. fprintf(stderr, "timer[%lu]\n", (unsigned long)j);
  56. }
  57. dispatch_release(s);
  58. });
  59. dispatch_resume(s);
  60. }
  61. __block int i = 0;
  62. dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q);
  63. test_ptr_notnull("dispatch_source_create", s);
  64. dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, 0), NSEC_PER_SEC, 0);
  65. dispatch_source_set_cancel_handler(s, ^{
  66. test_ptr_notnull("cancel handler run", s);
  67. dispatch_release(s);
  68. });
  69. dispatch_source_set_event_handler(s, ^{
  70. fprintf(stderr, "%d\n", ++i);
  71. if (i >= stop_at) {
  72. test_long("i", i, stop_at);
  73. dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, 0),
  74. DISPATCH_TIME_FOREVER, 0);
  75. dispatch_source_cancel(s);
  76. }
  77. });
  78. dispatch_set_context(s, s);
  79. dispatch_set_finalizer_f(s, test_fin);
  80. dispatch_resume(s);
  81. }
  82. int
  83. main(void)
  84. {
  85. test_timer();
  86. dispatch_main();
  87. return 0;
  88. }