Skip to content

Commit

Permalink
Merge pull request #145 from dorodnic/context-lifetime
Browse files Browse the repository at this point in the history
Let multiple users share ownership over rs_context object
  • Loading branch information
dorodnic committed May 22, 2016
2 parents 2d72a7a + 5f1476e commit bff3ab5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
30 changes: 22 additions & 8 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include "uvc.h"
#include "r200.h"
#include "f200.h"
#include <mutex>

rs_context::rs_context() : rs_context(0)
rs_context::rs_context()
{
context = rsimpl::uvc::create_context();

Expand All @@ -27,18 +28,31 @@ rs_context::rs_context() : rs_context(0)
}
}

// Enforce singleton semantics on rs_context
// Enforce singleton semantics on rs_context
rs_context* rs_context::instance = nullptr;
int rs_context::ref_count = 0;
std::mutex rs_context::instance_lock;

bool rs_context::singleton_alive = false;
rs_context* rs_context::acquire_instance()
{
std::lock_guard<std::mutex> lock(instance_lock);
if (ref_count++ == 0)
{
instance = new rs_context();
}
return instance;
}

rs_context::rs_context(int)
void rs_context::release_instance()
{
if(singleton_alive) throw std::runtime_error("rs_context has singleton semantics, only one may exist at a time");
singleton_alive = true;
std::lock_guard<std::mutex> lock(instance_lock);
if (--ref_count == 0)
{
delete instance;
}
}

rs_context::~rs_context()
{
assert(singleton_alive);
singleton_alive = false;
assert(ref_count == 0);
}
8 changes: 6 additions & 2 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ struct rs_context

rs_context();
~rs_context();

static rs_context* acquire_instance();
static void release_instance();
private:
rs_context(int);
static bool singleton_alive;
static int ref_count;
static std::mutex instance_lock;
static rs_context* instance;
};

#endif
4 changes: 2 additions & 2 deletions src/rs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ namespace rsimpl
rs_context * rs_create_context(int api_version, rs_error ** error) try
{
if (api_version != RS_API_VERSION) throw std::runtime_error("api version mismatch");
return new rs_context();
return rs_context::acquire_instance();
}
HANDLE_EXCEPTIONS_AND_RETURN(nullptr, api_version)

void rs_delete_context(rs_context * context, rs_error ** error) try
{
VALIDATE_NOT_NULL(context);
delete context;
rs_context::release_instance();
}
HANDLE_EXCEPTIONS_AND_RETURN(, context)

Expand Down
2 changes: 2 additions & 0 deletions unit-tests/unit-tests-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class safe_context
}
}

bool operator == (const safe_context& other) const { return context == other.context; }

operator rs_context * () const { return context; }
};

Expand Down
3 changes: 2 additions & 1 deletion unit-tests/unit-tests-offline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,6 @@ TEST_CASE( "rs_create_context() returns a valid context", "[offline] [validation
TEST_CASE( "rs_context has singleton semantics", "[offline] [validation]" )
{
safe_context ctx;
REQUIRE(rs_create_context(RS_API_VERSION, require_error("rs_context has singleton semantics, only one may exist at a time")) == nullptr);
safe_context second_ctx;
REQUIRE(second_ctx == ctx);
}

0 comments on commit bff3ab5

Please sign in to comment.