Atomic socket entry

I use process-send-stringto send data to a socket connection, but I am not satisfied with the effect of this function. Roughly speaking, the call (process-send-string "foo")may end with sending "bar", and then "foo", as described below.

As pointed out by the Emacs developers, the C code process-send-stringcalls a function wait_reading_process_output(even before actually writing something) that can start timers, which in turn can call process-send-string, and no ordering is performed between such nested calls.

This makes it almost impossible to implement the RPC protocol, which is intended for use by hooks called at uncontrolled times. So my question is, how could we achieve an atomic "synchronized" writing primitive for this purpose?

+5
source share
3 answers

Finally, the option that worked for me was to use the Transaction Queue API, which is a higher level (therefore, it cannot handle all kinds of protocols), but provides the correct sequence of message alternation.

+1
source

You can do something like:

(defun my-send-cmd (proc str)
  (if (process-get proc 'my-waiting)
      (process-put proc 'my-pending (append (process-get proc 'my-pending) (list str)))
    (process-put proc 'my-waiting t)
    (process-send-string proc str)))

, , `my-pending ' -nil, arg, , nil, , , .

, , , , , , , , , , M-x report-emacs-bug.

+3

Instead of using the process-send line directly in your hooks, add to the buffer, then calls with the processing line are executed in such a way that it is not asynchronous.

+2
source

All Articles