source: svn/newcon3bcm2_21bu/nexus/app/bapp_task.c

Last change on this file was 76, checked in by megakiss, 10 years ago

1W 대기전력을 만족시키기 위하여 POWEROFF시 튜너를 Standby 상태로 함

  • Property svn:executable set to *
File size: 6.6 KB
Line 
1#include <stdlib.h>
2#include <pthread.h>
3#include <unistd.h>
4#include <sys/time.h>
5#include "bapp_task.h"
6#include "bapp_util.h"
7#include "genericlist.h"
8
9extern int pthread_kill (pthread_t THREAD, int SIGNO);
10
11struct bapp_task_event
12{
13        LINKS_T                 links;
14        bapp_task_event_t               event;
15};
16
17struct bapp_task_queue
18{
19        LIST_T                  list;
20        pthread_mutex_t mutex;
21};
22
23struct bapp_task_mutex
24{
25        pthread_mutex_t mutex;
26};
27
28struct bapp_task
29{
30        pthread_t thread;
31};
32
33int g_ticks_per_ms = 1;
34int g_ticks_per_second = 1000;
35static pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;
36static int lflag = 0;
37
38void bapp_task_init(void)
39{
40}
41void bapp_task_start(void)
42{
43}
44
45bapp_result_t bapp_task_start_task( bapp_task_t *handle, const bapp_task_params *params, bapp_task_func func,  void *data )
46{
47        struct bapp_task *p_task = (struct bapp_task*)malloc(sizeof(struct bapp_task));
48
49        BAPP_UNUSED(params);
50
51        if (!p_task)
52                return eBAPP_RESULT_ALLOC_FAILURE;
53
54   if (pthread_create((pthread_t*)&p_task->thread, NULL,
55              (void *(*)(void*))func, data) == 0)
56        {
57            *handle = (bapp_task_t)p_task;
58                return eBAPP_RESULT_OK;
59        }
60   
61    free(p_task);
62        return eBAPP_RESULT_ALLOC_FAILURE;
63}
64void bapp_task_stop_task(bapp_task_t handle)
65{
66        struct bapp_task *p_task = (struct bapp_task *)handle;
67    pthread_kill((pthread_t)p_task->thread,-9);
68        free(p_task);
69}
70
71
72bapp_result_t bapp_task_create_queue(  bapp_task_queue_t *handle, bapp_task_event_t *events, int num_events )
73{
74        struct bapp_task_queue *p_bq = (struct bapp_task_queue*)malloc(sizeof(struct bapp_task_queue));
75        BAPP_UNUSED(events);
76        BAPP_UNUSED(num_events);
77        if (!p_bq)
78                return eBAPP_RESULT_ALLOC_FAILURE;
79
80        if (pthread_mutex_init(&p_bq->mutex,NULL) != 0)
81        {
82                free(p_bq);
83                return eBAPP_RESULT_ALLOC_FAILURE;
84        }
85
86        initl(&p_bq->list);
87        *handle = (bapp_task_queue_t)p_bq;
88        return eBAPP_RESULT_OK;
89}
90
91void bapp_task_delete_queue(  bapp_task_queue_t handle )
92{
93        struct bapp_task_queue *p_bq = (struct bapp_task_queue*)handle;
94        struct bapp_task_event *p_evt;
95
96        for (p_evt = (struct bapp_task_event*)remlh(&p_bq->list); p_evt != NULL; p_evt = (struct bapp_task_event*)remlh(&p_bq->list)) 
97        {
98                free(p_evt);
99        }
100        free(p_bq);
101}
102
103bapp_result_t bapp_task_post_event( bapp_task_queue_t handle, bapp_task_event_t event )
104{
105        struct bapp_task_queue *p_bq = (struct bapp_task_queue*)handle;
106        struct bapp_task_event *p_evt = NULL;
107       
108        p_evt = (struct bapp_task_event*)malloc(sizeof(struct bapp_task_event));
109        if (!p_evt)
110                return eBAPP_RESULT_ALLOC_FAILURE;
111
112        if (pthread_mutex_lock((pthread_mutex_t *)&p_bq->mutex) != 0)
113                goto error;
114
115        p_evt->event = event;
116        inslt(&p_bq->list,p_evt);
117        pthread_mutex_unlock((pthread_mutex_t *)&p_bq->mutex);
118
119        return eBAPP_RESULT_OK;
120
121error:
122        if (p_evt)
123                free(p_evt);
124        return eBAPP_RESULT_FAILURE;
125       
126}
127bapp_task_event_t bapp_task_pend_event( bapp_task_queue_t handle, int timeout_ms )
128{
129        struct bapp_task_queue *p_bq = (struct bapp_task_queue*)handle;
130        struct bapp_task_event *p_evt = NULL;
131        struct bapp_task_event *return_event = NULL;
132
133        unsigned int timout_ticks = bapp_task_getticks() + timeout_ms * g_ticks_per_ms;;
134
135        if (pthread_mutex_lock((pthread_mutex_t *)&p_bq->mutex) != 0)
136                goto exit;
137
138        while (LEMPTY(&p_bq->list))
139        {
140                pthread_mutex_unlock((pthread_mutex_t *)&p_bq->mutex);
141                bapp_task_sleep(1);
142               
143                if (timeout_ms >= 0)
144                {
145                        if (timout_ticks < bapp_task_getticks())
146                                goto exit;
147                }
148               
149                if (pthread_mutex_lock((pthread_mutex_t *)&p_bq->mutex) != 0)
150                        goto exit;
151        }
152        p_evt = (struct bapp_task_event*)LHEAD(&p_bq->list);
153        reml(&p_bq->list,p_evt);
154        return_event = p_evt->event;
155        free(p_evt);
156        pthread_mutex_unlock((pthread_mutex_t *)&p_bq->mutex);
157       
158exit:
159        return (bapp_task_event_t)return_event;
160}
161
162void bapp_task_reset_queue( bapp_task_queue_t handle)
163{
164        bapp_task_event_t event;
165        do
166        {
167                 event = bapp_task_pend_event(handle,0);
168        }while(event != NULL);
169}
170
171bapp_result_t bapp_task_create_mutex( bapp_task_mutex_t *handle )
172{
173        struct bapp_task_mutex *p_mutex;
174       
175        p_mutex = (struct bapp_task_mutex*)malloc(sizeof(struct bapp_task_mutex));
176        if (!p_mutex)
177                return eBAPP_RESULT_ALLOC_FAILURE;
178
179        if (pthread_mutex_init((pthread_mutex_t *)&p_mutex->mutex,NULL) == 0)
180        {
181                *handle = (bapp_task_mutex_t)p_mutex;
182                return eBAPP_RESULT_OK;
183        }
184
185        free(p_mutex);
186        return eBAPP_RESULT_ALLOC_FAILURE;
187}
188void bapp_task_delete_mutex( bapp_task_mutex_t handle)
189{
190        struct bapp_task_mutex *p_mutex = (struct bapp_task_mutex*)handle;
191        pthread_mutex_destroy((pthread_mutex_t* )&p_mutex->mutex);
192        free(p_mutex);
193}
194bapp_result_t bapp_task_acquire_mutex( bapp_task_mutex_t handle, int timeout_ms  )
195{
196        struct bapp_task_mutex *p_mutex = (struct bapp_task_mutex*)handle;
197        if (timeout_ms < 0)
198        {
199                if (pthread_mutex_lock((pthread_mutex_t *)&p_mutex->mutex) != 0)
200                        return eBAPP_RESULT_FAILURE; 
201        }
202        else
203        {
204                unsigned int timout_ticks = bapp_task_getticks() + timeout_ms * g_ticks_per_ms;
205                while (pthread_mutex_trylock((pthread_mutex_t *)&p_mutex->mutex) != 0)
206                {
207                        bapp_task_sleep(1);
208                        if (timout_ticks < bapp_task_getticks())
209                                return eBAPP_RESULT_TIMEOUT;
210                }
211        }
212        return eBAPP_RESULT_OK;
213}
214bapp_result_t bapp_task_release_mutex( bapp_task_mutex_t handle )
215{
216        struct bapp_task_mutex *p_mutex = (struct bapp_task_mutex*)handle;
217        pthread_mutex_unlock((pthread_mutex_t *)&p_mutex->mutex);
218        return eBAPP_RESULT_OK;
219}
220void bapp_task_sleep( unsigned int sleep_ms )
221{
222        if (sleep_ms >=1000)
223        {
224                sleep(sleep_ms/1000);
225                usleep((sleep_ms%1000) * 1000);
226        }
227        else
228        {
229                usleep(sleep_ms*1000);
230        }
231}
232#if 0
233static int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
234{
235        /* Perform the carry for the later subtraction by updating y. */
236        if (x->tv_usec < y->tv_usec)
237        {
238                int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
239                y->tv_usec -= 1000000 * nsec;
240                y->tv_sec += nsec;
241        }
242        if (x->tv_usec - y->tv_usec > 1000000)
243        {
244                int nsec = (x->tv_usec - y->tv_usec) / 1000000;
245                y->tv_usec += 1000000 * nsec;
246                y->tv_sec -= nsec;
247        }
248
249        /* Compute the time remaining to wait.
250           tv_usec is certainly positive. */
251        result->tv_sec = x->tv_sec - y->tv_sec;
252        result->tv_usec = x->tv_usec - y->tv_usec;
253
254        /* Return 1 if result is negative. */
255        return x->tv_sec < y->tv_sec;
256}
257#endif
258static struct timeval s_start_time = {0,0};
259unsigned int bapp_task_getticks( void )
260{
261        struct timeval tv;
262        struct timeval cur_tv;
263
264        if ((s_start_time.tv_sec == 0) && (s_start_time.tv_usec == 0))
265        {
266                gettimeofday(&s_start_time,NULL);
267        }
268        gettimeofday(&cur_tv,NULL);
269        timeval_subtract(&tv,&cur_tv,&s_start_time);
270        return (tv.tv_sec * 1000 + tv.tv_usec/1000);
271}
272unsigned int bapp_task_enter_critical(void)
273{
274        pthread_mutex_lock((pthread_mutex_t *)&s_mutex);
275        return lflag++;
276}
277void bapp_task_exit_critical(unsigned int flags)
278{
279        BAPP_UNUSED(flags);
280        pthread_mutex_unlock((pthread_mutex_t *)&s_mutex);
281}
282
283
284
Note: See TracBrowser for help on using the repository browser.