ctkEASyncDeliverTasks_p.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKEASYNCDELIVERTASKS_P_H
  16. #define CTKEASYNCDELIVERTASKS_P_H
  17. #include "ctkEADeliverTask_p.h"
  18. #include <QMutex>
  19. class ctkEADefaultThreadPool;
  20. class ctkEASyncMasterThread;
  21. /**
  22. * This class does the actual work of the synchronous event delivery.
  23. *
  24. * This is the heart of the event delivery. If an event is delivered
  25. * without timeout handling, the event is directly delivered using
  26. * the calling thread.
  27. * If timeout handling is enabled, a new thread is taken from the
  28. * thread pool and this thread is used to deliver the event.
  29. * The calling thread is blocked until either the deliver is finished
  30. * or the timeout occurs.
  31. * <p><tt>
  32. * Note that in case of a timeout while a task is disabled the thread
  33. * is released and we spin-off a new thread that resumes the disabled
  34. * task hence, this is the only place were we break the semantics of
  35. * the synchronous delivery. While the only one to notice this is the
  36. * timed-out handler - it is the fault of this handler too (i.e., it
  37. * blocked the dispatch for to long) but since it will not receive
  38. * events anymore it will not notice this semantic difference except
  39. * that it might not see events it already sent before.
  40. * </tt></pre>
  41. *
  42. * If during an event delivery a new event should be delivered from
  43. * within the event handler, the timeout handler is stopped for the
  44. * delivery time of the inner event!
  45. */
  46. template<class HandlerTask>
  47. class ctkEASyncDeliverTasks : public ctkEADeliverTask<ctkEASyncDeliverTasks<HandlerTask>, HandlerTask>
  48. {
  49. private:
  50. /** The thread pool used to spin-off new threads. */
  51. ctkEADefaultThreadPool* pool;
  52. /** This is a ctkEAInterruptibleThread used to execute the handlers */
  53. ctkEASyncMasterThread* syncMasterThread;
  54. /** The timeout for event handlers, 0 = disabled. */
  55. long timeout;
  56. /**
  57. * The matcher interface for checking if timeout handling
  58. * is disabled for the handler.
  59. * Matching is based on the class name of the event handler.
  60. */
  61. struct Matcher
  62. {
  63. virtual ~Matcher() {}
  64. virtual bool match(const QString& className) const = 0;
  65. };
  66. /** Match a class name. */
  67. struct ClassMatcher : public Matcher
  68. {
  69. private:
  70. const QString className;
  71. public:
  72. ClassMatcher(const QString& name)
  73. : className(name)
  74. {}
  75. bool match(const QString& name) const
  76. {
  77. return className == name;
  78. }
  79. };
  80. /** The matchers for ignore timeout handling. */
  81. QList<Matcher*> ignoreTimeoutMatcher;
  82. QMutex mutex;
  83. public:
  84. /**
  85. * Construct a new sync deliver tasks.
  86. * @param pool The thread pool used to spin-off new threads.
  87. * @param timeout The timeout for an event handler, 0 = disabled
  88. */
  89. ctkEASyncDeliverTasks(ctkEADefaultThreadPool* pool, ctkEASyncMasterThread* syncMasterThread,
  90. long timeout, const QList<QString>& ignoreTimeout);
  91. void update(long timeout, const QList<QString>& ignoreTimeout);
  92. /**
  93. * This blocks an unrelated thread used to send a synchronous event until the
  94. * event is send (or a timeout occurs).
  95. *
  96. * @param tasks The event handler dispatch tasks to execute
  97. *
  98. * @see ctkEADeliverTask#execute(const QList<ctkEAHandlerTask>&)
  99. */
  100. void execute(const QList<HandlerTask>& tasks);
  101. void executeInSyncMaster(const QList<HandlerTask>& tasks);
  102. private:
  103. /**
  104. * This method defines if a timeout handling should be used for the
  105. * task.
  106. * @param tasks The event handler dispatch tasks to execute
  107. */
  108. bool useTimeout(const HandlerTask& task);
  109. };
  110. #include "ctkEASyncDeliverTasks.tpp"
  111. #endif // CTKEASYNCDELIVERTASKS_P_H