ext/wdm/rb_monitor.c in wdm-0.1.1 vs ext/wdm/rb_monitor.c in wdm-0.2.0

- old
+ new

@@ -48,11 +48,11 @@ static void CALLBACK handle_entry_change(DWORD, DWORD, LPOVERLAPPED); static BOOL register_monitoring_entry(WDM_PEntry); static DWORD WINAPI start_monitoring(LPVOID); -static VALUE wait_for_changes(LPVOID); +static void *wait_for_changes(void *); static void process_changes(WDM_PQueue); static void stop_monitoring(LPVOID); static VALUE rb_monitor_run_bang(VALUE); static VALUE rb_monitor_stop(VALUE); @@ -97,18 +97,28 @@ } wdm_monitor_free(monitor); } +static const rb_data_type_t monitor_data_type = { + .wrap_struct_name = "WDM::Monitor", + .function = { + .dmark = monitor_mark, + .dfree = monitor_free, + .dsize = NULL, + }, + .flags = 0 +}; + static VALUE rb_monitor_alloc(VALUE self) { WDM_DEBUG("--------------------------------"); WDM_DEBUG("Allocating a new monitor object!"); WDM_DEBUG("--------------------------------"); - return Data_Wrap_Struct(self, monitor_mark, monitor_free, wdm_monitor_new()); + return TypedData_Wrap_Struct(self, &monitor_data_type, wdm_monitor_new()); } static DWORD id_to_flag(ID id) { @@ -154,11 +164,11 @@ BOOL running; // TODO: Maybe raise a more user-friendly error? rb_need_block(); - Data_Get_Struct(self, WDM_Monitor, monitor); + TypedData_Get_Struct(self, WDM_Monitor, &monitor_data_type, monitor); EnterCriticalSection(&monitor->lock); running = monitor->running; LeaveCriticalSection(&monitor->lock); @@ -194,11 +204,11 @@ entry->user_data->dir = ALLOCA_N(WCHAR, directory_letters_count); MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(os_encoded_directory), -1, entry->user_data->dir, directory_letters_count); - WDM_WDEBUG("New path to watch: '%s'", entry->user_data->dir); + WDM_DEBUG("New path to watch: '%S'", entry->user_data->dir); entry->user_data->dir = wdm_utils_full_pathname(entry->user_data->dir); if ( entry->user_data->dir == 0 ) { wdm_entry_free(entry); @@ -232,11 +242,11 @@ // won't be used when using callbacks. entry->event_container.hEvent = wdm_monitor_callback_param_new(monitor, entry); wdm_monitor_update_head(monitor, entry); - WDM_WDEBUG("Watching directory: '%s'", entry->user_data->dir); + WDM_DEBUG("Watching directory: '%S'", entry->user_data->dir); return Qnil; } static VALUE @@ -276,11 +286,11 @@ param = (WDM_PMonitorCallbackParam)event_container->hEvent; data_to_process = wdm_queue_item_new(WDM_QUEUE_ITEM_TYPE_DATA); data_to_process->data = wdm_queue_item_data_new(); - WDM_WDEBUG("Change detected in '%s'", param->entry->user_data->dir); + WDM_DEBUG("Change detected in '%S'", param->entry->user_data->dir); data_to_process->data->user_data = param->entry->user_data; // Copy change data to the backup buffer memcpy(data_to_process->data->buffer, param->entry->buffer, bytes_transfered); @@ -365,18 +375,18 @@ } return 0; } -static VALUE -wait_for_changes(LPVOID param) +static void * +wait_for_changes(void *param) { - HANDLE process_event; + HANDLE process_event = (HANDLE)param; + VALUE rb_res; - process_event = (HANDLE)param; - - return WaitForSingleObject(process_event, INFINITE) == WAIT_OBJECT_0 ? Qtrue : Qfalse; + rb_res = WaitForSingleObject(process_event, INFINITE) == WAIT_OBJECT_0 ? Qtrue : Qfalse; + return (void *)rb_res; } static void process_changes(WDM_PQueue changes) { @@ -465,11 +475,11 @@ waiting_succeeded; WDM_PMonitor monitor; WDM_DEBUG("Running the monitor!"); - Data_Get_Struct(self, WDM_Monitor, monitor); + TypedData_Get_Struct(self, WDM_Monitor, &monitor_data_type, monitor); already_running = FALSE; EnterCriticalSection(&monitor->lock); if ( monitor->running ) { already_running = TRUE; @@ -501,17 +511,11 @@ rb_raise(eWDM_Error, "Can't create a thread for the monitor!"); } while ( monitor->running ) { - // Ruby 2.2 removed the 'rb_thread_blocking_region' function. Hence, we now need - // to check if the replacement function is defined and use it if it's available. - #ifdef HAVE_RB_THREAD_CALL_WITHOUT_GVL - waiting_succeeded = rb_thread_call_without_gvl(wait_for_changes, monitor->process_event, stop_monitoring, monitor); - #else - waiting_succeeded = rb_thread_blocking_region(wait_for_changes, monitor->process_event, stop_monitoring, monitor); - #endif + waiting_succeeded = (VALUE)rb_thread_call_without_gvl(wait_for_changes, monitor->process_event, stop_monitoring, monitor); if ( waiting_succeeded == Qfalse ) { rb_raise(eWDM_Error, "Failed while waiting for a change in the watched directories!"); } @@ -533,11 +537,11 @@ static VALUE rb_monitor_stop(VALUE self) { WDM_PMonitor monitor; - Data_Get_Struct(self, WDM_Monitor, monitor); + TypedData_Get_Struct(self, WDM_Monitor, &monitor_data_type, monitor); stop_monitoring(monitor); WDM_DEBUG("Stopped the monitor!"); @@ -570,6 +574,6 @@ rb_define_alloc_func(cWDM_Monitor, rb_monitor_alloc); rb_define_method(cWDM_Monitor, "watch", RUBY_METHOD_FUNC(rb_monitor_watch), -1); rb_define_method(cWDM_Monitor, "watch_recursively", RUBY_METHOD_FUNC(rb_monitor_watch_recursively), -1); rb_define_method(cWDM_Monitor, "run!", RUBY_METHOD_FUNC(rb_monitor_run_bang), 0); rb_define_method(cWDM_Monitor, "stop", RUBY_METHOD_FUNC(rb_monitor_stop), 0); -} \ No newline at end of file +}