aboutsummaryrefslogtreecommitdiffstats
path: root/src/kqueue.c
diff options
context:
space:
mode:
authorMichael Albinus2015-11-09 20:26:10 +0100
committerMichael Albinus2015-11-25 15:07:10 +0100
commit7543d1cf46e475bd14a147ef676abe3935a8f96e (patch)
treee34124493ba1a54fb8a5056ec16c03176632ea99 /src/kqueue.c
parente3354e2265bc442e4c7b84b806be482db88581a2 (diff)
downloademacs-7543d1cf46e475bd14a147ef676abe3935a8f96e.tar.gz
emacs-7543d1cf46e475bd14a147ef676abe3935a8f96e.zip
Work on kqueue
* lisp/filenotify.el (file-notify--library) (file-notify-descriptors, file-notify-callback) (file-notify-add-watch, file-notify-rm-watch) (file-notify-valid-p): Add kqueue support. * src/keyboard.c (make_lispy_event): Check also for HAVE_KQUEUE.
Diffstat (limited to 'src/kqueue.c')
-rw-r--r--src/kqueue.c270
1 files changed, 100 insertions, 170 deletions
diff --git a/src/kqueue.c b/src/kqueue.c
index 69bf5f61080..a4c3892e9f2 100644
--- a/src/kqueue.c
+++ b/src/kqueue.c
@@ -21,10 +21,10 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21#ifdef HAVE_KQUEUE 21#ifdef HAVE_KQUEUE
22#include <stdio.h> 22#include <stdio.h>
23#include <sys/event.h> 23#include <sys/event.h>
24#include <sys/file.h>
24#include "lisp.h" 25#include "lisp.h"
25#include "coding.h"
26#include "termhooks.h"
27#include "keyboard.h" 26#include "keyboard.h"
27#include "process.h"
28 28
29 29
30/* File handle for kqueue. */ 30/* File handle for kqueue. */
@@ -33,149 +33,103 @@ static int kqueuefd = -1;
33/* This is a list, elements are triples (DESCRIPTOR FILE FLAGS CALLBACK) */ 33/* This is a list, elements are triples (DESCRIPTOR FILE FLAGS CALLBACK) */
34static Lisp_Object watch_list; 34static Lisp_Object watch_list;
35 35
36#if 0 36/* This is the callback function for arriving input on kqueuefd. It
37/* This is the callback function for arriving signals from 37 shall create a Lisp event, and put it into Emacs input queue. */
38 g_file_monitor. It shall create a Lisp event, and put it into 38static void
39 Emacs input queue. */ 39kqueue_callback (int fd, void *data)
40static gboolean
41dir_monitor_callback (GFileMonitor *monitor,
42 GFile *file,
43 GFile *other_file,
44 GFileMonitorEvent event_type,
45 gpointer user_data)
46{ 40{
47 Lisp_Object symbol, monitor_object, watch_object, flags; 41 for (;;) {
48 char *name = g_file_get_parse_name (file); 42 struct kevent kev;
49 char *oname = other_file ? g_file_get_parse_name (other_file) : NULL; 43 struct input_event event;
50 44 Lisp_Object monitor_object, watch_object, name, callback, actions;
51 /* Determine event symbol. */ 45
52 switch (event_type) 46 static const struct timespec nullts = { 0, 0 };
53 { 47 int ret = kevent (kqueuefd, NULL, 0, &kev, 1, NULL);
54 case G_FILE_MONITOR_EVENT_CHANGED: 48 if (ret < 1) {
55 symbol = Qchanged; 49 /* All events read. */
56 break; 50 return;
57 case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
58 symbol = Qchanges_done_hint;
59 break;
60 case G_FILE_MONITOR_EVENT_DELETED:
61 symbol = Qdeleted;
62 break;
63 case G_FILE_MONITOR_EVENT_CREATED:
64 symbol = Qcreated;
65 break;
66 case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
67 symbol = Qattribute_changed;
68 break;
69 case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
70 symbol = Qpre_unmount;
71 break;
72 case G_FILE_MONITOR_EVENT_UNMOUNTED:
73 symbol = Qunmounted;
74 break;
75 case G_FILE_MONITOR_EVENT_MOVED:
76 symbol = Qmoved;
77 break;
78 default:
79 goto cleanup;
80 } 51 }
81 52
82 /* Determine callback function. */ 53 /* Determine file name and callback function. */
83 monitor_object = make_pointer_integer (monitor); 54 monitor_object = make_number (kev.ident);
84 eassert (INTEGERP (monitor_object)); 55 watch_object = assq_no_quit (monitor_object, watch_list);
85 watch_object = assq_no_quit (monitor_object, watch_list);
86 56
87 if (CONSP (watch_object)) 57 if (CONSP (watch_object)) {
88 { 58 name = XCAR (XCDR (watch_object));
89 struct input_event event; 59 callback = XCAR (XCDR (XCDR (XCDR (watch_object))));
90 Lisp_Object otail = oname ? list1 (build_string (oname)) : Qnil; 60 }
91 61 else
92 /* Check, whether event_type is expected. */ 62 continue;
93 flags = XCAR (XCDR (XCDR (watch_object))); 63
94 if ((!NILP (Fmember (Qchange, flags)) && 64 /* Determine event actions. */
95 !NILP (Fmember (symbol, list5 (Qchanged, Qchanges_done_hint, 65 actions = Qnil;
96 Qdeleted, Qcreated, Qmoved)))) || 66 if (kev.fflags & NOTE_DELETE)
97 (!NILP (Fmember (Qattribute_change, flags)) && 67 actions = Fcons (Qdelete, actions);
98 ((EQ (symbol, Qattribute_changed))))) 68 if (kev.fflags & NOTE_WRITE)
99 { 69 actions = Fcons (Qwrite, actions);
100 /* Construct an event. */ 70 if (kev.fflags & NOTE_EXTEND)
101 EVENT_INIT (event); 71 actions = Fcons (Qextend, actions);
102 event.kind = FILE_NOTIFY_EVENT; 72 if (kev.fflags & NOTE_ATTRIB)
103 event.frame_or_window = Qnil; 73 actions = Fcons (Qattrib, actions);
104 event.arg = list2 (Fcons (monitor_object, 74 if (kev.fflags & NOTE_LINK)
105 Fcons (symbol, 75 actions = Fcons (Qlink, actions);
106 Fcons (build_string (name), 76 if (kev.fflags & NOTE_RENAME)
107 otail))), 77 actions = Fcons (Qrename, actions);
108 XCAR (XCDR (XCDR (XCDR (watch_object))))); 78
109 79 if (!NILP (actions)) {
110 /* Store it into the input event queue. */ 80 /* Construct an event. */
111 kbd_buffer_store_event (&event); 81 EVENT_INIT (event);
112 // XD_DEBUG_MESSAGE ("%s", XD_OBJECT_TO_STRING (event.arg)); 82 event.kind = FILE_NOTIFY_EVENT;
113 } 83 event.frame_or_window = Qnil;
114 84 event.arg = list2 (Fcons (monitor_object,
115 /* Cancel monitor if file or directory is deleted. */ 85 Fcons (actions, Fcons (name, Qnil))),
116 if (!NILP (Fmember (symbol, list2 (Qdeleted, Qmoved))) && 86 callback);
117 (strcmp (name, SSDATA (XCAR (XCDR (watch_object)))) == 0) && 87
118 !g_file_monitor_is_cancelled (monitor)) 88 /* Store it into the input event queue. */
119 g_file_monitor_cancel (monitor); 89 kbd_buffer_store_event (&event);
120 } 90 }
121 91
122 /* Cleanup. */ 92 /* Cancel monitor if file or directory is deleted. */
123 cleanup: 93 /* TODO: Implement it. */
124 g_free (name); 94 }
125 g_free (oname); 95 return;
126
127 return TRUE;
128} 96}
129#endif /* 0 */
130 97
131DEFUN ("kqueue-add-watch", Fkqueue_add_watch, Skqueue_add_watch, 3, 3, 0, 98DEFUN ("kqueue-add-watch", Fkqueue_add_watch, Skqueue_add_watch, 3, 3, 0,
132 doc: /* Add a watch for filesystem events pertaining to FILE. 99 doc: /* Add a watch for filesystem events pertaining to FILE.
133 100
134This arranges for filesystem events pertaining to FILE to be reported 101This arranges for filesystem events pertaining to FILE to be reported
135to Emacs. Use `gfile-rm-watch' to cancel the watch. 102to Emacs. Use `kqueue-rm-watch' to cancel the watch.
136 103
137Value is a descriptor for the added watch. If the file cannot be 104Value is a descriptor for the added watch. If the file cannot be
138watched for some reason, this function signals a `file-notify-error' error. 105watched for some reason, this function signals a `file-notify-error' error.
139 106
140FLAGS is a list of conditions to set what will be watched for. It can 107FLAGS is a list of events to be watched for. It can include the
141include the following symbols: 108following symbols:
142 109
143 `change' -- watch for file changes 110 `delete' -- FILE was deleted
144 `attribute-change' -- watch for file attributes changes, like 111 `write' -- FILE has changed
145 permissions or modification time 112 `extend' -- FILE was extended
146 `watch-mounts' -- watch for mount events 113 `attrib' -- a FILE attribute was changed
147 `send-moved' -- pair `deleted' and `created' events caused by 114 `link' -- a FILE's link count was changed
148 file renames and send a single `renamed' event 115 `rename' -- FILE was moved to FILE1
149 instead
150 116
151When any event happens, Emacs will call the CALLBACK function passing 117When any event happens, Emacs will call the CALLBACK function passing
152it a single argument EVENT, which is of the form 118it a single argument EVENT, which is of the form
153 119
154 (DESCRIPTOR ACTION FILE [FILE1]) 120 (DESCRIPTOR ACTIONS FILE [FILE1])
155 121
156DESCRIPTOR is the same object as the one returned by this function. 122DESCRIPTOR is the same object as the one returned by this function.
157ACTION is the description of the event. It could be any one of the 123ACTIONS is a list of events.
158following:
159
160 `changed' -- FILE has changed
161 `changes-done-hint' -- a hint that this was probably the last change
162 in a set of changes
163 `deleted' -- FILE was deleted
164 `created' -- FILE was created
165 `attribute-changed' -- a FILE attribute was changed
166 `pre-unmount' -- the FILE location will soon be unmounted
167 `unmounted' -- the FILE location was unmounted
168 `moved' -- FILE was moved to FILE1
169 124
170FILE is the name of the file whose event is being reported. FILE1 125FILE is the name of the file whose event is being reported. FILE1
171will be reported only in case of the `moved' event. */) 126will be reported only in case of the `rename' event. */)
172 (Lisp_Object file, Lisp_Object flags, Lisp_Object callback) 127 (Lisp_Object file, Lisp_Object flags, Lisp_Object callback)
173{ 128{
174 Lisp_Object watch_object; 129 Lisp_Object watch_object;
175 GFile *gfile; 130 int fd;
176 GFileMonitor *monitor; 131 u_short fflags = 0;
177 GFileMonitorFlags gflags = G_FILE_MONITOR_NONE; 132 struct kevent ev;
178 GError *gerror = NULL;
179 133
180 /* Check parameters. */ 134 /* Check parameters. */
181 CHECK_STRING (file); 135 CHECK_STRING (file);
@@ -183,80 +137,62 @@ will be reported only in case of the `moved' event. */)
183 if (NILP (Ffile_exists_p (file))) 137 if (NILP (Ffile_exists_p (file)))
184 report_file_error ("File does not exist", file); 138 report_file_error ("File does not exist", file);
185 139
140 /* TODO: Directories shall be supported as well. */
141 if (!NILP (Ffile_directory_p (file)))
142 report_file_error ("Directory watching is not supported (yet)", file);
143
186 CHECK_LIST (flags); 144 CHECK_LIST (flags);
187 145
188 if (!FUNCTIONP (callback)) 146 if (!FUNCTIONP (callback))
189 wrong_type_argument (Qinvalid_function, callback); 147 wrong_type_argument (Qinvalid_function, callback);
190 148
191 /* Create GFile name. */
192 // gfile = g_file_new_for_path (SSDATA (ENCODE_FILE (file)));
193
194 /* Assemble flags. */
195 // if (!NILP (Fmember (Qwatch_mounts, flags)))
196 // gflags |= G_FILE_MONITOR_WATCH_MOUNTS;
197 // if (!NILP (Fmember (Qsend_moved, flags)))
198 // gflags |= G_FILE_MONITOR_SEND_MOVED;
199
200 if (kqueuefd < 0) 149 if (kqueuefd < 0)
201 { 150 {
151 /* Create kqueue descriptor. */
202 kqueuefd = kqueue (); 152 kqueuefd = kqueue ();
203 if (kqueuefd < 0) 153 if (kqueuefd < 0)
204 report_file_notify_error ("File watching is not available", Qnil); 154 report_file_notify_error ("File watching is not available", Qnil);
205 watch_list = Qnil;
206 // add_read_fd (inotifyfd, &inotify_callback, NULL);
207 }
208
209
210}
211#if 0
212 155
213 mask = aspect_to_inotifymask (aspect); 156 /* Start monitoring for possible I/O. */
214 encoded_file_name = ENCODE_FILE (file_name); 157 add_read_fd (kqueuefd, kqueue_callback, NULL); //data);
215 watchdesc = inotify_add_watch (inotifyfd, SSDATA (encoded_file_name), mask);
216 if (watchdesc == -1)
217 report_file_notify_error ("Could not add watch for file", file_name);
218 158
219 /* Enable watch. */ 159 watch_list = Qnil;
220 monitor = g_file_monitor (gfile, gflags, NULL, &gerror);
221 g_object_unref (gfile);
222 if (gerror)
223 {
224 char msg[1024];
225 strcpy (msg, gerror->message);
226 g_error_free (gerror);
227 xsignal1 (Qfile_notify_error, build_string (msg));
228 } 160 }
229 if (! monitor)
230 xsignal2 (Qfile_notify_error, build_string ("Cannot watch file"), file);
231 161
232 Lisp_Object watch_descriptor = make_pointer_integer (monitor); 162 /* Open file. */
163 file = ENCODE_FILE (file);
164 fd = emacs_open (SSDATA (file), O_NONBLOCK | O_BINARY | O_RDONLY, 0);
165 if (fd == -1)
166 report_file_error ("File cannot be opened", file);
233 167
234 /* Check the dicey assumption that make_pointer_integer is safe. */ 168 /* Assemble filter flags */
235 if (! INTEGERP (watch_descriptor)) 169 if (!NILP (Fmember (Qdelete, flags))) fflags |= NOTE_DELETE;
236 { 170 if (!NILP (Fmember (Qwrite, flags))) fflags |= NOTE_WRITE;
237 g_object_unref (monitor); 171 if (!NILP (Fmember (Qextend, flags))) fflags |= NOTE_EXTEND;
238 xsignal2 (Qfile_notify_error, build_string ("Unsupported file watcher"), 172 if (!NILP (Fmember (Qattrib, flags))) fflags |= NOTE_ATTRIB;
239 file); 173 if (!NILP (Fmember (Qlink, flags))) fflags |= NOTE_LINK;
240 } 174 if (!NILP (Fmember (Qrename, flags))) fflags |= NOTE_RENAME;
241 175
242 /* The default rate limit is 800 msec. We adapt this. */ 176 /* Register event. */
243 g_file_monitor_set_rate_limit (monitor, 100); 177 EV_SET (&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
178 fflags, 0, NULL);
244 179
245 /* Subscribe to the "changed" signal. */ 180 if (kevent (kqueuefd, &ev, 1, NULL, 0, NULL) < 0)
246 g_signal_connect (monitor, "changed", 181 report_file_error ("Cannot watch file", file);
247 (GCallback) dir_monitor_callback, NULL);
248 182
249 /* Store watch object in watch list. */ 183 /* Store watch object in watch list. */
184 Lisp_Object watch_descriptor = make_number (fd);
250 watch_object = list4 (watch_descriptor, file, flags, callback); 185 watch_object = list4 (watch_descriptor, file, flags, callback);
251 watch_list = Fcons (watch_object, watch_list); 186 watch_list = Fcons (watch_object, watch_list);
252 187
253 return watch_descriptor; 188 return watch_descriptor;
254} 189}
255 190
256DEFUN ("gfile-rm-watch", Fgfile_rm_watch, Sgfile_rm_watch, 1, 1, 0, 191#if 0
192DEFUN ("kqueue-rm-watch", Fkqueue_rm_watch, Skqueue_rm_watch, 1, 1, 0,
257 doc: /* Remove an existing WATCH-DESCRIPTOR. 193 doc: /* Remove an existing WATCH-DESCRIPTOR.
258 194
259WATCH-DESCRIPTOR should be an object returned by `gfile-add-watch'. */) 195WATCH-DESCRIPTOR should be an object returned by `kqueue-add-watch'. */)
260 (Lisp_Object watch_descriptor) 196 (Lisp_Object watch_descriptor)
261{ 197{
262 Lisp_Object watch_object = assq_no_quit (watch_descriptor, watch_list); 198 Lisp_Object watch_object = assq_no_quit (watch_descriptor, watch_list);
@@ -317,12 +253,6 @@ syms_of_kqueue (void)
317 // defsubr (&Skqueue_rm_watch); 253 // defsubr (&Skqueue_rm_watch);
318 // defsubr (&Skqueue_valid_p); 254 // defsubr (&Skqueue_valid_p);
319 255
320 /* Filter objects. */
321 DEFSYM (Qchange, "change");
322 DEFSYM (Qattribute_change, "attribute-change");
323 DEFSYM (Qwatch_mounts, "watch-mounts"); /* G_FILE_MONITOR_WATCH_MOUNTS */
324 DEFSYM (Qsend_moved, "send-moved"); /* G_FILE_MONITOR_SEND_MOVED */
325
326 /* Event types. */ 256 /* Event types. */
327 DEFSYM (Qdelete, "delete"); /* NOTE_DELETE */ 257 DEFSYM (Qdelete, "delete"); /* NOTE_DELETE */
328 DEFSYM (Qwrite, "write"); /* NOTE_WRITE */ 258 DEFSYM (Qwrite, "write"); /* NOTE_WRITE */