@@ -82,89 +82,44 @@ class FSEventsWatcher {
82
82
void set_ref (FSEventStreamRef ref) { ref_ = ref; }
83
83
84
84
void Start () {
85
- // Schedule StartCallback to be executed in the RunLoop.
86
- CFRunLoopTimerContext context;
87
- memset (&context, 0 , sizeof (context));
88
- context.info = this ;
89
- CFRunLoopTimerRef timer =
90
- CFRunLoopTimerCreate (NULL , 0 , 0 , 0 , 0 , Node::StartCallback, &context);
91
- CFRunLoopAddTimer (watcher_->run_loop_ , timer, kCFRunLoopCommonModes );
92
- CFRelease (timer);
93
- watcher_->monitor_ .Enter ();
94
- while (!ready_) {
95
- watcher_->monitor_ .Wait (Monitor::kNoTimeout );
96
- }
97
- watcher_->monitor_ .Exit ();
98
- }
99
-
100
- static void StartCallback (CFRunLoopTimerRef timer, void * info) {
101
- Node* node = reinterpret_cast <Node*>(info);
102
- ASSERT (Thread::Compare (node->watcher_ ->threadId_ ,
103
- Thread::GetCurrentThreadId ()));
104
85
FSEventStreamContext context;
105
86
memset (&context, 0 , sizeof (context));
106
- context.info = reinterpret_cast <void *>(node );
87
+ context.info = reinterpret_cast <void *>(this );
107
88
CFArrayRef array = CFArrayCreate (
108
- NULL , reinterpret_cast <const void **>(&node-> path_ref_ ), 1 , NULL );
89
+ NULL , reinterpret_cast <const void **>(&path_ref_), 1 , NULL );
109
90
FSEventStreamRef ref = FSEventStreamCreate (
110
91
NULL , Callback, &context, array, kFSEventStreamEventIdSinceNow , 0.10 ,
111
92
kFSEventStreamCreateFlagFileEvents );
112
93
CFRelease (array);
113
94
114
- node->set_ref (ref);
95
+ set_ref (ref);
96
+ ready_.store (true , std::memory_order_release);
115
97
116
- FSEventStreamScheduleWithRunLoop (node-> ref_ , node-> watcher_ ->run_loop_ ,
98
+ FSEventStreamScheduleWithRunLoop (ref_, watcher_->run_loop_ ,
117
99
kCFRunLoopDefaultMode );
118
100
119
- FSEventStreamStart (node->ref_ );
120
- FSEventStreamFlushSync (node->ref_ );
121
-
122
- node->watcher_ ->monitor_ .Enter ();
123
- node->ready_ = true ;
124
- node->watcher_ ->monitor_ .Notify ();
125
- node->watcher_ ->monitor_ .Exit ();
101
+ FSEventStreamStart (ref_);
102
+ FSEventStreamFlushSync (ref_);
126
103
}
127
104
128
105
void Stop () {
129
- // Schedule StopCallback to be executed in the RunLoop.
130
106
ASSERT (ready_);
131
- CFRunLoopTimerContext context;
132
- memset (&context, 0 , sizeof (context));
133
- context.info = this ;
134
- CFRunLoopTimerRef timer =
135
- CFRunLoopTimerCreate (NULL , 0 , 0 , 0 , 0 , StopCallback, &context);
136
- CFRunLoopAddTimer (watcher_->run_loop_ , timer, kCFRunLoopCommonModes );
137
- CFRelease (timer);
138
- watcher_->monitor_ .Enter ();
139
- while (ready_) {
140
- watcher_->monitor_ .Wait (Monitor::kNoTimeout );
141
- }
142
- watcher_->monitor_ .Exit ();
143
- }
144
-
145
- static void StopCallback (CFRunLoopTimerRef timer, void * info) {
146
- Node* node = reinterpret_cast <Node*>(info);
147
- ASSERT (Thread::Compare (node->watcher_ ->threadId_ ,
148
- Thread::GetCurrentThreadId ()));
149
- FSEventStreamStop (node->ref_ );
150
- FSEventStreamInvalidate (node->ref_ );
151
- FSEventStreamRelease (node->ref_ );
152
- node->watcher_ ->monitor_ .Enter ();
153
- node->ready_ = false ;
154
- node->watcher_ ->monitor_ .Notify ();
155
- node->watcher_ ->monitor_ .Exit ();
107
+ FSEventStreamStop (ref_);
108
+ FSEventStreamInvalidate (ref_);
109
+ FSEventStreamRelease (ref_);
110
+ ready_.store (false , std::memory_order_release);
156
111
}
157
112
158
113
FSEventsWatcher* watcher () const { return watcher_; }
159
- bool ready () const { return ready_; }
114
+ bool ready () const { return ready_. load (std::memory_order_acquire) ; }
160
115
intptr_t base_path_length () const { return base_path_length_; }
161
116
int read_fd () const { return read_fd_; }
162
117
int write_fd () const { return write_fd_; }
163
118
bool recursive () const { return recursive_; }
164
119
165
120
private:
166
121
FSEventsWatcher* watcher_;
167
- bool ready_;
122
+ std::atomic< bool > ready_;
168
123
intptr_t base_path_length_;
169
124
CFStringRef path_ref_;
170
125
int read_fd_;
@@ -266,8 +221,6 @@ class FSEventsWatcher {
266
221
Node* node = reinterpret_cast <Node*>(client);
267
222
ASSERT (Thread::Compare (node->watcher ()->threadId_ ,
268
223
Thread::GetCurrentThreadId ()));
269
- // `ready` is set on same thread as this callback is invoked, so we don't
270
- // need to lock here.
271
224
if (!node->ready ()) {
272
225
return ;
273
226
}
0 commit comments