Armin Rigo avatar Armin Rigo committed b21237b

(sleepms)

Comments (0)

Files changed (2)

demo/list_transaction.duh

 
 (setq lst (list 0))
-(defun f (n)
+(defun g (n)
     (set lst 0 (+ (get lst 0) 1))
-    (if (< n 1000)
+    (if (< n 10)
         (transaction f (+ n 1))
-      (if (< (get lst 0) 2000)
+      (sleepms 20)
+      (if (< (get lst 0) 20)
           (print (quote not-enough))
         (print (quote ok)))))
-(transaction f 0)
-(transaction f 0)
+(defun f (n)
+  (g n))
+(transaction f 1)
+(transaction f 1)
 #include "duhton.h"
+#include <sys/select.h>
 
 
 DuObject *Du_Progn(DuObject *cons, DuObject *locals)
     return Du_None;
 }
 
+DuObject *du_sleepms(DuObject *cons, DuObject *locals)
+{
+    if (cons == Du_None || _DuCons_NEXT(cons) != Du_None)
+        Du_FatalError("sleepms: expected one argument");
+    DuObject *obj = Du_Eval(_DuCons_CAR(cons), locals);
+    int ms = DuInt_AsInt(obj);
+    Du_DECREF(obj);
+
+    struct timeval t;
+    t.tv_sec = ms / 1000;
+    t.tv_usec = (ms % 1000) * 1000;
+    select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
+
+    Du_INCREF(Du_None);
+    return Du_None;
+}
+
 
 DuObject *Du_Globals;
 
     DuFrame_SetBuiltinMacro(Du_Globals, "cdr", du_cdr);
     DuFrame_SetBuiltinMacro(Du_Globals, "not", du_not);
     DuFrame_SetBuiltinMacro(Du_Globals, "transaction", du_transaction);
+    DuFrame_SetBuiltinMacro(Du_Globals, "sleepms", du_sleepms);
     DuFrame_SetSymbolStr(Du_Globals, "None", Du_None);
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.