Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV
Before Posting...
Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


Fast_mutex in sub function

zinoberdevelopzinoberdevelop Member Posts: 7
edited January 3 in NTDEV

Hi everyone.
I have a function where I add a data to the List_Entry. In my function I call a subfunction(IsDuplicate) to check the whole list so that the data is not duplicate.
I called the ExAcquireFastMutex function in the main function that adds the data. And then I called my subfunction, IsDuplicate.
Does FastMutex work well in subfunction or should we get a new FastMutex?

bool AddItem(List_Entry Entry){
ExAcquireFastMutex (mutex);
if(isnotduplicate(entry)){
inserttaillist(Listhead,Entry);
}
ExReleaseFastMutex(mutex)
}

bool isnotduplicate(List_entry Entry){
//Searches the entire list for the desired data
//if is not duplicate return true
}

Comments

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 13,776

    Did you read the documentation?

    Fast mutexes cannot be acquired recursively. If a thread that is already holding a fast mutex tries to acquire it, that thread will deadlock.

    Your question implies that you do not understand the purpose of the fast mutex. A single mutex generally protects a single resource. It you have two routines access the resource, it does no good at all to hat two mutexes protecting it.

    In your case, you just need to establish a policy that whoever calls isnotduplicate must hold the mutex before calling.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • zinoberdevelopzinoberdevelop Member Posts: 7

    @Tim_Roberts said:
    Did you read the documentation?

    Fast mutexes cannot be acquired recursively. If a thread that is already holding a fast mutex tries to acquire it, that thread will deadlock.

    Your question implies that you do not understand the purpose of the fast mutex. A single mutex generally protects a single resource. It you have two routines access the resource, it does no good at all to hat two mutexes protecting it.

    In your case, you just need to establish a policy that whoever calls isnotduplicate must hold the mutex before calling.

    According to you, the above code example is correct because AddItem locked the Mutex before calling isnotduplicate.
    Right?

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 13,776

    As long as isnotduplicate does not ALSO grab the mutex, yes, this is fine.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • zinoberdevelopzinoberdevelop Member Posts: 7

    @Tim_Roberts said:
    As long as isnotduplicate does not ALSO grab the mutex, yes, this is fine.

    excellent.
    I ask to be sure.
    Considering that we lock mutex in the main function(AddItem), then mutex is locked in the isnotduplicate and no problem occurs?
    Is my data protected in isnotduplicate ? Is not isnotduplicate manipulated by two threads at the same time?

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 13,776

    You have to make a rule. Anyone that calls isnotduplicate must hold the mutex. As long as you follow that rule, then no, it cannot be called by two threads at the same time. One of them will be blocked waiting for the mutex. That's the point of the mutex.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • zinoberdevelopzinoberdevelop Member Posts: 7

    @Tim_Roberts said:
    You have to make a rule. Anyone that calls isnotduplicate must hold the mutex. As long as you follow that rule, then no, it cannot be called by two threads at the same time. One of them will be blocked waiting for the mutex. That's the point of the mutex.

    thank you so much

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Writing WDF Drivers 7 Dec 2020 LIVE ONLINE
Internals & Software Drivers 25 Jan 2021 LIVE ONLINE
Developing Minifilters 8 March 2021 LIVE ONLINE