| [5caec96] | 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 |
|
|---|
| [f1be440] | 12 | #include <civlc.h>
|
|---|
| 13 | #include <stdio.h>
|
|---|
| [c358a25] | 14 | $proc procs[4];
|
|---|
| [7a16ae8] | 15 | $scope scopes[4];
|
|---|
| [f1be440] | 16 |
|
|---|
| 17 | int min(int a, int b) {
|
|---|
| 18 | if (a < b)
|
|---|
| 19 | return a;
|
|---|
| 20 | else
|
|---|
| 21 | return b;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| [5caec96] | 24 | int fDouble(int (*f)(int, int), int a, int b) {
|
|---|
| [f1be440] | 25 | int result;
|
|---|
| 26 |
|
|---|
| 27 | result = f(a, b);
|
|---|
| 28 | return result * 2;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| [5caec96] | 31 | // f is a function pointer type argument
|
|---|
| [f1be440] | 32 | $proc proc_create(void (*f)(int), int x){
|
|---|
| 33 | $proc p = $spawn f(x);
|
|---|
| 34 |
|
|---|
| 35 | return p;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | void foo(int id) {
|
|---|
| 39 | printf("I'm spawned with id %d.\n", id);
|
|---|
| [7a16ae8] | 40 | $assert(procs[id] == $self);
|
|---|
| 41 | scopes[id] = $here;
|
|---|
| 42 | if($scope_defined(scopes[id]))
|
|---|
| 43 | printf("I own scope with id %d.\n", scopes[id]);
|
|---|
| 44 | else
|
|---|
| 45 | printf("Error: my scope is gone!\n");
|
|---|
| [f1be440] | 46 | }
|
|---|
| 47 |
|
|---|
| 48 | void main(){
|
|---|
| [5caec96] | 49 | // min here is a function pointer
|
|---|
| 50 | int k = fDouble(min, 5, 8);
|
|---|
| [c358a25] | 51 |
|
|---|
| [f1be440] | 52 | for(int i = 0; i < 4; i++){
|
|---|
| 53 | procs[i] = proc_create(foo, i);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | for(int i = 0; i < 4; i++){
|
|---|
| 57 | $wait(procs[i]);
|
|---|
| 58 | printf("Process %d terminates.\n", i);
|
|---|
| [7a16ae8] | 59 | if($proc_defined(procs[i]))
|
|---|
| 60 | printf("Process %d is not removed!\n", i);
|
|---|
| 61 | else
|
|---|
| 62 | printf("Process %d now has invalid reference: %d\n", i, procs[i]);
|
|---|
| [f1be440] | 63 | }
|
|---|
| [5caec96] | 64 |
|
|---|
| 65 | for(int i = 0; i < 4; i++){
|
|---|
| 66 | _Bool defined = $proc_defined(procs[i]);
|
|---|
| 67 |
|
|---|
| 68 | $assert(!defined);
|
|---|
| 69 | defined = $scope_defined(scopes[i]);
|
|---|
| 70 | $assert(!defined);
|
|---|
| 71 | }
|
|---|
| [f1be440] | 72 | }
|
|---|