UNIXTimer.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*****************************************************************************/
  2. /* ここから */
  3. /*****************************************************************************/
  4. #include <unistd.h>
  5. #include <signal.h>
  6. #include "UNIXTimerP.h"
  7. #include "etc.h"
  8. /*****************************************************************************/
  9. /* メンバ関数の定義 */
  10. /*****************************************************************************/
  11. /*---------------------------------------------------------------------------*/
  12. /* コールバック関数 */
  13. /*---------------------------------------------------------------------------*/
  14. /* pointed_unix_timer の値はいつ変化するかわからないので,最適化を禁止する */
  15. volatile static UNIXTimer pointed_unix_timer = NULL;
  16. static void UNIXTimer_CallBackFunction(int argument)
  17. {
  18. int ret;
  19. /*
  20. * static なフラグを立てて,コールバック関数からコールバック関数が
  21. * 呼ばれたのを検出するのが必要かも.
  22. */
  23. if (pointed_unix_timer == NULL) return;
  24. if (!pointed_unix_timer->timer_on) return;
  25. if (pointed_unix_timer->pause) {
  26. pointed_unix_timer->interrupt_in_pause++;
  27. } else {
  28. if (pointed_unix_timer->interrupt_in_pause == 0)
  29. pointed_unix_timer->interrupt_in_pause = 1;
  30. while (pointed_unix_timer->interrupt_in_pause > 0) {
  31. pointed_unix_timer->interrupt_in_pause--;
  32. if (pointed_unix_timer->callback != NULL) {
  33. /*
  34. * コールバック関数の中から UNIXTimer_Unpause() などが呼ばれて,
  35. * そこからさらにコールバック関数が呼ばれたりしたときのために,
  36. * ポーズする.
  37. */
  38. pointed_unix_timer->pause++;
  39. ret = (*pointed_unix_timer->callback)(pointed_unix_timer->parameter);
  40. pointed_unix_timer->pause--;
  41. /*
  42. * コールバック関数の中で UNIXTimer_* 関連の関数が呼び出されると,
  43. * ここで UNIXTimer オブジェクトの状態が変わってしまう可能性がある.
  44. */
  45. /*
  46. * 以下の処理は同じことを関数の先頭でやっているので,
  47. * 最適化されて削除されないように注意すること.
  48. */
  49. if (pointed_unix_timer == NULL) return;
  50. if (!pointed_unix_timer->timer_on) return;
  51. if (ret) {
  52. pointed_unix_timer->interrupt_in_pause = 0;
  53. UNIXTimer_OFF(pointed_unix_timer);
  54. return;
  55. }
  56. /*
  57. * コールバック関数中で,ポーズの状態が切り替わってしまった
  58. * ときのため.
  59. * 以下の処理は同じようなことをちょっと前にやっているので,
  60. * 最適化されて削除されないように注意すること.
  61. */
  62. if (pointed_unix_timer->pause) break;
  63. }
  64. }
  65. }
  66. if (pointed_unix_timer->auto_preset)
  67. UNIXTimer_ON(pointed_unix_timer);
  68. else
  69. UNIXTimer_OFF(pointed_unix_timer);
  70. return;
  71. }
  72. /*---------------------------------------------------------------------------*/
  73. /* タイマの ON, OFF */
  74. /*---------------------------------------------------------------------------*/
  75. /*
  76. * このあたりの処理は,処理中にタイマ割り込みが発生しないかどうかを
  77. * 常に注意すること.でないと不可解なバグの原因になる.
  78. */
  79. int UNIXTimer_ON(UNIXTimer unix_timer)
  80. {
  81. int t;
  82. unix_timer->timer_on = 1;
  83. pointed_unix_timer = unix_timer;
  84. /*
  85. * SIGALRM を使用するので,sleep() 中にアラームが起きた場合には,
  86. * コールバック関数からの復帰後に sleep() の残り時間は継続されない.
  87. */
  88. signal(SIGALRM, UNIXTimer_CallBackFunction);
  89. #if 0 /* use ualarm() */
  90. /* ualarm(); の引数に0を用いてはダメなので */
  91. t = unix_timer->interval * 1000;
  92. if (t < 1) t = 1;
  93. ualarm(t, 0);
  94. #else /* use alarm() */
  95. /* alarm(); の引数に0を用いてはダメなので */
  96. t = unix_timer->interval / 1000;
  97. if (t < 1) t = 1;
  98. alarm(t);
  99. #endif
  100. return (0);
  101. }
  102. int UNIXTimer_OFF(UNIXTimer unix_timer)
  103. {
  104. alarm(0); /* タイマを無効にする */
  105. unix_timer->timer_on = 0;
  106. pointed_unix_timer = NULL;
  107. return (0);
  108. }
  109. int UNIXTimer_IsON(UNIXTimer unix_timer)
  110. { return (unix_timer->timer_on != 0); }
  111. int UNIXTimer_IsOFF(UNIXTimer unix_timer)
  112. { return (unix_timer->timer_on == 0); }
  113. /*---------------------------------------------------------------------------*/
  114. /* 一時停止 */
  115. /*---------------------------------------------------------------------------*/
  116. /*
  117. * 関数の先頭と末尾を Pause, Unpause でくくるばあいなどは,
  118. * 関数から関数を呼び出したときに,2重 Pause, Unpause が起きるので
  119. * 注意すること.(ただし,ポーズはネストできる)
  120. */
  121. /*
  122. * ポーズはネストされるので,UNIXTimer_Unpause() を安易に繰り返し呼んだり
  123. * しないように注意すること.
  124. */
  125. /*
  126. * このあたりの処理は,処理中にタイマ割り込みが発生しないかどうかを
  127. * 常に注意すること.でないと不可解なバグの原因になる.
  128. */
  129. int UNIXTimer_Pause(UNIXTimer unix_timer)
  130. {
  131. unix_timer->pause++; /* ポーズはネストできる */
  132. return (0);
  133. }
  134. int UNIXTimer_Unpause(UNIXTimer unix_timer)
  135. {
  136. if (unix_timer->pause == 0)
  137. Wonx_Error("UNIXTimer_Unpause", "Duplicated unpause.");
  138. if (unix_timer->pause == 1) {
  139. if (unix_timer->interrupt_in_pause > 0) {
  140. /*
  141. * 万が一,ここでタイマ割り込みが発生しても,
  142. * unix_timer->pause は 1 なので,コールバック関数は呼ばれないので,
  143. * 問題無し.
  144. * オートプリセットならば,べつに問題無し.
  145. * オートプリセットでなければ timer_on フラグが落ちるので,問題無い.
  146. */
  147. /*
  148. * 処理中にタイマ割り込みが発生すると面倒なので,
  149. * タイマをいったん無効にする.
  150. */
  151. alarm(0);
  152. /*
  153. * 状態値を変更する直前・直後で割り込みが入ると面倒なので,
  154. * 割り込みをOFFにしてからデクリメントする.
  155. */
  156. /* コールバック関数を呼ぶ前に,pause フラグを無効にする */
  157. unix_timer->pause--; /* ポーズはネストできる */
  158. /* コールバック関数の呼び出し */
  159. UNIXTimer_CallBackFunction(0);
  160. }
  161. } else {
  162. unix_timer->pause--; /* ポーズはネストできる */
  163. }
  164. return (0);
  165. }
  166. int UNIXTimer_IsPause(UNIXTimer unix_timer)
  167. { return (unix_timer->pause != 0); }
  168. /*---------------------------------------------------------------------------*/
  169. /* オートプリセット */
  170. /*---------------------------------------------------------------------------*/
  171. int UNIXTimer_SetAutoPreset(UNIXTimer unix_timer)
  172. { return (unix_timer->auto_preset = 1); }
  173. int UNIXTimer_ResetAutoPreset(UNIXTimer unix_timer)
  174. { return (unix_timer->auto_preset = 0); }
  175. int UNIXTimer_IsAutoPreset(UNIXTimer unix_timer)
  176. { return (unix_timer->auto_preset != 0); }
  177. /*---------------------------------------------------------------------------*/
  178. /* インターバル */
  179. /*---------------------------------------------------------------------------*/
  180. int UNIXTimer_GetInterval(UNIXTimer unix_timer)
  181. { return (unix_timer->interval); }
  182. int UNIXTimer_SetInterval(UNIXTimer unix_timer, int interval)
  183. { return (unix_timer->interval = interval); }
  184. /*---------------------------------------------------------------------------*/
  185. /* コールバック関数の呼び出し時のパラメータ */
  186. /*---------------------------------------------------------------------------*/
  187. void * UNIXTimer_GetParameter(UNIXTimer unix_timer)
  188. { return (unix_timer->parameter); }
  189. void * UNIXTimer_SetParameter(UNIXTimer unix_timer, void * parameter)
  190. { return (unix_timer->parameter = parameter); }
  191. /*---------------------------------------------------------------------------*/
  192. /* コールバック関数の取得・登録 */
  193. /*---------------------------------------------------------------------------*/
  194. UNIXTimerCallBack UNIXTimer_GetCallBack(UNIXTimer unix_timer)
  195. { return (unix_timer->callback); }
  196. UNIXTimerCallBack UNIXTimer_SetCallBack(UNIXTimer unix_timer,
  197. UNIXTimerCallBack callback)
  198. { return (unix_timer->callback = callback); }
  199. /*---------------------------------------------------------------------------*/
  200. /* オブジェクトの作成 */
  201. /*---------------------------------------------------------------------------*/
  202. UNIXTimer UNIXTimer_Create(int auto_preset, int interval, void * parameter,
  203. UNIXTimerCallBack callback)
  204. {
  205. UNIXTimer unix_timer;
  206. unix_timer = (UNIXTimer)malloc(sizeof(_UNIXTimer));
  207. if (unix_timer == NULL)
  208. Wonx_Error("UNIXTimer_Create", "Cannot allocate memory.");
  209. unix_timer->timer_on = 0;
  210. unix_timer->pause = 0;
  211. unix_timer->interrupt_in_pause = 0;
  212. unix_timer->auto_preset = 0;
  213. unix_timer->interval = 0;
  214. unix_timer->parameter = NULL;
  215. unix_timer->callback = NULL;
  216. if (auto_preset) UNIXTimer_SetAutoPreset(unix_timer);
  217. else UNIXTimer_ResetAutoPreset(unix_timer);
  218. UNIXTimer_SetInterval(unix_timer, interval);
  219. UNIXTimer_SetParameter(unix_timer, parameter);
  220. UNIXTimer_SetCallBack(unix_timer, callback);
  221. return (unix_timer);
  222. }
  223. /*---------------------------------------------------------------------------*/
  224. /* オブジェクトの削除 */
  225. /*---------------------------------------------------------------------------*/
  226. /*
  227. * このあたりの処理は,処理中にタイマ割り込みが発生しないかどうかを
  228. * 常に注意すること.でないと不可解なバグの原因になる.
  229. */
  230. UNIXTimer UNIXTimer_Destroy(UNIXTimer unix_timer)
  231. {
  232. if (unix_timer == NULL)
  233. Wonx_Error("UNIXTimer_Destroy", "Object is not created.");
  234. UNIXTimer_OFF(unix_timer);
  235. free(unix_timer);
  236. return (NULL);
  237. }
  238. /*****************************************************************************/
  239. /* ここまで */
  240. /*****************************************************************************/
  241. /*****************************************************************************/
  242. /* End of File. */
  243. /*****************************************************************************/