ASSTIMER.CPP 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <stdlib.h>
  2. #include <dos.h>
  3. #include <conio.h>
  4. #include "timer.h"
  5. #include "globals.h"
  6. #include "typedefs.h"
  7. #include "misc.h"
  8. #include "task_man.h"
  9. #include "error.h"
  10. #define kMaxClients 16
  11. struct CLIENT_INFO
  12. {
  13. TIMER_CLIENT function;
  14. TASK *task; // TASK_MAN timer link
  15. };
  16. static CLIENT_INFO client[kMaxClients];
  17. static int nClients = 0;
  18. static BOOL timerActive = FALSE;
  19. typedef void (* TASK_CLIENT)( TASK * );
  20. void timerRemove( void )
  21. {
  22. if ( timerActive )
  23. {
  24. for (int i = 0; i < nClients; i++)
  25. {
  26. TS_Terminate(client[i].task);
  27. }
  28. TS_Shutdown();
  29. nClients = 0;
  30. dpmiUnlockMemory(FP_OFF(client), sizeof(client));
  31. dpmiUnlockMemory(FP_OFF(&nClients), sizeof(nClients));
  32. timerActive = FALSE;
  33. }
  34. }
  35. void timerInstall( void )
  36. {
  37. if ( !timerActive )
  38. {
  39. timerActive = TRUE;
  40. dpmiLockMemory(FP_OFF(client), sizeof(client));
  41. dpmiLockMemory(FP_OFF(&nClients), sizeof(nClients));
  42. TS_Dispatch();
  43. atexit(timerRemove);
  44. }
  45. }
  46. void timerSetRate( int /* rate */ )
  47. {
  48. // this function not implemented for ASS interface
  49. ThrowError("Call to unimplemented glue function", ES_ERROR);
  50. }
  51. void timerRegisterClient( TIMER_CLIENT timerClient, int rate )
  52. {
  53. if ( nClients < kMaxClients )
  54. {
  55. client[nClients].function = timerClient;
  56. client[nClients].task = TS_ScheduleTask((TASK_CLIENT)timerClient, rate, 1, NULL);
  57. nClients++;
  58. }
  59. }
  60. void timerRemoveClient( TIMER_CLIENT timerClient )
  61. {
  62. for (int i = 0; i < nClients; i++)
  63. {
  64. if ( client[i].function == timerClient )
  65. break;
  66. }
  67. if ( i < nClients )
  68. {
  69. _disable();
  70. nClients--;
  71. client[i] = client[nClients];
  72. _enable();
  73. }
  74. }
  75. void timerSetClientRate( TIMER_CLIENT timerClient, int rate )
  76. {
  77. for (int i = 0; i < nClients; i++)
  78. {
  79. if ( client[i].function == timerClient )
  80. break;
  81. }
  82. if ( i < nClients )
  83. {
  84. TS_SetTaskRate(client[i].task, rate);
  85. }
  86. }