main
test-branch
|
Last change
on this file since a389857 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago |
|
Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
1.3 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.cvh>
|
|---|
| 15 | #include <stdio.h>
|
|---|
| 16 | $proc procs[NPROCS];
|
|---|
| 17 | $scope scopes[NPROCS];
|
|---|
| 18 |
|
|---|
| 19 | int min(int a, int b) {
|
|---|
| 20 | if (a < b)
|
|---|
| 21 | return a;
|
|---|
| 22 | else
|
|---|
| 23 | return b;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | int 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 |
|
|---|
| 40 | void foo(int id) {
|
|---|
| 41 | printf("I'm spawned with id %d.\n", id);
|
|---|
| 42 | $assert(procs[id] == $self);
|
|---|
| 43 | scopes[id] = $here;
|
|---|
| 44 | printf("I own scope with id %d.\n", scopes[id]);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | void main(){
|
|---|
| 48 | // min here is a function pointer
|
|---|
| 49 | int k = fDouble(min, 5, 8);
|
|---|
| 50 |
|
|---|
| 51 | for(int i = 0; i < NPROCS; i++){
|
|---|
| 52 | procs[i] = proc_create(foo, i);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | for(int i = 0; i < NPROCS; i++){
|
|---|
| 56 | $wait(procs[i]);
|
|---|
| 57 | printf("Process %d terminates.\n", i);
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.