Hi Yann, Andrew J. said: The thread protection that you need for that driver doesn't necessarily mean the driver API calls all have to happen within the context of the port thread though, you may just have to create an epicsMutex for the driver
Hi Yann,
Andrew J. said:
- The thread protection that you need for that driver doesn't necessarily mean the driver API calls all have to happen
within the context of the port thread though, you may just have to create an epicsMutex for the driver and ensure that the API routines are only ever called by code that has successfully taken that mutex and releases it afterwards. That could get more complicated
and my advice might be wrong if the driver is written in C++ or is multi-threaded, but the latter at least seems unlikely.
I agree with Andrew, you can probably create the device handle in the thread that creates the asynPort, and use it in the port driver thread. If your communication with
the device happens in the derived method like writeInt32(), writeFloat64(), etc. then those will all be called from the port driver thread, and you don’t even need an additional mutex. If you create another thread in your driver, for example for polling the
device, then you need to surround the calls to the API functions with the asynPortDriver lock() and unlock() methods.
I would definitely suggest trying this simple approach first, and only use a more complex one if this does not work.
Mark
From: Tech-talk <tech-talk-bounces at aps.anl.gov>
On Behalf Of Yann Mandza via Tech-talk
Sent: Tuesday, February 20, 2024 9:09 AM
To: tech-talk at aps.anl.gov
Subject: Asyn connection/communication management with asynPortDriver
Hi, I would like to use the connection/communication management features of Asyn with a TCP/IP based driver using the asynPortDriver class. I have a limitation
to the underline device driver that expose the device has a non-thread-safe object
This Message Is From an External Sender
|
This message came from outside your organization.
|
|
|
I would like to use the connection/communication management features of Asyn with a TCP/IP based driver using the asynPortDriver class.
I have a limitation to the underline device driver that expose the device has a non-thread-safe object , meaning it should only be access from the thread that made the API call
to initiate the device handle.
what I want to do, is to create the device handle instance within the asyn port thread, instead of the main thread. thus, all record init and runtime operation should be performed
from inside the asyn porthread. is that possible?
my understand of asyn port drivers is still cloudy somehow. this works, but the handle is hosted by the main thread.
drvXXTest::drvXXTest( const char *portName, const char *interface, const char *icsCtrtype, epicsInt16 autoConnect )
: asynPortDriver( portName,
asynCommonMask | asynInt32Mask | asynUInt32DigitalMask | asynFloat64Mask | asynDrvUserMask | asynOctetMask,
asynCommonMask | asynInt32Mask | asynUInt32DigitalMask | asynFloat64Mask | asynOctetMask,
ASYN_CANBLOCK, // asynFlags.
autoConnect, // Autoconnect
0 ), // Default stack size
static const char *functionName = "drvXXTest";
pasynManager->exceptionDisconnect( pasynUserSelf );
// How to to call devConnect(deviceSession_, interface_) from port thread?
// as this call run from the main one.
asynStatus status = this->connect( pasynUserSelf );
asynPrint( pasynUserSelf, ASYN_TRACE_ERROR,
"drvXXTest, error calling connect - %s\n",
pasynUserSelf->errorMessage );
asynStatus drvXXTest::connect(asynUser *pasynUser) {
// devConnect instantiate the device handle and open a connection to it.
if( !devConnect(deviceSession_, interface_) ) {
epicsSnprintf( pasynUser->errorMessage,pasynUser->errorMessageSize,
"%s: Can't open %s: %s", deviceSession_, interface_, strerror( errno ) );
pasynManager->exceptionConnect( pasynUser );