You.i Engine
YiSignalHandler.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_SIGNAL_HANDLER_H_
3 #define _YI_SIGNAL_HANDLER_H_
4 
6 #include "thread/YiThread.h"
7 #include "utility/YiLazy.h"
9 
10 #define YI_RANDOM_SLEEP(X) YI_SLEEP(((rand() % 5) + 1) * (X))
11 
12 class CYISignalBase;
13 
176 {
177  friend class CYISignalBase;
178 
179 public:
181  CYISignalHandler(const CYISignalHandler &rSignalHandler);
182  virtual ~CYISignalHandler();
183  CYISignalHandler &operator=(const CYISignalHandler &rSignalHandler);
184 
258  void MoveToThread(CYIThread *pThread);
259 
272 
285  void SetThreadAffinity(const CYIThreadHandle &rThreadAffinity);
286 
295  virtual bool IsConnected() const;
296 
335  virtual bool IsConnected(const CYISignalBase &rSignal) const;
336 
349  void Disconnect(CYISignalBase &rSignal);
350 
360 
361 private:
362  enum class NotifyFlag
363  {
364  DoNotNotify,
365  Notify
366  };
367 
368  struct HandlerObjects
369  {
370  HandlerObjects();
371 
372  mutable CYIRecursiveSpinLock m_handlerMutex;
373  CYIThread *m_pThreadOverride;
375  };
376 
377  void AddSignal(CYISignalBase &rSignal) const;
378  void RemoveSignal(CYISignalBase &rSignal, NotifyFlag notifySignal);
379  void RemoveAllSignals(NotifyFlag notifySignal);
380  bool HasSignal(const CYISignalBase &rSignal) const;
381  void CopySignalHandler(const CYISignalHandler &rSignalHandler);
382 
383  void ExclusiveLock(CYIRecursiveSpinLock &rHandlerMutex) const;
384  void ExclusiveLock(const CYISignalBase &rSignal, CYIRecursiveSpinLock &rHandlerMutex) const;
385  void ExclusiveUnlock(CYIRecursiveSpinLock &rHandlerMutex) const;
386  void ExclusiveUnlock(const CYISignalBase &rSignal, CYIRecursiveSpinLock &rHandlerMutex) const;
387 
388  virtual void OnThreadDestroyed(CYIThread *pThread) override;
389 
390  CYILazy<HandlerObjects> m_handlerObjects;
391  CYIThreadHandle m_threadAffinity;
392 };
393 
396 #endif /* _YI_SIGNAL_HANDLER_H_ */
Provides access serialization between threads, where a thread trying to acquire the lock waits in a l...
Definition: YiRecursiveSpinLock.h:33
void DisconnectFromAllSignals()
CYISignalHandler & operator=(const CYISignalHandler &rSignalHandler)
The base abstract class for CYISignal.
Definition: YiSignalBase.h:20
void Disconnect(CYISignalBase &rSignal)
Definition: YiSignalHandler.h:175
virtual bool IsConnected() const
Definition: YiThread.h:102
Definition: YiThread.h:71
CYIThreadHandle GetThreadAffinity() const
void MoveToThread(CYIThread *pThread)
This function allows the user to override the default thread affinity to any CYIThread that may or ma...
void SetThreadAffinity(const CYIThreadHandle &rThreadAffinity)
virtual ~CYISignalHandler()
Definition: YiThreadHandle.h:45