890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
|
# File 'ext/ruby/passenger_native_support.c', line 890
static VALUE
fs_watcher_wait_for_change(VALUE self) {
FSWatcher *watcher;
pthread_t thr;
ssize_t ret;
int e, interrupted = 0;
FSWatcherReadByteData read_data;
Data_Get_Struct(self, FSWatcher, watcher);
if (watcher->preparation_error) {
return Qfalse;
}
/* Spawn a thread, and let the thread perform the blocking kqueue
* wait. When kevent() returns the thread will write its status to the
* notification pipe. In the mean time we let the Ruby interpreter wait
* on the other side of the pipe for us so that we don't block Ruby
* threads.
*/
e = pthread_create(&thr, NULL, fs_watcher_wait_on_kqueue, watcher);
if (e != 0) {
errno = e;
rb_sys_fail("pthread_create()");
return Qnil;
}
/* Note that rb_thread_wait() does not wait for the fd when the app
* is single threaded, so we must join the thread after we've read
* from the notification fd.
*/
rb_protect(fs_watcher_wait_fd, (VALUE) watcher->notification_fd[0], &interrupted);
if (interrupted) {
/* We got interrupted so tell the watcher thread to exit. */
ret = write(watcher->interruption_fd[1], "x", 1);
if (ret == -1) {
e = errno;
fs_watcher_real_close(watcher);
errno = e;
rb_sys_fail("write() to interruption pipe");
return Qnil;
}
pthread_join(thr, NULL);
/* Now clean up stuff. */
fs_watcher_real_close(watcher);
rb_jump_tag(interrupted);
return Qnil;
}
read_data.fd = watcher->notification_fd[0];
rb_protect(fs_watcher_read_byte_from_fd, (VALUE) &read_data, &interrupted);
if (interrupted) {
/* We got interrupted so tell the watcher thread to exit. */
ret = write(watcher->interruption_fd[1], "x", 1);
if (ret == -1) {
e = errno;
fs_watcher_real_close(watcher);
errno = e;
rb_sys_fail("write() to interruption pipe");
return Qnil;
}
pthread_join(thr, NULL);
/* Now clean up stuff. */
fs_watcher_real_close(watcher);
rb_jump_tag(interrupted);
return Qnil;
}
pthread_join(thr, NULL);
if (read_data.ret == -1) {
fs_watcher_real_close(watcher);
errno = read_data.error;
rb_sys_fail("read()");
return Qnil;
} else if (read_data.ret == 0) {
fs_watcher_real_close(watcher);
errno = read_data.error;
rb_raise(rb_eRuntimeError, "Unknown error: unexpected EOF");
return Qnil;
} else if (read_data.byte == 't') {
/* termination_fd or interruption_fd became readable */
return Qnil;
} else if (read_data.byte == 'f') {
/* a file or directory changed */
return Qtrue;
} else {
fs_watcher_real_close(watcher);
errno = read_data.error;
rb_raise(rb_eRuntimeError, "Unknown error: unexpected notification data");
return Qnil;
}
}
|