| 1 | # or add() take one object and multiple ids | ||
| 2 | # or new() accept signalids object too | ||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | # Copyright 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 7 | |||
| 8 | # This file is part of Chart. | ||
| 9 | # | ||
| 10 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 11 | # terms of the GNU General Public License as published by the Free Software | ||
| 12 | # Foundation; either version 3, or (at your option) any later version. | ||
| 13 | # | ||
| 14 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 15 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 16 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 17 | # details. | ||
| 18 | # | ||
| 19 | # You should have received a copy of the GNU General Public License along | ||
| 20 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | |||
| 22 | package App::Chart::Glib::Ex::SignalBlock; | ||
| 23 | use 5.008; | ||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | use Carp; | ||
| 27 | |||
| 28 | # uncomment this to run the ### lines | ||
| 29 | #use Smart::Comments; | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | my $class = shift; | ||
| 33 | my $self = bless [], $class; | ||
| 34 | $self->add (@_); | ||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub new_from_signalids { | ||
| 39 | my ($class, $ids) = @_; | ||
| 40 | return $class->new ($ids->object, $ids->ids); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub add { | ||
| 44 | my $self = shift; | ||
| 45 | ### SignalBlock on: "@pairs" | ||
| 46 | if ((@_ & 1) != 0) { | ||
| 47 | croak "SignalBlock expects even number of arguments for object,id pairs"; | ||
| 48 | } | ||
| 49 | require Scalar::Util; | ||
| 50 | while (@_) { | ||
| 51 | my $object = shift @_; | ||
| 52 | my $id = shift @_; | ||
| 53 | $object->handler_block ($id); | ||
| 54 | push @$self, $object,$id; | ||
| 55 | Scalar::Util::weaken ($self->[-2]); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub add_signalids { | ||
| 60 | my $self = shift; | ||
| 61 | while (@_) { | ||
| 62 | my $signalids = shift; | ||
| 63 | my $object = $signalids->object; | ||
| 64 | foreach my $id ($signalids->ids) { | ||
| 65 | $self->add ($object, $id); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | sub DESTROY { | ||
| 71 | my ($self) = @_; | ||
| 72 | while (@$self) { | ||
| 73 | my $object = shift @$self; | ||
| 74 | my $id = shift @$self; | ||
| 75 | next if (! defined $object); # possible weakening | ||
| 76 | |||
| 77 | # could have been disconnected altogether by the application | ||
| 78 | if ($object->signal_handler_is_connected ($id)) { | ||
| 79 | ### SignalBlock unblock: "$object" . "id=$id" | ||
| 80 | $object->handler_unblock ($id); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 | __END__ | ||
| 87 | |||
| 88 | =head1 NAME | ||
| 89 | |||
| 90 | App::Chart::Glib::Ex::SignalBlock -- block signal handlers with scope guard style | ||
| 91 | |||
| 92 | =for test_synopsis my ($obj, $id) | ||
| 93 | |||
| 94 | =head1 SYNOPSIS | ||
| 95 | |||
| 96 | use App::Chart::Glib::Ex::SignalBlock; | ||
| 97 | { | ||
| 98 | my $blocker = App::Chart::Glib::Ex::SignalBlock->new ($obj, $id); | ||
| 99 | # handler $id for activate is not called | ||
| 100 | $obj->activate; | ||
| 101 | } | ||
| 102 | # until $blocker goes out of scope ... | ||
| 103 | |||
| 104 | =head1 DESCRIPTION | ||
| 105 | |||
| 106 | B<Not sure about the arguments yet ...> | ||
| 107 | |||
| 108 | B<Blocking may be more work than disconnecting and re-connecting ...> | ||
| 109 | |||
| 110 | C<App::Chart::Glib::Ex::SignalBlock> temporarily blocks a particular signal handler | ||
| 111 | connection using C<signal_handler_block>. When the blocker object is | ||
| 112 | destroyed it unblocks with C<signal_handler_unblock>. | ||
| 113 | |||
| 114 | The idea is that it can be easier to manage the lifespan of an object than | ||
| 115 | to ensure every exit point from a particular bit of code includes an | ||
| 116 | unblock. For example a temporary blocking in a Perl scope, knowing no | ||
| 117 | matter how it exits (error, goto, return, etc) the signal block will be | ||
| 118 | undone. | ||
| 119 | |||
| 120 | { | ||
| 121 | my $blocker = App::Chart::Glib::Ex::SignalBlock->new ($obj,$id); | ||
| 122 | ... | ||
| 123 | } | ||
| 124 | |||
| 125 | Or objects can help to manage longer lived blocking, so as not to lose track | ||
| 126 | of things held for a period of time or main loop conditions etc. | ||
| 127 | |||
| 128 | It works to nest blockers, done either with SignalBlock or explicit calls. | ||
| 129 | Glib simply keeps a count of current blocks on each connected ID, which | ||
| 130 | means there's no need for proper nesting, blockers can overlap in any | ||
| 131 | fashion. | ||
| 132 | |||
| 133 | =head2 Alternatives | ||
| 134 | |||
| 135 | You can also simply arrange for your signal handler to do nothing when it | ||
| 136 | sees a global variable or a flag in an object. | ||
| 137 | |||
| 138 | our $update_in_progress; | ||
| 139 | |||
| 140 | sub my_handler { | ||
| 141 | return if $update_in_progress; | ||
| 142 | ... | ||
| 143 | } | ||
| 144 | |||
| 145 | { | ||
| 146 | local $update_in_progress = 1; | ||
| 147 | ... | ||
| 148 | } | ||
| 149 | |||
| 150 | If C<my_handler> is called many times during the "update" the repeated | ||
| 151 | do-nothing calls could be slow and a block (or disconnect) the signal may be | ||
| 152 | better. On the other hand if there's just a few calls then the overhead of | ||
| 153 | creating a blocker object might be the slowest part. | ||
| 154 | |||
| 155 | =head1 FUNCTIONS | ||
| 156 | |||
| 157 | =over 4 | ||
| 158 | |||
| 159 | =item C<< $blocker = App::Chart::Glib::Ex::SignalBlock->new ($object,$id,...) >> | ||
| 160 | |||
| 161 | Do a C<< $object->signal_handler_block >> on each given C<$object> and | ||
| 162 | signal handler C<$id>, and return a SignalBlock object which will make | ||
| 163 | corresponding C<< $object->signal_handler_unblock >> calls when it's | ||
| 164 | destroyed. So for instance if you were thinking of | ||
| 165 | |||
| 166 | $obj->signal_handler_block ($id); | ||
| 167 | ... | ||
| 168 | $obj->signal_handler_unblock ($id); | ||
| 169 | |||
| 170 | instead use | ||
| 171 | |||
| 172 | { | ||
| 173 | my $blocker = App::Chart::Glib::Ex::SignalBlock->new ($obj,$id); | ||
| 174 | ... | ||
| 175 | # automatic signal_handler_unblock when $blocker out of scope | ||
| 176 | } | ||
| 177 | |||
| 178 | SignalBlock holds weak references to the target objects, so the mere fact a | ||
| 179 | signal is blocked won't an object alive once nothing else cares if it lives | ||
| 180 | or dies. | ||
| 181 | |||
| 182 | =back | ||
| 183 | |||
| 184 | =head1 OTHER NOTES | ||
| 185 | |||
| 186 | When there's multiple signals in a SignalBlock it's currently unspecified | ||
| 187 | what order the unblock calls are made. (What would be good? First-in | ||
| 188 | first-out, or a stack?) You can create multiple SignalBlock objects and | ||
| 189 | arrange your blocks to destroyed them in a particular order if it matters. | ||
| 190 | |||
| 191 | There's quite a few general purpose block-scope cleanup systems if you want | ||
| 192 | more than signal blocking. L<Scope::Guard|Scope::Guard>, L<AtExit|AtExit>, | ||
| 193 | L<Sub::ScopeFinalizer|Scope::Guard> and L<Guard|Guard> use the destructor | ||
| 194 | style. L<Hook::Scope|Hook::Scope> and | ||
| 195 | L<B::Hooks::EndOfScope|B::Hooks::EndOfScope> manipulate the code in a block. | ||
| 196 | |||
| 197 | =head1 SEE ALSO | ||
| 198 | |||
| 199 | L<Glib::Object>, L<Glib::Ex::FreezeNotify> | ||
| 200 | |||
| 201 | =head1 HOME PAGE | ||
| 202 | |||
| 203 | L<http://user42.tuxfamily.org/glib-ex-objectbits/index.html> | ||
| 204 | |||
| 205 | =head1 LICENSE | ||
| 206 | |||
| 207 | Copyright 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 208 | |||
| 209 | Glib-Ex-ObjectBits is free software; you can redistribute it and/or modify | ||
| 210 | it under the terms of the GNU General Public License as published by the | ||
| 211 | Free Software Foundation; either version 3, or (at your option) any later | ||
| 212 | version. | ||
| 213 | |||
| 214 | Glib-Ex-ObjectBits is distributed in the hope that it will be useful, but | ||
| 215 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 216 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 217 | more details. | ||
| 218 | |||
| 219 | You should have received a copy of the GNU General Public License along with | ||
| 220 | Glib-Ex-ObjectBits. If not, see L<http://www.gnu.org/licenses/>. | ||
| 221 | |||
| 222 | =cut |