208ae407e3147366cb57540e522716b9ea81d715
[platform/core/api/maps-service.git] / src / session / thread.cpp
1 /* Copyright (c) 2010-2014 Samsung Electronics Co., Ltd. All rights reserved.
2  *
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 <unistd.h>
18 #include "thread.h"
19 #include "command_queue.h"
20 #include "command.h"
21
22 session::thread::thread()
23 {
24 }
25
26 session::thread::~thread()
27 {
28 }
29
30 /* Thread function: pops the item from the queue and performs the command */
31 void *session::thread::queue_thread(void *data)
32 {
33         sleep(0);               /* Just switch the thread to accomplish previous
34                                    initialization routines */
35
36         if (!data)
37                 return NULL;
38         plugin::plugin_s *p = (plugin::plugin_s *) data;
39
40         command_queue *cq = session::command_queue::interface();
41         while (p->is_working)
42                 cq->process(p); /* Continuously perform queue processing */
43
44         return NULL;
45 }
46
47 void session::thread::run(plugin::plugin_s *p)
48 {
49         if (!p)
50                 return;
51         if (p->thread)
52                 return;         /* Check whether the thread is already started, */
53         /* Start if needed */
54
55         p->is_working = true;
56
57         p->thread = g_thread_new("queue_thread", &queue_thread, p);
58         if(p->thread)
59                 g_thread_unref(p->thread);
60 }
61
62 void session::thread::stop(plugin::plugin_s *p)
63 {
64         if (!p)
65                 return;
66
67         p->is_working = false;
68
69         command_queue *cq = session::command_queue::interface();
70         cq->clear(p);
71 }