751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
|
# File 'ext/ruby/passenger_native_support.c', line 751
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;
}
}
|