Class: WDM::Monitor

Inherits:
Object
  • Object
show all
Defined in:
ext/wdm/rb_monitor.c

Instance Method Summary collapse

Instance Method Details

#run!Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'ext/wdm/rb_monitor.c', line 430

static VALUE
rb_monitor_run_bang(VALUE self) {
    DWORD thread_id;
    BOOL already_running,
         waiting_succeeded;
    WDM_PMonitor monitor;

    WDM_DEBUG("Running the monitor!");

    Data_Get_Struct(self, WDM_Monitor, monitor);
    already_running = FALSE;

    EnterCriticalSection(&monitor->lock);
        if ( monitor->running ) {
            already_running = TRUE;
        }
        else {
            monitor->running = TRUE;
        }
    LeaveCriticalSection(&monitor->lock);

    if (already_running) {
        WDM_DEBUG("Not doing anything because the monitor is already running!");
        return;
    }

    // Reset events
    ResetEvent(monitor->process_event);
    ResetEvent(monitor->stop_event);

    monitor->monitoring_thread = CreateThread(
        NULL,                     // default security attributes
        0,                        // use default stack size
        start_monitoring,         // thread function name
        monitor,                  // argument to thread function
        0,                        // use default creation flags
        &thread_id                // returns the thread identifier
    );

    if ( monitor->monitoring_thread == NULL ) {
        rb_raise(eWDM_Error, "Can't create a thread for the monitor!");
    }

    while ( monitor->running ) {
        waiting_succeeded = rb_thread_blocking_region(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!");
        }

        if ( ! monitor->running ) {
            wdm_queue_empty(monitor->changes);
            return;
        }

        process_changes(monitor->changes);

        if ( ! ResetEvent(monitor->process_event) ) {
            rb_raise(eWDM_Error, "Couldn't reset system events to watch for changes!");
        }
    }

    return Qnil;
}

#stopObject



495
496
497
498
499
500
501
502
503
504
505
506
# File 'ext/wdm/rb_monitor.c', line 495

static VALUE
rb_monitor_stop(VALUE self) {
    WDM_PMonitor monitor;

    Data_Get_Struct(self, WDM_Monitor, monitor);

    stop_monitoring(monitor);

    WDM_DEBUG("Stopped the monitor!");

    return Qnil;
}

#watch(*args) ⇒ Object



242
243
244
245
# File 'ext/wdm/rb_monitor.c', line 242

static VALUE
rb_monitor_watch(int argc, VALUE *argv, VALUE self) {
    return combined_watch(FALSE, argc, argv, self);
}

#watch_recursively(*args) ⇒ Object



247
248
249
250
# File 'ext/wdm/rb_monitor.c', line 247

static VALUE
rb_monitor_watch_recursively(int argc, VALUE *argv, VALUE self) {
    return combined_watch(TRUE, argc, argv, self);
}