login
Header Space

 
 

Re: thread scheduling at mutex unlock

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Andriy Gapon <avg@...>
Cc: <freebsd-stable@...>, David Xu <davidxu@...>, Brent Casavant <b.j.casavant@...>, <freebsd-threads@...>
Date: Thursday, May 15, 2008 - 1:54 pm

On Thu, 15 May 2008, Daniel Eischen wrote:


I suppose you could also enforce your own scheduling with
something like the following:

 	pthread_cond_t	writer_cv;
 	pthread_cond_t	reader_cv;
 	pthread_mutex_t	q_mutex;
 	...
 	thingy_q_t	thingy_q;
 	int		writers_waiting = 0;
 	int		readers_waiting = 0;
 	...

 	void
 	enqueue(thingy_t *thingy)
 	{
 		pthread_mutex_lock(q_mutex);
 		/* Insert into thingy q */
 		...
 		if (readers_waiting > 0) {
 			pthread_cond_broadcast(&reader_cv, &q_mutex);
 			readers_waiting = 0;
 		}
 		while (thingy_q.size > ENQUEUE_THRESHOLD_HIGH) {
 			writers_waiting++;
 			pthread_cond_wait(&writer_cv, &q_mutex);
 		}
 		pthread_mutex_unlock(&q_mutex);
 	}

 	thingy_t *
 	dequeue(void)
 	{
 		thingy_t *thingy;

 		pthread_mutex_lock(&q_mutex);
 		while (thingy_q.size == 0) {
 			readers_waiting++;
 			pthread_cond_wait(&reader_cv, &q_mutex);
 		}
 		/* Dequeue thingy */
 		...

 		if ((writers_waiting > 0)
 		    && thingy_q.size < ENQUEUE_THRESHOLD_LOW)) {
 			/* Wakeup the writers. */
 			pthread_cond_broadcast(&writer_cv, &q_mutex);
 			writers_waiting = 0;
 		}
 		pthread_mutex_unlock(&q_mutex);
 		return (thingy);
 	}

The above is completely untested and probably contains some
bugs ;-)

You probably shouldn't need anything like that if the kernel
scheduler is scheduling your threads fairly.

-- 
DE
_______________________________________________
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