Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [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 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 17 | #ifndef _LIBBACKTRACE_THREAD_ENTRY_H |
| 18 | #define _LIBBACKTRACE_THREAD_ENTRY_H |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 19 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 20 | #include <pthread.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 22 | #include <ucontext.h> |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 23 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 24 | class ThreadEntry { |
| 25 | public: |
| 26 | static ThreadEntry* Get(pid_t pid, pid_t tid, bool create = true); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 27 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 28 | static void Remove(ThreadEntry* entry); |
| 29 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 30 | void Wake(); |
| 31 | |
Christopher Ferris | 2d09171 | 2015-05-28 16:10:33 -0700 | [diff] [blame] | 32 | bool Wait(int); |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 33 | |
Christopher Ferris | e484607 | 2014-05-23 14:46:36 -0700 | [diff] [blame] | 34 | void CopyUcontextFromSigcontext(void*); |
| 35 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 36 | inline void Lock() { |
| 37 | pthread_mutex_lock(&mutex_); |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 38 | |
| 39 | // Always reset the wait value since this could be the first or nth |
| 40 | // time this entry is locked. |
| 41 | wait_value_ = 0; |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | inline void Unlock() { |
| 45 | pthread_mutex_unlock(&mutex_); |
| 46 | } |
| 47 | |
| 48 | inline ucontext_t* GetUcontext() { return &ucontext_; } |
| 49 | |
| 50 | private: |
| 51 | ThreadEntry(pid_t pid, pid_t tid); |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 52 | ~ThreadEntry(); |
| 53 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 54 | bool Match(pid_t chk_pid, pid_t chk_tid) { return (chk_pid == pid_ && chk_tid == tid_); } |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 55 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 56 | pid_t pid_; |
| 57 | pid_t tid_; |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 58 | int ref_count_; |
| 59 | pthread_mutex_t mutex_; |
Christopher Ferris | 3cdbfdc | 2014-11-08 15:57:11 -0800 | [diff] [blame] | 60 | pthread_mutex_t wait_mutex_; |
| 61 | pthread_cond_t wait_cond_; |
| 62 | int wait_value_; |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 63 | ThreadEntry* next_; |
| 64 | ThreadEntry* prev_; |
| 65 | ucontext_t ucontext_; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 66 | |
Christopher Ferris | a2efd3a | 2014-05-06 15:23:59 -0700 | [diff] [blame] | 67 | static ThreadEntry* list_; |
| 68 | static pthread_mutex_t list_mutex_; |
Christopher Ferris | 17e91d4 | 2013-10-21 13:30:52 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 71 | #endif // _LIBBACKTRACE_THREAD_ENTRY_H |