summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2015-02-11 13:25:23 -0800
committerKonstantin Käfer <mail@kkaefer.com>2015-03-06 15:42:33 +0100
commitcf235c65926d72d00d2eb1a6ace9f9ab2d701a5c (patch)
treea3c861ee46a49b8ecafdb0450c2f6cc9289a1b19 /src
parent86455585ee9a2be5aed210411605e712eafd98d7 (diff)
downloadqtlocation-mapboxgl-cf235c65926d72d00d2eb1a6ace9f9ab2d701a5c.tar.gz
move readlock/writelock to uv.hpp and add movable lock/mutex
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/util/uv.cpp55
-rw-r--r--src/mbgl/util/uv_detail.hpp35
2 files changed, 70 insertions, 20 deletions
diff --git a/src/mbgl/util/uv.cpp b/src/mbgl/util/uv.cpp
index a993e6b962..7de960cc85 100644
--- a/src/mbgl/util/uv.cpp
+++ b/src/mbgl/util/uv.cpp
@@ -1,4 +1,5 @@
#include <mbgl/util/uv.hpp>
+#include <mbgl/util/uv_detail.hpp>
#include <uv.h>
@@ -22,6 +23,60 @@ std::string cwd() {
#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));
diff --git a/src/mbgl/util/uv_detail.hpp b/src/mbgl/util/uv_detail.hpp
index 99f5edc145..6ae3713e09 100644
--- a/src/mbgl/util/uv_detail.hpp
+++ b/src/mbgl/util/uv_detail.hpp
@@ -1,6 +1,7 @@
#ifndef MBGL_UTIL_UV_DETAIL
#define MBGL_UTIL_UV_DETAIL
+#include <mbgl/util/uv.hpp>
#include <mbgl/util/uv-worker.h>
#include <mbgl/util/noncopyable.hpp>
@@ -85,6 +86,20 @@ private:
std::function<void ()> fn;
};
+class mutex : public mbgl::util::noncopyable {
+public:
+ inline mutex() {
+ if (uv_mutex_init(&mtx) != 0) {
+ throw std::runtime_error("failed to initialize mutex lock");
+ }
+ }
+ inline ~mutex() { uv_mutex_destroy(&mtx); }
+ inline void lock() { uv_mutex_lock(&mtx); }
+ inline void unlock() { uv_mutex_unlock(&mtx); }
+private:
+ uv_mutex_t mtx;
+};
+
class rwlock : public mbgl::util::noncopyable {
public:
inline rwlock() {
@@ -102,26 +117,6 @@ private:
uv_rwlock_t mtx;
};
-class readlock : public mbgl::util::noncopyable {
-public:
- inline readlock(rwlock &mtx_) : mtx(mtx_) { mtx.rdlock(); }
- inline readlock(const std::unique_ptr<rwlock> &mtx_) : mtx(*mtx_) { mtx.rdlock(); }
- inline ~readlock() { mtx.rdunlock(); }
-
-private:
- rwlock &mtx;
-};
-
-class writelock : public mbgl::util::noncopyable {
-public:
- inline writelock(rwlock &mtx_) : mtx(mtx_) { mtx.wrlock(); }
- inline writelock(const std::unique_ptr<rwlock> &mtx_) : mtx(*mtx_) { mtx.wrlock(); }
- inline ~writelock() { mtx.wrunlock(); }
-
-private:
- rwlock &mtx;
-};
-
class worker : public mbgl::util::noncopyable {
public:
inline worker(uv_loop_t *loop, unsigned int count, const char *name = nullptr) : w(new uv_worker_t) {