dispatch_suspend_timer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <assert.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <dispatch/dispatch.h>
  24. #include <bsdtests.h>
  25. #include "dispatch_test.h"
  26. dispatch_source_t tweedledee;
  27. dispatch_source_t tweedledum;
  28. static void
  29. fini(void *cxt)
  30. {
  31. test_ptr_notnull("finalizer ran", cxt);
  32. if (cxt == tweedledum) {
  33. test_stop();
  34. }
  35. }
  36. static void
  37. test_timer(void)
  38. {
  39. dispatch_test_start("Dispatch Suspend Timer");
  40. dispatch_queue_t main_q = dispatch_get_main_queue();
  41. //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());
  42. __block int i = 0, i_prime = 0;
  43. __block int j = 0;
  44. tweedledee = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q);
  45. test_ptr_notnull("dispatch_source_timer_create", tweedledee);
  46. dispatch_source_set_timer(tweedledee, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, 0);
  47. dispatch_source_set_cancel_handler(tweedledee, ^{
  48. dispatch_release(tweedledee);
  49. });
  50. dispatch_source_set_event_handler(tweedledee, ^{
  51. i_prime += dispatch_source_get_data(tweedledee);
  52. fprintf(stderr, "tweedledee %d (%d)\n", ++i, i_prime);
  53. if (i == 10) {
  54. dispatch_source_cancel(tweedledee);
  55. }
  56. });
  57. dispatch_set_context(tweedledee, tweedledee);
  58. dispatch_set_finalizer_f(tweedledee, fini);
  59. dispatch_resume(tweedledee);
  60. tweedledum = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q);
  61. test_ptr_notnull("dispatch_source_timer_create", tweedledum);
  62. dispatch_source_set_timer(tweedledum, dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC + NSEC_PER_SEC / 2), 3 * NSEC_PER_SEC, 0);
  63. dispatch_source_set_cancel_handler(tweedledum, ^{
  64. dispatch_release(tweedledum);
  65. });
  66. dispatch_source_set_event_handler(tweedledum, ^{
  67. switch(++j) {
  68. case 1:
  69. fprintf(stderr, "suspending timer for 3 seconds\n");
  70. dispatch_suspend(tweedledee);
  71. break;
  72. case 2:
  73. fprintf(stderr, "resuming timer\n");
  74. test_long("tweedledee tick count", i, 3);
  75. test_long("tweedledee virtual tick count", i_prime, 3);
  76. dispatch_resume(tweedledee);
  77. break;
  78. default:
  79. test_long("tweedledee tick count", i, 7);
  80. test_long("tweedledee virtual tick count", i_prime, 9);
  81. dispatch_source_cancel(tweedledum);
  82. break;
  83. }
  84. });
  85. dispatch_set_context(tweedledum, tweedledum);
  86. dispatch_set_finalizer_f(tweedledum, fini);
  87. dispatch_resume(tweedledum);
  88. }
  89. int
  90. main(void)
  91. {
  92. test_timer();
  93. dispatch_main();
  94. return 0;
  95. }