summaryrefslogtreecommitdiff
path: root/matlab/src/IceMatlab/Future.cpp
blob: dc439d0cd34874576a69092d376b9c05314193d0 (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
// **********************************************************************
//
// Copyright (c) 2003-2017 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.
//
// **********************************************************************

#define EXPORT_FCNS

#include "icematlab.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(!isFinished())
    {
        _token = std::move(t);
    }
}

bool
IceMatlab::Future::waitUntilFinished()
{
    unique_lock<mutex> lock(_mutex);
    _cond.wait(lock, [this]{ return this->isFinished(); });
    return !_exception;
}

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()
{
}

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

//
// SimpleFuture
//
IceMatlab::SimpleFuture::SimpleFuture() :
    _done(false)
{
}

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

string
IceMatlab::SimpleFuture::state() const
{
    lock_guard<mutex> lock(const_cast<mutex&>(_mutex));
    if(_exception || _done)
    {
        return "finished";
    }
    else
    {
        return "running";
    }
}

bool
IceMatlab::SimpleFuture::isFinished() const
{
    return _done || _exception;
}

#define SFSELF (*(reinterpret_cast<shared_ptr<SimpleFuture>*>(self)))

extern "C"
{

EXPORTED_FUNCTION mxArray*
Ice_SimpleFuture_unref(void* self)
{
    delete &SFSELF;
    return 0;
}

EXPORTED_FUNCTION mxArray*
Ice_SimpleFuture_wait(void* self, unsigned char* ok)
{
    // TBD: Timeout?

    bool b = SFSELF->waitUntilFinished();
    *ok = b ? 1 : 0;
    return 0;
}

EXPORTED_FUNCTION mxArray*
Ice_SimpleFuture_state(void* self)
{
    return createResultValue(createStringFromUTF8(SFSELF->state()));
}

EXPORTED_FUNCTION mxArray*
Ice_SimpleFuture_cancel(void* self)
{
    SFSELF->cancel();
    return 0;
}

EXPORTED_FUNCTION mxArray*
Ice_SimpleFuture_check(void* self)
{
    if(!SFSELF->waitUntilFinished())
    {
        assert(SFSELF->getException());
        try
        {
            rethrow_exception(SFSELF->getException());
        }
        catch(const std::exception& ex)
        {
            //
            // The C++ object won't be used after this.
            //
            delete &SFSELF;
            return convertException(ex);
        }
    }

    //
    // The C++ object won't be used after this.
    //
    delete &SFSELF;

    return 0;
}

}