09143fed9e11ef57052bc057ad91a5ac2336a242
[platform/core/system/libsf-common.git] / src / cworker.cpp
1 /*
2  *  libsf-common
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JuHyun Kim <jh8212.kim@samsung.com>
7  * 
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */ 
21
22
23
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <pthread.h>
29 #include <errno.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32 #include <signal.h>
33 #include <cobject_type.h>
34 #include <csync.h>
35 #include <clist.h>
36 #include <cmutex.h>
37 #include <cworker.h>
38 #include <common.h>
39
40 cworker::cworker(void)
41 : m_state(STOPPED)
42 , m_context(NULL)
43 , mutex_lock(PTHREAD_MUTEX_INITIALIZER)
44 {
45         register int i;
46
47         for (i = 0; i < ENUM_LAST; i ++) {
48                 m_func[i] = NULL;
49         }
50         
51         DBG("processor worker created\n");
52 }
53
54 cworker::~cworker(void)
55 {
56         DBG("----------Processor WORKER TERMINATED--------\n"); 
57         
58         m_state = TERMINATE;
59
60         if (m_func[TERMINATE])
61                 m_func[TERMINATE](m_context);
62
63         DBG("processor worker terminated\n");
64 }
65
66 bool cworker::start(void)
67 {
68         int ret = 0;
69
70         pthread_mutex_lock(&(mutex_lock));
71         if (m_state == START) {
72                 ERR("Already started\n");
73                 pthread_mutex_unlock(&(mutex_lock));
74                 return false;
75         }
76
77         m_state = START;
78         pthread_mutex_unlock(&(mutex_lock));
79
80         DBG("cworker start\n");
81
82         ret = pthread_create(&m_thid, NULL, started, this);
83         
84         if(ret != 0)
85         {
86                 pthread_mutex_lock(&(mutex_lock));
87                 m_state = STOP;
88                 pthread_mutex_unlock(&(mutex_lock));
89                 ERR("thread create fail\n");
90                 return false;
91         }
92         else
93         {
94                 ret = pthread_detach(m_thid);
95                 if(ret != 0)
96                 {
97                         ERR("thread detach fail\n");
98                         return false;
99                 }
100                 else
101                 {
102                         DBG("Thread creation for Processor worker END\n");
103                 }
104         }
105         
106         return true;
107 }
108
109 bool cworker::terminate(void)
110 {
111         pthread_mutex_lock(&(mutex_lock));
112         m_state = TERMINATE;
113         pthread_mutex_unlock(&(mutex_lock));
114         delete this;
115         return true;
116 }
117
118 void *cworker::started(void *data)
119 {
120         cworker *inst = (cworker*)data;
121         worker_state_s state;
122
123         do
124         {
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");
131                         return NULL;
132                 }
133         }while(state == STARTED && inst->m_state == START);
134
135         DBG("\n\n\n#############Processor worker thread END###########\n\n\n");
136         
137         return NULL;            
138 }
139
140 bool cworker::stop(void)
141 {
142         pthread_mutex_lock(&(mutex_lock));
143         if (m_state == STOP) {
144                 ERR("Already stopped\n");
145                 pthread_mutex_unlock(&(mutex_lock));
146                 return false;
147         }
148         
149         m_state = STOP;
150         pthread_mutex_unlock(&(mutex_lock));
151         DBG("Stop function for Processor worker END ");
152         return true;
153 }
154
155 cworker::worker_state_s cworker::state(void)
156 {
157         return m_state;
158 }
159
160 void cworker::set_start(void *(*start)(void *data))
161 {
162         m_func[START] = start;
163 }
164
165 void cworker::set_started(void *(*started)(void *data))
166 {
167         m_func[STARTED] = started;
168 }
169
170 void cworker::set_stop(void *(*stop)(void *data))
171 {
172         m_func[STOP] = stop;
173 }
174
175 void cworker::set_stopped(void *(*stopped)(void *data))
176 {
177         m_func[STOPPED] = stopped;
178 }
179
180 void cworker::set_terminate(void *(*term)(void *data))
181 {
182         m_func[TERMINATE] = term;
183 }
184
185 void cworker::set_context(void *ctx)
186 {
187         m_context = ctx;
188 }
189
190 //! End of a file