forked from zuowangda/Fast-Fluid-Dynamics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosimulation_interface.c
139 lines (118 loc) · 4.76 KB
/
cosimulation_interface.c
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
#ifdef _MSC_VER
#elseif
char ffdDataName[] = "FFDDataMappingObject";
char otherDataName[] = "ModelicaDataMappingObject";
#endif
/******************************************************************************
Write shared data to the shared memory
******************************************************************************/
int write_to_shared_memory(ffdSharedData *ffdData)
{
HANDLE DataMapFile;
LPCTSTR DataBuf;
char msg[100];
/*---------------------------------------------------------------------------
| Open the named file mapping objects
---------------------------------------------------------------------------*/
DataMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
false, // do not inherit the name
ffdDataName); // name of mapping object for FFD data
// Send warning if can not open shared memory
if(DataMapFile==NULL)
{
sprintf(msg, "cosimulation.c: Could not open FFD data file mapping object. Error code %d", GetLastError());
ffd_log("cosimulation.c: Could not open FFD data file mapping object.", FFD_ERROR);
return 1;
}
/*---------------------------------------------------------------------------
| Mps a view of a file mapping into the address space of a calling process
---------------------------------------------------------------------------*/
DataBuf = (LPTSTR) MapViewOfFile(DataMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_DATA_SIZE);
if(DataBuf == NULL)
{
sprintf(msg, "cosimulation.c: Could not map view of FFD data file. Error code %d", GetLastError());
CloseHandle(DataMapFile);
return 1;
}
// Copy a block of memory from ffdData to ffdDataBuf
CopyMemory((PVOID)DataBuf, ffdData, sizeof(ffdSharedData));
UnmapViewOfFile(DataBuf);
CloseHandle(DataMapFile);
return 0;
} // End of write_to_shared_memory()
/******************************************************************************
| Read shared data from the shared memory
| Data status indicated by status
| -1: feak data
| 0: data has been read by the other program
| 1: data waiting for the other program to read
******************************************************************************/
int read_from_shared_memory(otherSharedData *otherData)
{
HANDLE DataMapFile;
char msg[100];
otherSharedData *data;
int i, imax = 1000;
printf("Fixme: try to open map\n");
/*---------------------------------------------------------------------------
| Open the named file mapping objects
---------------------------------------------------------------------------*/
DataMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
otherDataName); // name of mapping object for FFD data
// open the map
i = 0;
while(DataMapFile==NULL && i<imax)
{
DataMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
otherDataName); // name of mapping object for FFD data
i++;
}
if(DataMapFile==NULL && i>=imax)
{
sprintf(msg, "cosimulation.c: fail to open file map after %d times", i);
ffd_log(msg, FFD_ERROR);
exit(1);
}
/*---------------------------------------------------------------------------
| Mps a view of a file mapping into the address space of a calling process
---------------------------------------------------------------------------*/
data = (otherSharedData *) MapViewOfFile(DataMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_DATA_SIZE);
if(data == NULL)
{
Sleep(100);
data = (otherSharedData *) MapViewOfFile(DataMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_DATA_SIZE);
}
while(data->status<1)
Sleep(100);
otherData->arr[0] = data->arr[0];
otherData->arr[1] = data->arr[1];
otherData->arr[2] = data->arr[2];
otherData->t = data->t;
otherData->status= data->status;
//printf("%s\n", data->message);
strcpy(otherData->message, data->message);
// Change the sign to indicate that the data has been read
data->status = 0;
// Copy a block of memory from Data to ffdDataBuf
//CopyMemory((PVOID)data, otherData, sizeof(otherSharedData));
UnmapViewOfFile(data);
CloseHandle(DataMapFile);
return 0;
} // End of read_from_shared_memory()