summaryrefslogtreecommitdiff
path: root/matlab/src/Future.cpp
blob: ef6908ce832dea03f71026726d05cd90208e9438 (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-present ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include "ice.h"
#include "Future.h"
#include "Util.h"

using namespace std;
using namespace IceMatlab;

void
IceMatlab::Future::token(function<void()> t)
{
    lock_guard<mutex> lock(_mutex);
    if(stateImpl() != State::Finished)
    {
        _token = std::move(t);
    }
}

bool
IceMatlab::Future::waitForState(State state, double timeout)
{
    unique_lock<mutex> lock(_mutex);
    if(timeout < 0)
    {
        _cond.wait(lock, [this, state]{ return state == this->stateImpl(); });
        return !_exception;
    }
    else
    {
        auto now = chrono::system_clock::now();
        auto stop = now + chrono::milliseconds(static_cast<long long>(timeout * 1000));
        bool b = _cond.wait_until(lock, stop, [this, state]{ return state == this->stateImpl(); });
        return b && !_exception;
    }
}

bool
IceMatlab::Future::waitForState(const string& s, double timeout)
{
    State state;
    if(s == "running")
    {
        state = State::Running;
    }
    else if(s == "sent")
    {
        state = State::Sent;
    }
    else if(s == "finished")
    {
        state = State::Finished;
    }
    else
    {
        throw std::invalid_argument("state must be one of 'running', 'sent' or 'finished'");
    }
    return waitForState(state, timeout);
}

void
IceMatlab::Future::exception(exception_ptr e)
{
    lock_guard<mutex> lock(_mutex);
    _token = nullptr;
    _exception = e;
    _cond.notify_all();
}

exception_ptr
IceMatlab::Future::getException() const
{
    lock_guard<mutex> lock(const_cast<mutex&>(_mutex));
    return _exception;
}

void
IceMatlab::Future::sent()
{
}

string
IceMatlab::Future::state() const
{
    lock_guard<mutex> lock(const_cast<mutex&>(_mutex));
    string st;
    switch(stateImpl())
    {
        case State::Running:
            st = "running";
            break;
        case State::Sent:
            st = "sent";
            break;
        case State::Finished:
            st = "finished";
            break;
    }
    return st;
}

void
IceMatlab::Future::cancel()
{
    lock_guard<mutex> lock(_mutex);
    if(_token)
    {
        _token();
        _token = nullptr;
    }
}

//
// SimpleFuture
//
IceMatlab::SimpleFuture::SimpleFuture() :
    _state(State::Running)
{
}

void
IceMatlab::SimpleFuture::done()
{
    lock_guard<mutex> lock(_mutex);
    _state = State::Finished;
    _cond.notify_all();
}

IceMatlab::Future::State
IceMatlab::SimpleFuture::stateImpl() const
{
    if(_exception)
    {
        return State::Finished;
    }
    else
    {
        return _state;
    }
}

extern "C"
{

mxArray*
Ice_SimpleFuture_unref(void* self)
{
    delete reinterpret_cast<shared_ptr<SimpleFuture>*>(self);
    return 0;
}

mxArray*
Ice_SimpleFuture_wait(void* self)
{
    bool b = deref<SimpleFuture>(self)->waitForState("finished", -1);
    return createResultValue(createBool(b));
}

mxArray*
Ice_SimpleFuture_waitState(void* self, mxArray* st, double timeout)
{
    try
    {
        string s = getStringFromUTF16(st);
        bool b = deref<SimpleFuture>(self)->waitForState(s, timeout);
        return createResultValue(createBool(b));
    }
    catch(const std::exception& ex)
    {
        return createResultException(convertException(ex));
    }
}

mxArray*
Ice_SimpleFuture_state(void* self)
{
    return createResultValue(createStringFromUTF8(deref<SimpleFuture>(self)->state()));
}

mxArray*
Ice_SimpleFuture_cancel(void* self)
{
    deref<SimpleFuture>(self)->cancel();
    return 0;
}

mxArray*
Ice_SimpleFuture_check(void* self)
{
    auto f = deref<SimpleFuture>(self);
    if(!f->waitForState(Future::State::Finished, -1))
    {
        assert(f->getException());
        try
        {
            rethrow_exception(f->getException());
        }
        catch(const std::exception& ex)
        {
            //
            // The C++ object won't be used after this.
            //
            delete reinterpret_cast<shared_ptr<SimpleFuture>*>(self);
            return convertException(ex);
        }
    }

    //
    // The C++ object won't be used after this.
    //
    delete reinterpret_cast<shared_ptr<SimpleFuture>*>(self);

    return 0;
}

}