fsmonitor: eliminate call to deprecated FSEventStream function

Replace the call to `FSEventStreamScheduleWithRunLoop()` function with
the suggested `FSEventStreamSetDispatchQueue()` function.

The MacOS version of the builtin FSMonitor feature uses the
`FSEventStreamScheduleWithRunLoop()` function to drive the event loop
and process FSEvents from the system.  This routine has now been
deprecated by Apple.  The MacOS 13 (Ventura) compiler tool chain now
generates a warning when compiling calls to this function.  In
DEVELOPER=1 mode, this now causes a compile error.

The `FSEventStreamSetDispatchQueue()` function is conceptually similar
and is the suggested replacement.  However, there are some subtle
thread-related differences.

Previously, the event stream would be processed by the
`fsm_listen__loop()` thread while it was in the `CFRunLoopRun()`
method.  (Conceptually, this was a blocking call on the lifetime of
the event stream where our thread drove the event loop and individual
events were handled by the `fsevent_callback()`.)

With the change, a "dispatch queue" is created and FSEvents will be
processed by a hidden queue-related thread (that calls the
`fsevent_callback()` on our behalf).  Our `fsm_listen__loop()` thread
maintains the original blocking model by waiting on a mutex/condition
variable pair while the hidden thread does all of the work.

While the deprecated API used by the original were introduced in
macOS 10.5 (Oct 2007), the API used by the updated code were
introduced back in macOS 10.6 (Aug 2009) and has been available
since then.  So this change _could_ break those who have happily
been using 10.5 (if there were such people), but these two dates
both predate the oldest versions of macOS Apple seems to support
anyway, so we should be safe.

Signed-off-by: Jeff Hostetler <jeffhostetler@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff Hostetler 2022-12-14 19:12:33 +00:00 committed by Junio C Hamano
parent e7e5c6f715
commit b0226007f0
2 changed files with 25 additions and 14 deletions

View File

@ -80,9 +80,7 @@ void CFRunLoopRun(void);
void CFRunLoopStop(CFRunLoopRef run_loop);
CFRunLoopRef CFRunLoopGetCurrent(void);
extern CFStringRef kCFRunLoopDefaultMode;
void FSEventStreamScheduleWithRunLoop(FSEventStreamRef stream,
CFRunLoopRef run_loop,
CFStringRef run_loop_mode);
void FSEventStreamSetDispatchQueue(FSEventStreamRef stream, dispatch_queue_t q);
unsigned char FSEventStreamStart(FSEventStreamRef stream);
void FSEventStreamStop(FSEventStreamRef stream);
void FSEventStreamInvalidate(FSEventStreamRef stream);

View File

@ -1,4 +1,5 @@
#ifndef __clang__
#include <dispatch/dispatch.h>
#include "fsm-darwin-gcc.h"
#else
#include <CoreFoundation/CoreFoundation.h>
@ -37,7 +38,9 @@ struct fsm_listen_data
FSEventStreamRef stream;
CFRunLoopRef rl;
dispatch_queue_t dq;
pthread_cond_t dq_finished;
pthread_mutex_t dq_lock;
enum shutdown_style {
SHUTDOWN_EVENT = 0,
@ -369,8 +372,11 @@ force_shutdown:
fsmonitor_batch__free_list(batch);
string_list_clear(&cookie_list, 0);
pthread_mutex_lock(&data->dq_lock);
data->shutdown_style = FORCE_SHUTDOWN;
CFRunLoopStop(data->rl);
pthread_cond_broadcast(&data->dq_finished);
pthread_mutex_unlock(&data->dq_lock);
strbuf_release(&tmp);
return;
}
@ -431,10 +437,6 @@ int fsm_listen__ctor(struct fsmonitor_daemon_state *state)
if (!data->stream)
goto failed;
/*
* `data->rl` needs to be set inside the listener thread.
*/
return 0;
failed:
@ -461,6 +463,11 @@ void fsm_listen__dtor(struct fsmonitor_daemon_state *state)
FSEventStreamRelease(data->stream);
}
if (data->dq)
dispatch_release(data->dq);
pthread_cond_destroy(&data->dq_finished);
pthread_mutex_destroy(&data->dq_lock);
FREE_AND_NULL(state->listen_data);
}
@ -469,9 +476,11 @@ void fsm_listen__stop_async(struct fsmonitor_daemon_state *state)
struct fsm_listen_data *data;
data = state->listen_data;
data->shutdown_style = SHUTDOWN_EVENT;
CFRunLoopStop(data->rl);
pthread_mutex_lock(&data->dq_lock);
data->shutdown_style = SHUTDOWN_EVENT;
pthread_cond_broadcast(&data->dq_finished);
pthread_mutex_unlock(&data->dq_lock);
}
void fsm_listen__loop(struct fsmonitor_daemon_state *state)
@ -480,9 +489,11 @@ void fsm_listen__loop(struct fsmonitor_daemon_state *state)
data = state->listen_data;
data->rl = CFRunLoopGetCurrent();
pthread_mutex_init(&data->dq_lock, NULL);
pthread_cond_init(&data->dq_finished, NULL);
data->dq = dispatch_queue_create("FSMonitor", NULL);
FSEventStreamScheduleWithRunLoop(data->stream, data->rl, kCFRunLoopDefaultMode);
FSEventStreamSetDispatchQueue(data->stream, data->dq);
data->stream_scheduled = 1;
if (!FSEventStreamStart(data->stream)) {
@ -491,7 +502,9 @@ void fsm_listen__loop(struct fsmonitor_daemon_state *state)
}
data->stream_started = 1;
CFRunLoopRun();
pthread_mutex_lock(&data->dq_lock);
pthread_cond_wait(&data->dq_finished, &data->dq_lock);
pthread_mutex_unlock(&data->dq_lock);
switch (data->shutdown_style) {
case FORCE_ERROR_STOP: