File: signal.yo

package info (click to toggle)
bobcat 6.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,960 kB
  • sloc: cpp: 18,954; fortran: 5,617; makefile: 2,787; sh: 659; perl: 401; ansic: 26
file content (149 lines) | stat: -rw-r--r-- 5,637 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
includefile(include/header)

COMMENT(manpage, section, releasedate, archive, short name)
manpage(FBB::Signal)(3bobcat)(_CurYrs_)(libbobcat-dev__CurVers_)
                    (signal handler)

manpagename(FBB::Signal)(Signal Handler)

manpagesynopsis()
    bf(#include <bobcat/signal>)nl()
    Linking option: tt(-lbobcat)

manpagedescription()

Signals have the well known drawback that signals arrive free of
context. E.g., assume a program runs a flow control loop like this:
    verb(
void Class::run()
{
    while (d_continue)
        handleTasks();
    cleanup();
}
    )
    then if the program needs to recognize  a termination signal then the
typical signal handler looks like this:
    verb(
void signalHandler(int signal)
{
    // perform required actions
}
    )
    Since the tt(signalHandler) is called asynchronically, there is no context
available, and the usual way of communicating between objects and signal
handlers is via static variables, like this:
    verb(
// declared as static bool s_continue;
bool Class::s_continue = true;

void Class::run()
{
    while (s_continue)
        handleTasks();
    cleanup();
}

// declared as static void signalHander(int signal);
void Class::signalHandler(int signal)
{
    s_continue = false;
}
    )
    The class tt(Signal) allows the signal handler to operate in the context
of a class. The advantage of this is that static data members are no longer
required and that the signal may be used to control data members of individual
objects.

    The signal is now handled by an object, whose class must define a member
        verb(
    void signalHandler(size_t signum) override;
        )
    and this function is responsible for handling the received signal. Since
it is a member function it may affect its object's local variables and it may
call its object's member functions. Static data members are not required
anymore (see below for an example).

    Note that, as the signal may arrive at unpredicable times data members
that can be modified by tt(signalHandler) should be declared using the
tt(volatile) modifier. Moreover, data that can be modified by
the tt(signalHandler) member and by other class members should be protected
by em(mutexes) (cf. the bf(C++-11) class tt(std::mutex) or
bf(pthread_mutex_lock)(3posix)).

includefile(include/namespace)

manpagesection(INHERITS FROM)
    tt(Signal) is not derived from other classes, but the classes for which
signals must be handled by tt(Signal) must themselves publicly be derived from
the class tt(FBB::SignalHandler) and must implement a member
        verb(
    void signalHandler(size_t signum) override;
        )
    handling the received signal.


manpagesection(CONSTRUCTORS AND OVERLOADED OPERATORS)
    tt(Signal) is defined as a em(singleton), and does not offer public or
protected constructors, nor does it offer overloaded operators.

manpagesection(STATIC MEMBER FUNCTION)
    itemization(
    itb(static Signal &instance())nl()
        This static member can be used to access a reference to the program's
single tt(Signal) object.
    )


manpagesection(MEMBER FUNCTIONS)
    All of tt(Signal)'s member functions can only be called through a
reference to the program's tt(Signal) object, returning a reference to the
program's single tt(Signal) object:
    itemization(
    itb(void add(size_t signum, SignalHandler &object))nl()
       tt(SignalHandler object) is activated on arrival of signal
        tt(signum). If multiple tt(SignalHandler) objects must be called then
        multiple tt(Signal::add) calls can be provided, and the various
        tt(SignalHandler::signalHandler) members are called in the same
        sequence as their respective tt(Signal::add) calls. If one of the
        earlier tt(signalHandler) members terminates the program then later
        tt(signalHandler) members are not activated anymore. If
        tt(Signal::add) is called by, e.g., an object's constructor, then its
        destructor should call tt(Signal::remove) to prevent the object's
        signal handler from being called after its destruction.
    itb(void remove(size_t signum, SignalHandler &object))nl()
       tt(SignalHandler object) for signal tt(signum) is removed from the
        tt(Signal) object. It is the responsibility of tt(object) to
        deregister itself from tt(Signal) just before tt(object) goes out of
        scope. Objects can only deregister themselves if they've previously
        registered themselves using tt(add).
    itb(void ignore(size_t signum))nl()
       Any previously installed tt(SignalHandler) object is no longer
        activated on arrival of signal tt(signum). In addition, if possible,
        signal tt(signum) is completely ignored (some signals cannot be
        caught, blocked, of ignored, like tt(SIGKILL) and tt(SIGSTOP)
        (cf. bf(signal)(7))).
    itb(void reset(size_t signum))nl()
       Any previously installed tt(SignalHandler) object is no longer
        activated on arrival of signal tt(signum). In addition, the default
        action the program takes on arrival of signal tt(signum) is
        reinstalled (cf. bf(signal)(7)).
    )

    If the tt(signum) value that is passed to tt(Signal)'s members is not a
defined signal value, then an bf(FBB::Exception) exception is thrown.

manpagesection(EXAMPLE)
    verbinclude(../../signal/driver/driver.cc)

manpagefiles()
    em(bobcat/signal) - defines the class interface

manpageseealso()
    bf(bobcat)(7), bf(pthread_mutex_lock)(3posix), bf(signal)(7),nl()
    and the bf(C++-11) class tt(std::mutex).

manpagebugs()
    None Reported.

includefile(include/trailer)