Re: Threading and Synchronization in Mass storage driver
Re: Threading and Synchronization in Mass storage driver
- Subject: Re: Threading and Synchronization in Mass storage driver
- From: Cliff Russell <email@hidden>
- Date: Tue, 13 Dec 2005 23:10:20 -0800
At a glance I see several problems in the code that you posted. The
line containing:
a_request = new WriteBuffReq[sizeof(WriteBuffReq)];
is allocating an *array* of WriteBuffReq objects (with sizeof
(WriteBuffReq) objects in the array, in-fact it's actually allocating
sizeof(WriteBuffReq) ^ 2 bytes here), is that really what you want?
Wouldn't the following be sufficient
a_request = new WriteBuffReq;
In the first iteration of the loop, the line containing:
a_request = objDriver->get_request(request_mutex);
Is leaking the large array that you previously allocated, probably
not what you intended (if the request is coming from get_request you
probably don't need to allocate a WriteBuffReq at all).
And, I can't see what - if anything request_mutex is actually
protecting. If com_My_driver::handle_request_loop is executed on
multiple threads, each thread will have it's own request_mutex object
- there will be no synchronization between the threads. If
com_My_driver::handle_request_loop is run only on a single thread,
request_mutex doesn't seem to have an obvious purpose either. Nobody
will ever be able to signal request_mutex because it it a stack
variable, the IOLockSleep will sleep forever.
As for what is crashing - nothing obvious stood out. But, I don't
think it really matters at this point as the algorithm is flawed.
The real issue is IOKit probably does everything you are trying to
recreate, and quite elegantly too. Your time would be best spent
figuring out how to build this part of your driver with the already
provided IOKit classes.
On 13-Dec-05, at 5:49 AM, Yogesh P wrote:
Hi Herb,
As per your suggestion I have tried using IOLockSleep() in my driver
code to put the current running thread in wait.
But when these API gets called my driver crashes.
The Code snippet for the same is as follow:
void* com_My_driver::handle_request_loop(void *data)
{
com_My_driver* objDriver;
WriteBuffReq* a_request; /* pointer to a request.*/
a_request = new WriteBuffReq[sizeof(WriteBuffReq)];
int thread_id = *((int*) data); /* thread identifying
number */
request_mutex = IOLockAlloc();
/* lock the mutex, to access the requests list exclusively. */
IOLockLock(request_mutex);
/* do forever... */
while (1)
{
if(num_requests > 0) /* a request is pending */
{
a_request = objDriver->get_request(request_mutex);
// get the write request node from the linklist
if(a_request) /* got a request - handle it and free
it */
{
IOLockUnlock(request_mutex);
objDriver->handle_request(a_request, thread_id);
// handle the incoming write request
delete[] a_request;
/* and lock the mutex again. */
IOLockLock(request_mutex);
} /* if(a_request) */
} /* if(num_requests > 0) */
else
{
/* wait for a request to arrive. note the mutex will be */
/* unlocked here, thus allowing other threads access to */
/* requests list. */
IOLog("handle_request_loop():: thread '%d' before
IOLockSleep\n", thread_id);
IOLockSleep(request_mutex, (void*) thread_id, 0);
// Driver Crashing Here
/* and after we return from IOLockSleep, the mutex */
/* is locked again */
} /* else */
} /* while */
}
Is that I am doing something wrong in using these API's ??
Looking for your valuable suggestions.
Thanks & Regards,
Yogesh Pahilwan
On Fri, 2005-12-09 at 00:04, Herb Petschauer wrote:
No worries, the less confusion that my addled brain is exposed to,
the better...
Have you looked at IOLockSleep()? Same header file as IOLockLock().
Depending on what kind of driver you are implementing you may want to
search the docs and the list archives (and developer.apple.com) for
"primary interrupt" and "secondary interrupt".
Cheers,
H.
On 08 Dec 2005 18:31:02 +0530, Yogesh P
<email@hidden> wrote:
Hi Herb,
Sorry Herb.
Now I am replying to the old thread.
I am actually implementing a threading and synchronization
mechanism for
accessing a request queue which is a queue of write data structure
having singly linklist.
I am creating kernel threads in my init() driver function. These
threads
access the request queue paralelly by using IOKit's IOLock
synchronization primitives.
Each thread acquire the lock calling IOLockLock() function and
check the
request queue whether it is empty or not. If the request queue is
empty
then the thread release the lock by calling IOLockUnLock()
function. So
that other threads can access the request queue.
After loading the driver, threads get created successfully and each
thread goes in a big while loop and the loop goes on infinitely.
Now I have to put each thread on a conditional wait. The conditional
wait means that if there is no request in the request queue the
thread
delays for a nanosecond and release the lock from the request
queue for
that specified time delay, and as soon as the request comes in the
request queue, thread wake ups, acquire the lock and process that
specified request.
Is there any mechanism in IOKit so that I can put the thread on
conditional wait ??
After that condition met, wake up that thread to process the
specified
request ??
Thanks in advance,
Yogesh Pahilwan
On Wed, 2005-12-07 at 22:01, Herb Petschauer wrote:
Again, this email seems to be a new thread. Why not just reply
to the
old thread?
Anway: http://developer.apple.com/documentation/Darwin/
Conceptual/KernelProgramming/services/chapter_16_section_2.html
You will find the entire document enlightening actually :-)
H.
On 07 Dec 2005 18:22:18 +0530, Yogesh P
<email@hidden> wrote:
Hi Herb,
Thanks for your valuable suggestion about the threading and
synchronization in Mac OS X.
I am also thankful to Garth for his suggestions.
Now my driver is not getting crash when I call the
IOCreateThread()
function.
Is there any API to put the kernel thread in a wait for a
nanosecond in
sleep in IOKit??
Thanks,
Yogesh
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40atimi.com
This email sent to email@hidden
_______________________________________________
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