File: multi_thread_sighandler.cpp

package info (click to toggle)
tango 10.0.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 89,936 kB
  • sloc: cpp: 201,786; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 272; sql: 72; ruby: 24
file content (322 lines) | stat: -rw-r--r-- 8,511 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// NOLINTBEGIN(*)

#include <condition_variable>
#include <csignal>
#include <thread>

#include <tango/tango.h>

#ifdef _TG_WINDOWS_
  #include <windows.h>

static inline void sleep(DWORD seconds)
{
    Sleep(seconds * 1000);
}
#else
  #include <sys/wait.h>
#endif

// Define a simple device server
#ifndef COMPAT
class StepperMotor : public TANGO_BASE_CLASS
{
#else
class StepperMotor : public Tango::Device_5Impl
{
#endif
  public:
    StepperMotor(Tango::DeviceClass *, std::string &);
    StepperMotor(Tango::DeviceClass *, const char *);
    StepperMotor(Tango::DeviceClass *, const char *, const char *);
    StepperMotor(Tango::DeviceClass *, const char *, const char *, Tango::DevState, const char *);

    // using Device_5Impl::Device_5Impl;
    void init_device() override { }
};

#ifndef COMPAT
StepperMotor::StepperMotor(Tango::DeviceClass *cl, std::string &s) :
    TANGO_BASE_CLASS(cl, s.c_str())
{
    init_device();
}

StepperMotor::StepperMotor(Tango::DeviceClass *cl, const char *s) :
    TANGO_BASE_CLASS(cl, s)
{
    init_device();
}

StepperMotor::StepperMotor(Tango::DeviceClass *cl, const char *s, const char *d) :
    TANGO_BASE_CLASS(cl, s, d)
{
    init_device();
}

StepperMotor::StepperMotor(
    Tango::DeviceClass *cl, const char *s, const char *d, Tango::DevState state, const char *status) :
    TANGO_BASE_CLASS(cl, s, d, state, status)
{
    init_device();
}
#else
StepperMotor::StepperMotor(Tango::DeviceClass *cl, string &s) :
    Tango::Device_3Impl(cl, s.c_str())
{
    init_device();
}

StepperMotor::StepperMotor(Tango::DeviceClass *cl, const char *s) :
    Tango::Device_3Impl(cl, s)
{
    init_device();
}

StepperMotor::StepperMotor(Tango::DeviceClass *cl, const char *s, const char *d) :
    Tango::Device_3Impl(cl, s, d)
{
    init_device();
}

StepperMotor::StepperMotor(
    Tango::DeviceClass *cl, const char *s, const char *d, Tango::DevState state, const char *status) :
    Tango::Device_3Impl(cl, s, d, state, status)
{
    init_device();
}
#endif

class StepperMotorClass : public Tango::DeviceClass
{
  public:
    static StepperMotorClass *init(const char *s)
    {
        if(!_instance)
        {
            std::string str(s);
            _instance = new StepperMotorClass(str);
        }
        return _instance;
    }

    static StepperMotorClass *instance()
    {
        return _instance;
    }

    ~StepperMotorClass()
    {
        _instance = NULL;
    }

  protected:
    StepperMotorClass(std::string &s) :
        Tango::DeviceClass(s)
    {
    }

    static StepperMotorClass *_instance;

    void command_factory() { }

    void attribute_factory(std::vector<Tango::Attr *> &) { }

