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 _LIBBACKTRACE_THREAD_ENTRY_H
18 #define _LIBBACKTRACE_THREAD_ENTRY_H
19 
20 #include <pthread.h>
21 #include <sys/types.h>
22 #include <ucontext.h>
23 
24 class ThreadEntry {
25  public:
26   static ThreadEntry* Get(pid_t pid, pid_t tid, bool create = true);
27 
28   static void Remove(ThreadEntry* entry);
29 
30   void Wake();
31 
32   bool Wait(int);
33 
34   void CopyUcontextFromSigcontext(void*);
35 
Lock()36   inline void Lock() {
37     pthread_mutex_lock(&mutex_);
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;
42   }
43 
Unlock()44   inline void Unlock() {
45     pthread_mutex_unlock(&mutex_);
46   }
47 
GetUcontext()48   inline ucontext_t* GetUcontext() { return &ucontext_; }
49 
50  private:
51   ThreadEntry(pid_t pid, pid_t tid);
52   ~ThreadEntry();
53 
Match(pid_t chk_pid,pid_t chk_tid)54   bool Match(pid_t chk_pid, pid_t chk_tid) { return (chk_pid == pid_ && chk_tid == tid_); }
55 
56   pid_t pid_;
57   pid_t tid_;
58   int ref_count_;
59   pthread_mutex_t mutex_;
60   pthread_mutex_t wait_mutex_;
61   pthread_cond_t wait_cond_;
62   int wait_value_;
63   ThreadEntry* next_;
64   ThreadEntry* prev_;
65   ucontext_t ucontext_;
66 
67   static ThreadEntry* list_;
68   static pthread_mutex_t list_mutex_;
69 };
70 
71 #endif // _LIBBACKTRACE_THREAD_ENTRY_H
72