dispatch_queue_finalizer.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <bsdtests.h>
  29. #include "dispatch_test.h"
  30. void *ctxt_magic = NULL;
  31. static void
  32. finalize(void *ctxt)
  33. {
  34. test_ptr_null("finalizer ran", NULL);
  35. test_ptr("correct context", ctxt, ctxt_magic);
  36. test_stop();
  37. }
  38. static void
  39. never_call(void *ctxt)
  40. {
  41. test_ptr_notnull("never_call should not run", NULL);
  42. test_ptr("correct context", ctxt, NULL);
  43. }
  44. int
  45. main(void)
  46. {
  47. dispatch_test_start("Dispatch Queue Finalizer");
  48. #if HAS_ARC4RANDOM
  49. #if defined(__LP64__) || defined(_WIN64)
  50. ctxt_magic = (void*)((uintptr_t)arc4random() << 32 | arc4random());
  51. #else
  52. ctxt_magic = (void*)arc4random();
  53. #endif
  54. #else // that is, if !HAS_ARC4RANDOM
  55. ctxt_magic = (void *)random();
  56. #endif
  57. // we need a non-NULL value for the tests to work properly
  58. if (ctxt_magic == NULL) {
  59. ctxt_magic = &ctxt_magic;
  60. }
  61. dispatch_queue_t q = dispatch_queue_create("com.apple.testing.finalizer", NULL);
  62. test_ptr_notnull("dispatch_queue_new", q);
  63. dispatch_set_finalizer_f(q, finalize);
  64. dispatch_queue_t q_null_context = dispatch_queue_create("com.apple.testing.finalizer.context_null", NULL);
  65. dispatch_set_context(q_null_context, NULL);
  66. dispatch_set_finalizer_f(q_null_context, never_call);
  67. dispatch_release(q_null_context);
  68. // Don't test k
  69. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  70. // Usign async to set the context helps test that blocks are
  71. // run before the release as opposed to just thrown away.
  72. dispatch_async(q, ^{
  73. dispatch_set_context(q, ctxt_magic);
  74. });
  75. dispatch_release(q);
  76. });
  77. dispatch_main();
  78. return 0;
  79. }