summaryrefslogtreecommitdiffstats
path: root/xbmc/threads/Lockables.h
blob: 18509b14d8cc5393eb2288f018214a7125b8bf25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

namespace XbmcThreads
{

  /**
   * This template will take any implementation of the "Lockable" concept
   * and allow it to be used as an "Exitable Lockable."
   *
   * Something that implements the "Lockable concept" simply means that
   * it has the three methods:
   *
   *   lock();
   *   try_lock();
   *   unlock();
   *   IsLocked();
   *
   * "Exitable" specifically means that, no matter how deep the recursion
   * on the mutex/critical section, we can exit from it and then restore
   * the state.
   *
   * This requires us to extend the Lockable so that we can keep track of the
   * number of locks that have been recursively acquired so that we can
   * undo it, and then restore that (See class CSingleExit).
   *
   * All xbmc code expects Lockables to be recursive.
   */
  template<class L> class CountingLockable
  {
    friend class ConditionVariable;

    CountingLockable(const CountingLockable&) = delete;
    CountingLockable& operator=(const CountingLockable&) = delete;
  protected:
    L mutex;
    unsigned int count = 0;

  public:
    inline CountingLockable() = default;

    // STL Lockable concept
    inline void lock() { mutex.lock(); count++; }
    inline bool try_lock() { return mutex.try_lock() ? count++, true : false; }
    inline void unlock() { count--; mutex.unlock(); }

    /*!
     * \brief Check if have a lock owned
     * \return True if have a lock, otherwise false
     */
    inline bool IsLocked() const { return count > 0; }

    /**
     * This implements the "exitable" behavior mentioned above.
     */
    inline unsigned int exit(unsigned int leave = 0)
    {
      // it's possible we don't actually own the lock
      // so we will try it.
      unsigned int ret = 0;
      if (try_lock())
      {
        if (leave < (count - 1))
        {
          ret = count - 1 - leave;  // The -1 is because we don't want
                                    //  to count the try_lock increment.
          // We must NOT compare "count" in this loop since
          // as soon as the last unlock is called another thread
          // can modify it.
          for (unsigned int i = 0; i < ret; i++)
            unlock();
        }
        unlock(); // undo the try_lock before returning
      }

      return ret;
    }

    /**
     * Restore a previous exit to the provided level.
     */
    inline void restore(unsigned int restoreCount)
    {
      for (unsigned int i = 0; i < restoreCount; i++)
        lock();
    }

    /**
     * Some implementations (see pthreads) require access to the underlying
     *  CCriticalSection, which is also implementation specific. This
     *  provides access to it through the same method on the guard classes
     *  UniqueLock, and SharedLock.
     *
     * There really should be no need for the users of the threading library
     *  to call this method.
     */
    inline L& get_underlying() { return mutex; }
  };

}