| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2013 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 PTHREAD_ACCESSOR_H | 
|  | 18 | #define PTHREAD_ACCESSOR_H | 
|  | 19 |  | 
|  | 20 | #include <pthread.h> | 
|  | 21 |  | 
| Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 22 | #include "private/bionic_macros.h" | 
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 23 | #include "pthread_internal.h" | 
|  | 24 |  | 
|  | 25 | class pthread_accessor { | 
|  | 26 | public: | 
|  | 27 | explicit pthread_accessor(pthread_t desired_thread) { | 
|  | 28 | Lock(); | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 29 | for (thread_ = g_thread_list; thread_ != NULL; thread_ = thread_->next) { | 
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 30 | if (thread_ == reinterpret_cast<pthread_internal_t*>(desired_thread)) { | 
|  | 31 | break; | 
|  | 32 | } | 
|  | 33 | } | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | ~pthread_accessor() { | 
|  | 37 | Unlock(); | 
|  | 38 | } | 
|  | 39 |  | 
| Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 40 | void Unlock() { | 
|  | 41 | if (is_locked_) { | 
|  | 42 | is_locked_ = false; | 
|  | 43 | thread_ = NULL; | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 44 | pthread_mutex_unlock(&g_thread_list_lock); | 
| Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 45 | } | 
|  | 46 | } | 
|  | 47 |  | 
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 48 | pthread_internal_t& operator*() const { return *thread_; } | 
|  | 49 | pthread_internal_t* operator->() const { return thread_; } | 
|  | 50 | pthread_internal_t* get() const { return thread_; } | 
|  | 51 |  | 
|  | 52 | private: | 
|  | 53 | pthread_internal_t* thread_; | 
|  | 54 | bool is_locked_; | 
|  | 55 |  | 
|  | 56 | void Lock() { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 57 | pthread_mutex_lock(&g_thread_list_lock); | 
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 58 | is_locked_ = true; | 
|  | 59 | } | 
|  | 60 |  | 
| Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 61 | DISALLOW_COPY_AND_ASSIGN(pthread_accessor); | 
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 62 | }; | 
|  | 63 |  | 
|  | 64 | #endif // PTHREAD_ACCESSOR_H |