File: callback.pl

package info (click to toggle)
perl 5.40.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 126,156 kB
  • sloc: ansic: 668,539; perl: 525,522; sh: 72,038; pascal: 6,925; xml: 2,428; yacc: 1,410; makefile: 1,191; cpp: 208; lisp: 1
file content (123 lines) | stat: -rwxr-xr-x 3,038 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/perl

# Simplified example illustrating event handling and callback threads

# Callback threads register their queues with the event handler thread.
# Events are passed to the event handler via a queue.
# The event handler then disseminates the event to the appropriately
#   registered thread.

use strict;
use warnings;

use threads;
use Thread::Queue;

MAIN:
{
    # Queue for registering callbacks
    my $regis_q = Thread::Queue->new();

    # Queue for disseminating events
    my $event_q = Thread::Queue->new();

    # Create callback threads
    threads->create('CallBack', 'USR1', $regis_q)->detach();
    threads->create('CallBack', 'USR2', $regis_q)->detach();
    threads->create('CallBack', 'HUP', $regis_q)->detach();
    threads->create('CallBack', 'ALRM', $regis_q)->detach();

    # Create event handler thread
    threads->create('EventHandler', $regis_q, $event_q)->detach();

    # Capture SIGUSR1 events
    $SIG{'USR1'} = sub {
        $event_q->enqueue('USR1');  # Send to event handler
    };

    # Capture SIGUSR1 events
    $SIG{'USR2'} = sub {
        $event_q->enqueue('USR2');  # Send to event handler
    };

    # Capture SIGHUP events
    $SIG{'HUP'} = sub {
        $event_q->enqueue('HUP');  # Send to event handler
    };

    # Capture SIGHUP events
    $SIG{'ALRM'} = sub {
        $event_q->enqueue('ALRM');  # Send to event handler
        alarm(5);                   # Reset alarm
    };

    # Ready
    print(<<_MSG_);
Send signals to PID = $$
  (e.g., 'kill -USR1 $$')
Use ^C (or 'kill -INT $$') to terminate
_MSG_

    # Set initial alarm
    alarm(5);

    # Just hang around
    while (1) {
        sleep(10);
    }
}

### Subroutines ###

sub EventHandler
{
    my ($regis_q, $event_q) = @_;

    my %callbacks;   # Registered callback queues

    while (1) {
        # Check for any registrations
        while (my ($event_type, $q) = $regis_q->dequeue_nb(2)) {
            if ($q) {
                $callbacks{$event_type} = $q;
            } else {
                warn("BUG: Bad callback registration for event type $event_type\n");
            }
        }

        # Wait for event
        if (my $event = $event_q->dequeue()) {
            # Send event to appropriate queue
            if (exists($callbacks{$event})) {
                $callbacks{$event}->enqueue($event);
            } else {
                warn("WARNING: No callback for event type $event\n");
            }
        }
    }
}


sub CallBack
{
    my $event_type = shift;   # The type of event I'm handling
    my $regis_q    = shift;

    # Announce registration
    my $tid = threads->tid();
    print("Callback thread $tid registering for $event_type events\n");

    # Register my queue for my type of event
    my $q = Thread::Queue->new();
    $regis_q->enqueue($event_type, $q);

    # Process loop
    while (1) {
        # Wait for event callback
        my $item = $q->dequeue();
        # Process event
        print("Callback thread $tid notified of $item event\n") if $item;
    }
}

# EOF