Home | History | Annotate | Download | only in default
      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 #include <ThreadCreationWrapper.h>
     18 
     19 void* threadFunc(void* arg) {
     20     ThreadFuncArgs* threadArgs = reinterpret_cast<ThreadFuncArgs*>(arg);
     21     threadArgs->fptr(threadArgs->args);
     22     return nullptr;
     23 }
     24 
     25 pthread_t createPthread(const char* name,
     26                         void (*start)(void*),
     27                         void* arg, std::vector<std::unique_ptr<ThreadFuncArgs>> * listArgs) {
     28     pthread_t threadId;
     29     auto threadArgs = new ThreadFuncArgs(start, arg);
     30     auto argPtr = std::unique_ptr<ThreadFuncArgs>(threadArgs);
     31 
     32     listArgs->push_back(std::move(argPtr));
     33 
     34     int ret = pthread_create(&threadId, nullptr, threadFunc, reinterpret_cast<void*>(
     35             threadArgs));
     36     if (ret != 0) {
     37         ALOGE("pthread creation unsuccessful");
     38     } else {
     39         pthread_setname_np(threadId, name);
     40     }
     41     return threadId;
     42 }
     43