login
Header Space

 
 

Re: thread scheduling at mutex unlock

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: David Xu <davidxu@...>, Brent Casavant <b.j.casavant@...>
Cc: <freebsd-stable@...>, <freebsd-threads@...>
Date: Thursday, May 15, 2008 - 4:35 am

on 15/05/2008 07:22 David Xu said the following:

Brent, David,

thank you for the responses.
I think I incorrectly formulated my original concern.
It is not about behavior at mutex unlock but about behavior at mutex 
re-lock. You are right that waking waiters at unlock would hurt 
performance. But I think that it is not "fair" that at re-lock former 
owner gets the lock immediately and the thread that waited on it for 
longer time doesn't get a chance.

Here's a more realistic example than the mock up code.
Say you have a worker thread that processes queued requests and the load 
is such that there is always something on the queue. Thus the worker 
thread doesn't ever have to block waiting on it.
And let's say that there is a GUI thread that wants to convey some 
information to the worker thread. And for that it needs to acquire some 
mutex and "do something".
With current libthr behavior the GUI thread would never have a chance to 
get the mutex as worker thread would always be a winner (as my small 
program shows).
Or even more realistic: there should be a feeder thread that puts things 
on the queue, it would never be able to enqueue new items until the 
queue becomes empty if worker thread's code looks like the following:

while(1)
{
pthread_mutex_lock(&work_mutex);
while(queue.is_empty())
	pthread_cond_wait(...);
//dequeue item
...
pthread_mutex_unlock(&work mutex);
//perform some short and non-blocking processing of the item
...
}

Because the worker thread (while the queue is not empty) would never 
enter cond_wait and would always re-lock the mutex shortly after 
unlocking it.

So while improving performance on small scale this mutex re-acquire-ing 
unfairness may be hurting interactivity and thread concurrency and thus 
performance in general. E.g. in the above example queue would always be 
effectively of depth 1.
Something about "lock starvation" comes to mind.

So, yes, this is not about standards, this is about reasonable 
expectations about thread concurrency behavior in a particular 
implementation (libthr).
I see now that performance advantage of libthr over libkse came with a 
price. I think that something like queued locks is needed. They would 
clearly reduce raw throughput performance, so maybe that should be a new 
(non-portable?) mutex attribute.

-- 
Andriy Gapon
_______________________________________________
freebsd-threads@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-threads
To unsubscribe, send any mail to "freebsd-threads-unsubscribe@freebsd.org"
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
thread scheduling at mutex unlock, Andriy Gapon, (Wed May 14, 11:17 am)
Re: thread scheduling at mutex unlock, Brent Casavant, (Thu May 15, 1:16 am)
Re: thread scheduling at mutex unlock, David Xu, (Thu May 15, 12:22 am)
Re: thread scheduling at mutex unlock, Andriy Gapon, (Thu May 15, 4:35 am)
Re: thread scheduling at mutex unlock, Alfred Perlstein, (Fri May 16, 4:15 pm)
Re: thread scheduling at mutex unlock, Brent Casavant, (Fri May 16, 5:17 pm)
Re: thread scheduling at mutex unlock, Daniel Eischen, (Fri May 16, 5:55 pm)
Re: thread scheduling at mutex unlock, Alfred Perlstein, (Fri May 16, 9:50 pm)
Re: thread scheduling at mutex unlock, Daniel Eischen, (Sat May 17, 1:11 am)
RE: thread scheduling at mutex unlock, David Schwartz, (Thu May 15, 4:25 pm)
Re: thread scheduling at mutex unlock, Brent Casavant, (Thu May 15, 3:51 pm)
Re: thread scheduling at mutex unlock, Andriy Gapon, (Thu May 15, 5:02 pm)
Re: thread scheduling at mutex unlock, Brent Casavant, (Thu May 15, 6:23 pm)
Re: thread scheduling at mutex unlock, Daniel Eischen, (Thu May 15, 9:24 am)
Re: thread scheduling at mutex unlock, Daniel Eischen, (Thu May 15, 1:54 pm)
Re: thread scheduling at mutex unlock, David Xu, (Thu May 15, 5:05 am)
Re: thread scheduling at mutex unlock, Andriy Gapon, (Thu May 15, 5:27 am)
Re: thread scheduling at mutex unlock, David Xu, (Thu May 15, 8:57 am)
Re: thread scheduling at mutex unlock, Andriy Gapon, (Thu May 15, 4:48 pm)
RE: thread scheduling at mutex unlock, David Schwartz, (Wed May 14, 8:29 pm)
Re: thread scheduling at mutex unlock, Andriy Gapon, (Wed May 14, 2:50 pm)
speck-geostationary