Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
node/lib/shmem.cpp at python-wrapper · VILLASframework/node · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
VILLASframework
/
node
Public
Notifications
You must be signed in to change notification settings
Fork
22
Star
21
Code
Issues
62
Pull requests
15
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Files
Expand file tree
python-wrapper
Breadcrumbs
node
/
lib
/
shmem.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
209 lines (162 loc) · 5.49 KB
python-wrapper
Breadcrumbs
node
/
lib
/
shmem.cpp
Copy path
Top
File metadata and controls
Code
Blame
209 lines (162 loc) · 5.49 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
Shared-memory interface: The interface functions that the external program should use.
*
* Author: Georg Martin Reinke <georg.reinke@rwth-aachen.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#
include
<
cerrno
>
#
include
<
fcntl.h
>
#
include
<
semaphore.h
>
#
include
<
sys/mman.h
>
#
include
<
sys/stat.h
>
#
include
<
villas/kernel/kernel.hpp
>
#
include
<
villas/node/memory.hpp
>
#
include
<
villas/sample.hpp
>
#
include
<
villas/shmem.hpp
>
#
include
<
villas/utils.hpp
>
using
namespace
villas
;
using
namespace
villas
::node
;
size_t
villas::node::shmem_total_size
(
int
queuelen,
int
samplelen) {
//
We have the constant const of the memory_type header
return
sizeof
(
struct
memory
::Type)
//
and the shared struct itself
+
sizeof
(
struct
ShmemShared
)
//
the size of the actual queue and the queue for the pool
+ queuelen * (
2
*
sizeof
(
struct
CQueue_cell
))
//
the size of the pool
+ queuelen *
kernel::getCachelineSize
() *
CEIL
(
SAMPLE_LENGTH
(samplelen),
kernel::getCachelineSize
())
//
a memblock for each allocation (1 shmem_shared, 2 queues, 1 pool)
+
4
*
sizeof
(
struct
memory
::Block)
//
and some extra buffer for alignment
+
1024
;
}
int
villas::node::shmem_int_open
(
const
char
*wname,
const
char
*rname,
struct
ShmemInterface
*shm,
struct
ShmemConfig
*conf) {
char
*cptr;
int
fd, ret;
size_t
len;
void
*base;
struct
memory
::Type *manager;
struct
ShmemShared
*shared;
struct
stat
stat_buf;
sem_t
*sem_own, *sem_other;
//
Ensure both semaphores exist
sem_own =
sem_open
(wname,
O_CREAT
,
0600
,
0
);
if
(sem_own ==
SEM_FAILED
)
return
-
1
;
sem_other =
sem_open
(rname,
O_CREAT
,
0600
,
0
);
if
(sem_other ==
SEM_FAILED
)
return
-
2
;
//
Open and initialize the shared region for the output queue
retry:
fd =
shm_open
(wname,
O_RDWR
|
O_CREAT
|
O_EXCL
,
0600
);
if
(fd <
0
) {
if
(errno ==
EEXIST
) {
ret =
shm_unlink
(wname);
if
(ret)
return
-
12
;
goto
retry;
}
return
-
3
;
}
len =
shmem_total_size
(conf->
queuelen
, conf->
samplelen
);
if
(
ftruncate
(fd, len) <
0
)
return
-
1
;
base =
mmap
(
nullptr
, len,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
, fd,
0
);
if
(base ==
MAP_FAILED
)
return
-
4
;
close
(fd);
manager =
memory::managed
(base, len);
shared =
(
struct
ShmemShared
*)
memory::alloc
(
sizeof
(
struct
ShmemShared
), manager);
if
(!shared) {
errno =
ENOMEM
;
return
-
5
;
}
shared->
polling
= conf->
polling
;
int
flags = (
int
)QueueSignalledFlags::
PROCESS_SHARED
;
enum
QueueSignalledMode mode =
conf->
polling
? QueueSignalledMode::
POLLING
: QueueSignalledMode::
PTHREAD
;
ret =
queue_signalled_init
(&shared->
queue
, conf->
queuelen
, manager, mode,
flags);
if
(ret) {
errno =
ENOMEM
;
return
-
6
;
}
ret =
pool_init
(&shared->
pool
, conf->
queuelen
,
SAMPLE_LENGTH
(conf->
samplelen
),
manager);
if
(ret) {
errno =
ENOMEM
;
return
-
7
;
}
shm->
write
.
base
= base;
shm->
write
.
name
= wname;
shm->
write
.
len
= len;
shm->
write
.
shared
= shared;
/*
Post own semaphore and wait on the other one, so both processes know that
* both regions are initialized
*/
sem_post
(sem_own);
sem_wait
(sem_other);
//
Open and map the other region
fd =
shm_open
(rname,
O_RDWR
,
0
);
if
(fd <
0
)
return
-
8
;
if
(
fstat
(fd, &stat_buf) <
0
)
return
-
9
;
len = stat_buf.
st_size
;
base =
mmap
(
nullptr
, len,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
, fd,
0
);
if
(base ==
MAP_FAILED
)
return
-
10
;
cptr =
(
char
*)base +
sizeof
(
struct
memory
::Type) +
sizeof
(
struct
memory
::Block);
shared =
reinterpret_cast
<
struct
ShmemShared
*>(cptr);
shm->
read
.
base
= base;
shm->
read
.
name
= rname;
shm->
read
.
len
= len;
shm->
read
.
shared
= shared;
shm->
readers
=
0
;
shm->
writers
=
0
;
shm->
closed
=
0
;
//
Unlink the semaphores; we don't need them anymore
sem_unlink
(wname);
return
0
;
}
int
villas::node::shmem_int_close
(
struct
ShmemInterface
*shm) {
int
ret;
atomic_store
(&shm->
closed
,
1
);
ret =
queue_signalled_close
(&shm->
write
.
shared
->
queue
);
if
(ret)
return
ret;
shm_unlink
(shm->
write
.
name
);
if
(
atomic_load
(&shm->
readers
) ==
0
)
munmap
(shm->
read
.
base
, shm->
read
.
len
);
if
(
atomic_load
(&shm->
writers
) ==
0
)
munmap
(shm->
write
.
base
, shm->
write
.
len
);
return
0
;
}
int
villas::node::shmem_int_read
(
struct
ShmemInterface
*shm,
struct
Sample
*
const
smps[],
unsigned
cnt) {
int
ret;
atomic_fetch_add
(&shm->
readers
,
1
);
ret =
queue_signalled_pull_many
(&shm->
read
.
shared
->
queue
, (
void
**)smps, cnt);
if
(
atomic_fetch_sub
(&shm->
readers
,
1
) ==
1
&&
atomic_load
(&shm->
closed
) ==
1
)
munmap
(shm->
read
.
base
, shm->
read
.
len
);
return
ret;
}
int
villas::node::shmem_int_write
(
struct
ShmemInterface
*shm,
const
struct
Sample
*
const
smps[],
unsigned
cnt) {
int
ret;
atomic_fetch_add
(&shm->
writers
,
1
);
ret =
queue_signalled_push_many
(&shm->
write
.
shared
->
queue
, (
void
**)smps, cnt);
if
(
atomic_fetch_sub
(&shm->
writers
,
1
) ==
1
&&
atomic_load
(&shm->
closed
) ==
1
)
munmap
(shm->
write
.
base
, shm->
write
.
len
);
return
ret;
}
int
villas::node::shmem_int_alloc
(
struct
ShmemInterface
*shm,
struct
Sample
*smps[],
unsigned
cnt) {
return
sample_alloc_many
(&shm->
write
.
shared
->
pool
, smps, cnt);
}
You can’t perform that action at this time.