18c2d3d5afc51d3f35150f748f263870367771b6fEd Tam/* Copyright (c) 2011, The Linux Foundation. All rights reserved.
28c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *
38c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * Redistribution and use in source and binary forms, with or without
48c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * modification, are permitted provided that the following conditions are
58c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * met:
68c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *     * Redistributions of source code must retain the above copyright
78c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *       notice, this list of conditions and the following disclaimer.
88c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *     * Redistributions in binary form must reproduce the above
98c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *       copyright notice, this list of conditions and the following
108c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *       disclaimer in the documentation and/or other materials provided
118c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *       with the distribution.
128c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *     * Neither the name of The Linux Foundation nor the names of its
138c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *       contributors may be used to endorse or promote products derived
148c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *       from this software without specific prior written permission.
158c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *
168c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
178c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
188c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
198c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
208c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
218c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
228c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
238c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
248c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
258c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
268c2d3d5afc51d3f35150f748f263870367771b6fEd Tam * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278c2d3d5afc51d3f35150f748f263870367771b6fEd Tam *
288c2d3d5afc51d3f35150f748f263870367771b6fEd Tam */
298c2d3d5afc51d3f35150f748f263870367771b6fEd Tam#include <stdio.h>
308c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
318c2d3d5afc51d3f35150f748f263870367771b6fEd Tam#include "log_util.h"
328c2d3d5afc51d3f35150f748f263870367771b6fEd Tam#include "platform_lib_includes.h"
338c2d3d5afc51d3f35150f748f263870367771b6fEd Tam#include "loc_eng_dmn_conn_thread_helper.h"
348c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
358c2d3d5afc51d3f35150f748f263870367771b6fEd Tam/*===========================================================================
368c2d3d5afc51d3f35150f748f263870367771b6fEd TamFUNCTION    thelper_signal_init
378c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
388c2d3d5afc51d3f35150f748f263870367771b6fEd TamDESCRIPTION
398c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   This function will initialize the conditional variable resources.
408c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
418c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   thelper - thelper instance
428c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
438c2d3d5afc51d3f35150f748f263870367771b6fEd TamDEPENDENCIES
448c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   None
458c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
468c2d3d5afc51d3f35150f748f263870367771b6fEd TamRETURN VALUE
478c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   0: success or negative value for failure
488c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
498c2d3d5afc51d3f35150f748f263870367771b6fEd TamSIDE EFFECTS
508c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   N/A
518c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
528c2d3d5afc51d3f35150f748f263870367771b6fEd Tam===========================================================================*/
538c2d3d5afc51d3f35150f748f263870367771b6fEd Tamint thelper_signal_init(struct loc_eng_dmn_conn_thelper * thelper)
548c2d3d5afc51d3f35150f748f263870367771b6fEd Tam{
558c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int result;
568c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper->thread_exit  = 0;
578c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper->thread_ready = 0;
588c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    result = pthread_cond_init( &thelper->thread_cond, NULL);
598c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (result) {
608c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        return result;
618c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
628c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
638c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    result = pthread_mutex_init(&thelper->thread_mutex, NULL);
648c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (result) {
658c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        pthread_cond_destroy(&thelper->thread_cond);
668c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
678c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    return result;
688c2d3d5afc51d3f35150f748f263870367771b6fEd Tam}
698c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
708c2d3d5afc51d3f35150f748f263870367771b6fEd Tam/*===========================================================================
718c2d3d5afc51d3f35150f748f263870367771b6fEd TamFUNCTION
728c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
738c2d3d5afc51d3f35150f748f263870367771b6fEd TamDESCRIPTION
748c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   This function will destroy the conditional variable resources
758c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
768c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper - pointer to thelper instance
778c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
788c2d3d5afc51d3f35150f748f263870367771b6fEd TamDEPENDENCIES
798c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   None
808c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
818c2d3d5afc51d3f35150f748f263870367771b6fEd TamRETURN VALUE
828c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   0: success or negative value for failure
838c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
848c2d3d5afc51d3f35150f748f263870367771b6fEd TamSIDE EFFECTS
858c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   N/A
868c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
878c2d3d5afc51d3f35150f748f263870367771b6fEd Tam===========================================================================*/
888c2d3d5afc51d3f35150f748f263870367771b6fEd Tamint thelper_signal_destroy(struct loc_eng_dmn_conn_thelper * thelper)
898c2d3d5afc51d3f35150f748f263870367771b6fEd Tam{
908c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int result, ret_result = 0;
918c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    result = pthread_cond_destroy( &thelper->thread_cond);
928c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (result) {
938c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        ret_result = result;
948c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
958c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
968c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    result = pthread_mutex_destroy(&thelper->thread_mutex);
978c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (result) {
988c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        ret_result = result;
998c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
1008c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1018c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    return ret_result;
1028c2d3d5afc51d3f35150f748f263870367771b6fEd Tam}
1038c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1048c2d3d5afc51d3f35150f748f263870367771b6fEd Tam/*===========================================================================
1058c2d3d5afc51d3f35150f748f263870367771b6fEd TamFUNCTION    thelper_signal_wait
1068c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1078c2d3d5afc51d3f35150f748f263870367771b6fEd TamDESCRIPTION
1088c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   This function will be blocked on the conditional variable until thelper_signal_ready
1098c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   is called
1108c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1118c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper - pointer to thelper instance
1128c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1138c2d3d5afc51d3f35150f748f263870367771b6fEd TamDEPENDENCIES
1148c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   None
1158c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1168c2d3d5afc51d3f35150f748f263870367771b6fEd TamRETURN VALUE
1178c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   0: success or negative value for failure
1188c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1198c2d3d5afc51d3f35150f748f263870367771b6fEd TamSIDE EFFECTS
1208c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   N/A
1218c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1228c2d3d5afc51d3f35150f748f263870367771b6fEd Tam===========================================================================*/
1238c2d3d5afc51d3f35150f748f263870367771b6fEd Tamint thelper_signal_wait(struct loc_eng_dmn_conn_thelper * thelper)
1248c2d3d5afc51d3f35150f748f263870367771b6fEd Tam{
1258c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int result = 0;
1268c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1278c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    pthread_mutex_lock(&thelper->thread_mutex);
1288c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (!thelper->thread_ready && !thelper->thread_exit) {
1298c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        result = pthread_cond_wait(&thelper->thread_cond, &thelper->thread_mutex);
1308c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
1318c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1328c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (thelper->thread_exit) {
1338c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        result = -1;
1348c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
1358c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    pthread_mutex_unlock(&thelper->thread_mutex);
1368c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1378c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    return result;
1388c2d3d5afc51d3f35150f748f263870367771b6fEd Tam}
1398c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1408c2d3d5afc51d3f35150f748f263870367771b6fEd Tam/*===========================================================================
1418c2d3d5afc51d3f35150f748f263870367771b6fEd TamFUNCTION     thelper_signal_ready
1428c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1438c2d3d5afc51d3f35150f748f263870367771b6fEd TamDESCRIPTION
1448c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   This function will wake up the conditional variable
1458c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1468c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper - pointer to thelper instance
1478c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1488c2d3d5afc51d3f35150f748f263870367771b6fEd TamDEPENDENCIES
1498c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   None
1508c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1518c2d3d5afc51d3f35150f748f263870367771b6fEd TamRETURN VALUE
1528c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   0: success or negative value for failure
1538c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1548c2d3d5afc51d3f35150f748f263870367771b6fEd TamSIDE EFFECTS
1558c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   N/A
1568c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1578c2d3d5afc51d3f35150f748f263870367771b6fEd Tam===========================================================================*/
1588c2d3d5afc51d3f35150f748f263870367771b6fEd Tamint thelper_signal_ready(struct loc_eng_dmn_conn_thelper * thelper)
1598c2d3d5afc51d3f35150f748f263870367771b6fEd Tam{
1608c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int result;
1618c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1628c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    LOC_LOGD("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
1638c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1648c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    pthread_mutex_lock(&thelper->thread_mutex);
1658c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper->thread_ready = 1;
1668c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    result = pthread_cond_signal(&thelper->thread_cond);
1678c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    pthread_mutex_unlock(&thelper->thread_mutex);
1688c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1698c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    return result;
1708c2d3d5afc51d3f35150f748f263870367771b6fEd Tam}
1718c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1728c2d3d5afc51d3f35150f748f263870367771b6fEd Tam/*===========================================================================
1738c2d3d5afc51d3f35150f748f263870367771b6fEd TamFUNCTION     thelper_signal_block
1748c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1758c2d3d5afc51d3f35150f748f263870367771b6fEd TamDESCRIPTION
1768c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   This function will set the thread ready to 0 to block the thelper_signal_wait
1778c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1788c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper - pointer to thelper instance
1798c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1808c2d3d5afc51d3f35150f748f263870367771b6fEd TamDEPENDENCIES
1818c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   None
1828c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1838c2d3d5afc51d3f35150f748f263870367771b6fEd TamRETURN VALUE
1848c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   if thread_ready is set
1858c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1868c2d3d5afc51d3f35150f748f263870367771b6fEd TamSIDE EFFECTS
1878c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   N/A
1888c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1898c2d3d5afc51d3f35150f748f263870367771b6fEd Tam===========================================================================*/
1908c2d3d5afc51d3f35150f748f263870367771b6fEd Tamint thelper_signal_block(struct loc_eng_dmn_conn_thelper * thelper)
1918c2d3d5afc51d3f35150f748f263870367771b6fEd Tam{
1928c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int result = thelper->thread_ready;
1938c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1948c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    LOC_LOGD("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
1958c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
1968c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    pthread_mutex_lock(&thelper->thread_mutex);
1978c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper->thread_ready = 0;
1988c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    pthread_mutex_unlock(&thelper->thread_mutex);
1998c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2008c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    return result;
2018c2d3d5afc51d3f35150f748f263870367771b6fEd Tam}
2028c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2038c2d3d5afc51d3f35150f748f263870367771b6fEd Tam/*===========================================================================
2048c2d3d5afc51d3f35150f748f263870367771b6fEd TamFUNCTION    thelper_main
2058c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2068c2d3d5afc51d3f35150f748f263870367771b6fEd TamDESCRIPTION
2078c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   This function is the main thread. It will be launched as a child thread
2088c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2098c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    data - pointer to the instance
2108c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2118c2d3d5afc51d3f35150f748f263870367771b6fEd TamDEPENDENCIES
2128c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   None
2138c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2148c2d3d5afc51d3f35150f748f263870367771b6fEd TamRETURN VALUE
2158c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   NULL
2168c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2178c2d3d5afc51d3f35150f748f263870367771b6fEd TamSIDE EFFECTS
2188c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   N/A
2198c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2208c2d3d5afc51d3f35150f748f263870367771b6fEd Tam===========================================================================*/
2218c2d3d5afc51d3f35150f748f263870367771b6fEd Tamstatic void * thelper_main(void *data)
2228c2d3d5afc51d3f35150f748f263870367771b6fEd Tam{
2238c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int result = 0;
2248c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    struct loc_eng_dmn_conn_thelper * thelper = (struct loc_eng_dmn_conn_thelper *) data;
2258c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2268c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (thelper->thread_proc_init) {
2278c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        result = thelper->thread_proc_init(thelper->thread_context);
2288c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        if (result < 0) {
2298c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            thelper->thread_exit = 1;
2308c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            thelper_signal_ready(thelper);
2318c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            LOC_LOGE("%s:%d] error: 0x%lx\n", __func__, __LINE__, (long) thelper);
2328c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            return NULL;
2338c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        }
2348c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
2358c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2368c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper_signal_ready(thelper);
2378c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2388c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (thelper->thread_proc_pre) {
2398c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        result = thelper->thread_proc_pre(thelper->thread_context);
2408c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        if (result < 0) {
2418c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            thelper->thread_exit = 1;
2428c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            LOC_LOGE("%s:%d] error: 0x%lx\n", __func__, __LINE__, (long) thelper);
2438c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            return NULL;
2448c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        }
2458c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
2468c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2478c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    do {
2488c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        if (thelper->thread_proc) {
2498c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            result = thelper->thread_proc(thelper->thread_context);
2508c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            if (result < 0) {
2518c2d3d5afc51d3f35150f748f263870367771b6fEd Tam                thelper->thread_exit = 1;
2528c2d3d5afc51d3f35150f748f263870367771b6fEd Tam                LOC_LOGE("%s:%d] error: 0x%lx\n", __func__, __LINE__, (long) thelper);
2538c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            }
2548c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        }
2558c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    } while (thelper->thread_exit == 0);
2568c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2578c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (thelper->thread_proc_post) {
2588c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        result = thelper->thread_proc_post(thelper->thread_context);
2598c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
2608c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2618c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (result != 0) {
2628c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        LOC_LOGE("%s:%d] error: 0x%lx\n", __func__, __LINE__, (long) thelper);
2638c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
2648c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    return NULL;
2658c2d3d5afc51d3f35150f748f263870367771b6fEd Tam}
2668c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2678c2d3d5afc51d3f35150f748f263870367771b6fEd Tamstatic void thelper_main_2(void *data)
2688c2d3d5afc51d3f35150f748f263870367771b6fEd Tam{
2698c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper_main(data);
2708c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    return;
2718c2d3d5afc51d3f35150f748f263870367771b6fEd Tam}
2728c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2738c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2748c2d3d5afc51d3f35150f748f263870367771b6fEd Tam/*===========================================================================
2758c2d3d5afc51d3f35150f748f263870367771b6fEd TamFUNCTION    loc_eng_dmn_conn_launch_thelper
2768c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2778c2d3d5afc51d3f35150f748f263870367771b6fEd TamDESCRIPTION
2788c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   This function will initialize the thread context and launch the thelper_main
2798c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2808c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper - pointer to thelper instance
2818c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thread_proc_init - The initialization function pointer
2828c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thread_proc_pre  - The function to call before task loop and after initialization
2838c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thread_proc      - The task loop
2848c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thread_proc_post - The function to call after the task loop
2858c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    context          - the context for the above four functions
2868c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2878c2d3d5afc51d3f35150f748f263870367771b6fEd TamDEPENDENCIES
2888c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   None
2898c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2908c2d3d5afc51d3f35150f748f263870367771b6fEd TamRETURN VALUE
2918c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   0: success or negative value for failure
2928c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2938c2d3d5afc51d3f35150f748f263870367771b6fEd TamSIDE EFFECTS
2948c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   N/A
2958c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
2968c2d3d5afc51d3f35150f748f263870367771b6fEd Tam===========================================================================*/
2978c2d3d5afc51d3f35150f748f263870367771b6fEd Tamint loc_eng_dmn_conn_launch_thelper(struct loc_eng_dmn_conn_thelper * thelper,
2988c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int (*thread_proc_init) (void * context),
2998c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int (*thread_proc_pre) (void * context),
3008c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int (*thread_proc) (void * context),
3018c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int (*thread_proc_post) (void * context),
3028c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper_create_thread   create_thread_cb,
3038c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    void * context)
3048c2d3d5afc51d3f35150f748f263870367771b6fEd Tam{
3058c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int result;
3068c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3078c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper_signal_init(thelper);
3088c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3098c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (context) {
3108c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        thelper->thread_context    = context;
3118c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
3128c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3138c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper->thread_proc_init  = thread_proc_init;
3148c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper->thread_proc_pre   = thread_proc_pre;
3158c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper->thread_proc       = thread_proc;
3168c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper->thread_proc_post  = thread_proc_post;
3178c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3188c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    LOC_LOGD("%s:%d] 0x%lx call pthread_create\n", __func__, __LINE__, (long) thelper);
3198c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (create_thread_cb) {
3208c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        result = 0;
3218c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        thelper->thread_id = create_thread_cb("loc_eng_dmn_conn",
3228c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            thelper_main_2, (void *)thelper);
3238c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    } else {
3248c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        result = pthread_create(&thelper->thread_id, NULL,
3258c2d3d5afc51d3f35150f748f263870367771b6fEd Tam            thelper_main, (void *)thelper);
3268c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
3278c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3288c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (result != 0) {
3298c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        LOC_LOGE("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
3308c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        return -1;
3318c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
3328c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3338c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    LOC_LOGD("%s:%d] 0x%lx pthread_create done\n", __func__, __LINE__, (long) thelper);
3348c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3358c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper_signal_wait(thelper);
3368c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3378c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    LOC_LOGD("%s:%d] 0x%lx pthread ready\n", __func__, __LINE__, (long) thelper);
3388c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    return thelper->thread_exit;
3398c2d3d5afc51d3f35150f748f263870367771b6fEd Tam}
3408c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3418c2d3d5afc51d3f35150f748f263870367771b6fEd Tam/*===========================================================================
3428c2d3d5afc51d3f35150f748f263870367771b6fEd TamFUNCTION    loc_eng_dmn_conn_unblock_thelper
3438c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3448c2d3d5afc51d3f35150f748f263870367771b6fEd TamDESCRIPTION
3458c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   This function unblocks thelper_main to release the thread
3468c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3478c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper - pointer to thelper instance
3488c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3498c2d3d5afc51d3f35150f748f263870367771b6fEd TamDEPENDENCIES
3508c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   None
3518c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3528c2d3d5afc51d3f35150f748f263870367771b6fEd TamRETURN VALUE
3538c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   0: success
3548c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3558c2d3d5afc51d3f35150f748f263870367771b6fEd TamSIDE EFFECTS
3568c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   N/A
3578c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3588c2d3d5afc51d3f35150f748f263870367771b6fEd Tam===========================================================================*/
3598c2d3d5afc51d3f35150f748f263870367771b6fEd Tamint loc_eng_dmn_conn_unblock_thelper(struct loc_eng_dmn_conn_thelper * thelper)
3608c2d3d5afc51d3f35150f748f263870367771b6fEd Tam{
3618c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    LOC_LOGD("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
3628c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper->thread_exit = 1;
3638c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    return 0;
3648c2d3d5afc51d3f35150f748f263870367771b6fEd Tam}
3658c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3668c2d3d5afc51d3f35150f748f263870367771b6fEd Tam/*===========================================================================
3678c2d3d5afc51d3f35150f748f263870367771b6fEd TamFUNCTION    loc_eng_dmn_conn_join_thelper
3688c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3698c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper - pointer to thelper instance
3708c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3718c2d3d5afc51d3f35150f748f263870367771b6fEd TamDESCRIPTION
3728c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   This function will wait for the thread of thelper_main to finish
3738c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3748c2d3d5afc51d3f35150f748f263870367771b6fEd TamDEPENDENCIES
3758c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   None
3768c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3778c2d3d5afc51d3f35150f748f263870367771b6fEd TamRETURN VALUE
3788c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   0: success or negative value for failure
3798c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3808c2d3d5afc51d3f35150f748f263870367771b6fEd TamSIDE EFFECTS
3818c2d3d5afc51d3f35150f748f263870367771b6fEd Tam   N/A
3828c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3838c2d3d5afc51d3f35150f748f263870367771b6fEd Tam===========================================================================*/
3848c2d3d5afc51d3f35150f748f263870367771b6fEd Tamint loc_eng_dmn_conn_join_thelper(struct loc_eng_dmn_conn_thelper * thelper)
3858c2d3d5afc51d3f35150f748f263870367771b6fEd Tam{
3868c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    int result;
3878c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3888c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    LOC_LOGD("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
3898c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    result = pthread_join(thelper->thread_id, NULL);
3908c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    if (result != 0) {
3918c2d3d5afc51d3f35150f748f263870367771b6fEd Tam        LOC_LOGE("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
3928c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    }
3938c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    LOC_LOGD("%s:%d] 0x%lx\n", __func__, __LINE__, (long) thelper);
3948c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3958c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    thelper_signal_destroy(thelper);
3968c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
3978c2d3d5afc51d3f35150f748f263870367771b6fEd Tam    return result;
3988c2d3d5afc51d3f35150f748f263870367771b6fEd Tam}
3998c2d3d5afc51d3f35150f748f263870367771b6fEd Tam
400