-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathfusion_call.c
More file actions
225 lines (171 loc) · 6.36 KB
/
fusion_call.c
File metadata and controls
225 lines (171 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
(c) Copyright 2012-2013 DirectFB integrated media GmbH
(c) Copyright 2001-2013 The world wide DirectFB Open Source Community (directfb.org)
(c) Copyright 2000-2004 Convergence (integrated media) GmbH
All rights reserved.
Written by Denis Oliver Kropp <dok@directfb.org>,
Andreas Shimokawa <andi@directfb.org>,
Marek Pikarski <mass@directfb.org>,
Sven Neumann <neo@directfb.org>,
Ville Syrjälä <syrjala@sci.fi> and
Claudio Ciccani <klan@users.sf.net>.
This file is subject to the terms and conditions of the MIT License:
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <direct/messages.h>
#include <fusion/call.h>
#include <fusion/lock.h>
#include <fusion/fusion.h>
#include <fusion/shm/pool.h>
#ifndef HAVE_FORK
# define fork() -1
#endif
/**********************************************************************************************************************/
static int parse_cmdline ( int argc, char *argv[] );
static int show_usage ( void );
/**********************************************************************************************************************/
#define MAX_PARTICIPANTS (20)
static int participants = 2;
static int participant;
struct sharedData {
volatile int initDone;
FusionCall call[MAX_PARTICIPANTS+1]; /* 0 = empty. just being lazy. */
FusionSkirmish skirmish;
};
/**********************************************************************************************************************/
static FusionCallHandlerResult
call_handler( int caller,
int call_arg,
void *call_ptr,
void *ctx,
unsigned int serial,
int *ret_val )
{
struct sharedData *shared = ctx;
int ret = 0;
fusion_skirmish_prevail( &shared->skirmish );
if (participant < participants)
if (random() & 1)
fusion_call_execute( &shared->call[participant+1], FCEF_NONE, 0, 0, &ret );
fusion_skirmish_dismiss( &shared->skirmish );
*ret_val = ret + 1;
return FCHR_RETURN;
}
/**********************************************************************************************************************/
int
main( int argc, char *argv[] )
{
DirectResult ret;
FusionWorld *world;
FusionSHMPoolShared *pool;
struct sharedData *shared;
sigset_t block;
int retcall;
int i;
if (parse_cmdline( argc, argv ))
return -1;
ret = fusion_enter( 0, 23, FER_ANY, &world );
if (ret)
return ret;
ret = fusion_shm_pool_create( world, "Shared Memory", 8192, false, &pool );
if (ret)
return ret;
ret = fusion_shm_pool_allocate( pool, sizeof(struct sharedData), true, true, (void**)&shared );
if (ret)
return ret;
/*
* Do the fork() magic!
*/
fusion_world_set_fork_action( world, FFA_FORK );
for (i=1; i<participants; i++) {
pid_t f = fork();
if (f == -1) {
D_PERROR( "fork() failed!\n" );
return -1;
}
if (f) {
/* parent */
participant = i;
break;
}
participant = i+1;
}
fusion_world_set_fork_action( world, FFA_CLOSE );
ret = fusion_call_init( &shared->call[participant], call_handler, shared, world );
if (ret)
return ret;
shared->initDone++;
/* wait a bit before start, make sure the calls exist */
while( shared->initDone != participants )
usleep(50000);
if (participant == 1) {
/* we generate a skirmish that will be transported along */
ret = fusion_skirmish_init( &shared->skirmish, "Test", world );
if (ret)
return -1;
fusion_skirmish_prevail( &shared->skirmish );
for(;;) {
fusion_call_execute( &shared->call[2], FCEF_NONE, 0, 0, &retcall );
printf("pcp %d, result: %d\n", participant, retcall);
}
}
/* we rely on exit() */
sigemptyset( &block );
sigsuspend( &block );
}
/**********************************************************************************************************************/
static int
parse_cmdline( int argc, char *argv[] )
{
int i;
char *end;
for (i=1; i<argc; i++) {
if (!strcmp( argv[i], "-p" )) {
if (++i < argc) {
participants = strtoul( argv[i], &end, 10 );
if (end && *end) {
D_ERROR( "Parse error in number '%s'!\n", argv[i] );
return -1;
}
if (participants < 1 || participants > MAX_PARTICIPANTS)
return show_usage();
}
else
return show_usage();
}
}
return 0;
}
static int
show_usage( void )
{
fprintf( stderr, "\n"
"Usage:\n"
" fusion_call [options]\n"
"\n"
"Options:\n"
" -p <2-MAX> Number of participants\n"
"\n"
);
return -1;
}