qThreadWrapper.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "qthreadwrapper.h"
  2. #ifdef _SYMBIAN
  3. #include <QObject>
  4. class SymbianTimer : public QObject
  5. {
  6. public:
  7. SymbianTimer(ThreadFunction func, ThreadArgument arg):
  8. m_threadFunction(func),
  9. m_threadArgument(arg)
  10. {
  11. }
  12. void timerEvent(QTimerEvent* event)
  13. {
  14. m_threadFunction(m_threadArgument);
  15. }
  16. void setTimerID(int id)
  17. {
  18. m_timerID = id;
  19. }
  20. inline int getTimerID() const
  21. {
  22. return m_timerID;
  23. }
  24. private:
  25. ThreadFunction m_threadFunction;
  26. void* m_threadArgument;
  27. int m_timerID;
  28. };
  29. Thread createThread(ThreadFunction func, ThreadArgument arg)
  30. {
  31. SymbianTimer* timer = new SymbianTimer(func, arg);
  32. int id = timer->startTimer(10);
  33. timer->setTimerID(id);
  34. return timer;
  35. }
  36. void sleepThread(Thread thread, unsigned long usec)
  37. {
  38. }
  39. void destroyThread(Thread thread)
  40. {
  41. Q_ASSERT(thread);
  42. SymbianTimer* timer = (SymbianTimer*)thread;
  43. timer->killTimer(timer->getTimerID());
  44. }
  45. #else
  46. #include <QThread>
  47. #include "qDebugWrapper.h"
  48. // QThread Implementation
  49. class ThreadIpml : public QThread
  50. {
  51. public:
  52. ThreadIpml(ThreadFunction func, ThreadArgument arg):
  53. QThread(0),
  54. m_threadFunction(func),
  55. m_threadArgument(arg)
  56. {}
  57. void run()
  58. {
  59. m_threadFunction(m_threadArgument);
  60. }
  61. void sleep(unsigned long usec)
  62. {
  63. QThread::usleep(usec);
  64. }
  65. private:
  66. ThreadFunction m_threadFunction;
  67. void* m_threadArgument;
  68. };
  69. Thread createThread(ThreadFunction func, ThreadArgument arg)
  70. {
  71. ThreadIpml* qThread = new ThreadIpml(func, arg);
  72. qThread->start(QThread::NormalPriority);
  73. return (Thread)qThread;
  74. }
  75. void sleepThread(Thread thread, unsigned long usec)
  76. {
  77. ThreadIpml* qThread = (ThreadIpml*)thread;
  78. qThread->sleep(usec);
  79. }
  80. void destroyThread(Thread thread)
  81. {
  82. Q_ASSERT(thread);
  83. ThreadIpml* qThread = (ThreadIpml*)thread;
  84. if (!qThread->wait())
  85. {
  86. Q_PRINT("Timeout! Terminating...");
  87. qThread->terminate();
  88. }
  89. }
  90. #endif