summaryrefslogtreecommitdiffstats
path: root/gps/libloc_api_50001/loc_eng_dmn_conn_thread_helper.c
blob: 67e31879266640f05904126270e91d18eda6880c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* Copyright (c) 2011, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <stdio.h>

#include "log_util.h"
#include "loc_eng_dmn_conn_thread_helper.h"

/*===========================================================================
FUNCTION    thelper_signal_init

DESCRIPTION
   This function will initialize the conditional variable resources.

   thelper - thelper instance

DEPENDENCIES
   None

RETURN VALUE
   0: success or negative value for failure

SIDE EFFECTS
   N/A

===========================================================================*/
int thelper_signal_init(struct loc_eng_dmn_conn_thelper * thelper)
{
    int result;
    thelper->thread_exit  = 0;
    thelper->thread_ready = 0;
    result = pthread_cond_init( &thelper->thread_cond, NULL);
    if (result) {
        return result;
    }

    result = pthread_mutex_init(&thelper->thread_mutex, NULL);
    if (result) {
        pthread_cond_destroy(&thelper->thread_cond);
    }
    return result;
}

/*===========================================================================
FUNCTION

DESCRIPTION
   This function will destroy the conditional variable resources

    thelper - pointer to thelper instance

DEPENDENCIES
   None

RETURN VALUE
   0: success or negative value for failure

SIDE EFFECTS
   N/A

===========================================================================*/
int thelper_signal_destroy(struct loc_eng_dmn_conn_thelper * thelper)
{
    int result, ret_result = 0;
    result = pthread_cond_destroy( &thelper->thread_cond);
    if (result) {
        ret_result = result;
    }

    result = pthread_mutex_destroy(&thelper->thread_mutex);
    if (result) {
        ret_result = result;
    }

    return ret_result;
}

/*===========================================================================
FUNCTION    thelper_signal_wait

DESCRIPTION
   This function will be blocked on the conditional variable until thelper_signal_ready
   is called

    thelper - pointer to thelper instance

DEPENDENCIES
   None

RETURN VALUE
   0: success or negative value for failure

SIDE EFFECTS
   N/A

===========================================================================*/
int thelper_signal_wait(struct loc_eng_dmn_conn_thelper * thelper)
{
    int result = 0;

    pthread_mutex_lock(&thelper->thread_mutex);
    if (!thelper->thread_ready && !thelper->thread_exit) {
        result = pthread_cond_wait(&thelper->thread_cond, &thelper->thread_mutex);
    }

    if (thelper->thread_exit) {
        result = -1;
    }
    pthread_mutex_unlock(&thelper->thread_mutex);

    return result;
}

