source: CIVL/examples/languageFeatures/functionPointer.cvl@ fe8b067

1.23 2.0 main test-branch
Last change on this file since fe8b067 was 9e0c40f, checked in by Stephen Siegel <siegel@…>, 12 years ago

Slight generalization to function pointer example using NPROCS macro instead of magic numbers 4

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

  • Property mode set to 100644
File size: 1.7 KB
Line 
1/* This program demonstrates the use of function pointer, checking
2 * of the equality of $proc and $scope type expressions, and the
3 * use of system functions $proc_defined/$scope_defined to check if
4 * a $proc/$scope expression is defined.
5 * Command line example:
6 * civl verify functionPointer.cvl
7 * or
8 * civl verify functionPointer.cvl -enablePrintf=false
9 * (if you do not like to see the message printed by those printf function calls.)
10 */
11
12#define NPROCS 3
13
14#include <civlc.h>
15#include <stdio.h>
16$proc procs[NPROCS];
17$scope scopes[NPROCS];
18
19int min(int a, int b) {
20 if (a < b)
21 return a;
22 else
23 return b;
24}
25
26int fDouble(int (*f)(int, int), int a, int b) {
27 int result;
28
29 result = f(a, b);
30 return result * 2;
31}
32
33// f is a function pointer type argument
34$proc proc_create(void (*f)(int), int x){
35 $proc p = $spawn f(x);
36
37 return p;
38}
39
40void foo(int id) {
41 printf("I'm spawned with id %d.\n", id);
42 $assert(procs[id] == $self);
43 scopes[id] = $here;
44 if($scope_defined(scopes[id]))
45 printf("I own scope with id %d.\n", scopes[id]);
46 else
47 printf("Error: my scope is gone!\n");
48}
49
50void main(){
51 // min here is a function pointer
52 int k = fDouble(min, 5, 8);
53
54 for(int i = 0; i < NPROCS; i++){
55 procs[i] = proc_create(foo, i);
56 }
57
58 for(int i = 0; i < NPROCS; i++){
59 $wait(procs[i]);
60 printf("Process %d terminates.\n", i);
61 if($proc_defined(procs[i]))
62 printf("Process %d is not removed!\n", i);
63 else
64 printf("Process %d now has invalid reference: %d\n", i, procs[i]);
65 }
66
67 for(int i = 0; i < NPROCS; i++){
68 _Bool defined = $proc_defined(procs[i]);
69
70 $assert(!defined);
71 defined = $scope_defined(scopes[i]);
72 $assert(!defined);
73 }
74}
Note: See TracBrowser for help on using the repository browser.