source: svn/trunk/newcon3bcm2_21bu/dta/src/sim/bos_pthread.c

Last change on this file was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 5.0 KB
Line 
1#include <stdlib.h>
2#include <pthread.h>
3#include <sys/time.h>
4#include "bstd.h"
5#include "bos.h"
6#include "genericlist.h"
7
8typedef struct bos_event_t
9{
10        LINKS_T                 links;
11        b_event_t               *event;
12}bos_event_t;
13
14typedef struct bos_queue_t
15{
16        LIST_T                  list;
17        pthread_mutex_t mutex;
18}bos_queue_t;
19
20int g_ticks_per_ms = 1;
21static pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;
22static int lflag = 0;
23
24void bos_init(void)
25{
26}
27void bos_start(void)
28{
29}
30
31bresult bos_start_task( b_task_t *handle, const b_task_params *params, b_task_func func,  void *data )
32{
33    if (pthread_create((pthread_t*)handle, NULL,
34              (void *(*)(void*))func, data) == 0)
35        {
36                return b_ok;
37        }
38        return berr_out_of_memory;
39}
40void bos_stop_task(b_task_t handle)
41{
42#ifndef __MINGW32__
43    pthread_kill((pthread_t*)handle,-9);
44#endif
45}
46
47
48bresult bos_create_queue(  b_queue_t *handle, b_event_t *events, int num_events )
49{
50        bos_queue_t *p_bq = (bos_queue_t*)malloc(sizeof(bos_queue_t));
51        if (!p_bq)
52                return berr_out_of_memory;
53
54        if (pthread_mutex_init(&p_bq->mutex,NULL) != 0)
55        {
56                free(p_bq);
57                return berr_out_of_memory;
58        }
59
60        initl(&p_bq->list);
61        *handle = (b_queue_t)p_bq;
62        return b_ok;
63}
64
65void bos_delete_queue(  b_queue_t *handle )
66{
67        bos_queue_t *p_bq = (bos_queue_t*)handle;
68        bos_event_t *p_evt;
69
70        for (p_evt = (bos_event_t*)remlh(&p_bq->list); p_evt != NULL; p_evt = (bos_event_t*)remlh(&p_bq->list)) 
71        {
72                free(p_evt);
73        }
74        free(p_bq);
75}
76bresult bos_post_event( b_queue_t handle, b_event_t *event )
77{
78        bos_queue_t *p_bq = (bos_queue_t*)handle;
79        bos_event_t *p_evt = NULL;
80       
81        p_evt = (bos_event_t*)malloc(sizeof(bos_event_t));
82        if (!p_bq)
83                return berr_out_of_memory;
84
85        if (pthread_mutex_lock((pthread_mutex_t *)&p_bq->mutex) != 0)
86                goto error;
87
88        p_evt->event = event;
89        inslt(&p_bq->list,p_evt);
90        pthread_mutex_unlock((pthread_mutex_t *)&p_bq->mutex);
91
92        return b_ok;
93
94error:
95        if (p_evt)
96                free(p_evt);
97        return berr_timeout;
98       
99}
100b_event_t *bos_pend_event( b_queue_t handle, int timeout_ms )
101{
102        bos_queue_t *p_bq = (bos_queue_t*)handle;
103        bos_event_t *p_evt = NULL;
104        b_event_t *return_event = NULL;
105
106        unsigned int timout_ticks = bos_getticks() + timeout_ms * g_ticks_per_ms;;
107
108        if (pthread_mutex_lock((pthread_mutex_t *)&p_bq->mutex) != 0)
109                goto exit;
110
111        while (LEMPTY(&p_bq->list))
112        {
113                pthread_mutex_unlock((pthread_mutex_t *)&p_bq->mutex);
114                bos_sleep(1);
115               
116                if (timeout_ms >= 0)
117                {
118                        if (timout_ticks < bos_getticks())
119                                goto exit;
120                }
121               
122                if (pthread_mutex_lock((pthread_mutex_t *)&p_bq->mutex) != 0)
123                        goto exit;
124        }
125        p_evt = (bos_event_t*)LHEAD(&p_bq->list);
126        reml(&p_bq->list,p_evt);
127        return_event = p_evt->event;
128        free(p_evt);
129        pthread_mutex_unlock((pthread_mutex_t *)&p_bq->mutex);
130       
131exit:
132        return return_event;
133}
134bresult bos_create_mutex( b_mutex_t *handle )
135{
136        handle->queue = (b_queue_t)malloc(sizeof(pthread_mutex_t));
137        if (pthread_mutex_init((pthread_mutex_t *)(handle->queue),NULL) == 0)
138                return b_ok;
139        return berr_out_of_memory;
140}
141void bos_delete_mutex( b_mutex_t *handle)
142{
143        pthread_mutex_destroy((pthread_mutex_t *)(handle->queue));
144        free((void*)(handle->queue));
145}
146bresult bos_acquire_mutex( b_mutex_t *handle, int timeout_ms  )
147{
148        if (timeout_ms < 0)
149        {
150                if (pthread_mutex_lock((pthread_mutex_t *)(handle->queue)) != 0)
151                        return berr_timeout; 
152        }
153        else
154        {
155                unsigned int timout_ticks = bos_getticks() + timeout_ms * g_ticks_per_ms;
156                while (pthread_mutex_trylock((pthread_mutex_t *)(handle->queue)) != 0)
157                {
158                        bos_sleep(1);
159                        if (timout_ticks < bos_getticks())
160                                return berr_timeout;
161                }
162        }
163        return b_ok;
164}
165bresult bos_release_mutex( b_mutex_t *handle )
166{
167        pthread_mutex_unlock((pthread_mutex_t *)(handle->queue));
168        return b_ok;
169}
170void bos_sleep( unsigned int sleep_ms )
171{
172        if (sleep_ms >=1000)
173        {
174                g_usleep(sleep_ms * 1000);
175        }
176        else
177        {
178                usleep(sleep_ms*1000);
179        }
180}
181
182static int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
183{
184        /* Perform the carry for the later subtraction by updating y. */
185        if (x->tv_usec < y->tv_usec)
186        {
187                int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
188                y->tv_usec -= 1000000 * nsec;
189                y->tv_sec += nsec;
190        }
191        if (x->tv_usec - y->tv_usec > 1000000)
192        {
193                int nsec = (x->tv_usec - y->tv_usec) / 1000000;
194                y->tv_usec += 1000000 * nsec;
195                y->tv_sec -= nsec;
196        }
197
198        /* Compute the time remaining to wait.
199           tv_usec is certainly positive. */
200        result->tv_sec = x->tv_sec - y->tv_sec;
201        result->tv_usec = x->tv_usec - y->tv_usec;
202
203        /* Return 1 if result is negative. */
204        return x->tv_sec < y->tv_sec;
205}
206
207static struct timeval s_start_time = {0,0};
208unsigned int bos_getticks( void )
209{
210        struct timeval tv;
211        struct timeval cur_tv;
212
213        if ((s_start_time.tv_sec == 0) && (s_start_time.tv_usec == 0))
214        {
215                gettimeofday(&s_start_time,NULL);
216        }
217        gettimeofday(&cur_tv,NULL);
218        timeval_subtract(&tv,&cur_tv,&s_start_time);
219        return (tv.tv_sec * 1000 + tv.tv_usec/1000);
220}
221void bos_getclock(b_clock *clock)
222{
223}
224unsigned int bos_enter_critical(void)
225{
226        pthread_mutex_lock((pthread_mutex_t *)&s_mutex);
227        return lflag++;
228}
229void bos_exit_critical(unsigned int flags)
230{
231        pthread_mutex_unlock((pthread_mutex_t *)&s_mutex);
232}
233
234
Note: See TracBrowser for help on using the repository browser.