Skip to content

Commit 40944fe

Browse files
committed
Merge pull request #219 from eregon/boostvm_refs
Remove permanent BoostVM references
2 parents 5e14f15 + 9d3d27a commit 40944fe

13 files changed

+87
-86
lines changed

vm/boostenv/main/boostenv-decl.hh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public:
8282

8383
inline
8484
bool findVM(VMIdentifier identifier,
85-
std::function<void(BoostVM& boostVM)> onSuccess);
85+
std::function<void(BoostVM&)> onSuccess);
8686

8787
inline
8888
UnstableNode listVMs(VM vm);
@@ -99,6 +99,15 @@ public:
9999
void removeTerminatedVM(VMIdentifier identifier, nativeint exitCode,
100100
boost::asio::io_service::work* work);
101101

102+
// Executes callback on behalf of the target VM if it is still alive.
103+
inline
104+
bool postVMEvent(VMIdentifier identifier,
105+
std::function<void(BoostVM&)> callback) {
106+
return findVM(identifier, [&callback] (BoostVM& boostVM) {
107+
boostVM.postVMEvent(callback);
108+
});
109+
}
110+
102111
// Configuration
103112

104113
public:

vm/boostenv/main/boostenv.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ VMIdentifier BoostEnvironment::checkValidIdentifier(VM vm, RichNode vmIdentifier
145145
the BoostVM will not be terminated during the call.
146146
Returns whether it found the VM represented by identifier. */
147147
bool BoostEnvironment::findVM(VMIdentifier identifier,
148-
std::function<void(BoostVM& boostVM)> onSuccess) {
148+
std::function<void(BoostVM&)> onSuccess) {
149149
boost::lock_guard<boost::mutex> lock(_vmsMutex);
150150
for (BoostVM& vm : _vms) {
151151
if (vm.identifier == identifier) {
@@ -166,7 +166,7 @@ UnstableNode BoostEnvironment::listVMs(VM vm) {
166166

167167
void BoostEnvironment::killVM(VMIdentifier identifier, nativeint exitCode,
168168
const std::string& reason) {
169-
findVM(identifier, [this, exitCode, reason] (BoostVM& targetVM) {
169+
postVMEvent(identifier, [this, exitCode, reason] (BoostVM& targetVM) {
170170
targetVM.requestTermination(exitCode, reason);
171171
});
172172
}

vm/boostenv/main/boostenvpipe-decl.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PipeConnection: public BaseSocketConnection<PipeConnection,
4141
boost::asio::local::stream_protocol> {
4242
public:
4343
inline
44-
PipeConnection(BoostVM& boostVM);
44+
PipeConnection(VM vm);
4545
};
4646

4747
#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS

vm/boostenv/main/boostenvpipe.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ namespace mozart { namespace boostenv {
3939
// PipeConnection //
4040
////////////////////
4141

42-
PipeConnection::PipeConnection(BoostVM& boostVM):
43-
BaseSocketConnection(boostVM) {
42+
PipeConnection::PipeConnection(VM vm):
43+
BaseSocketConnection(vm) {
4444
}
4545

4646
#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS

vm/boostenv/main/boostenvtcp-decl.hh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class TCPConnection: public BaseSocketConnection<TCPConnection,
3939
boost::asio::ip::tcp> {
4040
public:
4141
inline
42-
TCPConnection(BoostVM& boostVM);
42+
TCPConnection(VM vm);
4343

4444
public:
4545
inline
@@ -61,9 +61,9 @@ private:
6161
public:
6262
typedef std::shared_ptr<TCPAcceptor> pointer;
6363

64-
static pointer create(BoostVM& boostVM, const tcp::endpoint& endpoint) {
64+
static pointer create(VM vm, const tcp::endpoint& endpoint) {
6565
// Cannot use make_shared as constructor is private
66-
return pointer(new TCPAcceptor(boostVM, endpoint));
66+
return pointer(new TCPAcceptor(vm, endpoint));
6767
}
6868

6969
public:
@@ -72,7 +72,8 @@ public:
7272
}
7373

7474
inline
75-
void startAsyncAccept(const ProtectedNode& connectionNode);
75+
void startAsyncAccept(TCPConnection::pointer connection,
76+
const ProtectedNode& connectionNode);
7677

7778
inline
7879
boost::system::error_code cancel();
@@ -82,10 +83,11 @@ public:
8283

8384
private:
8485
inline
85-
TCPAcceptor(BoostVM& boostVM, const tcp::endpoint& endpoint);
86+
TCPAcceptor(VM vm, const tcp::endpoint& endpoint);
8687

8788
private:
88-
BoostVM& boostVM;
89+
BoostEnvironment& env;
90+
VMIdentifier vm;
8991
tcp::acceptor _acceptor;
9092
};
9193

vm/boostenv/main/boostenvtcp.hh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ namespace mozart { namespace boostenv {
3737
// TCPConnection //
3838
///////////////////
3939

40-
TCPConnection::TCPConnection(BoostVM& boostVM):
41-
BaseSocketConnection(boostVM), _resolver(boostVM.env.io_service) {
40+
TCPConnection::TCPConnection(VM vm):
41+
BaseSocketConnection(vm), _resolver(env.io_service) {
4242
}
4343

4444
void TCPConnection::startAsyncConnect(std::string host, std::string service,
@@ -51,12 +51,12 @@ void TCPConnection::startAsyncConnect(std::string host, std::string service,
5151
auto connectHandler = [=] (const boost::system::error_code& error,
5252
protocol::resolver::iterator selected_endpoint) {
5353
if (!error) {
54-
boostVM.postVMEvent([=] () {
54+
env.postVMEvent(vm, [=] (BoostVM& boostVM) {
5555
boostVM.bindAndReleaseAsyncIOFeedbackNode(
5656
statusNode, build(boostVM.vm, self));
5757
});
5858
} else {
59-
boostVM.postVMEvent([=] () {
59+
env.postVMEvent(vm, [=] (BoostVM& boostVM) {
6060
boostVM.raiseAndReleaseAsyncIOFeedbackNode(
6161
statusNode, "socket", "connect", error.value());
6262
});
@@ -65,7 +65,7 @@ void TCPConnection::startAsyncConnect(std::string host, std::string service,
6565

6666
boost::asio::async_connect(socket(), endpoints, connectHandler);
6767
} else {
68-
boostVM.postVMEvent([=] () {
68+
env.postVMEvent(vm, [=] (BoostVM& boostVM) {
6969
boostVM.raiseAndReleaseAsyncIOFeedbackNode(
7070
statusNode, "socket", "resolve", error.value());
7171
});
@@ -80,27 +80,28 @@ void TCPConnection::startAsyncConnect(std::string host, std::string service,
8080
// TCPAcceptor //
8181
/////////////////
8282

83-
TCPAcceptor::TCPAcceptor(BoostVM& boostVM,
83+
TCPAcceptor::TCPAcceptor(VM vm,
8484
const tcp::endpoint& endpoint):
85-
boostVM(boostVM), _acceptor(boostVM.env.io_service, endpoint) {
85+
env(BoostEnvironment::forVM(vm)),
86+
vm(BoostVM::forVM(vm).identifier),
87+
_acceptor(env.io_service, endpoint) {
8688
}
8789

88-
void TCPAcceptor::startAsyncAccept(const ProtectedNode& connectionNode) {
89-
TCPConnection::pointer connection = TCPConnection::create(boostVM);
90-
90+
void TCPAcceptor::startAsyncAccept(TCPConnection::pointer connection,
91+
const ProtectedNode& connectionNode) {
9192
auto handler = [=] (const boost::system::error_code& error) {
9293
if (!error) {
93-
boostVM.postVMEvent([=] () {
94+
env.postVMEvent(vm, [=] (BoostVM& boostVM) {
9495
boostVM.bindAndReleaseAsyncIOFeedbackNode(
9596
connectionNode, build(boostVM.vm, connection));
9697
});
9798
} else if (error == boost::asio::error::operation_aborted) {
98-
boostVM.postVMEvent([=] () {
99+
env.postVMEvent(vm, [=] (BoostVM& boostVM) {
99100
boostVM.releaseAsyncIONode(connectionNode);
100101
});
101102
} else {
102103
// Try again
103-
startAsyncAccept(connectionNode);
104+
startAsyncAccept(connection, connectionNode);
104105
}
105106
};
106107

vm/boostenv/main/boostenvutils-decl.hh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public:
4949
public:
5050
typedef std::shared_ptr<T> pointer;
5151

52-
static pointer create(BoostVM& boostVM) {
53-
return std::make_shared<T>(boostVM);
52+
static pointer create(VM vm) {
53+
return std::make_shared<T>(vm);
5454
}
5555

5656
public:
5757
inline
58-
BaseSocketConnection(BoostVM& boostVM);
58+
BaseSocketConnection(VM vm);
5959

6060
public:
6161
typename protocol::socket& socket() {
@@ -89,7 +89,8 @@ protected:
8989
const ProtectedNode& statusNode);
9090

9191
protected:
92-
BoostVM& boostVM;
92+
BoostEnvironment& env;
93+
VMIdentifier vm;
9394
typename protocol::socket _socket;
9495

9596
std::vector<char> _readData;

vm/boostenv/main/boostenvutils.hh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ namespace mozart { namespace boostenv {
3838
//////////////////////////
3939

4040
template <typename T, typename P>
41-
BaseSocketConnection<T, P>::BaseSocketConnection(BoostVM& boostVM):
42-
boostVM(boostVM), _socket(boostVM.env.io_service) {
41+
BaseSocketConnection<T, P>::BaseSocketConnection(VM vm):
42+
env(BoostEnvironment::forVM(vm)),
43+
vm(BoostVM::forVM(vm).identifier),
44+
_socket(env.io_service) {
4345
}
4446

4547
template <typename T, typename P>
@@ -75,12 +77,12 @@ void BaseSocketConnection<T, P>::startAsyncWrite(
7577
pointer self = this->shared_from_this();
7678
auto handler = [=] (const boost::system::error_code& error,
7779
size_t bytes_transferred) {
78-
self->boostVM.postVMEvent([=] () {
80+
self->env.postVMEvent(self->vm, [=] (BoostVM& boostVM) {
7981
if (!error) {
80-
self->boostVM.bindAndReleaseAsyncIOFeedbackNode(
82+
boostVM.bindAndReleaseAsyncIOFeedbackNode(
8183
statusNode, bytes_transferred);
8284
} else {
83-
self->boostVM.raiseAndReleaseAsyncIOFeedbackNode(
85+
boostVM.raiseAndReleaseAsyncIOFeedbackNode(
8486
statusNode, "socketOrPipe", "write", error.value());
8587
}
8688
});
@@ -95,7 +97,7 @@ void BaseSocketConnection<T, P>::readHandler(
9597
const ProtectedNode& tailNode, const ProtectedNode& statusNode) {
9698

9799
pointer self = this->shared_from_this();
98-
boostVM.postVMEvent([=] () {
100+
env.postVMEvent(vm, [=] (BoostVM& boostVM) {
99101
if (!error) {
100102
VM vm = boostVM.vm;
101103

@@ -104,10 +106,10 @@ void BaseSocketConnection<T, P>::readHandler(
104106
head = buildCons(vm, (nativeint) (unsigned char) _readData[i-1],
105107
std::move(head));
106108

107-
self->boostVM.bindAndReleaseAsyncIOFeedbackNode(
109+
boostVM.bindAndReleaseAsyncIOFeedbackNode(
108110
statusNode, "succeeded", bytes_transferred, std::move(head));
109111
} else {
110-
self->boostVM.raiseAndReleaseAsyncIOFeedbackNode(
112+
boostVM.raiseAndReleaseAsyncIOFeedbackNode(
111113
statusNode, "socketOrPipe", "read", error.value());
112114
}
113115

vm/boostenv/main/boostvm-decl.hh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class BoostEnvironment;
4444
// BoostVM //
4545
/////////////
4646

47+
// All member functions are called by the thread running the VM.
48+
4749
class BoostVM : VirtualMachine {
4850
public:
4951
BoostVM(BoostEnvironment& environment, VMIdentifier parent,
@@ -69,11 +71,9 @@ private:
6971
static std::uint64_t bytes2uint64(const std::uint8_t* bytes);
7072

7173
// VM Port
72-
public:
74+
private:
7375
bool streamAsked();
74-
75-
bool portClosed();
76-
76+
public:
7777
UnstableNode getStream();
7878

7979
void closeStream();
@@ -121,9 +121,10 @@ public:
121121
LT&& label, Args&&... args);
122122

123123
// Notification from asynchronous work
124-
public:
124+
private:
125+
friend class BoostEnvironment;
125126
inline
126-
void postVMEvent(std::function<void()> callback);
127+
void postVMEvent(std::function<void(BoostVM&)> callback);
127128

128129
// GC
129130
public:
@@ -145,10 +146,11 @@ private:
145146
boost::uuids::random_generator uuidGenerator;
146147

147148
// VM stream
149+
public:
150+
std::atomic_bool portClosed;
148151
private:
149152
StableNode* _headOfStream;
150153
StableNode* _stream;
151-
std::atomic_bool _portClosed;
152154

153155
// Number of asynchronous IO nodes - used for termination detection
154156
private:
@@ -166,12 +168,11 @@ private:
166168

167169
// IO-driven events that must work with the VM store
168170
private:
169-
std::queue<std::function<void()> > _vmEventsCallbacks;
171+
std::queue<std::function<void(BoostVM&)> > _vmEventsCallbacks;
170172

171173
// Monitors
172174
private:
173175
std::vector<VMIdentifier> _monitors;
174-
boost::mutex _monitorsMutex;
175176

176177
// Termination
177178
private:

0 commit comments

Comments
 (0)