GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/lib/event.c Lines: 47 47 100.0 %
Date: 2020-09-22 13:02:15 Branches: 3 4 75.0 %

Line Branch Exec Source
1
#include "gwion_util.h"
2
#include "gwion_ast.h"
3
#include "gwion_env.h"
4
#include "vm.h"
5
#include "gwion.h"
6
#include "instr.h"
7
#include "object.h"
8
#include "operator.h"
9
#include "import.h"
10
#include "gwi.h"
11
12
8
static CTOR(event_ctor) {
13
8
  EV_SHREDS(o) = new_vector(shred->info->mp);
14
8
}
15
16
14
static DTOR(event_dtor) {
17
14
  free_vector(shred->info->mp, EV_SHREDS(o));
18
14
}
19
20
6
static OP_CHECK(opck_eventwait) {
21
6
  return env->gwion->type[et_int];
22
}
23
24
5
static INSTR(EventWait) {
25
5
  POP_REG(shred, SZ_FLOAT);
26
5
  const M_Object event = *(M_Object*)REG(-SZ_INT);
27
5
  shreduler_remove(shred->tick->shreduler, shred, 0);
28
5
  const Vector v = EV_SHREDS(event);
29
5
  vector_add(v, (vtype)shred);
30
5
  *(m_int*)REG(-SZ_INT) = 1;
31
5
  _release(event, shred);
32
5
}
33
34
1
static MFUN(event_signal) {
35
1
  const Vector v = EV_SHREDS(o);
36
1
  const VM_Shred sh = (VM_Shred)vector_front(v);
37
1
  if(sh) {
38
1
    shredule(sh->tick->shreduler, sh, GWION_EPSILON);
39
1
    vector_rem(v, 0);
40
  }
41
1
}
42
43
7
ANN void broadcast(const M_Object o) {
44
12
  for(m_uint i = 0; i < vector_size(EV_SHREDS(o)); i++) {
45
5
    const VM_Shred sh = (VM_Shred)vector_at(EV_SHREDS(o), i);
46
5
    shredule(sh->tick->shreduler, sh, GWION_EPSILON);
47
  }
48
7
  vector_clear(EV_SHREDS(o));
49
7
}
50
51
3
static MFUN(event_broadcast) {
52
3
  broadcast(o);
53
3
}
54
55
712
GWION_IMPORT(event) {
56
712
  const Type t_event = gwi_class_ini(gwi, "Event", "Object");
57
712
  gwi_class_xtor(gwi, event_ctor, event_dtor);
58
712
  gwi->gwion->type[et_event] = t_event; // use func
59
60
712
  GWI_BB(gwi_item_ini(gwi, "@internal", "@shreds"))
61
712
  GWI_BB(gwi_item_end(gwi, ae_flag_member, NULL))
62
712
  GWI_BB(gwi_func_ini(gwi, "void", "signal"))
63
712
  GWI_BB(gwi_func_end(gwi, event_signal, ae_flag_none))
64
712
  GWI_BB(gwi_func_ini(gwi, "void", "broadcast"))
65
712
  GWI_BB(gwi_func_end(gwi, event_broadcast, ae_flag_none))
66
712
  GWI_BB(gwi_class_end(gwi))
67
712
  GWI_BB(gwi_oper_ini(gwi, "nonnull Event", "@now", "int"))
68
712
  _CHECK_OP("=>", eventwait, EventWait)
69
712
  return GW_OK;
70
}