Twitch SDK (Internal)
mutex.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #pragma once
10 
13 
14 #include <memory>
15 
16 #ifdef CreateMutex
17  #undef CreateMutex
18 #endif
19 
20 namespace ttv
21 {
22  class IMutex;
23  class IConditionMutex;
24  class IMutexFactory;
25  class AutoMutex;
26 
27  void SetMutexFactory(std::shared_ptr<IMutexFactory> factory);
28 
29  TTV_ErrorCode CreateMutex(std::shared_ptr<IMutex>& result);
30  TTV_ErrorCode CreateConditionMutex(std::shared_ptr<IConditionMutex>& result);
31 }
32 
33 
38 {
39 public:
40  virtual ~IMutex();
41 
47  virtual TTV_ErrorCode Lock() = 0;
48 
55  virtual TTV_ErrorCode TryLock() = 0;
56 
62  virtual TTV_ErrorCode Unlock() = 0;
63 };
64 
65 /* Interface for a mutex with condition variable functionality.
66  *
67  * Wait() and WaitFor() must be called under lock (i.e. after Lock())
68  * Signal() and Broadcast() in general should be called outside of lock to avoid pessimization.
69  */
71 {
72 public:
73  virtual ~IConditionMutex();
74 
81  virtual TTV_ErrorCode Wait() = 0;
82 
91  virtual TTV_ErrorCode WaitFor(uint64_t timeoutMilliseconds) = 0;
92 
93  /* Notify one thread waiting to be notified (through Wait()/WaitFor()).
94  * Nothing happens if there are no threads waiting.
95  * @return
96  * - TTV_EC_SUCCESS: Successfully notified a waiting thread (or there were no waiting threads).
97  */
98  virtual TTV_ErrorCode Signal() = 0;
99 
100  /* Notify all threads waiting to be notified (through Wait()/WaitFor()).
101  * Nothing happens if there are no threads waiting.
102  * @return
103  * - TTV_EC_SUCCESS: Successfully notified all waiting threads (or there were no waiting threads).
104  */
105  virtual TTV_ErrorCode Broadcast() = 0;
106 };
107 
108 
110 {
111 public:
112  virtual ~IMutexFactory();
113 
114  virtual TTV_ErrorCode CreateMutex(std::shared_ptr<IMutex>& result) = 0;
115  virtual TTV_ErrorCode CreateConditionMutex(std::shared_ptr<IConditionMutex>& result) = 0;
116 };
117 
118 
120 {
121 public:
122  inline explicit AutoMutex(const std::shared_ptr<IMutex>& mutex)
123  : mMutex(mutex)
124  {
125  assert(mutex != nullptr);
126  mutex->Lock();
127  }
128 
130  {
131  mMutex->Unlock();
132  }
133 
134 private:
135  std::shared_ptr<IMutex> mMutex;
136 };
virtual ~IMutex()
Definition: mutex.h:37
virtual TTV_ErrorCode Unlock()=0
virtual TTV_ErrorCode TryLock()=0
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
AutoMutex(const std::shared_ptr< IMutex > &mutex)
Definition: mutex.h:122
#define assert(expr)
Definition: assertion.h:47
std::shared_ptr< IMutex > mMutex
Definition: mutex.h:135
TTV_ErrorCode CreateConditionMutex(std::shared_ptr< IConditionMutex > &result)
TTV_ErrorCode CreateMutex(std::shared_ptr< IMutex > &result)
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
Definition: mutex.h:109
~AutoMutex()
Definition: mutex.h:129
virtual TTV_ErrorCode Lock()=0
void SetMutexFactory(std::shared_ptr< IMutexFactory > factory)
Definition: mutex.h:119
Definition: mutex.h:70