Przeglądaj źródła

ncd: modules: blocker: Implement initial_state argument.

Ambroz Bizjak 11 lat temu
rodzic
commit
cce6adc039
2 zmienionych plików z 54 dodań i 9 usunięć
  1. 19 9
      ncd/modules/blocker.c
  2. 35 0
      ncd/tests/blocker.ncd

+ 19 - 9
ncd/modules/blocker.c

@@ -31,10 +31,11 @@
  * Blocker module. Provides a statement that blocks when initialized, and which can be blocked
  * and unblocked from outside.
  * 
- * Synopsis: blocker()
- * Description: provides blocking operations. Initially the blocking state is down (but this statement
- *              does not block). On deinitialization, waits for all corresponding use() statements
- *              to die before dying itself.
+ * Synopsis: blocker([string initial_state])
+ * Description: provides blocking operations. On deinitialization, waits for all corresponding
+ *              use() statements to die before dying itself.
+ *              The optional boolean argument initial_state specifies the initial up-state
+ *              of the blocker. If not given, the default is false (not up).
  * Variables: string (empty) - the up-state (false or true).
  * 
  * Synopsis: blocker::up()
@@ -107,21 +108,30 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
     struct instance *o = vo;
     o->i = i;
     
-    // check arguments
-    if (!NCDVal_ListRead(params->args, 0)) {
+    // read arguments
+    NCDValRef initial_state = NCDVal_NewInvalid();
+    if (!NCDVal_ListRead(params->args, 0) &&
+        !NCDVal_ListRead(params->args, 1, &initial_state)
+    ) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         goto fail0;
     }
     
+    // get the initial state
+    o->up = 0;
+    if (!NCDVal_IsInvalid(initial_state)) {
+        if (!ncd_read_boolean(initial_state, &o->up)) {
+            ModuleLog(o->i, BLOG_ERROR, "bad initial_state argument");
+            goto fail0;
+        }
+    }
+    
     // init users list
     LinkedList1_Init(&o->users);
     
     // init rdownups list
     LinkedList0_Init(&o->rdownups_list);
     
-    // set not up
-    o->up = 0;
-    
     // set not dying
     o->dying = 0;
     

+ 35 - 0
ncd/tests/blocker.ncd

@@ -0,0 +1,35 @@
+process main {
+    If (@true) {
+        value({}) list;
+        var("0") i;
+        blocker() blk;
+        blk->up();
+        blk->use();
+        num_lesser(i, "100") do_more;
+        If (do_more) {
+            list->insert(i);
+            num_add(i, "1") new_i;
+            i->set(new_i);
+            blk->downup();
+        };
+        val_equal(list.length, "100") a;
+        assert(a);
+    };
+    
+    If (@true) {
+        blocker() blk;
+        assert_false(blk);
+    };
+    
+    If (@true) {
+        blocker(@false) blk;
+        assert_false(blk);
+    };
+    
+    If (@true) {
+        blocker(@true) blk;
+        assert(blk);
+    };
+    
+    exit("0");
+}