Home | History | Annotate | Download | only in sanitizer_common
      1 //===-- sanitizer_thread_registry.h -----------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file is shared between sanitizer tools.
     11 //
     12 // General thread bookkeeping functionality.
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef SANITIZER_THREAD_REGISTRY_H
     16 #define SANITIZER_THREAD_REGISTRY_H
     17 
     18 #include "sanitizer_common.h"
     19 #include "sanitizer_list.h"
     20 #include "sanitizer_mutex.h"
     21 
     22 namespace __sanitizer {
     23 
     24 enum ThreadStatus {
     25   ThreadStatusInvalid,   // Non-existent thread, data is invalid.
     26   ThreadStatusCreated,   // Created but not yet running.
     27   ThreadStatusRunning,   // The thread is currently running.
     28   ThreadStatusFinished,  // Joinable thread is finished but not yet joined.
     29   ThreadStatusDead       // Joined, but some info is still available.
     30 };
     31 
     32 // Generic thread context. Specific sanitizer tools may inherit from it.
     33 // If thread is dead, context may optionally be reused for a new thread.
     34 class ThreadContextBase {
     35  public:
     36   explicit ThreadContextBase(u32 tid);
     37   ~ThreadContextBase();  // Should never be called.
     38 
     39   const u32 tid;  // Thread ID. Main thread should have tid = 0.
     40   u64 unique_id;  // Unique thread ID.
     41   uptr os_id;     // PID (used for reporting).
     42   uptr user_id;   // Some opaque user thread id (e.g. pthread_t).
     43   char name[64];  // As annotated by user.
     44 
     45   ThreadStatus status;
     46   bool detached;
     47   int reuse_count;
     48 
     49   u32 parent_tid;
     50   ThreadContextBase *next;  // For storing thread contexts in a list.
     51 
     52   void SetName(const char *new_name);
     53 
     54   void SetDead();
     55   void SetJoined(void *arg);
     56   void SetFinished();
     57   void SetStarted(uptr _os_id, void *arg);
     58   void SetCreated(uptr _user_id, u64 _unique_id, bool _detached,
     59                   u32 _parent_tid, void *arg);
     60   void Reset();
     61 
     62   // The following methods may be overriden by subclasses.
     63   // Some of them take opaque arg that may be optionally be used
     64   // by subclasses.
     65   virtual void OnDead() {}
     66   virtual void OnJoined(void *arg) {}
     67   virtual void OnFinished() {}
     68   virtual void OnStarted(void *arg) {}
     69   virtual void OnCreated(void *arg) {}
     70   virtual void OnReset() {}
     71 };
     72 
     73 typedef ThreadContextBase* (*ThreadContextFactory)(u32 tid);
     74 
     75 class ThreadRegistry {
     76  public:
     77   static const u32 kUnknownTid;
     78 
     79   ThreadRegistry(ThreadContextFactory factory, u32 max_threads,
     80                  u32 thread_quarantine_size);
     81   void GetNumberOfThreads(uptr *total = 0, uptr *running = 0, uptr *alive = 0);
     82   uptr GetMaxAliveThreads();
     83 
     84   void Lock() { mtx_.Lock(); }
     85   void CheckLocked() { mtx_.CheckLocked(); }
     86   void Unlock() { mtx_.Unlock(); }
     87 
     88   // Should be guarded by ThreadRegistryLock.
     89   ThreadContextBase *GetThreadLocked(u32 tid) {
     90     DCHECK_LT(tid, n_contexts_);
     91     return threads_[tid];
     92   }
     93 
     94   u32 CreateThread(uptr user_id, bool detached, u32 parent_tid, void *arg);
     95 
     96   typedef void (*ThreadCallback)(ThreadContextBase *tctx, void *arg);
     97   // Invokes callback with a specified arg for each thread context.
     98   // Should be guarded by ThreadRegistryLock.
     99   void RunCallbackForEachThreadLocked(ThreadCallback cb, void *arg);
    100 
    101   typedef bool (*FindThreadCallback)(ThreadContextBase *tctx, void *arg);
    102   // Finds a thread using the provided callback. Returns kUnknownTid if no
    103   // thread is found.
    104   u32 FindThread(FindThreadCallback cb, void *arg);
    105   // Should be guarded by ThreadRegistryLock. Return 0 if no thread
    106   // is found.
    107   ThreadContextBase *FindThreadContextLocked(FindThreadCallback cb,
    108                                              void *arg);
    109   ThreadContextBase *FindThreadContextByOsIDLocked(uptr os_id);
    110 
    111   void SetThreadName(u32 tid, const char *name);
    112   void DetachThread(u32 tid);
    113   void JoinThread(u32 tid, void *arg);
    114   void FinishThread(u32 tid);
    115   void StartThread(u32 tid, uptr os_id, void *arg);
    116 
    117  private:
    118   const ThreadContextFactory context_factory_;
    119   const u32 max_threads_;
    120   const u32 thread_quarantine_size_;
    121 
    122   BlockingMutex mtx_;
    123 
    124   u32 n_contexts_;      // Number of created thread contexts,
    125                         // at most max_threads_.
    126   u64 total_threads_;   // Total number of created threads. May be greater than
    127                         // max_threads_ if contexts were reused.
    128   uptr alive_threads_;  // Created or running.
    129   uptr max_alive_threads_;
    130   uptr running_threads_;
    131 
    132   ThreadContextBase **threads_;  // Array of thread contexts is leaked.
    133   IntrusiveList<ThreadContextBase> dead_threads_;
    134   IntrusiveList<ThreadContextBase> invalid_threads_;
    135 
    136   void QuarantinePush(ThreadContextBase *tctx);
    137   ThreadContextBase *QuarantinePop();
    138 };
    139 
    140 typedef GenericScopedLock<ThreadRegistry> ThreadRegistryLock;
    141 
    142 }  // namespace __sanitizer
    143 
    144 #endif  // SANITIZER_THREAD_REGISTRY_H
    145 
    146