summaryrefslogtreecommitdiff
path: root/src/mongo/scripting/mozjs/countdownlatch.cpp
blob: 1e3974b89591dca5707db8a27f04038ff1de7863 (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
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include "mongo/scripting/mozjs/countdownlatch.h"

#include <cmath>

#include "mongo/platform/mutex.h"
#include "mongo/scripting/mozjs/implscope.h"
#include "mongo/scripting/mozjs/objectwrapper.h"
#include "mongo/scripting/mozjs/valuewriter.h"
#include "mongo/stdx/condition_variable.h"
#include "mongo/stdx/unordered_map.h"

namespace mongo {
namespace mozjs {

const char* const CountDownLatchInfo::className = "CountDownLatch";

const JSFunctionSpec CountDownLatchInfo::methods[5] = {
    MONGO_ATTACH_JS_FUNCTION(_new),
    MONGO_ATTACH_JS_FUNCTION(_await),
    MONGO_ATTACH_JS_FUNCTION(_countDown),
    MONGO_ATTACH_JS_FUNCTION(_getCount),
    JS_FS_END,
};

/**
 * The global CountDownLatch holder.
 *
 * Provides an interface for communicating between JSThread's
 */
class CountDownLatchHolder {
public:
    CountDownLatchHolder() : _counter(0) {}

    int32_t make(int32_t count) {
        uassert(ErrorCodes::JSInterpreterFailure, "argument must be >= 0", count >= 0);
        stdx::lock_guard<Latch> lock(_mutex);

        int32_t desc = ++_counter;
        _latches.insert(std::make_pair(desc, std::make_shared<CountDownLatch>(count)));

        return desc;
    }

    void await(int32_t desc) {
        auto latch = get(desc);
        stdx::unique_lock<Latch> lock(latch->mutex);

        while (latch->count != 0) {
            latch->cv.wait(lock);
        }
    }

    void countDown(int32_t desc) {
        auto latch = get(desc);
        stdx::unique_lock<Latch> lock(latch->mutex);

        if (latch->count > 0)
            latch->count--;

        if (latch->count == 0)
            latch->cv.notify_all();
    }

    int32_t getCount(int32_t desc) {
        auto latch = get(desc);
        stdx::unique_lock<Latch> lock(latch->mutex);

        return latch->count;
    }

private:
    /**
     * Latches for communication between threads
     */
    struct CountDownLatch {
        CountDownLatch(int32_t count) : count(count) {}

        Mutex mutex = MONGO_MAKE_LATCH("Latch::mutex");
        stdx::condition_variable cv;
        int32_t count;
    };

    std::shared_ptr<CountDownLatch> get(int32_t desc) {
        stdx::lock_guard<Latch> lock(_mutex);

        auto iter = _latches.find(desc);
        uassert(ErrorCodes::JSInterpreterFailure,
                "not a valid CountDownLatch descriptor",
                iter != _latches.end());

        return iter->second;
    }

    using Map = stdx::unordered_map<int32_t, std::shared_ptr<CountDownLatch>>;

    Mutex _mutex = MONGO_MAKE_LATCH("CountDownLatchHolder::_mutex");
    Map _latches;
    int32_t _counter;
};

namespace {
CountDownLatchHolder globalCountDownLatchHolder;
}  // namespace

/**
 * The argument for _new is a count value. We restrict it to be a 32 bit integer.
 *
 * The argument for _await/_countDown/_getCount is an id for CountDownLatch instance returned from
 * _new call. It must be a 32 bit integer.
 */
auto uassertGet(JS::CallArgs args, unsigned int i) {
    uassert(ErrorCodes::JSInterpreterFailure, "need exactly one argument", args.length() == 1);

    auto int32Arg = args.get(i);
    if (int32Arg.isDouble()) {
        uassert(ErrorCodes::JSInterpreterFailure,
                "argument must not be an NaN",
                !int32Arg.isDouble() || !std::isnan(int32Arg.toDouble()));
        auto val = int32Arg.toDouble();
        uassert(ErrorCodes::JSInterpreterFailure,
                "argument must be a 32 bit integer",
                INT_MIN <= val && val <= INT_MAX);

        return static_cast<int32_t>(val);
    }

    uassert(
        ErrorCodes::JSInterpreterFailure, "argument must be a 32 bit integer", int32Arg.isInt32());

    return int32Arg.toInt32();
}

void CountDownLatchInfo::Functions::_new::call(JSContext* cx, JS::CallArgs args) {
    args.rval().setInt32(globalCountDownLatchHolder.make(uassertGet(args, 0)));
}

void CountDownLatchInfo::Functions::_await::call(JSContext* cx, JS::CallArgs args) {
    globalCountDownLatchHolder.await(uassertGet(args, 0));

    args.rval().setUndefined();
}

void CountDownLatchInfo::Functions::_countDown::call(JSContext* cx, JS::CallArgs args) {
    globalCountDownLatchHolder.countDown(uassertGet(args, 0));

    args.rval().setUndefined();
}

void CountDownLatchInfo::Functions::_getCount::call(JSContext* cx, JS::CallArgs args) {
    args.rval().setInt32(globalCountDownLatchHolder.getCount(uassertGet(args, 0)));
}

/**
 * We have to do this odd dance here because we need the methods from
 * CountDownLatch to be installed in a plain object as enumerable properties.
 * This is due to the way CountDownLatch is invoked, specifically after being
 * transmitted across our js fork(). So we can't inherit and can't rely on the
 * type. Practically, we also end up wrapping up all of these functions in pure
 * js variants that call down, which makes them bson <-> js safe.
 */
void CountDownLatchInfo::postInstall(JSContext* cx,
                                     JS::HandleObject global,
                                     JS::HandleObject proto) {
    auto objPtr = JS_NewPlainObject(cx);
    uassert(ErrorCodes::JSInterpreterFailure, "Failed to JS_NewPlainObject", objPtr);

    JS::RootedObject obj(cx, objPtr);
    ObjectWrapper objWrapper(cx, obj);
    ObjectWrapper protoWrapper(cx, proto);

    JS::RootedValue val(cx);
    for (auto iter = methods; iter->name; ++iter) {
        invariant(!iter->name.isSymbol());
        ObjectWrapper::Key key(iter->name.string());
        protoWrapper.getValue(key, &val);
        objWrapper.setValue(key, val);
    }

    val.setObjectOrNull(obj);
    ObjectWrapper(cx, global).setValue("CountDownLatch", val);
}

}  // namespace mozjs
}  // namespace mongo