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