  public:
    void device_factory(const Tango::DevVarStringArray *devlist_ptr)
    {
        for(_CORBA_ULong i = 0; i < devlist_ptr->length(); i++)
        {
            device_list.push_back(new StepperMotor(this, (*devlist_ptr)[i]));
            if(Tango::Util::_UseDb)
            {
                export_device(device_list.back());
            }
            else
            {
                export_device(device_list.back(), ((*devlist_ptr)[i]));
            }
        }
    }
};

StepperMotorClass *StepperMotorClass::_instance = NULL;

void Tango::DServer::class_factory()
{
    add_class(StepperMotorClass::init("StepperMotor"));
}

// Install the specified signal handler in the device server
void install_signal_handler(int handlers)
{
#ifndef _TG_WINDOWS_
    struct sigaction sig;
    sig.sa_flags = SA_RESTART;
    sig.sa_handler = [](int signal) { std::cout << "Signal received: " << signal << '\n'; };

    auto _install_for_signal = [&](int signal, const std::string &signal_name)
    {
        if(sigaction(signal, &sig, nullptr) == -1)
        {
            perror("sigaction");
            return;
        }
        std::cout << "Installed " << signal_name << " signal handler\n";
    };

    switch(handlers)
    {
    case SIGINT:
        _install_for_signal(SIGINT, "SIGINT");
        break;
    case SIGTERM:
        _install_for_signal(SIGTERM, "SIGTERM");
        break;
    case -1:
        _install_for_signal(SIGINT, "SIGINT");
        _install_for_signal(SIGTERM, "SIGTERM");
        break;
    default:
        std::cout << "NOT installing any signal handlers" << std::endl;
        break;
    }
#endif
}

struct condition
{
    bool stopped{false};
    std::condition_variable cv;
    std::mutex mutex;
};

// Start thread if do_start_thread and wait for condition to be stopped, otherwise, do nothing
std::thread start_thread(condition &c, bool do_start_thread)
{
    if(do_start_thread)
    {
        return std::thread(
            [&]()
            {
                std::cout << "Started background thread\n";
                std::unique_lock<std::mutex> lock{c.mutex};
                c.cv.wait(lock, [&] { return c.stopped; });
                lock.unlock();
                std::cout << "Exiting background thread\n";
            });
    }
    return std::thread{};
}

// Create the device server; if do_start_thread or signal handlers specified, then
// do this before initialising the device server.
void create_device_server(int argc, char *argv[], bool do_start_thread, int handlers)
{
    struct condition c;
    auto thread = start_thread(c, do_start_thread);
    sleep(1); // Wait for thread to start.
    install_signal_handler(handlers);

    try
    {
        Tango::Util *tg = Tango::Util::init(argc, argv);
        tg->server_init();
        std::cout << "Device server initialised" << std::endl;

        std::cout << "Ready to accept request" << std::endl;
        tg->server_run();
        tg->server_cleanup();
        std::cout << "Server running" << std::endl;
    }
    catch(std::bad_alloc &)
    {
        std::cout << "Can't allocate memory to store device object !!!" << std::endl;
        std::cout << "Exiting" << std::endl;
    }
    catch(Tango::DevFailed &e)
    {
        Tango::Except::print_exception(e);
    }
    catch(CORBA::Exception &e)
    {
        Tango::Except::print_exception(e);

        std::cout << "Received a CORBA_Exception" << std::endl;
        std::cout << "Exiting" << std::endl;
    }

    if(thread.joinable())
    {
        {
            std::cout << "Stopping thread..." << std::endl;
            std::lock_guard<std::mutex> guard(c.mutex);
            c.stopped = true;
        }
        c.cv.notify_one();
        thread.join();
    }
}

// Fork the device server and send it SIGTERM.
void run_test(int argc, char *argv[], bool do_start_thread, int handlers)
{
#ifndef _TG_WINDOWS_
    int pid = fork();
    if(pid < 0)
    {
        perror("fork");
        exit(1);
    }
    else if(!pid)
    {
        create_device_server(argc, argv, do_start_thread, handlers);
    }
    else
    {
        sleep(3); // Wait for device server to initialise
        std::cout << "PARENT pid=" << getpid() << " CHILD pid=" << pid << std::endl;

        std::cout << "PARENT sending SIGTERM to " << pid << "..." << std::endl;
        kill(pid, SIGTERM);
        sleep(1); // Wait for child to process the signal

        int status = 0;
        std::cout << "Waiting for " << pid << "...\n";
        pid_t wait_result = waitpid(pid, &status, WNOHANG);

        std::cout << "wait()=" << wait_result << "\n  WIFEXITED=" << WIFEXITED(status)
                  << "\n  WEXITSTATUS=" << WEXITSTATUS(status) << "\n  WIFSIGNALED=" << WIFSIGNALED(status);
        if(WIFSIGNALED(status))
        {
            std::cout << "\n  WTERMSIG=" << WTERMSIG(status) << "\n  WCOREDUMP=" << WCOREDUMP(status);
        }
        std::cout << "\n  WIFSTOPPED=" << WIFSTOPPED(status) << "\n  WSTOPSIG=" << WSTOPSIG(status)
                  << "\n  WIFCONTINUED=" << WIFCONTINUED(status) << std::endl;

        // Device server should already be terminated, therefore kill() should return -1
        // and not zero.
        std::cout << "PARENT sending SIGINT to " << pid << "..." << std::endl;
        int result = kill(pid, SIGINT);
        std::cout << "kill(" << pid << ", SIGINT) == " << result << std::endl;
        if(result != -1)
        {
            int dev_server_stopped = 0;
            assert(dev_server_stopped);
        }
    }
#endif
}

int main()
{
    // This test only concerns the signal handling, therefore we don't need to
    // use the database.
    char *args[5] = {
        (char *) "SignalTest", (char *) "test", (char *) "-nodb", (char *) "-ORBendPoint", (char *) "giop:tcp::11000"};
    run_test(5, args, true, SIGTERM);

    return 0;
}

// NOLINTEND(*)