1*4d7e907cSAndroid Build Coastguard Worker /* 2*4d7e907cSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*4d7e907cSAndroid Build Coastguard Worker * 4*4d7e907cSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*4d7e907cSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*4d7e907cSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*4d7e907cSAndroid Build Coastguard Worker * 8*4d7e907cSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*4d7e907cSAndroid Build Coastguard Worker * 10*4d7e907cSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*4d7e907cSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*4d7e907cSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*4d7e907cSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*4d7e907cSAndroid Build Coastguard Worker * limitations under the License. 15*4d7e907cSAndroid Build Coastguard Worker */ 16*4d7e907cSAndroid Build Coastguard Worker 17*4d7e907cSAndroid Build Coastguard Worker #ifndef ANDROID_HARDWARE_GNSS_THREADCREATIONWRAPPER_H 18*4d7e907cSAndroid Build Coastguard Worker #define ANDROID_HARDWARE_GNSS_THREADCREATIONWRAPPER_H 19*4d7e907cSAndroid Build Coastguard Worker 20*4d7e907cSAndroid Build Coastguard Worker #include <log/log.h> 21*4d7e907cSAndroid Build Coastguard Worker #include <pthread.h> 22*4d7e907cSAndroid Build Coastguard Worker #include <vector> 23*4d7e907cSAndroid Build Coastguard Worker 24*4d7e907cSAndroid Build Coastguard Worker typedef void (*threadEntryFunc)(void* ret); 25*4d7e907cSAndroid Build Coastguard Worker 26*4d7e907cSAndroid Build Coastguard Worker /* 27*4d7e907cSAndroid Build Coastguard Worker * This class facilitates createThreadCb methods in various GNSS interfaces to wrap 28*4d7e907cSAndroid Build Coastguard Worker * pthread_create() from libc since its function signature differs from what is required by the 29*4d7e907cSAndroid Build Coastguard Worker * conventional GNSS HAL. The arguments passed to pthread_create() need to be on heap and not on 30*4d7e907cSAndroid Build Coastguard Worker * the stack of createThreadCb. 31*4d7e907cSAndroid Build Coastguard Worker */ 32*4d7e907cSAndroid Build Coastguard Worker struct ThreadFuncArgs { ThreadFuncArgsThreadFuncArgs33*4d7e907cSAndroid Build Coastguard Worker ThreadFuncArgs(void (*start)(void*), void* arg) : fptr(start), args(arg) {} 34*4d7e907cSAndroid Build Coastguard Worker 35*4d7e907cSAndroid Build Coastguard Worker /* pointer to the function of type void()(void*) that needs to be wrapped */ 36*4d7e907cSAndroid Build Coastguard Worker threadEntryFunc fptr; 37*4d7e907cSAndroid Build Coastguard Worker /* argument for fptr to be called with */ 38*4d7e907cSAndroid Build Coastguard Worker void* args; 39*4d7e907cSAndroid Build Coastguard Worker }; 40*4d7e907cSAndroid Build Coastguard Worker 41*4d7e907cSAndroid Build Coastguard Worker /* 42*4d7e907cSAndroid Build Coastguard Worker * This method is simply a wrapper. It is required since pthread_create() requires an entry 43*4d7e907cSAndroid Build Coastguard Worker * function pointer of type void*()(void*) and the GNSS hal requires as input a function pointer of 44*4d7e907cSAndroid Build Coastguard Worker * type void()(void*). 45*4d7e907cSAndroid Build Coastguard Worker */ 46*4d7e907cSAndroid Build Coastguard Worker void* threadFunc(void* arg); 47*4d7e907cSAndroid Build Coastguard Worker 48*4d7e907cSAndroid Build Coastguard Worker /* 49*4d7e907cSAndroid Build Coastguard Worker * This method is called by createThreadCb with a pointer to the vector that 50*4d7e907cSAndroid Build Coastguard Worker * holds the pointers to the thread arguments. The arg and start parameters are 51*4d7e907cSAndroid Build Coastguard Worker * first used to create a ThreadFuncArgs object which is then saved in the 52*4d7e907cSAndroid Build Coastguard Worker * listArgs parameters. The created ThreadFuncArgs object is then used to invoke 53*4d7e907cSAndroid Build Coastguard Worker * threadFunc() method which in-turn invokes pthread_create. 54*4d7e907cSAndroid Build Coastguard Worker */ 55*4d7e907cSAndroid Build Coastguard Worker pthread_t createPthread(const char* name, void (*start)(void*), void* arg, 56*4d7e907cSAndroid Build Coastguard Worker std::vector<std::unique_ptr<ThreadFuncArgs>> * listArgs); 57*4d7e907cSAndroid Build Coastguard Worker 58*4d7e907cSAndroid Build Coastguard Worker #endif 59