blob: 11924a3adf989a3c601435d02493277809dfa438 [file] [log] [blame]
Christopher Ferris17e91d42013-10-21 13:30:52 -07001/*
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 Ferris2c43cff2015-03-26 19:18:36 -070017#ifndef _LIBBACKTRACE_THREAD_ENTRY_H
18#define _LIBBACKTRACE_THREAD_ENTRY_H
Christopher Ferris17e91d42013-10-21 13:30:52 -070019
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070020#include <pthread.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070021#include <sys/types.h>
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070022#include <ucontext.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070023
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070024class ThreadEntry {
25public:
26 static ThreadEntry* Get(pid_t pid, pid_t tid, bool create = true);
Christopher Ferris17e91d42013-10-21 13:30:52 -070027
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070028 static void Remove(ThreadEntry* entry);
29
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070030 void Wake();
31
Christopher Ferris2d091712015-05-28 16:10:33 -070032 bool Wait(int);
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070033
Christopher Ferrise4846072014-05-23 14:46:36 -070034 void CopyUcontextFromSigcontext(void*);
35
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070036 inline void Lock() {
37 pthread_mutex_lock(&mutex_);
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -080038
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 Ferrisa2efd3a2014-05-06 15:23:59 -070042 }
43
44 inline void Unlock() {
45 pthread_mutex_unlock(&mutex_);
46 }
47
48 inline ucontext_t* GetUcontext() { return &ucontext_; }
49
50private:
51 ThreadEntry(pid_t pid, pid_t tid);
Christopher Ferris17e91d42013-10-21 13:30:52 -070052 ~ThreadEntry();
53
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070054 bool Match(pid_t chk_pid, pid_t chk_tid) { return (chk_pid == pid_ && chk_tid == tid_); }
Christopher Ferris17e91d42013-10-21 13:30:52 -070055
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070056 pid_t pid_;
57 pid_t tid_;
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070058 int ref_count_;
59 pthread_mutex_t mutex_;
Christopher Ferris3cdbfdc2014-11-08 15:57:11 -080060 pthread_mutex_t wait_mutex_;
61 pthread_cond_t wait_cond_;
62 int wait_value_;
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070063 ThreadEntry* next_;
64 ThreadEntry* prev_;
65 ucontext_t ucontext_;
Christopher Ferris17e91d42013-10-21 13:30:52 -070066
Christopher Ferrisa2efd3a2014-05-06 15:23:59 -070067 static ThreadEntry* list_;
68 static pthread_mutex_t list_mutex_;
Christopher Ferris17e91d42013-10-21 13:30:52 -070069};
70
Christopher Ferris2c43cff2015-03-26 19:18:36 -070071#endif // _LIBBACKTRACE_THREAD_ENTRY_H