source: CIVL/examples/languageFeatures/functionPointer.cvl@ 5f8dc09

1.23 2.0 main test-branch
Last change on this file since 5f8dc09 was 3ff27cf, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

updated examples since $assert/$assume has been changed to functions; fixed the model builder for the new side-effect remover.

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

  • Property mode set to 100644
File size: 1.7 KB
RevLine 
[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
[9e0c40f]12#define NPROCS 3
13
[e6b02c8]14#include <civlc.cvh>
[f1be440]15#include <stdio.h>
[9e0c40f]16$proc procs[NPROCS];
17$scope scopes[NPROCS];
[f1be440]18
19int min(int a, int b) {
20 if (a < b)
21 return a;
22 else
23 return b;
24}
25
[5caec96]26int fDouble(int (*f)(int, int), int a, int b) {
[f1be440]27 int result;
28
29 result = f(a, b);
30 return result * 2;
31}
32
[5caec96]33// f is a function pointer type argument
[f1be440]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);
[3ff27cf]42 $assert((procs[id] == $self));
[7a16ae8]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");
[f1be440]48}
49
50void main(){
[5caec96]51 // min here is a function pointer
52 int k = fDouble(min, 5, 8);
[c358a25]53
[9e0c40f]54 for(int i = 0; i < NPROCS; i++){
[f1be440]55 procs[i] = proc_create(foo, i);
56 }
57
[9e0c40f]58 for(int i = 0; i < NPROCS; i++){
[f1be440]59 $wait(procs[i]);
60 printf("Process %d terminates.\n", i);
[7a16ae8]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]);
[f1be440]65 }
[5caec96]66
[9e0c40f]67 for(int i = 0; i < NPROCS; i++){
[5caec96]68 _Bool defined = $proc_defined(procs[i]);
69
[3ff27cf]70 $assert((!defined));
[5caec96]71 defined = $scope_defined(scopes[i]);
[3ff27cf]72 $assert((!defined));
[5caec96]73 }
[f1be440]74}
Note: See TracBrowser for help on using the repository browser.