Skip to content

Commit

Permalink
Move calls to Win32 API calls outside of locked sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljmccarthy committed Aug 27, 2014
1 parent 17b3f5d commit ecb2533
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions fsmonitor/win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def __del__(self):
self.close()

def close(self):
self.remove_all_watches()
if self.__cphandle is not None:
win32file.CloseHandle(self.__cphandle)
self.__cphandle = None
Expand All @@ -129,36 +130,40 @@ def add_dir_watch(self, path, flags=FSEvent.All, user=None, recursive=False):
watch = FSMonitorWatch(path, flags, user, recursive)
with self.__lock:
key = self.__last_key
win32file.CreateIoCompletionPort(watch._hDir, self.__cphandle, key, 0)
self.__last_key += 1
read_changes(watch)
watch._key = key
self.__key_to_watch[key] = watch
win32file.CreateIoCompletionPort(watch._hDir, self.__cphandle, key, 0)
read_changes(watch)
return watch
except pywintypes.error as e:
raise FSMonitorWindowsError(*e.args)

def add_file_watch(self, path, flags=FSEvent.All, user=None):
raise NotImplementedError()

def __remove_watch(self, watch):
def remove_watch(self, watch):
if not watch._removed:
watch._removed = True
try:
watch._removed = True
close_watch(watch)
return True
except pywintypes.error:
pass
return False

def remove_watch(self, watch):
with self.__lock:
return self.__remove_watch(watch)

def remove_all_watches(self):
with self.__lock:
for watch in self.__key_to_watch.itervalues():
self.__remove_watch(watch)
watches_to_close = []
for watch in self.__key_to_watch.values():
if not watch._removed:
watch._removed = True
watches_to_close.append(watch)
for watch in watches_to_close:
try:
close_watch(watch)
except pywintypes.error:
pass

def enable_watch(self, watch, enable=True):
watch.enabled = enable
Expand Down

0 comments on commit ecb2533

Please sign in to comment.