Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Mike Yu | c6d4c6b | 2019-03-14 15:14:44 +0800 | [diff] [blame] | 17 | #ifndef NETDUTILS_THREADUTIL_H |
| 18 | #define NETDUTILS_THREADUTIL_H |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 19 | |
| 20 | #include <pthread.h> |
| 21 | #include <memory> |
| 22 | |
Ken Chen | bab5014 | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 23 | #include <android-base/logging.h> |
| 24 | |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 25 | namespace android { |
Mike Yu | c6d4c6b | 2019-03-14 15:14:44 +0800 | [diff] [blame] | 26 | namespace netdutils { |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 27 | |
| 28 | struct scoped_pthread_attr { |
| 29 | scoped_pthread_attr() { pthread_attr_init(&attr); } |
| 30 | ~scoped_pthread_attr() { pthread_attr_destroy(&attr); } |
| 31 | |
Mike Yu | c6d4c6b | 2019-03-14 15:14:44 +0800 | [diff] [blame] | 32 | int detach() { return -pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); } |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 33 | |
| 34 | pthread_attr_t attr; |
| 35 | }; |
| 36 | |
Mike Yu | 5c7a3b3 | 2019-05-23 20:08:24 +0800 | [diff] [blame] | 37 | inline void setThreadName(std::string name) { |
| 38 | // MAX_TASK_COMM_LEN=16 is not exported by bionic. |
| 39 | const size_t MAX_TASK_COMM_LEN = 16; |
| 40 | |
| 41 | // Crop name to 16 bytes including the NUL byte, as required by pthread_setname_np() |
| 42 | if (name.size() >= MAX_TASK_COMM_LEN) name.resize(MAX_TASK_COMM_LEN - 1); |
| 43 | |
| 44 | if (int ret = pthread_setname_np(pthread_self(), name.c_str()); ret != 0) { |
| 45 | LOG(WARNING) << "Unable to set thread name to " << name << ": " << strerror(ret); |
| 46 | } |
| 47 | } |
| 48 | |
Mike Yu | c6d4c6b | 2019-03-14 15:14:44 +0800 | [diff] [blame] | 49 | template <typename T> |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 50 | inline void* runAndDelete(void* obj) { |
| 51 | std::unique_ptr<T> handler(reinterpret_cast<T*>(obj)); |
Mike Yu | 5c7a3b3 | 2019-05-23 20:08:24 +0800 | [diff] [blame] | 52 | setThreadName(handler->threadName().c_str()); |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 53 | handler->run(); |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
Mike Yu | c6d4c6b | 2019-03-14 15:14:44 +0800 | [diff] [blame] | 57 | template <typename T> |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 58 | inline int threadLaunch(T* obj) { |
Mike Yu | c6d4c6b | 2019-03-14 15:14:44 +0800 | [diff] [blame] | 59 | if (obj == nullptr) { |
| 60 | return -EINVAL; |
| 61 | } |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 62 | |
| 63 | scoped_pthread_attr scoped_attr; |
| 64 | |
| 65 | int rval = scoped_attr.detach(); |
Mike Yu | c6d4c6b | 2019-03-14 15:14:44 +0800 | [diff] [blame] | 66 | if (rval != 0) { |
| 67 | return rval; |
| 68 | } |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 69 | |
| 70 | pthread_t thread; |
| 71 | rval = pthread_create(&thread, &scoped_attr.attr, &runAndDelete<T>, obj); |
| 72 | if (rval != 0) { |
Ken Chen | bab5014 | 2019-03-19 17:41:28 +0800 | [diff] [blame] | 73 | LOG(WARNING) << __func__ << ": pthread_create failed: " << rval; |
George Burgess IV | 0411356 | 2017-09-26 16:35:23 -0700 | [diff] [blame] | 74 | return -rval; |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | return rval; |
| 78 | } |
| 79 | |
Mike Yu | c6d4c6b | 2019-03-14 15:14:44 +0800 | [diff] [blame] | 80 | } // namespace netdutils |
Erik Kline | c509046 | 2017-03-09 19:01:24 +0900 | [diff] [blame] | 81 | } // namespace android |
| 82 | |
Mike Yu | c6d4c6b | 2019-03-14 15:14:44 +0800 | [diff] [blame] | 83 | #endif // NETDUTILS_THREADUTIL_H |