Commit | Line | Data |
4536f655 |
1 | # File: Stem/Event/Queue.pm |
2 | |
3 | # This file is part of Stem. |
4 | # Copyright (C) 1999, 2000, 2001 Stem Systems, Inc. |
5 | |
6 | # Stem is free software; you can redistribute it and/or modify |
7 | # it under the terms of the GNU General Public License as published by |
8 | # the Free Software Foundation; either version 2 of the License, or |
9 | # (at your option) any later version. |
10 | |
11 | # Stem is distributed in the hope that it will be useful, |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | # GNU General Public License for more details. |
15 | |
16 | # You should have received a copy of the GNU General Public License |
17 | # along with Stem; if not, write to the Free Software |
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | |
20 | # For a license to use the Stem under conditions other than those |
21 | # described here, to purchase support for this software, or to purchase a |
22 | # commercial warranty contract, please contact Stem Systems at: |
23 | |
24 | # Stem Systems, Inc. 781-643-7504 |
25 | # 79 Everett St. info@stemsystems.com |
26 | # Arlington, MA 02474 |
27 | # USA |
28 | |
29 | # this class provides a way to deliver certain events and messages |
30 | # synchronously with the main event loop. this is done by queueing the |
31 | # actual event/message and writing a byte down a special pipe used |
32 | # only inside this process. the other side of the pipe has a read |
33 | # event that when triggered will then deliver the queued |
34 | # events/messages. |
35 | |
36 | # when using Stem::Event::Signal you need to use this module as |
37 | # well. perl signals will be delivered (safely) between perl |
38 | # operations but they could then be delivered inside an executing |
39 | # event handler and that means possible corruption. so this module |
40 | # allows those signal events to be delivered by the event loop itself. |
41 | |
42 | |
43 | package Stem::Event::Queue ; |
44 | |
45 | use strict ; |
46 | use warnings ; |
47 | |
48 | use Socket; |
49 | use IO::Handle ; |
50 | |
4932dd97 |
51 | my( $self, $queue_read, $queue_write, $queue_read_event, $queue_has_event ) ; |
4536f655 |
52 | |
4932dd97 |
53 | sub _init_event_queue { |
4536f655 |
54 | |
4932dd97 |
55 | return if $self ; |
4536f655 |
56 | |
4932dd97 |
57 | $self = bless {} ; |
4536f655 |
58 | |
59 | socketpair( $queue_read, $queue_write, |
60 | AF_UNIX, SOCK_STREAM, PF_UNSPEC ) || die <<DIE ; |
61 | can't create socketpair $! |
62 | DIE |
63 | |
64 | #print fileno( $queue_read ), " FILENO\n" ; |
65 | |
4536f655 |
66 | |
67 | $queue_read->blocking( 0 ) ; |
68 | $queue_read_event = Stem::Event::Read->new( |
69 | 'object' => $self, |
70 | 'fh' => $queue_read, |
71 | ) ; |
72 | |
73 | ref $queue_read_event or die <<DIE ; |
74 | can't create Stem::Event::Queue read event: $queue_read_event |
75 | DIE |
76 | |
77 | } |
78 | |
4932dd97 |
79 | sub queue_has_event { |
4536f655 |
80 | |
81 | my( $always_mark ) = @_ ; |
82 | |
83 | # don't mark the queue if it is already marked and we aren't forced |
84 | # the signal queue always marks the queue |
85 | |
4932dd97 |
86 | return if $queue_has_event && !$always_mark ; |
4536f655 |
87 | |
88 | syswrite( $queue_write, 'x' ) ; |
89 | |
4932dd97 |
90 | $queue_has_event = 1 ; |
4536f655 |
91 | } |
92 | |
93 | sub readable { |
94 | |
95 | sysread( $queue_read, my $buf, 10 ) ; |
96 | |
4932dd97 |
97 | $queue_has_event = 0 ; |
4536f655 |
98 | |
99 | # Stem::Event::Plain::process_queue(); |
100 | Stem::Event::Signal::process_signal_queue(); |
101 | # Stem::Msg::process_queue() if defined &Stem::Msg::process_queue; |
102 | |
103 | return ; |
104 | } |
105 | |
106 | 1 ; |