4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: JuHyun Kim <jh8212.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
33 #include <cobject_type.h>
40 cworker::cworker(void)
43 , mutex_lock(PTHREAD_MUTEX_INITIALIZER)
47 for (i = 0; i < ENUM_LAST; i ++) {
51 DBG("processor worker created\n");
54 cworker::~cworker(void)
56 DBG("----------Processor WORKER TERMINATED--------\n");
60 if (m_func[TERMINATE])
61 m_func[TERMINATE](m_context);
63 DBG("processor worker terminated\n");
66 bool cworker::start(void)
70 pthread_mutex_lock(&(mutex_lock));
71 if (m_state == START) {
72 ERR("Already started\n");
73 pthread_mutex_unlock(&(mutex_lock));
78 pthread_mutex_unlock(&(mutex_lock));
80 DBG("cworker start\n");
82 ret = pthread_create(&m_thid, NULL, started, this);
86 pthread_mutex_lock(&(mutex_lock));
88 pthread_mutex_unlock(&(mutex_lock));
89 ERR("thread create fail\n");
94 ret = pthread_detach(m_thid);
97 ERR("thread detach fail\n");
102 DBG("Thread creation for Processor worker END\n");
109 bool cworker::terminate(void)
111 pthread_mutex_lock(&(mutex_lock));
113 pthread_mutex_unlock(&(mutex_lock));
118 void *cworker::started(void *data)
120 cworker *inst = (cworker*)data;
121 worker_state_s state;
125 state = (worker_state_s)(int)inst->m_func[STARTED](inst->m_context);
126 if (state == STOPPED) {
127 pthread_mutex_lock(&(inst->mutex_lock));
128 inst->m_state = STOP;
129 pthread_mutex_unlock(&(inst->mutex_lock));
130 ERR("Abnormal Situation: processor_plugin->working() returned STOPPED\n");
133 }while(state == STARTED && inst->m_state == START);
135 DBG("\n\n\n#############Processor worker thread END###########\n\n\n");
140 bool cworker::stop(void)
142 pthread_mutex_lock(&(mutex_lock));
143 if (m_state == STOP) {
144 ERR("Already stopped\n");
145 pthread_mutex_unlock(&(mutex_lock));
150 pthread_mutex_unlock(&(mutex_lock));
151 DBG("Stop function for Processor worker END ");
155 cworker::worker_state_s cworker::state(void)
160 void cworker::set_start(void *(*start)(void *data))
162 m_func[START] = start;
165 void cworker::set_started(void *(*started)(void *data))
167 m_func[STARTED] = started;
170 void cworker::set_stop(void *(*stop)(void *data))
175 void cworker::set_stopped(void *(*stopped)(void *data))
177 m_func[STOPPED] = stopped;
180 void cworker::set_terminate(void *(*term)(void *data))
182 m_func[TERMINATE] = term;
185 void cworker::set_context(void *ctx)