/*===========================================================================
FUNCTION     thelper_signal_ready

DESCRIPTION
   This function will wake up the conditional variable

    thelper - pointer to thelper instance

DEPENDENCIES
   None

RETURN VALUE
   0: success or negative value for failure

SIDE EFFECTS
   N/A

===========================================================================*/
int thelper_signal_ready(struct loc_eng_dmn_conn_thelper * thelper)
{
    int result;

    LOC_LOGD("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);

    pthread_mutex_lock(&thelper->thread_mutex);
    thelper->thread_ready = 1;
    result = pthread_cond_signal(&thelper->thread_cond);
    pthread_mutex_unlock(&thelper->thread_mutex);

    return result;
}

/*===========================================================================
FUNCTION     thelper_signal_block

DESCRIPTION
   This function will set the thread ready to 0 to block the thelper_signal_wait

    thelper - pointer to thelper instance

DEPENDENCIES
   None

RETURN VALUE
   if thread_ready is set

SIDE EFFECTS
   N/A

===========================================================================*/
int thelper_signal_block(struct loc_eng_dmn_conn_thelper * thelper)
{
    int result = thelper->thread_ready;

    LOC_LOGD("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);

    pthread_mutex_lock(&thelper->thread_mutex);
    thelper->thread_ready = 0;
    pthread_mutex_unlock(&thelper->thread_mutex);

    return result;
}

/*===========================================================================
FUNCTION    thelper_main

DESCRIPTION
   This function is the main thread. It will be launched as a child thread

    data - pointer to the instance

DEPENDENCIES
   None

RETURN VALUE
   NULL

SIDE EFFECTS
   N/A

===========================================================================*/
static void * thelper_main(void *data)
{
    int result = 0;
    struct loc_eng_dmn_conn_thelper * thelper = (struct loc_eng_dmn_conn_thelper *) data;

    if (thelper->thread_proc_init) {
        result = thelper->thread_proc_init(thelper->thread_context);
        if (result < 0) {
            thelper->thread_exit = 1;
            thelper_signal_ready(thelper);
            LOC_LOGE("%s:%d] error: 0x%lx\n", __func__, __LINE__, (long) thelper);
            return NULL;
        }
    }

    thelper_signal_ready(thelper);

    if (thelper->thread_proc_pre) {
        result = thelper->thread_proc_pre(thelper->thread_context);
        if (result < 0) {
            thelper->thread_exit = 1;
            LOC_LOGE("%s:%d] error: 0x%lx\n", __func__, __LINE__, (long) thelper);
            return NULL;
        }
    }

    do {
        if (thelper->thread_proc) {
            result = thelper->thread_proc(thelper->thread_context);
            if (result < 0) {
                thelper->thread_exit = 1;
                LOC_LOGE("%s:%d] error: 0x%lx\n", __func__, __LINE__, (long) thelper);
            }
        }
    } while (thelper->thread_exit == 0);

    if (thelper->thread_proc_post) {
        result = thelper->thread_proc_post(thelper->thread_context);
    }

    if (result != 0) {
        LOC_LOGE("%s:%d] error: 0x%lx\n", __func__, __LINE__, (long) thelper);
    }
    return NULL;
}

static void thelper_main_2(void *data)
{
    thelper_main(data);
    return;
}


/*===========================================================================
FUNCTION    loc_eng_dmn_conn_launch_thelper

DESCRIPTION
   This function will initialize the thread context and launch the thelper_main

    thelper - pointer to thelper instance
    thread_proc_init - The initialization function pointer
    thread_proc_pre  - The function to call before task loop and after initialization
    thread_proc      - The task loop
    thread_proc_post - The function to call after the task loop
    context          - the context for the above four functions

DEPENDENCIES
   None

RETURN VALUE
   0: success or negative value for failure

SIDE EFFECTS
   N/A

===========================================================================*/
int loc_eng_dmn_conn_launch_thelper(struct loc_eng_dmn_conn_thelper * thelper,
    int (*thread_proc_init) (void * context),
    int (*thread_proc_pre) (void * context),
    int (*thread_proc) (void * context),
    int (*thread_proc_post) (void * context),
    thelper_create_thread   create_thread_cb,
    void * context)
{
    int result;

    thelper_signal_init(thelper);

    if (context) {
        thelper->thread_context    = context;
    }

    thelper->thread_proc_init  = thread_proc_init;
    thelper->thread_proc_pre   = thread_proc_pre;
    thelper->thread_proc       = thread_proc;
    thelper->thread_proc_post  = thread_proc_post;

    LOC_LOGD("%s:%d] 0x%lx call pthread_create\n", __func__, __LINE__, (long) thelper);
    if (create_thread_cb) {
        result = 0;
        thelper->thread_id = create_thread_cb("loc_eng_dmn_conn",
            thelper_main_2, (void *)thelper);
    } else {
        result = pthread_create(&thelper->thread_id, NULL,
            thelper_main, (void *)thelper);
    }

    if (result != 0) {
        LOC_LOGE("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
        return -1;
    }

    LOC_LOGD("%s:%d] 0x%lx pthread_create done\n", __func__, __LINE__, (long) thelper);

    thelper_signal_wait(thelper);

    LOC_LOGD("%s:%d] 0x%lx pthread ready\n", __func__, __LINE__, (long) thelper);
    return thelper->thread_exit;
}

/*===========================================================================
FUNCTION    loc_eng_dmn_conn_unblock_thelper

DESCRIPTION
   This function unblocks thelper_main to release the thread

    thelper - pointer to thelper instance

DEPENDENCIES
   None

RETURN VALUE
   0: success

SIDE EFFECTS
   N/A

===========================================================================*/
int loc_eng_dmn_conn_unblock_thelper(struct loc_eng_dmn_conn_thelper * thelper)
{
    LOC_LOGD("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
    thelper->thread_exit = 1;
    return 0;
}

/*===========================================================================
FUNCTION    loc_eng_dmn_conn_join_thelper

    thelper - pointer to thelper instance

DESCRIPTION
   This function will wait for the thread of thelper_main to finish

DEPENDENCIES
   None

RETURN VALUE
   0: success or negative value for failure

SIDE EFFECTS
   N/A

===========================================================================*/
int loc_eng_dmn_conn_join_thelper(struct loc_eng_dmn_conn_thelper * thelper)
{
    int result;

    LOC_LOGD("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
    result = pthread_join(thelper->thread_id, NULL);
    if (result != 0) {
        LOC_LOGE("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
    }
    LOC_LOGD("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);

    thelper_signal_destroy(thelper);

    return result;
}