none
Synchronization among all devices from a driver RRS feed

  • Question

  • Hi,

    I'm wondering how to share a variable accessible from many devices managed by a driver.

    As an example, think about a variable that tells if device#n is present or not. That is OK using bit array with InterlockedOr/InterlockedAnd for a 32 or 64bit variable but what if the number of devices can be larger?

    Note that I would like to achieve a shared variable/array with no hole (i.e. if device 2 out of 10 becomes absent, next device will get number 2). So I can't use InterLockedDecrement/InterLockedIncrement.

    What I would need is kind of spin lock at driver level, but AFAIK, there is no possible such thing in DRIVER_EXTENSION.

    Any clue?

    Thanks.

    Monday, August 5, 2013 1:22 PM

Answers

  • Note that I would like to achieve a shared variable/array with no hole (i.e. if device 2 out of 10 becomes absent, next device will get number 2).

    You don't need to mess with low-level interlocked integer primitives to build this.  See RtlFindClearBitsAndSet.


    Monday, August 5, 2013 6:35 PM
  • Since this is all within one driver, just use global variables for the bitset and the spinlock.  While most of the information about drivers warns against global's this is because the authors are thinking of per device data.   Driver wide data is fine for a global variable.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Monday, August 5, 2013 1:45 PM

All replies

  • Since this is all within one driver, just use global variables for the bitset and the spinlock.  While most of the information about drivers warns against global's this is because the authors are thinking of per device data.   Driver wide data is fine for a global variable.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Monday, August 5, 2013 1:45 PM
  • hum, thinking about it:

    what about a global variable as a byte array protected by a global spinlock initialized in DriverEntry before any AddDevice fires?

    Monday, August 5, 2013 1:45 PM
  • Hi Don,

    thanks for the response I posted in the same minute as well :-)

    Monday, August 5, 2013 2:01 PM
  • Note that I would like to achieve a shared variable/array with no hole (i.e. if device 2 out of 10 becomes absent, next device will get number 2).

    You don't need to mess with low-level interlocked integer primitives to build this.  See RtlFindClearBitsAndSet.


    Monday, August 5, 2013 6:35 PM
  • Hi Jeffrey,

    thanks for the hint, nice API to use.

    Have a nice day.

    Tuesday, August 6, 2013 2:54 PM