1276789Sdim//===-- sanitizer_deadlock_detector_interface.h -----------------*- C++ -*-===//
2276789Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6276789Sdim//
7276789Sdim//===----------------------------------------------------------------------===//
8276789Sdim//
9276789Sdim// This file is a part of Sanitizer runtime.
10276789Sdim// Abstract deadlock detector interface.
11276789Sdim// FIXME: this is work in progress, nothing really works yet.
12276789Sdim//
13276789Sdim//===----------------------------------------------------------------------===//
14276789Sdim
15276789Sdim#ifndef SANITIZER_DEADLOCK_DETECTOR_INTERFACE_H
16276789Sdim#define SANITIZER_DEADLOCK_DETECTOR_INTERFACE_H
17276789Sdim
18276789Sdim#ifndef SANITIZER_DEADLOCK_DETECTOR_VERSION
19276789Sdim# define SANITIZER_DEADLOCK_DETECTOR_VERSION 1
20276789Sdim#endif
21276789Sdim
22276789Sdim#include "sanitizer_internal_defs.h"
23276789Sdim#include "sanitizer_atomic.h"
24276789Sdim
25276789Sdimnamespace __sanitizer {
26276789Sdim
27276789Sdim// dd - deadlock detector.
28276789Sdim// lt - logical (user) thread.
29276789Sdim// pt - physical (OS) thread.
30276789Sdim
31276789Sdimstruct DDPhysicalThread;
32276789Sdimstruct DDLogicalThread;
33276789Sdim
34276789Sdimstruct DDMutex {
35276789Sdim#if SANITIZER_DEADLOCK_DETECTOR_VERSION == 1
36276789Sdim  uptr id;
37276789Sdim  u32  stk;  // creation stack
38276789Sdim#elif SANITIZER_DEADLOCK_DETECTOR_VERSION == 2
39276789Sdim  u32              id;
40276789Sdim  u32              recursion;
41276789Sdim  atomic_uintptr_t owner;
42276789Sdim#else
43276789Sdim# error "BAD SANITIZER_DEADLOCK_DETECTOR_VERSION"
44276789Sdim#endif
45276789Sdim  u64  ctx;
46276789Sdim};
47276789Sdim
48276789Sdimstruct DDFlags {
49276789Sdim  bool second_deadlock_stack;
50276789Sdim};
51276789Sdim
52276789Sdimstruct DDReport {
53309124Sdim  enum { kMaxLoopSize = 20 };
54276789Sdim  int n;  // number of entries in loop
55276789Sdim  struct {
56276789Sdim    u64 thr_ctx;   // user thread context
57276789Sdim    u64 mtx_ctx0;  // user mutex context, start of the edge
58276789Sdim    u64 mtx_ctx1;  // user mutex context, end of the edge
59276789Sdim    u32 stk[2];  // stack ids for the edge
60276789Sdim  } loop[kMaxLoopSize];
61276789Sdim};
62276789Sdim
63276789Sdimstruct DDCallback {
64276789Sdim  DDPhysicalThread *pt;
65276789Sdim  DDLogicalThread  *lt;
66276789Sdim
67276789Sdim  virtual u32 Unwind() { return 0; }
68276789Sdim  virtual int UniqueTid() { return 0; }
69276789Sdim};
70276789Sdim
71276789Sdimstruct DDetector {
72276789Sdim  static DDetector *Create(const DDFlags *flags);
73276789Sdim
74296417Sdim  virtual DDPhysicalThread* CreatePhysicalThread() { return nullptr; }
75276789Sdim  virtual void DestroyPhysicalThread(DDPhysicalThread *pt) {}
76276789Sdim
77296417Sdim  virtual DDLogicalThread* CreateLogicalThread(u64 ctx) { return nullptr; }
78276789Sdim  virtual void DestroyLogicalThread(DDLogicalThread *lt) {}
79276789Sdim
80276789Sdim  virtual void MutexInit(DDCallback *cb, DDMutex *m) {}
81276789Sdim  virtual void MutexBeforeLock(DDCallback *cb, DDMutex *m, bool wlock) {}
82276789Sdim  virtual void MutexAfterLock(DDCallback *cb, DDMutex *m, bool wlock,
83276789Sdim      bool trylock) {}
84276789Sdim  virtual void MutexBeforeUnlock(DDCallback *cb, DDMutex *m, bool wlock) {}
85276789Sdim  virtual void MutexDestroy(DDCallback *cb, DDMutex *m) {}
86276789Sdim
87296417Sdim  virtual DDReport *GetReport(DDCallback *cb) { return nullptr; }
88276789Sdim};
89276789Sdim
90276789Sdim} // namespace __sanitizer
91276789Sdim
92276789Sdim#endif // SANITIZER_DEADLOCK_DETECTOR_INTERFACE_H
93