aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEli Zaretskii2015-03-28 20:37:47 +0300
committerEli Zaretskii2015-03-28 20:37:47 +0300
commit22ece83a655a18db67c6de190b6804c4d8d182a6 (patch)
treefb7c2af8fbc1aa4d37cd4e0dfa9c2fbaf5fa5619 /src
parent8478885dfab16359b989e030949b4d485062f54b (diff)
downloademacs-22ece83a655a18db67c6de190b6804c4d8d182a6.tar.gz
emacs-22ece83a655a18db67c6de190b6804c4d8d182a6.zip
src/w32proc.c: Describe in a comment w32 subprocess implementation.
Diffstat (limited to 'src')
-rw-r--r--src/w32proc.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/w32proc.c b/src/w32proc.c
index c067eba2b4e..dbd9573d633 100644
--- a/src/w32proc.c
+++ b/src/w32proc.c
@@ -787,6 +787,138 @@ alarm (int seconds)
787#endif 787#endif
788} 788}
789 789
790
791
792/* Here's an overview of how support for subprocesses and
793 network/serial streams is implemented on MS-Windows.
794
795 The management of both subprocesses and network/serial streams
796 circles around the child_procs[] array, which can record up to the
797 grand total of MAX_CHILDREN (= 32) of these. (The reasons for the
798 32 limitation will become clear below.) Each member of
799 child_procs[] is a child_process structure, defined on w32.h.
800
801 A related data structure is the fd_info[] array, which holds twice
802 as many members, 64, and records the information about file
803 descriptors used for communicating with subprocesses and
804 network/serial devices. Each member of the array is the filedesc
805 structure, which records the Windows handle for communications,
806 such as the read end of the pipe to a subprocess, a socket handle,
807 etc.
808
809 Both these arrays reference each other: there's a member of
810 child_process structure that records the file corresponding
811 descriptor, and there's a member of filedesc structure that holds a
812 pointer to the corresponding child_process.
813
814 Whenever Emacs starts a subprocess or opens a network/serial
815 stream, the function new_child is called to prepare a new
816 child_process structure. new_child looks for the first vacant slot
817 in the child_procs[] array, initializes it, and starts a "reader
818 thread" that will watch the output of the subprocess/stream and its
819 status. (If no vacant slot can be found, new_child returns a
820 failure indication to its caller, and the higher-level Emacs
821 primitive will then fail with EMFILE or EAGAIN.)
822
823 The reader thread started by new_child communicates with the main
824 (a.k.a. "Lisp") thread via two event objects and a status, all of
825 them recorded by the members of the child_process structure in
826 child_procs[]. The event objects serve as semaphores between the
827 reader thread and the 'select' emulation in sys_select, as follows:
828
829 . Initially, the reader thread is waiting for the char_consumed
830 event to become signaled by sys_select, which is an indication
831 for the reader thread to go ahead and try reading more stuff
832 from the subprocess/stream.
833
834 . The reader thread then attempts to read by calling a
835 blocking-read function. When the read call returns, either
836 successfully or with some failure indication, the reader thread
837 updates the status of the read accordingly, and signals the 2nd
838 event object, char_avail, on whose handle sys_select is
839 waiting. This tells sys_select that the file descriptor
840 allocated for the subprocess or the the stream is ready to be
841 read from.
842
843 When the subprocess exits or the network/serial stream is closed,
844 the reader thread sets the status accordingly and exits. It also
845 exits when the main thread sets the ststus to STATUS_READ_ERROR
846 and/or the char_avail and char_consumed event handles are NULL;
847 this is how delete_child, called by Emacs when a subprocess or a
848 stream is terminated, terminates the reader thread as part of
849 deleting the child_process object.
850
851 The sys_select function emulates the Posix 'pselect' function; it
852 is needed because the Windows 'select' function supports only
853 network sockets, while Emacs expects 'pselect' to work for any file
854 descriptor, including pipes and serial streams.
855
856 When sys_select is called, it uses the information in fd_info[]
857 array to convert the file descriptors which it was asked to watch
858 into Windows handles. In general, the handle to watch is the
859 handle of the char_avail event of the child_process structure that
860 corresponds to the file descriptor. In addition, for subprocesses,
861 sys_select watches one more handle: the handle for the subprocess,
862 so that it could emulate the SIGCHLD signal when the subprocess
863 exits.
864
865 If file descriptor zero (stdin) doesn't have its bit set in the
866 'rfds' argument to sys_select, the function always watches for
867 keyboard interrupts, to be able to return when the user presses
868 C-g.
869
870 Having collected the handles to watch, sys_select calls
871 WaitForMultipleObjects to wait for any one of them to become
872 signaled. Since WaitForMultipleObjects can only watch up to 64
873 handles, Emacs on Windows is limited to maximum 32 child_process
874 objects (since a subprocess consumes 2 handles to be watched, see
875 above).
876
877 When any of the handles become signaled, sys_select does whatever
878 is appropriate for the corresponding child_process object:
879
880 . If it's a handle to the char_avail event, sys_select marks the
881 corresponding bit in 'rfds', and Emacs will then read from that
882 file descriptor.
883
884 . If it's a handle to the process, sys_select calls the SIGCHLD
885 handler, to inform Emacs of the fact that the subprocess
886 exited.
887
888 The waitpid emulation works very similar to sys_select, except that
889 it only watches handles of subprocesses, and doesn't synchronize
890 with the reader thread.
891
892 Because socket descriptors on Windows are handles, while Emacs
893 expects them to be file descriptors, all low-level I/O functions,
894 such as 'read' and 'write', and all socket operations, like
895 'connect', 'recvfrom', 'accept', etc., are redirected to the
896 corresponding 'sys_*' functions, which must convert a file
897 descriptor to a handle using the fd_info[] array, and then invoke
898 the corresponding Windows API on the handle. Most of these
899 redirected 'sys_*' functions are implemented on w32.c.
900
901 When the file descriptor was produced by functions such as 'open',
902 the corresponding handle is obtained by calling _get_osfhandle. To
903 produce a file descriptor for a socket handle, which has no file
904 descriptor as far as Windows is concerned, the function
905 socket_to_fd opens the null device; the resulting file descriptor
906 will never be used directly in any I/O API, but serves as an index
907 into the fd_info[] array, where the socket handle is stored. The
908 SOCK_HANDLE macro retrieves the handle when given the file
909 descriptor.
910
911 The function sys_kill emulates the Posix 'kill' functionality to
912 terminate other processes. It does that by attaching to the
913 foreground window of the process and sending a Ctrl-C or Ctrl-BREAK
914 signal to the process; if that doesn't work, then it calls
915 TerminateProcess to forcibly terminate the process. Note that this
916 only terminates the immediate process whose PID was passed to
917 sys_kill; it doesn't terminate the child processes of that process.
918 This means, for example, that an Emacs subprocess run through a
919 shell might not be killed, because sys_kill will only terminate the
920 shell. (In practice, however, such problems are very rare.) */
921
790/* Defined in <process.h> which conflicts with the local copy */ 922/* Defined in <process.h> which conflicts with the local copy */
791#define _P_NOWAIT 1 923#define _P_NOWAIT 1
792 924