Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 212 lines (173 sloc) 5.938 kB
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
1 // shared memory interface to the supercollider server
2 // Copyright (C) 2011 Tim Blechmann
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
3 // Copyright (C) 2011 Jakob Leben
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; see the file COPYING. If not, write to
17 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 // Boston, MA 02111-1307, USA.
19
20 #ifndef SERVER_SHM_HPP
21 #define SERVER_SHM_HPP
22
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
23 #include "scope_buffer.hpp"
24
c25885d @timblechmann common: adapt server shm interface for boost 1.51
timblechmann authored
25 #include <boost/version.hpp>
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
26 #include <boost/foreach.hpp>
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
27 #include <boost/ref.hpp>
28 #include <boost/lexical_cast.hpp>
29 #include <boost/interprocess/managed_shared_memory.hpp>
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
30 #include <boost/interprocess/containers/vector.hpp>
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
31 #include <boost/atomic.hpp>
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
32
ca826e2 @timblechmann common: move shared memory implementation into namespace
timblechmann authored
33 namespace detail_server_shm {
34
1f929ea @timblechmann shm server interface: namespace cleanup
timblechmann authored
35 using std::string; using std::pair;
36
37 using boost::ref;
38
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
39 namespace bi = boost::interprocess;
1f929ea @timblechmann shm server interface: namespace cleanup
timblechmann authored
40 using bi::managed_shared_memory; using bi::shared_memory_object;
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
41
83f43aa @timblechmann win32 compile fixes
timblechmann authored
42 static inline string make_shmem_name(unsigned int port_number)
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
43 {
1f929ea @timblechmann shm server interface: namespace cleanup
timblechmann authored
44 return string("SuperColliderServer_") + boost::lexical_cast<string>(port_number);
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
45 }
46
535a7e6 @joshpar make struct forward decs class for scope_buffer reader and writer
joshpar authored
47 class server_shared_memory
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
48 {
535a7e6 @joshpar make struct forward decs class for scope_buffer reader and writer
joshpar authored
49 public:
ca826e2 @timblechmann common: move shared memory implementation into namespace
timblechmann authored
50 typedef offset_ptr<float> sh_float_ptr;
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
51 typedef offset_ptr<scope_buffer> scope_buffer_ptr;
52
1bfd692 @timblechmann shm server interface: avoid named objects in shared memory
timblechmann authored
53 typedef bi::allocator<scope_buffer_ptr, managed_shared_memory::segment_manager> scope_buffer_ptr_allocator;
54 typedef bi::vector<scope_buffer_ptr, scope_buffer_ptr_allocator> scope_buffer_vector;
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
55
1bfd692 @timblechmann shm server interface: avoid named objects in shared memory
timblechmann authored
56 server_shared_memory(managed_shared_memory & segment, int control_busses, int num_scope_buffers = 128):
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
57 num_control_busses(control_busses),
1bfd692 @timblechmann shm server interface: avoid named objects in shared memory
timblechmann authored
58
59 scope_buffers(scope_buffer_ptr_allocator(segment.get_segment_manager()))
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
60 {
61 control_busses_ = (float*)segment.allocate(control_busses * sizeof(float));
62 std::fill(control_busses_.get(), control_busses_.get() + control_busses, 0);
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
63
64 for (int i = 0; i != num_scope_buffers; ++i) {
1bfd692 @timblechmann shm server interface: avoid named objects in shared memory
timblechmann authored
65 scope_buffer * raw_scope_ptr = (scope_buffer*)segment.allocate(sizeof(scope_buffer));
66 new(raw_scope_ptr) scope_buffer();
67 scope_buffer_ptr buf = raw_scope_ptr;
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
68 scope_buffers.push_back(buf);
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
69 }
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
70 }
71
72 void destroy(managed_shared_memory & segment)
73 {
74 segment.deallocate(control_busses_.get());
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
75
9f4644e @timblechmann supernova/common: fix some integer types
timblechmann authored
76 for (size_t i = 0; i != scope_buffers.size(); ++i)
1bfd692 @timblechmann shm server interface: avoid named objects in shared memory
timblechmann authored
77 segment.deallocate(scope_buffers[i].get());
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
78 }
79
80 void set_control_bus(int bus, float value)
81 {
82 // TODO: we need to set the control busses via a work queue
83 }
84
85 float * get_control_busses(void)
86 {
87 return control_busses_.get();
88 }
89
83f43aa @timblechmann win32 compile fixes
timblechmann authored
90 scope_buffer * get_scope_buffer(unsigned int index)
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
91 {
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
92 if (index < scope_buffers.size())
93 return scope_buffers[index].get();
94 else
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
95 return 0;
96 }
97
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
98 private:
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
99 string shmem_name;
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
100 int num_control_busses;
101 sh_float_ptr control_busses_; // control busses
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
102 scope_buffer_vector scope_buffers;
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
103 };
104
105 class server_shared_memory_creator
106 {
107 public:
83f43aa @timblechmann win32 compile fixes
timblechmann authored
108 server_shared_memory_creator(unsigned int port_number, unsigned int control_busses):
ca826e2 @timblechmann common: move shared memory implementation into namespace
timblechmann authored
109 shmem_name(detail_server_shm::make_shmem_name(port_number)),
1f929ea @timblechmann shm server interface: namespace cleanup
timblechmann authored
110 segment(bi::open_or_create, shmem_name.c_str(), 8192 * 1024)
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
111 {
c25885d @timblechmann common: adapt server shm interface for boost 1.51
timblechmann authored
112 #if (BOOST_VERSION < 105100)
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
113 segment.flush();
c25885d @timblechmann common: adapt server shm interface for boost 1.51
timblechmann authored
114 #endif
4ab10b4 @timblechmann common: allocate scope buffers from a real-time memory pool
timblechmann authored
115
116 const int num_scope_buffers = 128;
117 size_t scope_pool_size = num_scope_buffers * sizeof(float) * 8192; // pessimize, about 4 MB
118 void * memory_for_scope_pool = segment.allocate(scope_pool_size);
119 scope_pool.init(memory_for_scope_pool, scope_pool_size);
120
1bfd692 @timblechmann shm server interface: avoid named objects in shared memory
timblechmann authored
121 shm = segment.construct<server_shared_memory>(shmem_name.c_str())(ref(segment), control_busses,
4ab10b4 @timblechmann common: allocate scope buffers from a real-time memory pool
timblechmann authored
122 num_scope_buffers);
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
123 }
124
83f43aa @timblechmann win32 compile fixes
timblechmann authored
125 static void cleanup(unsigned int port_number)
fe50f6f @timblechmann server: remove shared memory region before reboot
timblechmann authored
126 {
ca826e2 @timblechmann common: move shared memory implementation into namespace
timblechmann authored
127 shared_memory_object::remove(detail_server_shm::make_shmem_name(port_number).c_str());
fe50f6f @timblechmann server: remove shared memory region before reboot
timblechmann authored
128 }
129
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
130 ~server_shared_memory_creator(void)
131 {
258c1a1 @timblechmann shm: disconnect shm before replying to /quit
timblechmann authored
132 if (shm)
133 disconnect();
134 }
135
136 void disconnect()
137 {
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
138 shm->destroy(segment);
139 segment.destroy<server_shared_memory>(shmem_name.c_str());
140 shared_memory_object::remove(shmem_name.c_str());
258c1a1 @timblechmann shm: disconnect shm before replying to /quit
timblechmann authored
141 shm = NULL;
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
142 }
143
144 float * get_control_busses(void)
145 {
146 return shm->get_control_busses();
147 }
148
83f43aa @timblechmann win32 compile fixes
timblechmann authored
149 scope_buffer_writer get_scope_buffer_writer(unsigned int index, unsigned int channels, unsigned int size)
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
150 {
151 scope_buffer *buf = shm->get_scope_buffer(index);
152 if (buf)
4ab10b4 @timblechmann common: allocate scope buffers from a real-time memory pool
timblechmann authored
153 return scope_buffer_writer(buf, scope_pool, channels, size);
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
154 else
155 return scope_buffer_writer();
156 }
157
158 void release_scope_buffer_writer( scope_buffer_writer & writer )
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
159 {
4ab10b4 @timblechmann common: allocate scope buffers from a real-time memory pool
timblechmann authored
160 writer.release( scope_pool );
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
161 }
162
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
163 private:
164 string shmem_name;
165 managed_shared_memory segment;
4ab10b4 @timblechmann common: allocate scope buffers from a real-time memory pool
timblechmann authored
166 scope_buffer_pool scope_pool;
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
167
168 protected:
169 server_shared_memory * shm;
170 };
171
172
173 class server_shared_memory_client
174 {
175 public:
83f43aa @timblechmann win32 compile fixes
timblechmann authored
176 server_shared_memory_client(unsigned int port_number):
ca826e2 @timblechmann common: move shared memory implementation into namespace
timblechmann authored
177 shmem_name(detail_server_shm::make_shmem_name(port_number)),
1f929ea @timblechmann shm server interface: namespace cleanup
timblechmann authored
178 segment(bi::open_only, shmem_name.c_str())
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
179 {
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
180 pair<server_shared_memory*, size_t> res = segment.find<server_shared_memory> (shmem_name.c_str());
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
181 if (res.second != 1)
182 throw std::runtime_error("Cannot connect to shared memory");
183 shm = res.first;
184 }
185
186 float * get_control_busses(void)
187 {
188 return shm->get_control_busses();
189 }
190
83f43aa @timblechmann win32 compile fixes
timblechmann authored
191 scope_buffer_reader get_scope_buffer_reader(unsigned int index)
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
192 {
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
193 scope_buffer *buf = shm->get_scope_buffer(index);
194 return scope_buffer_reader(buf);
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
195 }
196
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
197 private:
198 string shmem_name;
199 managed_shared_memory segment;
5f133b9 @timblechmann shared memory interface: prototype scope interface
timblechmann authored
200 server_shared_memory * shm;
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
201 };
202
ca826e2 @timblechmann common: move shared memory implementation into namespace
timblechmann authored
203 }
204
205 using detail_server_shm::server_shared_memory_client;
206 using detail_server_shm::server_shared_memory_creator;
cbb1c87 @jleben server|plugins: improve scope shm interface
jleben authored
207 using detail_server_shm::scope_buffer_writer;
208 using detail_server_shm::scope_buffer_reader;
209 using detail_server_shm::scope_buffer;
ca826e2 @timblechmann common: move shared memory implementation into namespace
timblechmann authored
210
466a08a @timblechmann supernova: prototype shared-memory interface to supernova
timblechmann authored
211 #endif /* SERVER_SHM_HPP */
Something went wrong with that request. Please try again.