summaryrefslogtreecommitdiff
path: root/src/mbgl/util/uv.cpp
blob: 7de960cc85c2cb1b3b4a3ad133e4575af8da3573 (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
#include <mbgl/util/uv.hpp>
#include <mbgl/util/uv_detail.hpp>

#include <uv.h>

namespace uv {

std::string cwd() {
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
    char dir[512];
    uv_cwd(dir, 512);
    return dir;
#else
    size_t max = 0;
    std::string dir;
    do {
        max += 256;
        dir.resize(max);
        uv_cwd(const_cast<char *>(dir.data()), &max);
    } while (max == dir.size());
    dir.resize(max - 1);
    return dir;
#endif
}


lock::lock(mutex &mtx_) : mtx(&mtx_) {
    if (mtx) { mtx->lock(); }
}
lock::lock(const std::unique_ptr<mutex> &mtx_) : mtx(mtx_.get()) {
    if (mtx) { mtx->lock(); }
}
lock::~lock() {
    if (mtx) { mtx->unlock(); }
}
lock::lock(lock &&lock) {
    std::swap(mtx, lock.mtx);
}
lock &lock::operator=(lock &&lock) {
    std::swap(mtx, lock.mtx);
    return *this;
}


readlock::readlock(rwlock &mtx_) : mtx(&mtx_) {
    if (mtx) { mtx->rdlock(); }
}
readlock::readlock(const std::unique_ptr<rwlock> &mtx_) : mtx(mtx_.get()) {
    if (mtx) { mtx->rdlock(); }
}
readlock::~readlock() {
    if (mtx) { mtx->rdunlock(); }
}
readlock::readlock(readlock &&lock) {
    std::swap(mtx, lock.mtx);
}
readlock &readlock::operator=(readlock &&lock) {
    std::swap(mtx, lock.mtx);
    return *this;
}


writelock::writelock(rwlock &mtx_) : mtx(&mtx_) {
    if (mtx) { mtx->wrlock(); }
}
writelock::writelock(const std::unique_ptr<rwlock> &mtx_) : mtx(mtx_.get()) {
    if (mtx) { mtx->wrlock(); }
}
writelock::~writelock() {
    if (mtx) { mtx->wrunlock(); }
}
writelock::writelock(writelock &&lock) {
    std::swap(mtx, lock.mtx);
}
writelock &writelock::operator=(writelock &&lock) {
    std::swap(mtx, lock.mtx);
    return *this;
}

const char *getFileRequestError(uv_fs_t *req) {
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
    return uv_strerror(uv_last_error(req->loop));
#else
    return uv_strerror(int(req->result));
#endif
}

}