UNIXTimer.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. unix_timer->timer_on = 1;
  82. pointed_unix_timer = unix_timer;
  83. ualarm(unix_timer->interval, 0);
  84. return (0);
  85. }
  86. int UNIXTimer_OFF(UNIXTimer unix_timer)
  87. {
  88. alarm(0); /* タイマを無効にする */
  89. unix_timer->timer_on = 0;
  90. pointed_unix_timer = NULL;
  91. return (0);
  92. }
  93. int UNIXTimer_IsON(UNIXTimer unix_timer)
  94. { return (unix_timer->timer_on != 0); }
  95. int UNIXTimer_IsOFF(UNIXTimer unix_timer)
  96. { return (unix_timer->timer_on == 0); }
  97. /*---------------------------------------------------------------------------*/
  98. /* 一時停止 */
  99. /*---------------------------------------------------------------------------*/
  100. /*
  101. * 関数の先頭と末尾を Pause, Unpause でくくるばあいなどは,
  102. * 関数から関数を呼び出したときに,2重 Pause, Unpause が起きるので
  103. * 注意すること.(ただし,ポーズはネストできる)
  104. */
  105. /*
  106. * ポーズはネストされるので,UNIXTimer_Unpause() を安易に繰り返し呼んだり
  107. * しないように注意すること.
  108. */
  109. /*
  110. * このあたりの処理は,処理中にタイマ割り込みが発生しないかどうかを
  111. * 常に注意すること.でないと不可解なバグの原因になる.
  112. */
  113. int UNIXTimer_Pause(UNIXTimer unix_timer)
  114. {
  115. unix_timer->pause++; /* ポーズはネストできる */
  116. return (0);
  117. }
  118. int UNIXTimer_Unpause(UNIXTimer unix_timer)
  119. {
  120. if (unix_timer->pause == 0)
  121. Wonx_Error("UNIXTimer_Unpause", "Duplicated unpause.");
  122. if (unix_timer->pause == 1) {
  123. if (unix_timer->interrupt_in_pause > 0) {
  124. /*
  125. * 万が一,ここでタイマ割り込みが発生しても,
  126. * unix_timer->pause は 1 なので,コールバック関数は呼ばれないので,
  127. * 問題無し.
  128. * オートプリセットならば,べつに問題無し.
  129. * オートプリセットでなければ timer_on フラグが落ちるので,問題無い.
  130. */
  131. /*
  132. * 処理中にタイマ割り込みが発生すると面倒なので,
  133. * タイマをいったん無効にする.
  134. */
  135. alarm(0);
  136. /*
  137. * 状態値を変更する直前・直後で割り込みが入ると面倒なので,
  138. * 割り込みをOFFにしてからデクリメントする.
  139. */
  140. /* コールバック関数を呼ぶ前に,pause フラグを無効にする */
  141. unix_timer->pause--; /* ポーズはネストできる */
  142. /* コールバック関数の呼び出し */
  143. UNIXTimer_CallBackFunction(0);
  144. }
  145. } else {
  146. unix_timer->pause--; /* ポーズはネストできる */
  147. }
  148. return (0);
  149. }
  150. int UNIXTimer_IsPause(UNIXTimer unix_timer)
  151. { return (unix_timer->pause != 0); }
  152. /*---------------------------------------------------------------------------*/
  153. /* オートプリセット */
  154. /*---------------------------------------------------------------------------*/
  155. int UNIXTimer_SetAutoPreset(UNIXTimer unix_timer)
  156. { return (unix_timer->auto_preset = 1); }
  157. int UNIXTimer_ResetAutoPreset(UNIXTimer unix_timer)
  158. { return (unix_timer->auto_preset = 0); }
  159. int UNIXTimer_IsAutoPreset(UNIXTimer unix_timer)
  160. { return (unix_timer->auto_preset != 0); }
  161. /*---------------------------------------------------------------------------*/
  162. /* インターバル */
  163. /*---------------------------------------------------------------------------*/
  164. int UNIXTimer_GetInterval(UNIXTimer unix_timer)
  165. { return (unix_timer->interval); }
  166. int UNIXTimer_SetInterval(UNIXTimer unix_timer, int interval)
  167. {
  168. /* ualarm(); の引数に0を用いてはダメなので */
  169. if (interval < 1) interval = 1;
  170. return (unix_timer->interval = interval);
  171. }
  172. /*---------------------------------------------------------------------------*/
  173. /* コールバック関数の呼び出し時のパラメータ */
  174. /*---------------------------------------------------------------------------*/
  175. void * UNIXTimer_GetParameter(UNIXTimer unix_timer)
  176. { return (unix_timer->parameter); }
  177. void * UNIXTimer_SetParameter(UNIXTimer unix_timer, void * parameter)
  178. { return (unix_timer->parameter = parameter); }
  179. /*---------------------------------------------------------------------------*/
  180. /* コールバック関数の取得・登録 */
  181. /*---------------------------------------------------------------------------*/
  182. UNIXTimerCallBack UNIXTimer_GetCallBack(UNIXTimer unix_timer)
  183. { return (unix_timer->callback); }
  184. UNIXTimerCallBack UNIXTimer_SetCallBack(UNIXTimer unix_timer,
  185. UNIXTimerCallBack callback)
  186. { return (unix_timer->callback = callback); }
  187. /*---------------------------------------------------------------------------*/
  188. /* オブジェクトの作成 */
  189. /*---------------------------------------------------------------------------*/
  190. UNIXTimer UNIXTimer_Create(int auto_preset, int interval, void * parameter,
  191. UNIXTimerCallBack callback)
  192. {
  193. UNIXTimer unix_timer;
  194. unix_timer = (UNIXTimer)malloc(sizeof(_UNIXTimer));
  195. if (unix_timer == NULL)
  196. Wonx_Error("UNIXTimer_Create", "Cannot allocate memory.");
  197. unix_timer->timer_on = 0;
  198. unix_timer->pause = 0;
  199. unix_timer->interrupt_in_pause = 0;
  200. unix_timer->auto_preset = 0;
  201. unix_timer->interval = 0;
  202. unix_timer->parameter = NULL;
  203. unix_timer->callback = NULL;
  204. if (auto_preset) UNIXTimer_SetAutoPreset(unix_timer);
  205. else UNIXTimer_ResetAutoPreset(unix_timer);
  206. UNIXTimer_SetInterval(unix_timer, interval);
  207. UNIXTimer_SetParameter(unix_timer, parameter);
  208. UNIXTimer_SetCallBack(unix_timer, callback);
  209. /*
  210. * SIGALRM を使用するので,sleep() 中にアラームが起きた場合には,
  211. * コールバック関数からの復帰後に sleep() の残り時間は継続されない.
  212. */
  213. signal(SIGALRM, UNIXTimer_CallBackFunction);
  214. return (unix_timer);
  215. }
  216. /*---------------------------------------------------------------------------*/
  217. /* オブジェクトの削除 */
  218. /*---------------------------------------------------------------------------*/
  219. /*
  220. * このあたりの処理は,処理中にタイマ割り込みが発生しないかどうかを
  221. * 常に注意すること.でないと不可解なバグの原因になる.
  222. */
  223. UNIXTimer UNIXTimer_Destroy(UNIXTimer unix_timer)
  224. {
  225. if (unix_timer == NULL)
  226. Wonx_Error("UNIXTimer_Destroy", "Object is not created.");
  227. UNIXTimer_OFF(unix_timer);
  228. free(unix_timer);
  229. return (NULL);
  230. }
  231. /*****************************************************************************/
  232. /* ここまで */
  233. /*****************************************************************************/
  234. /*****************************************************************************/
  235. /* End of File. */
  236. /*****************************************************************************/