4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <sensor_logs.h>
21 #include <worker_thread.h>
26 worker_thread::worker_thread()
27 : m_state(WORKER_STATE_INITIAL)
29 , m_thread_created(false)
31 for (int i = 0; i < TRANS_FUNC_CNT; ++i)
32 m_trans_func[i] = NULL;
35 worker_thread::~worker_thread()
40 bool worker_thread::transition_function(trans_func_index index)
42 if (m_trans_func[index] != NULL) {
43 if(!m_trans_func[index](m_context)) {
44 ERR("Transition[%d] function returning false", index);
52 worker_thread::worker_state_t worker_thread::get_state(void)
59 bool worker_thread::start(void)
63 if (m_state == WORKER_STATE_WORKING) {
64 INFO("Worker thread is already working");
68 if ((m_state == WORKER_STATE_INITIAL) || (m_state == WORKER_STATE_STOPPED)) {
69 m_state = WORKER_STATE_WORKING;
71 if (!m_thread_created) {
72 thread th(&worker_thread::main, this);
76 } else if (m_state == WORKER_STATE_PAUSED) {
77 m_state = WORKER_STATE_WORKING;
78 m_cond_working.notify_one();
82 ERR("Failed to start, because current state(%d) is not for START", m_state);
87 bool worker_thread::stop(void)
91 if (m_state == WORKER_STATE_STOPPED) {
92 INFO("Worker thread is already stopped");
96 if ((m_state == WORKER_STATE_WORKING) || (m_state == WORKER_STATE_PAUSED)) {
98 if (m_state == WORKER_STATE_PAUSED)
99 m_cond_working.notify_one();
101 m_state = WORKER_STATE_STOPPED;
105 ERR("Failed to stop, because current state(%d) is not for STOP", m_state);
109 bool worker_thread::pause(void)
113 if (m_state == WORKER_STATE_PAUSED) {
114 INFO("Worker thread is already paused");
118 if (m_state == WORKER_STATE_WORKING) {
119 m_state = WORKER_STATE_PAUSED;
123 ERR("Failed to pause, because current state(%d) is not for PAUSE", m_state);
129 bool worker_thread::resume(void)
133 if (m_state == WORKER_STATE_WORKING) {
134 INFO("Worker thread is already working");
138 if (m_state == WORKER_STATE_PAUSED) {
139 m_state = WORKER_STATE_WORKING;
140 m_cond_working.notify_one();
144 ERR("Failed to resume, because current state(%d) is not for RESUME", m_state);
150 * After state changed to STOPPED, it should not access member fields,
151 because some transition funciton of STOPPED delete this pointer
154 void worker_thread::main(void)
156 DBG("Worker thread(0x%x) is created", std::this_thread::get_id());
158 transition_function(STARTED);
161 worker_state_t state;
164 if (state == WORKER_STATE_WORKING) {
165 if (!transition_function(WORKING)) {
166 m_state = WORKER_STATE_STOPPED;
167 DBG("Worker thread(0x%x) exits from working state", std::this_thread::get_id());
168 m_thread_created = false;
169 transition_function(STOPPED);
177 if (m_state == WORKER_STATE_PAUSED) {
178 transition_function(PAUSED);
180 DBG("Worker thread(0x%x) is paused", std::this_thread::get_id());
181 m_cond_working.wait(u);
183 if (m_state == WORKER_STATE_WORKING) {
184 transition_function(RESUMED);
185 DBG("Worker thread(0x%x) is resumed", std::this_thread::get_id());
186 } else if (m_state == WORKER_STATE_STOPPED) {
187 m_thread_created = false;
188 transition_function(STOPPED);
191 } else if (m_state == WORKER_STATE_STOPPED) {
192 m_thread_created = false;
193 transition_function(STOPPED);
197 INFO("Worker thread(0x%x)'s main is terminated", std::this_thread::get_id());
200 void worker_thread::set_started(trans_func_t func)
202 m_trans_func[STARTED] = func;
205 void worker_thread::set_stopped(trans_func_t func)
207 m_trans_func[STOPPED] = func;
210 void worker_thread::set_paused(trans_func_t func)
212 m_trans_func[PAUSED] = func;
215 void worker_thread::set_resumed(trans_func_t func)
217 m_trans_func[RESUMED] = func;
220 void worker_thread::set_working(trans_func_t func)
222 m_trans_func[WORKING] = func;
225 void worker_thread::set_context(void *ctx)