Re: kernel thread join?
Re: kernel thread join?
- Subject: Re: kernel thread join?
- From: "Duane Murphy" <email@hidden>
- Date: Fri, 18 Apr 2008 08:45:54 -0700
--- At Fri, 18 Apr 2008 10:54:01 +1000, Godfrey van der Linden wrote:
>msleep/wakeup & commandSleep/commandWakeup are basically equivalent.
>The difference is the locking requirments. Presumably you are using a
>roll-your-own locking mechanism rather than the IOKit::IOWorkLoop
>mechanism. Anyway yes you have found the non-iokit solution to your
>synchronisation problem. However if you do have a driver that is
>using, or client of, a work loop you probably should change over from
>msleep to commandSleep.
Thank you for this recommendation. I am working in an IOKit kext (disk
filter) so I am probably doing something incorrectly here.
Any recommendations for reading on how to use these calls as well as the
workloop would be great. I'm using the workloop for some things, but I
may be missing it for others.
In particular when using msleep/wakeup, I sometimes end up in a dead
lock. There are three threads involved; the background processing
thread, the thread triggering the close of the background processing
thread, and another user land request thread. It appears that when I use
msleep() the other two threads are locked while in a read() call to the
next driver below.
I'm guessing that the closing thread must have some kind of lock that is
causing the others to not continue until it is released. The trigger for
stopping the thread is indirect from other IOStorage objects. Could I be
using the workloop incorrectly (like not at all) that could cause this
problem?
I guess I need to dig into the sources a little more.
Thanks for the help. This is quite useful.
>On 2008-04-15, at 15:06 , Duane Murphy wrote:
>
>> --- At Tue, 15 Apr 2008 14:43:40 +1000, Godfrey van der Linden wrote:
>>
>>> The best IOKit aware equivalent to a condition variable is the
>>> IOCommandGate::commandSleep/commandWakeup APIs.
>>>
>>> However the way you describe the problem the reference counting
>>> solution seems ideal. Especially as it is a data removal cleanup
>>> type
>>> of problem. I assume your problem is that you need to execute some
>>> code to do the data cleanup? Is that correct?
>>>
>>> Can you describe the action you want to take on completion?
>>
>> The fundamental problem is that the case that I am working with, the
>> system is either going to sleep, shutting down, or in general going
>> off-
>> line. I can't return from the call that says "you're going off-line"
>> until the other thread has completed and exited. The final stage of
>> the
>> exit of the other thread, records the information about why the thread
>> exited so that it can be brought back up again appropriately.
>>
>> I'm using msleep() and it seems to be doing the job just great.
>> Conveniently all the pieces were there in place; a variable to track
>> the
>> completion, and a mutex lock for that variable. Enter msleep() with a
>> reasonable timeout and a wakeup() call at the end of the thread, and
>> viola.
>>
>>> On 2008-04-15, at 1:45 , Duane Murphy wrote:
>>>
>>>> --- At Mon, 14 Apr 2008 11:48:06 +1000, Godfrey van der Linden
>>>> wrote:
>>>>
>>>>> There isn't really a 100% reliable way of waiting for a thread to
>>>>> terminate completely. However that isn't really a problem. The
>>>>> key
>>>>> is to recognise what you are really trying to do.
>>>>>
>>>>> Presumably you are trying to clean up resources that the thread may
>>>>> be
>>>>> using?
>>>>>
>>>>> In this case the correct kernel way of cleaning this up is just to
>>>>> reference count your resources. Then it doesn't matter exactly
>>>>> what
>>>>> order the objects get released in. There are a couple of other
>>>>> tricks
>>>>> but the OSObject reference counting will probably get you out of
>>>>> trouble.
>>>>
>>>> Thanks for the idea. Unfortunately, in this case I think I need to
>>>> wait
>>>> for the thread to stop. The thread is processing data. The data is
>>>> going
>>>> to be removed and can't be removed until the processing has been
>>>> halted
>>>> and the removal noted. The waiting thread has received the notice
>>>> that
>>>> the data is going away and should clean up.
>>>>
>>>> I'm going to use a condition variable to signal the end of the
>>>> thread.
>>>>
>>>> Now I need to work out how to do that. Any recommendations for a
>>>> kernel
>>>> condition variable?
>>>>
>>>> Thanks for the ideas,
>>>>
>>>>> On 2008-04-12, at 5:08 , Duane Murphy wrote:
>>>>>
>>>>>> What is the equivalent of pthread_join() for a kernel thread?
>>>>>>
>>>>>> I am using IOThreadCreate() to create a new thread. At some later
>>>>>> point
>>>>>> I indicate to that thread to stop. How do I detect that the thread
>>>>>> as exited?
>>>>
>>>> ...Duane
>>>>
>>>> _______________________________________________
>>>> Do not post admin requests to the list. They will be ignored.
>>>> Darwin-kernel mailing list (email@hidden)
>>>> Help/Unsubscribe/Update your Subscription:
>>>>
>>>> This email sent to email@hidden
>>>
>>>
>>
>> ...Duane
>>
>
>
...Duane
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden