| 29 | In !MapGuide this issue is addressed by having all the unmanaged API classes extend from !MgGuardDisposable, which uses a mutex to limit access to the ref-count member to one thread at a time. |
| 30 | |
| 31 | A similar solution will be implemented for the FDO API, with a slight variation. The FDO API will use !InterlockedIncrement and !InterlockedDecrement as opposed to a mutex. Also, the use of these thread safe functions will be limited to FDO objects that are wrapped by a Managed object wrapper. |
| 32 | |
| 33 | Here are the detailed steps that will need to be taken to implement the solution: |
| 34 | |
| 35 | * Add a flag to FdoIDisposable (Boolean member variable) to indicate if Interlocked support should be enabled for FDO disposable objects |
| 36 | * Add a pure virtual method on FdoIDispoable that will indicate if Interlocked access should be enabled. E.g. bool !EnableInterlocked() |
| 37 | * Add Interlocked calls to !AddRef and Release (!GetRefCount) that will only be if the boolean flag set by !EnableInterlocked() is True. |
| 38 | * Change the Managed FDO API's MgFdoIDisposable class to call !EnableInterlocked whenever creating an FDO managed wrapper object around an FDO unmanaged object. |
| 39 | |
| 40 | This solution should provide thread safe access for clients using the FDO Managed API while allowing those who use the unmanaged FDO API directly to avoid the added overhead of Interlocked support. |
| 41 | |
| 42 | Managed API callers will experience some performance impact as outlined below. All clients will incur a larger memory allocation/overhead due to the addition of the Boolean variable. |
| 43 | |
| 44 | NOTE: Timing tests were made to test the impact of implementing an Interlocked strategy vs using a Mutex. The testing involved repeatedly calling !AddRef and Release on a single unmanaged FDO object. 100 million repetitions ( 100 million !AddRef and 100 million Release calls) were done using a release build. 3 different cases were tried: |
| 45 | |
| 46 | * Unguarded !AddRef and Release (as is now) |
| 47 | * !AddRef and Release guarded by Mutex |
| 48 | * !AddRef and Release implemented by !InterlockedIncrement and !InterlockedDecrement. |
| 49 | |
| 50 | The times in seconds were: |
| 51 | |
| 52 | * Unguarded: 0.876 sec |
| 53 | * With Mutex: 4.034 sec |
| 54 | * Interlocked: 2.314 sec |
| 55 | |