-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect ca_client_context operations
Each instance of the caContext class represents a separate CA context, so each CAChannelProvider creates one and keeps a shared_ptr to it, making that available to its channels and channel operations. These also take their own shared_ptr to it as well so the context cannot be destroyed while it might be needed. A related caContext Attach object is intended to be short-lived, and to be allocated on the stack. When created it saves the current CA context for the thread, replacing it from the caContext given to its constructor. CA operations will now use the attached context. When the Attach destructor runs it detaches the thread from the current context (checking still has the expected value) and re-attaches the thread to any context that was saved by the constructor.
- Loading branch information
Showing
8 changed files
with
175 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright - See the COPYRIGHT that is included with this distribution. | ||
* pvAccessCPP is distributed subject to a Software License Agreement found | ||
* in file LICENSE that is included with this distribution. | ||
*/ | ||
|
||
#include <cadef.h> | ||
|
||
#define epicsExportSharedSymbols | ||
#include "caContext.h" | ||
|
||
namespace epics { | ||
namespace pvAccess { | ||
namespace ca { | ||
|
||
CAContext::CAContext() | ||
{ | ||
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(); | ||
} | ||
|
||
ca_client_context* CAContext::attach() | ||
{ | ||
ca_client_context *thread_context = ca_current_context(); | ||
if (thread_context != ca_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"); | ||
} | ||
return thread_context; | ||
} | ||
|
||
void CAContext::detach(ca_client_context* restore) \ | ||
{ | ||
ca_client_context *thread_context = ca_current_context(); | ||
if (thread_context != ca_context) | ||
std::cerr << "CA context was changed!" << std::endl; | ||
|
||
ca_detach_context(); | ||
|
||
if (restore) { | ||
int result = ca_attach_context(restore); | ||
if (result != ECA_NORMAL) | ||
std::cerr << "Can't re-attach to CA context" << std::endl; | ||
} | ||
} | ||
|
||
CAContext::~CAContext() | ||
{ | ||
ca_client_context *thread_context = attach(); | ||
ca_context_destroy(); | ||
if (thread_context != ca_context) { | ||
int result = ca_attach_context(ca_context); | ||
if (result != ECA_NORMAL) | ||
std::cerr << "Can't re-attach to CA context" << std::endl; | ||
} | ||
} | ||
|
||
}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright - See the COPYRIGHT that is included with this distribution. | ||
* pvAccessCPP is distributed subject to a Software License Agreement found | ||
* in file LICENSE that is included with this distribution. | ||
*/ | ||
|
||
#ifndef INC_caContext_H | ||
#define INC_caContext_H | ||
|
||
#include <cadef.h> | ||
#include <pv/pvAccess.h> | ||
|
||
namespace epics { | ||
namespace pvAccess { | ||
namespace ca { | ||
|
||
|
||
class Attach; | ||
class CAContext; | ||
typedef std::tr1::shared_ptr<CAContext> CAContextPtr; | ||
|
||
class CAContext | ||
{ | ||
public: | ||
CAContext(); | ||
~CAContext(); | ||
private: | ||
ca_client_context* ca_context; | ||
|
||
private: // Internal API | ||
friend class Attach; | ||
ca_client_context* attach(); | ||
void detach(ca_client_context* restore); | ||
}; | ||
|
||
class Attach | ||
{ | ||
public: | ||
Attach(const CAContextPtr & to) : | ||
context(to), saved_context(to->attach()) {} | ||
~Attach() { | ||
context->detach(saved_context); | ||
} | ||
private: | ||
CAContextPtr context; | ||
ca_client_context* saved_context; | ||
}; | ||
|
||
}}} | ||
|
||
#endif // INC_caContext_H |
Oops, something went wrong.