dispatch_vnode.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <stdio.h>
  21. #include <fcntl.h>
  22. #include <stdlib.h>
  23. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  24. #include <unistd.h>
  25. #endif
  26. #include <dispatch/dispatch.h>
  27. #include <bsdtests.h>
  28. #include "dispatch_test.h"
  29. #define ITERATIONS 1000
  30. long iterations, notifications;
  31. int
  32. main(void)
  33. {
  34. dispatch_test_start("Dispatch VNODE RENAME");
  35. char path1[] = "/tmp/dispatchtest_vnode.XXXXXX";
  36. char path2[] = "/tmp/dispatchtest_vnode.XXXXXX";
  37. int fd1 = mkstemp(path1);
  38. if (fd1 == -1) {
  39. test_errno("mkstemp", errno, 0);
  40. test_stop();
  41. }
  42. close(fd1);
  43. if (!mktemp(path2)) {
  44. test_errno("mktemp", errno, 0);
  45. test_stop();
  46. }
  47. char *currentName = path1;
  48. char *renameDest = path2;
  49. dispatch_semaphore_t renamedSemaphore = dispatch_semaphore_create(0);
  50. dispatch_group_t g = dispatch_group_create();
  51. while (iterations++ < ITERATIONS) {
  52. int fd = open(currentName, O_EVTONLY);
  53. if (fd == -1) {
  54. test_errno("open", errno, 0);
  55. test_stop();
  56. }
  57. dispatch_source_t monitorSource = dispatch_source_create(
  58. DISPATCH_SOURCE_TYPE_VNODE, fd, DISPATCH_VNODE_RENAME,
  59. dispatch_get_global_queue(0, 0));
  60. dispatch_source_set_event_handler(monitorSource, ^{
  61. dispatch_semaphore_signal(renamedSemaphore);
  62. });
  63. dispatch_source_set_cancel_handler(monitorSource, ^{
  64. close(fd);
  65. });
  66. #if DISPATCH_API_VERSION >= 20100818 // <rdar://problem/7731284>
  67. dispatch_group_enter(g);
  68. dispatch_source_set_registration_handler(monitorSource, ^{
  69. dispatch_group_leave(g);
  70. });
  71. #endif
  72. dispatch_resume(monitorSource);
  73. dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
  74. if(rename(currentName, renameDest)) {
  75. test_errno("rename", errno, 0);
  76. test_stop();
  77. }
  78. char *tmp = currentName;
  79. currentName = renameDest;
  80. renameDest = tmp;
  81. if(!dispatch_semaphore_wait(renamedSemaphore,
  82. dispatch_time(DISPATCH_TIME_NOW, 10 * 1000 * NSEC_PER_USEC))) {
  83. fprintf(stderr, ".");
  84. notifications++;
  85. dispatch_source_cancel(monitorSource);
  86. } else {
  87. fprintf(stderr, "!");
  88. dispatch_source_cancel(monitorSource);
  89. dispatch_semaphore_signal(renamedSemaphore);
  90. }
  91. if (!(iterations % 80)) {
  92. fprintf(stderr, "\n");
  93. }
  94. dispatch_release(monitorSource);
  95. }
  96. fprintf(stderr, "\n");
  97. unlink(currentName);
  98. dispatch_release(g);
  99. dispatch_release(renamedSemaphore);
  100. test_long("VNODE RENAME notifications", notifications, ITERATIONS);
  101. test_stop();
  102. return 0;
  103. }