1
0

timermul.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include <bfc/platform/platform.h>
  2. #include "timermul.h"
  3. #include <api.h>
  4. #include <api/config/items/attribs.h>
  5. #include <api/config/items/cfgitem.h>
  6. // {9149C445-3C30-4e04-8433-5A518ED0FDDE}
  7. const GUID uioptions_guid =
  8. { 0x9149c445, 0x3c30, 0x4e04, { 0x84, 0x33, 0x5a, 0x51, 0x8e, 0xd0, 0xfd, 0xde } };
  9. PtrListQuickSorted<MultiplexerServer, MultiplexerServerComparatorTID> servers_tid;
  10. PtrListQuickSorted<MultiplexerServer, MultiplexerServerComparatorTID> servers_mux;
  11. TimerMultiplexer::TimerMultiplexer() {
  12. timerset = 0;
  13. nslices = 0;
  14. resolution = -1;
  15. check_resolution = true;
  16. client = NULL;
  17. curslice = 0;
  18. running_timer = NULL;
  19. uioptions = NULL;
  20. justexited = 0;
  21. firstevent = 1;
  22. resetTimer(50); // initial, is changed for config value on first event
  23. }
  24. TimerMultiplexer::~TimerMultiplexer() {
  25. doShutdown();
  26. }
  27. void TimerMultiplexer::setClient(TimerMultiplexerClient *_client) {
  28. client = _client;
  29. }
  30. void TimerMultiplexer::addTimer(int ms, void *data) {
  31. //if (ms < 0) { DebugString("Timer with negative delay set, ignored coz the time machine service isn't ready yet\n"); }
  32. MultiplexedTimer *t = new MultiplexedTimer(ms, data);
  33. if (ms >= MAX_TIMER_DELAY) {
  34. lptimers.addItem(t);
  35. t->nexttick = Wasabi::Std::getTickCount() + t->ms;
  36. } else {
  37. timers.addItem(t);
  38. if (nslices > 0)
  39. distribute(t);
  40. }
  41. }
  42. void TimerMultiplexer::removeTimer(void *data) {
  43. if (running_timer && running_timer->data == data)
  44. running_timer = NULL;
  45. int i;
  46. for (i=0;i<timers.getNumItems();i++) {
  47. MultiplexedTimer *t = timers.enumItem(i);
  48. if (t->data == data) {
  49. removeFromWheel(t);
  50. timers.removeByPos(i);
  51. delete t;
  52. return;
  53. }
  54. }
  55. for (i=0;i<lptimers.getNumItems();i++) {
  56. MultiplexedTimer *t = lptimers.enumItem(i);
  57. if (t->data == data) {
  58. removeFromLowPrecision(t);
  59. delete t;
  60. return;
  61. }
  62. }
  63. }
  64. void TimerMultiplexer::setResolution(int ms) {
  65. resolution = ms;
  66. }
  67. void TimerMultiplexer::shutdown() {
  68. doShutdown();
  69. }
  70. void TimerMultiplexer::doShutdown() {
  71. timers.deleteAll();
  72. wheel.deleteAll();
  73. lptimers.deleteAll();
  74. if (timerset) {
  75. MultiplexerServer *s = servers_mux.findItem((const wchar_t *)this);
  76. if (s) {
  77. #ifdef WIN32
  78. KillTimer(NULL, s->getId());
  79. #elif defined(LINUX)
  80. #endif
  81. }
  82. timerset = 0;
  83. }
  84. }
  85. void TimerMultiplexer::checkResolution(DWORD now) {
  86. if (check_resolution == true)
  87. {
  88. if (WASABI_API_CONFIG)
  89. {
  90. if (uioptions == NULL)
  91. {
  92. uioptions = WASABI_API_CONFIG->config_getCfgItemByGuid(uioptions_guid);
  93. if (uioptions)
  94. {
  95. ifc_dependent *ui_change = uioptions->getDependencyPtr();
  96. ui_change->dependent_regViewer(this, 1);
  97. }
  98. }
  99. check_resolution = uioptions?false:true;
  100. int nresolution = uioptions ? _intVal(uioptions, L"Multiplexed timers resolution") : DEF_RES;
  101. nresolution = MAX(10, MIN(MAX_TIMER_DELAY/LOW_RES_DIV, nresolution));
  102. if (nresolution != resolution) {
  103. resetTimer(nresolution);
  104. resolution = nresolution;
  105. resetWheel();
  106. }
  107. }
  108. }
  109. }
  110. VOID CALLBACK timerMultiplexerServerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
  111. MultiplexerServer *s = servers_tid.findItem((const wchar_t *)&idEvent);
  112. if (s) s->getMultiplexer()->onServerTimer();
  113. }
  114. void TimerMultiplexer::resetTimer(int newresolution) {
  115. if (timerset) {
  116. MultiplexerServer *s = servers_mux.findItem((const wchar_t *)this);
  117. if (s)
  118. KillTimer(NULL, s->getId());
  119. }
  120. // linux port implements settimer
  121. UINT_PTR id = SetTimer(NULL, 0, newresolution, timerMultiplexerServerProc);
  122. MultiplexerServer *s = servers_mux.findItem((const wchar_t *)this);
  123. if (!s) {
  124. s = new MultiplexerServer(this, (UINT)id);
  125. servers_mux.addItem(s);
  126. servers_tid.addItem(s);
  127. } else {
  128. s->setId(id);
  129. servers_tid.sort();
  130. }
  131. timerset = 1;
  132. }
  133. PtrList<MultiplexedTimer> *TimerMultiplexer::getSlice(int n) {
  134. ASSERT(nslices > 0);
  135. return wheel.enumItem(n % nslices);
  136. }
  137. void TimerMultiplexer::resetWheel() {
  138. wheel.deleteAll();
  139. nslices = MAX_TIMER_DELAY / resolution;
  140. for (int i=0;i<nslices;i++)
  141. wheel.addItem(new PtrList< MultiplexedTimer >);
  142. curslice = 0;
  143. distributeAll();
  144. }
  145. void TimerMultiplexer::distributeAll() {
  146. for (int i=0;i<timers.getNumItems();i++) {
  147. distribute(timers.enumItem(i));
  148. }
  149. }
  150. void TimerMultiplexer::distribute(MultiplexedTimer *t) {
  151. ASSERT(t != NULL);
  152. int delay = t->ms;
  153. int slice = delay / resolution + curslice;
  154. PtrList<MultiplexedTimer> *l = getSlice(slice);
  155. ASSERT(l != NULL);
  156. l->addItem(t);
  157. }
  158. void TimerMultiplexer::onServerTimer() {
  159. justexited = 0;
  160. DWORD now = Wasabi::Std::getTickCount();
  161. checkResolution(now);
  162. runCurSlice(now);
  163. if ((curslice % (nslices/LOW_RES_DIV)) == 0) { // execute low precision timers every MAX_TIMER_DELAY/LOW_RES_DIV
  164. runLowPrecisionTimers(now);
  165. }
  166. if (!justexited) {
  167. curslice++;
  168. curslice %= nslices;
  169. }
  170. justexited = 1;
  171. if (firstevent) {
  172. firstevent = 0;
  173. checkResolution(Wasabi::Std::getTickCount());
  174. }
  175. }
  176. void TimerMultiplexer::runCurSlice(DWORD now) {
  177. //DebugString("Running slice %d\n", curslice);
  178. PtrList<MultiplexedTimer> *slice = getSlice(curslice);
  179. ASSERT(slice != NULL);
  180. // mark them clean
  181. int i;
  182. for (i=0;i<slice->getNumItems();i++)
  183. slice->enumItem(i)->flag = 0;
  184. // run events
  185. int n;
  186. do {
  187. n = 0;
  188. for (i=0;i<slice->getNumItems();i++) {
  189. MultiplexedTimer *t = slice->enumItem(i);
  190. if (t == NULL) break; // do not remove this line even if you think it's useless
  191. // t might have been removed by a previous runTimer in this slice, so see if it's still here and if not, ignore
  192. if (!timers.haveItem(t)) { slice->removeItem(t); i--; continue; }
  193. if (t->flag == 1) continue;
  194. t->flag = 1;
  195. int lastdelay = MAX(0, (int)(now - t->lastmscount));
  196. DWORD last = t->lastmscount;
  197. if (last == 0) last = now;
  198. t->lastmscount = now;
  199. t->lastdelay = lastdelay;
  200. running_timer = t;
  201. runTimer(now, last, t, slice, i);
  202. // -----------------------------------------------------------------------
  203. // WARNING
  204. //
  205. // below this line, you can no longer assume that t is pointing at valid
  206. // memory, because runTimer can eventually call removeTimer
  207. // -----------------------------------------------------------------------
  208. n++;
  209. }
  210. } while (n > 0);
  211. }
  212. void TimerMultiplexer::runTimer(DWORD now, DWORD last, MultiplexedTimer *t, PtrList<MultiplexedTimer> *slice, int pos) {
  213. int nextslice = curslice + t->ms / resolution;
  214. int spent = now - last;
  215. int lost = spent - t->ms;
  216. if (lost > 0) {
  217. t->lost += (float)lost / (float)t->ms;
  218. }
  219. PtrList<MultiplexedTimer> *next = getSlice(nextslice);
  220. ASSERT(next != NULL);
  221. if (slice == next) {
  222. nextslice++;
  223. next = getSlice(nextslice);
  224. }
  225. slice->removeByPos(pos);
  226. next->addItem(t);
  227. int skip = (int)t->lost;
  228. t->lost -= (int)t->lost;
  229. if (client) {
  230. client->onMultiplexedTimer(t->data, skip, t->lastdelay);
  231. // -----------------------------------------------------------------------
  232. // WARNING
  233. //
  234. // below this line, you can no longer assume that t is pointing at valid
  235. // memory, because onMultiplexedTimer can eventually call removeTimer
  236. // -----------------------------------------------------------------------
  237. }
  238. }
  239. void TimerMultiplexer::removeFromWheel(MultiplexedTimer *t) {
  240. for (int i=0;i<nslices;i++) {
  241. PtrList<MultiplexedTimer> *slice = getSlice(i);
  242. for (int j=0;j<slice->getNumItems();j++) {
  243. if (slice->enumItem(j) == t) {
  244. slice->removeByPos(j);
  245. j--;
  246. }
  247. }
  248. }
  249. }
  250. void TimerMultiplexer::removeFromLowPrecision(MultiplexedTimer *t) {
  251. for (int i=0;i<lptimers.getNumItems();i++) {
  252. if (lptimers.enumItem(i) == t) {
  253. lptimers.removeByPos(i);
  254. i--;
  255. }
  256. }
  257. }
  258. void TimerMultiplexer::runLowPrecisionTimers(DWORD now) {
  259. int restart;
  260. do {
  261. restart = 0;
  262. for (int i=0;i<lptimers.getNumItems();i++) {
  263. MultiplexedTimer *t = lptimers.enumItem(i);
  264. if (t->nexttick < now) {
  265. if (client) {
  266. running_timer = t;
  267. t->lost += (now - t->nexttick) / t->ms;
  268. int skip = (int)t->lost;
  269. t->lost -= skip; // remove integer part
  270. DWORD last = t->lastmscount;
  271. t->lastdelay = now-last;
  272. t->lastmscount = now;
  273. t->nexttick = t->nexttick+(t->ms)*(skip+1);
  274. client->onMultiplexedTimer(t->data, skip, t->lastdelay);
  275. // -----------------------------------------------------------------------
  276. // WARNING
  277. //
  278. // below this line, you can no longer assume that t is pointing at valid
  279. // memory, because onMultiplexedTimer can eventually call removeTimer
  280. // -----------------------------------------------------------------------
  281. }
  282. if (running_timer == NULL) { // onMultiplexedTimer called removeTimer
  283. restart =1;
  284. break;
  285. }
  286. }
  287. }
  288. } while (restart);
  289. }
  290. int TimerMultiplexer::getNumTimers() {
  291. return timers.getNumItems();
  292. }
  293. int TimerMultiplexer::getNumTimersLP() {
  294. return lptimers.getNumItems();
  295. }
  296. int TimerMultiplexer::dependentViewer_callback(ifc_dependent *item, const GUID *classguid, int cb, intptr_t param1, intptr_t param2 , void *ptr, size_t ptrlen)
  297. {
  298. if (param1 == CfgItem::Event_ATTRIBUTE_CHANGED)
  299. {
  300. check_resolution=true;
  301. }
  302. else if (param1 == CfgItem::Event_ATTRIBUTE_REMOVED)
  303. {
  304. uioptions=0;
  305. }
  306. return 1;
  307. }
  308. #define CBCLASS TimerMultiplexer
  309. START_DISPATCH;
  310. CB(DEPENDENTVIEWER_CALLBACK, dependentViewer_callback)
  311. END_DISPATCH;
  312. #undef CBCLASS