caContext: Restore existing context in constructor
Don't assume other code might be using our context. Fix destructor to reattach the right context.
This commit is contained in:

committed by
mdavidsaver

parent
b0b2228558
commit
345f2782cd
@ -16,23 +16,31 @@ namespace ca {
|
||||
|
||||
CAContext::CAContext()
|
||||
{
|
||||
ca_client_context *thread_context = ca_current_context();
|
||||
if (thread_context)
|
||||
ca_detach_context();
|
||||
int result = ca_context_create(ca_enable_preemptive_callback);
|
||||
if (result != ECA_NORMAL)
|
||||
throw std::runtime_error("Can't create CA context");
|
||||
|
||||
ca_context = ca_current_context();
|
||||
this->ca_context = ca_current_context();
|
||||
detach(thread_context);
|
||||
}
|
||||
|
||||
ca_client_context* CAContext::attach()
|
||||
{
|
||||
ca_client_context *thread_context = ca_current_context();
|
||||
if (thread_context != ca_context) {
|
||||
if (thread_context)
|
||||
ca_detach_context();
|
||||
if (thread_context)
|
||||
ca_detach_context();
|
||||
|
||||
int result = ca_attach_context(ca_context);
|
||||
if (result != ECA_NORMAL)
|
||||
throw std::runtime_error("Can't attach to CA context");
|
||||
int result = ca_attach_context(this->ca_context);
|
||||
if (result != ECA_NORMAL) {
|
||||
if (thread_context) {
|
||||
result = ca_attach_context(thread_context);
|
||||
if (result != ECA_NORMAL)
|
||||
std::cerr << "Lost thread's CA context" << std::endl;
|
||||
}
|
||||
throw std::runtime_error("Can't attach to my CA context");
|
||||
}
|
||||
return thread_context;
|
||||
}
|
||||
@ -40,7 +48,7 @@ ca_client_context* CAContext::attach()
|
||||
void CAContext::detach(ca_client_context* restore) \
|
||||
{
|
||||
ca_client_context *thread_context = ca_current_context();
|
||||
if (thread_context != ca_context)
|
||||
if (thread_context != this->ca_context)
|
||||
std::cerr << "CA context was changed!" << std::endl;
|
||||
|
||||
ca_detach_context();
|
||||
@ -48,7 +56,7 @@ void CAContext::detach(ca_client_context* restore) \
|
||||
if (restore) {
|
||||
int result = ca_attach_context(restore);
|
||||
if (result != ECA_NORMAL)
|
||||
std::cerr << "Can't re-attach to CA context" << std::endl;
|
||||
std::cerr << "Lost thread's CA context" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,10 +64,11 @@ CAContext::~CAContext()
|
||||
{
|
||||
ca_client_context *thread_context = attach();
|
||||
ca_context_destroy();
|
||||
if (thread_context != ca_context) {
|
||||
int result = ca_attach_context(ca_context);
|
||||
|
||||
if (thread_context) {
|
||||
int result = ca_attach_context(thread_context);
|
||||
if (result != ECA_NORMAL)
|
||||
std::cerr << "Can't re-attach to CA context" << std::endl;
|
||||
std::cerr << "Lost thread's CA context" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user