source: CIVL/examples/languageFeatures/functionPointer.cvl@ 79740cc

1.23 2.0 main test-branch
Last change on this file since 79740cc was 7a16ae8, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

implemented system functions in civlc.h for $proc_defined($proc) (and ditto for scope, gcomm, comm); allowed $proc type operands for equality operators (== and !=); reported errors if a scope/process/gcomm/comm value is undefined when evaluating it in an equality operation expression.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@734 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 980 bytes
Line 
1#include <civlc.h>
2#include <stdio.h>
3$proc procs[4];
4$scope scopes[4];
5
6int min(int a, int b) {
7 if (a < b)
8 return a;
9 else
10 return b;
11}
12
13int minDouble(int (*f)(int, int), int a, int b) {
14 int result;
15
16 result = f(a, b);
17 return result * 2;
18}
19
20$proc proc_create(void (*f)(int), int x){
21 $proc p = $spawn f(x);
22
23 return p;
24}
25
26void foo(int id) {
27 printf("I'm spawned with id %d.\n", id);
28 $assert(procs[id] == $self);
29 scopes[id] = $here;
30 if($scope_defined(scopes[id]))
31 printf("I own scope with id %d.\n", scopes[id]);
32 else
33 printf("Error: my scope is gone!\n");
34}
35
36void main(){
37 int k = minDouble(min, 5, 8);
38
39 for(int i = 0; i < 4; i++){
40 procs[i] = proc_create(foo, i);
41 }
42
43 for(int i = 0; i < 4; i++){
44 $wait(procs[i]);
45 printf("Process %d terminates.\n", i);
46 if($proc_defined(procs[i]))
47 printf("Process %d is not removed!\n", i);
48 else
49 printf("Process %d now has invalid reference: %d\n", i, procs[i]);
50 }
51}
Note: See TracBrowser for help on using the repository browser.