~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/browser_gc.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <gc.h>
 
3
#include <assert.h>
 
4
#include <emscripten.h>
 
5
 
 
6
void *global;
 
7
 
 
8
int freed = 0;
 
9
 
 
10
void finalizer(void *ptr, void *arg) {
 
11
  printf("finalizing %d (global == %d)\n", (int)arg, ptr == global);
 
12
  freed++;
 
13
  if (ptr == global) global = 0;
 
14
}
 
15
 
 
16
int stage = 0;
 
17
float start = 0;
 
18
 
 
19
void waiter(void*) {
 
20
  if (stage == 0) { // wait for a while, see no GCing
 
21
    assert(global);
 
22
    if (emscripten_get_now() - start > 2100) {
 
23
      GC_MALLOC(1024*1024*2); // allocate enough to trigger a GC
 
24
      start = emscripten_get_now();
 
25
      stage = 1;
 
26
      printf("stage 1\n");
 
27
    }
 
28
  } else if (stage == 1) {
 
29
    assert(global);
 
30
    if (freed > 0) {
 
31
      GC_FREE(global);
 
32
      stage = 2;
 
33
      start = emscripten_get_now();
 
34
      printf("stage 2\n");
 
35
    }
 
36
    if (emscripten_get_now() - start > 2100) {
 
37
      printf("fail, too much time passed (a)\n");
 
38
      return;
 
39
    }
 
40
  } else if (stage == 2) {
 
41
    if (emscripten_get_now() - start > 2100) { // wait and see that no gc'ing happens yet
 
42
      GC_MALLOC(1024*1024*2); // allocate enough to trigger a GC
 
43
      stage = 3;
 
44
      start = emscripten_get_now();
 
45
      printf("stage 3\n");
 
46
    }
 
47
  } else if (stage == 3) {
 
48
    assert(!global);
 
49
    if (freed == 5) {
 
50
      printf("Ok.\n");
 
51
      int result = 1;
 
52
      REPORT_RESULT();
 
53
      return;
 
54
    }
 
55
    if (emscripten_get_now() - start > 2100) {
 
56
      printf("fail, too much time passed (b)\n");
 
57
      return;
 
58
    }
 
59
  }
 
60
 
 
61
  emscripten_async_call(waiter, NULL, 100);
 
62
}
 
63
 
 
64
int main() {
 
65
  start = emscripten_get_now();
 
66
 
 
67
  GC_INIT();
 
68
 
 
69
  void *local, *local2, *local3, *local4;
 
70
 
 
71
  global = GC_MALLOC(12);
 
72
  GC_REGISTER_FINALIZER_NO_ORDER(global, finalizer, 0, 0, 0);
 
73
  local = GC_MALLOC(12);
 
74
  GC_REGISTER_FINALIZER_NO_ORDER(local, finalizer, (void*)1, 0, 0);
 
75
  local2 = GC_MALLOC_ATOMIC(12);
 
76
  GC_REGISTER_FINALIZER_NO_ORDER(local2, finalizer, (void*)2, 0, 0);
 
77
  local3 = GC_MALLOC(12);
 
78
  GC_REGISTER_FINALIZER_NO_ORDER(local3, finalizer, (void*)3, 0, 0);
 
79
  local4 = GC_MALLOC(12);
 
80
  GC_REGISTER_FINALIZER_NO_ORDER(local4, finalizer, (void*)4, 0, 0);
 
81
 
 
82
  void **globalData = (void**)global;
 
83
  globalData[0] = local;
 
84
  globalData[1] = local2;
 
85
 
 
86
  void **localData = (void**)local;
 
87
  localData[0] = local3;
 
88
 
 
89
  void **local2Data = (void**)local2;
 
90
  local2Data[0] = local4; // actually ignored, because local2 is atomic, so 4 is freeable
 
91
 
 
92
  emscripten_async_call(waiter, NULL, 100);
 
93
 
 
94
  return 0;
 
95
}
 
96