diff options
author | John Birrell <jb@FreeBSD.org> | 1999-03-23 05:07:56 +0000 |
---|---|---|
committer | John Birrell <jb@FreeBSD.org> | 1999-03-23 05:07:56 +0000 |
commit | 58a7cc5d1be640708d2c5e05bf48a8ae379a8d3e (patch) | |
tree | eea0761f32968ad5456e1c13f86e990df50fa554 /lib/libpthread/thread/thr_create.c | |
parent | f5a78334492dccb1f3e9debe40f6e1deb14638fd (diff) | |
download | src-58a7cc5d1be640708d2c5e05bf48a8ae379a8d3e.tar.gz src-58a7cc5d1be640708d2c5e05bf48a8ae379a8d3e.zip |
[ The author's description... ]
o Runnable threads are now maintained in priority queues. The
implementation requires two things:
1.) The priority queues must be protected during insertion
and removal of threads. Since the kernel scheduler
must modify the priority queues, a spinlock for
protection cannot be used. The functions
_thread_kern_sched_defer() and _thread_kern_sched_undefer()
were added to {un}defer kernel scheduler activation.
2.) A thread (active) priority change can be performed only
when the thread is removed from the priority queue. The
implementation uses a threads active priority when
inserting it into the queue.
A by-product is that thread switches are much faster. A
separate queue is used for waiting and/or blocked threads,
and it is searched at most 2 times in the kernel scheduler
when there are active threads. It should be possible to
reduce this to once by combining polling of threads waiting
on I/O with the loop that looks for timed out threads and
the minimum timeout value.
o Functions to defer kernel scheduler activation were added. These
are _thread_kern_sched_defer() and _thread_kern_sched_undefer()
and may be called recursively. These routines do not block the
scheduling signal, but latch its occurrence. The signal handler
will not call the kernel scheduler when the running thread has
deferred scheduling, but it will be called when running thread
undefers scheduling.
o Added support for _POSIX_THREAD_PRIORITY_SCHEDULING. All the
POSIX routines required by this should now be implemented.
One note, SCHED_OTHER, SCHED_FIFO, and SCHED_RR are required
to be defined by including pthread.h. These defines are currently
in sched.h. I modified pthread.h to include sched.h but don't
know if this is the proper thing to do.
o Added support for priority protection and inheritence mutexes.
This allows definition of _POSIX_THREAD_PRIO_PROTECT and
_POSIX_THREAD_PRIO_INHERIT.
o Added additional error checks required by POSIX for mutexes and
condition variables.
o Provided a wrapper for sigpending which is marked as a hidden
syscall.
o Added a non-portable function as a debugging aid to allow an
application to monitor thread context switches. An application
can install a routine that gets called everytime a thread
(explicitly created by the application) gets context switched.
The routine gets passed the pthread IDs of the threads that are
being switched in and out.
Submitted by: Dan Eischen <eischen@vigrid.com>
Changes by me:
o Added a PS_SPINBLOCK state to deal with the priority inversion
problem most often (I think) seen by threads calling malloc/free/realloc.
o Dispatch signals to the running thread directly rather than at a
context switch to avoid the situation where the switch never occurs.
Notes
Notes:
svn path=/head/; revision=44963
Diffstat (limited to 'lib/libpthread/thread/thr_create.c')
-rw-r--r-- | lib/libpthread/thread/thr_create.c | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/lib/libpthread/thread/thr_create.c b/lib/libpthread/thread/thr_create.c index 416946131add..438e52701992 100644 --- a/lib/libpthread/thread/thr_create.c +++ b/lib/libpthread/thread/thr_create.c @@ -99,12 +99,6 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, */ new_thread->magic = PTHREAD_MAGIC; - if (pattr->suspend == PTHREAD_CREATE_SUSPENDED) { - PTHREAD_NEW_STATE(new_thread,PS_SUSPENDED); - } else { - PTHREAD_NEW_STATE(new_thread,PS_RUNNING); - } - /* Initialise the thread for signals: */ new_thread->sigmask = _thread_run->sigmask; @@ -162,21 +156,26 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, */ if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) { /* Copy the scheduling attributes: */ - new_thread->pthread_priority = _thread_run->pthread_priority; - new_thread->attr.prio = _thread_run->pthread_priority; - new_thread->attr.schedparam_policy = _thread_run->attr.schedparam_policy; + new_thread->base_priority = _thread_run->base_priority; + new_thread->attr.prio = _thread_run->base_priority; + new_thread->attr.sched_policy = _thread_run->attr.sched_policy; } else { /* * Use just the thread priority, leaving the * other scheduling attributes as their * default values: */ - new_thread->pthread_priority = new_thread->attr.prio; + new_thread->base_priority = new_thread->attr.prio; } + new_thread->active_priority = new_thread->base_priority; + new_thread->inherited_priority = 0; /* Initialise the join queue for the new thread: */ _thread_queue_init(&(new_thread->join_queue)); + /* Initialize the mutex queue: */ + TAILQ_INIT(&new_thread->mutexq); + /* Initialise hooks in the thread structure: */ new_thread->specific_data = NULL; new_thread->cleanup = NULL; @@ -200,6 +199,27 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, /* Unlock the thread list: */ _unlock_thread_list(); + /* + * Guard against preemption by a scheduling signal. + * A change of thread state modifies the waiting + * and priority queues. + */ + _thread_kern_sched_defer(); + + if (pattr->suspend == PTHREAD_CREATE_SUSPENDED) { + new_thread->state = PS_SUSPENDED; + PTHREAD_WAITQ_INSERT(new_thread); + } else { + new_thread->state = PS_RUNNING; + PTHREAD_PRIOQ_INSERT_TAIL(new_thread); + } + + /* + * Reenable preemption and yield if a scheduling + * signal occurred while in the critical region. + */ + _thread_kern_sched_undefer(); + /* Return a pointer to the thread structure: */ (*thread) = new_thread; |