diff ios/dw.m @ 2584:2acc7ba5dea0

Add HTIMER type and change dw_timer_() functions to use it. On existing platforms, HTIMER will be int for now allowing API backward compatibility. On new platforms, iOS and Android, these will be pointers to the timer objects. This allows simplifying the code paths on those platforms. May change it on other platforms such as Mac in a future major version.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 23 May 2021 21:39:25 +0000
parents 1d2f5c4eccc5
children 461006160d11
line wrap: on
line diff
--- a/ios/dw.m	Sun May 23 09:43:04 2021 +0000
+++ b/ios/dw.m	Sun May 23 21:39:25 2021 +0000
@@ -9361,9 +9361,6 @@
     return FALSE;
 }
 
-#define DW_TIMER_MAX 64
-static NSTimer *DWTimers[DW_TIMER_MAX];
-
 /*
  * Add a callback to a timer event.
  * Parameters:
@@ -9373,27 +9370,19 @@
  * Returns:
  *       Timer ID for use with dw_timer_disconnect(), 0 on error.
  */
-DW_FUNCTION_DEFINITION(dw_timer_connect, int, int interval, void *sigfunc, void *data)
+DW_FUNCTION_DEFINITION(dw_timer_connect, HTIMER, int interval, void *sigfunc, void *data)
 DW_FUNCTION_ADD_PARAM3(interval, sigfunc, data)
-DW_FUNCTION_RETURN(dw_timer_connect, int)
+DW_FUNCTION_RETURN(dw_timer_connect, HTIMER)
 DW_FUNCTION_RESTORE_PARAM3(interval, int, sigfunc, void *, data, void *)
 {
-    int z, retval = 0;
-
-    for(z=0;z<DW_TIMER_MAX;z++)
-    {
-        if(!DWTimers[z])
-        {
-            break;
-        }
-    }
-
-    if(sigfunc && !DWTimers[z])
+    HTIMER retval = 0;
+
+    if(sigfunc)
     {
         NSTimeInterval seconds = (double)interval / 1000.0;
-        NSTimer *thistimer = DWTimers[z] = [NSTimer scheduledTimerWithTimeInterval:seconds target:DWHandler selector:@selector(runTimer:) userInfo:nil repeats:YES];
-        _dw_new_signal(0, thistimer, z+1, sigfunc, NULL, data);
-        retval = z+1;
+
+        retval = [NSTimer scheduledTimerWithTimeInterval:seconds target:DWHandler selector:@selector(runTimer:) userInfo:nil repeats:YES];
+        _dw_new_signal(0, retval, 0, sigfunc, NULL, data);
     }
     DW_FUNCTION_RETURN_THIS(retval);
 }
@@ -9403,25 +9392,23 @@
  * Parameters:
  *       id: Timer ID returned by dw_timer_connect().
  */
-DW_FUNCTION_DEFINITION(dw_timer_disconnect, void, int timerid)
+DW_FUNCTION_DEFINITION(dw_timer_disconnect, void, HTIMER timerid)
 DW_FUNCTION_ADD_PARAM1(timerid)
 DW_FUNCTION_NO_RETURN(dw_timer_disconnect)
-DW_FUNCTION_RESTORE_PARAM1(timerid, int)
+DW_FUNCTION_RESTORE_PARAM1(timerid, HTIMER)
 {
     SignalHandler *prev = NULL, *tmp = DWRoot;
-    NSTimer *thistimer;
 
     /* 0 is an invalid timer ID */
-    if(timerid > 0 && timerid < DW_TIMER_MAX && DWTimers[timerid-1])
-    {
-        thistimer = DWTimers[timerid-1];
-        DWTimers[timerid-1] = nil;
+    if(timerid)
+    {
+        NSTimer *thistimer = timerid;
 
         [thistimer invalidate];
 
         while(tmp)
         {
-            if(tmp->id == timerid && tmp->window == thistimer)
+            if(tmp->window == thistimer)
             {
                 if(prev)
                 {