none
Using global variable in kmdf driver or passing local variable using wdfrequestsetcompletionroutine? RRS feed

  • Question

  • I am using a Parallel queue for receiving read and write request in my kmdf based filter driver.

    There are 2 questions :

    1)

    Now just before sending read request down. I use a global variable and set it value simply like SomeGlobalVariable = 1 or some other value .

    Now I use the same global variable in completion routine of this read request. Is there any chance the value of this variable get change???

    I mean If this request is sent and before reaching to completion routine if another request comes and change the value before sending another request. Is it possible or any chance of race condition?

    -----------------

    2)

    To avoid the chance of changing the value before it is used in completion routine. I use a local variable in pass the address of this variable to wdfrequestsetcompletionroutine as a last parameter and when I get this value in completion routine it gives fatal error.

    evtiowrite(){

    update the localvariable

    wdfrequestsetcompletionroutine(request, mycompletionroutine, &localvariable);

    wdfrequestsend()

    }

    mycompletionroutine(

    IN WDFREQUEST Request,

    IN WDFIOTARGET Target,

    IN PWDF_REQUEST_COMPLETION_PARAMS Params,

    IN WDFCONTEXT Context

    ){

    INT32 flag;

    flag = *(INT32 *)Context;

    ---

    --

    }

    flag = *(INT32 *)Context; 

    this gives fatal error. what should I do for this ?

    And suppose if I would have used global variable instead of passing local variable then global variable might change before reaching to completion routine?????

    Friday, December 27, 2013 1:37 PM

Answers

  • Yes, the global variable can change if another Io request comes in. You should use the context of the request object instead. Call WdfDeviceInitSetRequestAttributes during your add device routine to have each request allocated by the queue to contain your context type. Store the variable (there can be many) in that context

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, December 27, 2013 4:22 PM

All replies

  • Yes, the global variable can change if another Io request comes in. You should use the context of the request object instead. Call WdfDeviceInitSetRequestAttributes during your add device routine to have each request allocated by the queue to contain your context type. Store the variable (there can be many) in that context

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, December 27, 2013 4:22 PM
  • And if you don't understand why passing a pointer to a local stack variable doesn't work and causes a BSOD, you need to take a step back and understand C and the system you are working in.

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, December 27, 2013 4:23 PM
  • No sir, I do understand but I thought it might copy data from local variable address to some other address that it will hold and will be given to completion routine thats why I got confused.
    Saturday, December 28, 2013 2:07 PM
  • How would kmdf know a) to make a deep copy of the pointer at all (you could pass an index number as the context) or b) how many bytes to deep copy if it did make the copy. By induction you can see that kmdf cant make a copy based on lack of info available to the framework

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Saturday, December 28, 2013 3:03 PM
  • Yes, you are right. kmdf would not be able to make deep copy in this case.
    Saturday, December 28, 2013 7:41 PM
  • What if I create a request and I want to use a context structure?? because WdfDeviceInitSetRequestAttributes will only be applicable for framework created request.
    Tuesday, December 31, 2013 12:33 PM
  • You can specify whatever context you want when creating the request with the object attributes parameter

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Tuesday, December 31, 2013 4:56 PM