Add monitor plugin requires for install dependency
[platform/core/connectivity/stc-manager.git] / unittest / common.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "common.h"
18
19 GMainLoop *MainLoop::m_mainLoop = NULL;
20 guint MainLoop::m_timerId = 0;
21
22 MainLoop::MainLoop(void)
23 {
24 }
25
26 MainLoop::~MainLoop(void)
27 {
28         if (m_mainLoop)
29                 g_main_loop_quit(m_mainLoop);
30         if (m_timerId)
31                 g_source_remove(m_timerId);
32
33         m_mainLoop = NULL;
34         m_timerId = 0;
35 }
36
37 gboolean MainLoop::timeoutCb(gpointer data)
38 {
39         if (m_mainLoop)
40                 g_main_loop_quit(m_mainLoop);
41         if (m_timerId)
42                 g_source_remove(m_timerId);
43
44         m_mainLoop = NULL;
45         m_timerId = 0;
46
47         return false;
48 }
49
50 void MainLoop::run(unsigned int timeout)
51 {
52         if (m_timerId > 0)
53                 return;
54
55         m_mainLoop = g_main_loop_new(NULL, false);
56         m_timerId = g_timeout_add(timeout,
57                         (GSourceFunc) &MainLoop::timeoutCb,
58                         NULL);
59         g_main_loop_run(m_mainLoop);
60 }
61
62 void MainLoop::quit(void)
63 {
64         timeoutCb(NULL);
65 }
66