1996-01-22 00:23:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2007-01-12 07:26:21 +00:00
|
|
|
* 3. Neither the name of the author nor the names of any co-contributors
|
1996-01-22 00:23:58 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1999-03-23 05:07:56 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1996-01-22 00:23:58 +00:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1996-01-22 00:23:58 +00:00
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
1998-06-09 23:21:05 +00:00
|
|
|
#include <string.h>
|
1999-03-23 05:07:56 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
1996-01-22 00:23:58 +00:00
|
|
|
#include <pthread.h>
|
2002-09-16 08:45:36 +00:00
|
|
|
#include "thr_private.h"
|
1996-01-22 00:23:58 +00:00
|
|
|
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
#if defined(_PTHREADS_INVARIANTS)
|
2003-04-18 05:04:16 +00:00
|
|
|
#define MUTEX_INIT_LINK(m) do { \
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
(m)->m_qe.tqe_prev = NULL; \
|
|
|
|
(m)->m_qe.tqe_next = NULL; \
|
|
|
|
} while (0)
|
2003-04-18 05:04:16 +00:00
|
|
|
#define MUTEX_ASSERT_IS_OWNED(m) do { \
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
if ((m)->m_qe.tqe_prev == NULL) \
|
|
|
|
PANIC("mutex is not on list"); \
|
|
|
|
} while (0)
|
2003-04-18 05:04:16 +00:00
|
|
|
#define MUTEX_ASSERT_NOT_OWNED(m) do { \
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
if (((m)->m_qe.tqe_prev != NULL) || \
|
|
|
|
((m)->m_qe.tqe_next != NULL)) \
|
|
|
|
PANIC("mutex is on list"); \
|
|
|
|
} while (0)
|
2003-04-18 05:04:16 +00:00
|
|
|
#define THR_ASSERT_NOT_IN_SYNCQ(thr) do { \
|
|
|
|
THR_ASSERT(((thr)->sflags & THR_FLAGS_IN_SYNCQ) == 0, \
|
|
|
|
"thread in syncq when it shouldn't be."); \
|
|
|
|
} while (0);
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
#else
|
2003-04-18 05:04:16 +00:00
|
|
|
#define MUTEX_INIT_LINK(m)
|
|
|
|
#define MUTEX_ASSERT_IS_OWNED(m)
|
|
|
|
#define MUTEX_ASSERT_NOT_OWNED(m)
|
|
|
|
#define THR_ASSERT_NOT_IN_SYNCQ(thr)
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
#endif
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-09-04 14:06:43 +00:00
|
|
|
#define THR_IN_MUTEXQ(thr) (((thr)->sflags & THR_FLAGS_IN_SYNCQ) != 0)
|
2003-11-04 19:53:32 +00:00
|
|
|
#define MUTEX_DESTROY(m) do { \
|
|
|
|
_lock_destroy(&(m)->m_lock); \
|
|
|
|
free(m); \
|
|
|
|
} while (0)
|
|
|
|
|
2003-09-04 14:06:43 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* Prototypes
|
|
|
|
*/
|
2003-07-23 02:11:07 +00:00
|
|
|
static struct kse_mailbox *mutex_handoff(struct pthread *,
|
|
|
|
struct pthread_mutex *);
|
2003-04-18 05:04:16 +00:00
|
|
|
static inline int mutex_self_trylock(struct pthread *, pthread_mutex_t);
|
|
|
|
static inline int mutex_self_lock(struct pthread *, pthread_mutex_t);
|
|
|
|
static int mutex_unlock_common(pthread_mutex_t *, int);
|
|
|
|
static void mutex_priority_adjust(struct pthread *, pthread_mutex_t);
|
|
|
|
static void mutex_rescan_owned (struct pthread *, struct pthread *,
|
|
|
|
struct pthread_mutex *);
|
1999-03-23 05:07:56 +00:00
|
|
|
static inline pthread_t mutex_queue_deq(pthread_mutex_t);
|
|
|
|
static inline void mutex_queue_remove(pthread_mutex_t, pthread_t);
|
|
|
|
static inline void mutex_queue_enq(pthread_mutex_t, pthread_t);
|
2004-12-18 18:07:37 +00:00
|
|
|
static void mutex_lock_backout(void *arg);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
static struct pthread_mutex_attr static_mutex_attr =
|
|
|
|
PTHREAD_MUTEXATTR_STATIC_INITIALIZER;
|
|
|
|
static pthread_mutexattr_t static_mattr = &static_mutex_attr;
|
|
|
|
|
2006-03-13 00:59:51 +00:00
|
|
|
LT10_COMPAT_PRIVATE(__pthread_mutex_init);
|
|
|
|
LT10_COMPAT_PRIVATE(_pthread_mutex_init);
|
|
|
|
LT10_COMPAT_DEFAULT(pthread_mutex_init);
|
|
|
|
LT10_COMPAT_PRIVATE(__pthread_mutex_lock);
|
|
|
|
LT10_COMPAT_PRIVATE(_pthread_mutex_lock);
|
|
|
|
LT10_COMPAT_DEFAULT(pthread_mutex_lock);
|
|
|
|
LT10_COMPAT_PRIVATE(__pthread_mutex_timedlock);
|
|
|
|
LT10_COMPAT_PRIVATE(_pthread_mutex_timedlock);
|
|
|
|
LT10_COMPAT_DEFAULT(pthread_mutex_timedlock);
|
|
|
|
LT10_COMPAT_PRIVATE(__pthread_mutex_trylock);
|
|
|
|
LT10_COMPAT_PRIVATE(_pthread_mutex_trylock);
|
|
|
|
LT10_COMPAT_DEFAULT(pthread_mutex_trylock);
|
|
|
|
LT10_COMPAT_PRIVATE(_pthread_mutex_destroy);
|
|
|
|
LT10_COMPAT_DEFAULT(pthread_mutex_destroy);
|
|
|
|
LT10_COMPAT_PRIVATE(_pthread_mutex_unlock);
|
|
|
|
LT10_COMPAT_DEFAULT(pthread_mutex_unlock);
|
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Single underscore versions provided for libc internal usage: */
|
2004-12-18 18:07:37 +00:00
|
|
|
__weak_reference(__pthread_mutex_init, pthread_mutex_init);
|
2001-04-10 04:19:21 +00:00
|
|
|
__weak_reference(__pthread_mutex_lock, pthread_mutex_lock);
|
2003-09-04 14:06:43 +00:00
|
|
|
__weak_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock);
|
2003-04-18 05:04:16 +00:00
|
|
|
__weak_reference(__pthread_mutex_trylock, pthread_mutex_trylock);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
|
|
|
/* No difference between libc and application usage of these: */
|
2001-04-10 04:19:21 +00:00
|
|
|
__weak_reference(_pthread_mutex_destroy, pthread_mutex_destroy);
|
|
|
|
__weak_reference(_pthread_mutex_unlock, pthread_mutex_unlock);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
|
1996-01-22 00:23:58 +00:00
|
|
|
int
|
2004-12-18 18:07:37 +00:00
|
|
|
__pthread_mutex_init(pthread_mutex_t *mutex,
|
2003-04-18 05:04:16 +00:00
|
|
|
const pthread_mutexattr_t *mutex_attr)
|
1996-01-22 00:23:58 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread_mutex *pmutex;
|
|
|
|
enum pthread_mutextype type;
|
1999-03-23 05:07:56 +00:00
|
|
|
int protocol;
|
|
|
|
int ceiling;
|
2001-02-26 01:06:52 +00:00
|
|
|
int flags;
|
2000-10-13 22:12:32 +00:00
|
|
|
int ret = 0;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
if (mutex == NULL)
|
1997-11-25 01:29:16 +00:00
|
|
|
ret = EINVAL;
|
1998-04-29 09:59:34 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Check if default mutex attributes: */
|
|
|
|
else if (mutex_attr == NULL || *mutex_attr == NULL) {
|
|
|
|
/* Default to a (error checking) POSIX mutex: */
|
|
|
|
type = PTHREAD_MUTEX_ERRORCHECK;
|
|
|
|
protocol = PTHREAD_PRIO_NONE;
|
2003-04-18 05:04:16 +00:00
|
|
|
ceiling = THR_MAX_PRIORITY;
|
2001-02-26 01:06:52 +00:00
|
|
|
flags = 0;
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check mutex type: */
|
|
|
|
else if (((*mutex_attr)->m_type < PTHREAD_MUTEX_ERRORCHECK) ||
|
2005-08-19 21:31:42 +00:00
|
|
|
((*mutex_attr)->m_type >= PTHREAD_MUTEX_TYPE_MAX))
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Return an invalid argument error: */
|
|
|
|
ret = EINVAL;
|
|
|
|
|
|
|
|
/* Check mutex protocol: */
|
|
|
|
else if (((*mutex_attr)->m_protocol < PTHREAD_PRIO_NONE) ||
|
|
|
|
((*mutex_attr)->m_protocol > PTHREAD_MUTEX_RECURSIVE))
|
|
|
|
/* Return an invalid argument error: */
|
|
|
|
ret = EINVAL;
|
|
|
|
|
|
|
|
else {
|
|
|
|
/* Use the requested mutex type and protocol: */
|
|
|
|
type = (*mutex_attr)->m_type;
|
|
|
|
protocol = (*mutex_attr)->m_protocol;
|
|
|
|
ceiling = (*mutex_attr)->m_ceiling;
|
2001-02-26 01:06:52 +00:00
|
|
|
flags = (*mutex_attr)->m_flags;
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check no errors so far: */
|
|
|
|
if (ret == 0) {
|
|
|
|
if ((pmutex = (pthread_mutex_t)
|
|
|
|
malloc(sizeof(struct pthread_mutex))) == NULL)
|
|
|
|
ret = ENOMEM;
|
2003-04-18 05:04:16 +00:00
|
|
|
else if (_lock_init(&pmutex->m_lock, LCK_ADAPTIVE,
|
|
|
|
_thr_lock_wait, _thr_lock_wakeup) != 0) {
|
|
|
|
free(pmutex);
|
|
|
|
*mutex = NULL;
|
|
|
|
ret = ENOMEM;
|
|
|
|
} else {
|
2001-02-26 01:06:52 +00:00
|
|
|
/* Set the mutex flags: */
|
|
|
|
pmutex->m_flags = flags;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/* Process according to mutex type: */
|
|
|
|
switch (type) {
|
|
|
|
/* case PTHREAD_MUTEX_DEFAULT: */
|
|
|
|
case PTHREAD_MUTEX_ERRORCHECK:
|
|
|
|
case PTHREAD_MUTEX_NORMAL:
|
|
|
|
/* Nothing to do here. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Single UNIX Spec 2 recursive mutex: */
|
|
|
|
case PTHREAD_MUTEX_RECURSIVE:
|
|
|
|
/* Reset the mutex count: */
|
2003-04-18 05:04:16 +00:00
|
|
|
pmutex->m_count = 0;
|
1999-03-23 05:07:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Trap invalid mutex types: */
|
|
|
|
default:
|
|
|
|
/* Return an invalid argument error: */
|
|
|
|
ret = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
/* Initialise the rest of the mutex: */
|
|
|
|
TAILQ_INIT(&pmutex->m_queue);
|
|
|
|
pmutex->m_flags |= MUTEX_FLAGS_INITED;
|
|
|
|
pmutex->m_owner = NULL;
|
|
|
|
pmutex->m_type = type;
|
|
|
|
pmutex->m_protocol = protocol;
|
|
|
|
pmutex->m_refcount = 0;
|
|
|
|
if (protocol == PTHREAD_PRIO_PROTECT)
|
|
|
|
pmutex->m_prio = ceiling;
|
|
|
|
else
|
2003-04-18 05:04:16 +00:00
|
|
|
pmutex->m_prio = -1;
|
1999-03-23 05:07:56 +00:00
|
|
|
pmutex->m_saved_prio = 0;
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_INIT_LINK(pmutex);
|
1999-03-23 05:07:56 +00:00
|
|
|
*mutex = pmutex;
|
|
|
|
} else {
|
2003-06-03 02:21:01 +00:00
|
|
|
/* Free the mutex lock structure: */
|
2003-11-04 19:53:32 +00:00
|
|
|
MUTEX_DESTROY(pmutex);
|
1999-03-23 05:07:56 +00:00
|
|
|
*mutex = NULL;
|
1996-08-20 08:22:01 +00:00
|
|
|
}
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
1996-08-20 08:22:01 +00:00
|
|
|
/* Return the completion status: */
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1996-08-20 08:22:01 +00:00
|
|
|
}
|
1996-01-22 00:23:58 +00:00
|
|
|
|
2004-12-18 18:07:37 +00:00
|
|
|
int
|
|
|
|
_pthread_mutex_init(pthread_mutex_t *mutex,
|
|
|
|
const pthread_mutexattr_t *mutex_attr)
|
|
|
|
{
|
|
|
|
struct pthread_mutex_attr mattr, *mattrp;
|
|
|
|
|
|
|
|
if ((mutex_attr == NULL) || (*mutex_attr == NULL))
|
|
|
|
return (__pthread_mutex_init(mutex, &static_mattr));
|
|
|
|
else {
|
|
|
|
mattr = **mutex_attr;
|
|
|
|
mattr.m_flags |= MUTEX_FLAGS_PRIVATE;
|
|
|
|
mattrp = &mattr;
|
|
|
|
return (__pthread_mutex_init(mutex, &mattrp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-04 19:53:32 +00:00
|
|
|
void
|
|
|
|
_thr_mutex_reinit(pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
_lock_reinit(&(*mutex)->m_lock, LCK_ADAPTIVE,
|
|
|
|
_thr_lock_wait, _thr_lock_wakeup);
|
|
|
|
TAILQ_INIT(&(*mutex)->m_queue);
|
|
|
|
(*mutex)->m_owner = NULL;
|
|
|
|
(*mutex)->m_count = 0;
|
|
|
|
(*mutex)->m_refcount = 0;
|
|
|
|
(*mutex)->m_prio = 0;
|
|
|
|
(*mutex)->m_saved_prio = 0;
|
|
|
|
}
|
|
|
|
|
1996-08-20 08:22:01 +00:00
|
|
|
int
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_destroy(pthread_mutex_t *mutex)
|
1996-08-20 08:22:01 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
pthread_mutex_t m;
|
|
|
|
int ret = 0;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
1998-04-29 09:59:34 +00:00
|
|
|
if (mutex == NULL || *mutex == NULL)
|
1997-11-25 01:29:16 +00:00
|
|
|
ret = EINVAL;
|
1998-04-29 09:59:34 +00:00
|
|
|
else {
|
|
|
|
/* Lock the mutex structure: */
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_ACQUIRE(curthread, &(*mutex)->m_lock);
|
1998-04-29 09:59:34 +00:00
|
|
|
|
|
|
|
/*
|
1999-03-23 05:07:56 +00:00
|
|
|
* Check to see if this mutex is in use:
|
1998-04-29 09:59:34 +00:00
|
|
|
*/
|
1999-03-23 05:07:56 +00:00
|
|
|
if (((*mutex)->m_owner != NULL) ||
|
2006-04-13 03:09:34 +00:00
|
|
|
(!TAILQ_EMPTY(&(*mutex)->m_queue)) ||
|
1999-03-23 05:07:56 +00:00
|
|
|
((*mutex)->m_refcount != 0)) {
|
|
|
|
ret = EBUSY;
|
1998-04-29 09:59:34 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Unlock the mutex structure: */
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_RELEASE(curthread, &(*mutex)->m_lock);
|
|
|
|
} else {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Save a pointer to the mutex so it can be free'd
|
|
|
|
* and set the caller's pointer to NULL:
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
m = *mutex;
|
|
|
|
*mutex = NULL;
|
|
|
|
|
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &m->m_lock);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Free the memory allocated for the mutex
|
|
|
|
* structure:
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_ASSERT_NOT_OWNED(m);
|
2003-11-04 19:53:32 +00:00
|
|
|
MUTEX_DESTROY(m);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the completion status: */
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
1998-08-02 17:04:25 +00:00
|
|
|
static int
|
2003-04-18 05:04:16 +00:00
|
|
|
init_static(struct pthread *thread, pthread_mutex_t *mutex)
|
1998-08-02 17:04:25 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
int ret;
|
1998-08-02 17:04:25 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
|
1998-08-02 17:04:25 +00:00
|
|
|
|
1998-08-02 23:07:25 +00:00
|
|
|
if (*mutex == NULL)
|
1998-08-02 17:04:25 +00:00
|
|
|
ret = pthread_mutex_init(mutex, NULL);
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_RELEASE(thread, &_mutex_static_lock);
|
1998-08-02 17:04:25 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1998-08-02 17:04:25 +00:00
|
|
|
}
|
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
static int
|
2003-04-18 05:04:16 +00:00
|
|
|
init_static_private(struct pthread *thread, pthread_mutex_t *mutex)
|
1996-01-22 00:23:58 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
int ret;
|
2001-01-24 13:03:38 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
|
|
|
if (*mutex == NULL)
|
|
|
|
ret = pthread_mutex_init(mutex, &static_mattr);
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_RELEASE(thread, &_mutex_static_lock);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
2001-01-24 13:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_trylock_common(struct pthread *curthread, pthread_mutex_t *mutex)
|
2001-01-24 13:03:38 +00:00
|
|
|
{
|
2004-12-18 18:07:37 +00:00
|
|
|
int private;
|
2003-04-18 05:04:16 +00:00
|
|
|
int ret = 0;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_ASSERT((mutex != NULL) && (*mutex != NULL),
|
2001-01-24 13:03:38 +00:00
|
|
|
"Uninitialized mutex in pthread_mutex_trylock_basic");
|
1998-04-04 11:33:01 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Lock the mutex structure: */
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_ACQUIRE(curthread, &(*mutex)->m_lock);
|
2004-12-18 18:07:37 +00:00
|
|
|
private = (*mutex)->m_flags & MUTEX_FLAGS_PRIVATE;
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/*
|
|
|
|
* If the mutex was statically allocated, properly
|
|
|
|
* initialize the tail queue.
|
|
|
|
*/
|
|
|
|
if (((*mutex)->m_flags & MUTEX_FLAGS_INITED) == 0) {
|
|
|
|
TAILQ_INIT(&(*mutex)->m_queue);
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_INIT_LINK(*mutex);
|
2001-01-24 13:03:38 +00:00
|
|
|
(*mutex)->m_flags |= MUTEX_FLAGS_INITED;
|
|
|
|
}
|
1999-05-23 10:55:33 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Process according to mutex type: */
|
|
|
|
switch ((*mutex)->m_protocol) {
|
|
|
|
/* Default POSIX mutex: */
|
|
|
|
case PTHREAD_PRIO_NONE:
|
|
|
|
/* Check if this mutex is not locked: */
|
|
|
|
if ((*mutex)->m_owner == NULL) {
|
|
|
|
/* Lock the mutex for the running thread: */
|
|
|
|
(*mutex)->m_owner = curthread;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Add to the list of owned mutexes: */
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_ASSERT_NOT_OWNED(*mutex);
|
2001-01-24 13:03:38 +00:00
|
|
|
TAILQ_INSERT_TAIL(&curthread->mutexq,
|
|
|
|
(*mutex), m_qe);
|
|
|
|
} else if ((*mutex)->m_owner == curthread)
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = mutex_self_trylock(curthread, *mutex);
|
2001-01-24 13:03:38 +00:00
|
|
|
else
|
|
|
|
/* Return a busy error: */
|
|
|
|
ret = EBUSY;
|
|
|
|
break;
|
1996-08-20 08:22:01 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* POSIX priority inheritence mutex: */
|
|
|
|
case PTHREAD_PRIO_INHERIT:
|
|
|
|
/* Check if this mutex is not locked: */
|
|
|
|
if ((*mutex)->m_owner == NULL) {
|
|
|
|
/* Lock the mutex for the running thread: */
|
|
|
|
(*mutex)->m_owner = curthread;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-28 23:56:12 +00:00
|
|
|
THR_SCHED_LOCK(curthread, curthread);
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Track number of priority mutexes owned: */
|
|
|
|
curthread->priority_mutex_count++;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/*
|
|
|
|
* The mutex takes on the attributes of the
|
|
|
|
* running thread when there are no waiters.
|
|
|
|
*/
|
|
|
|
(*mutex)->m_prio = curthread->active_priority;
|
|
|
|
(*mutex)->m_saved_prio =
|
|
|
|
curthread->inherited_priority;
|
2003-09-24 12:52:57 +00:00
|
|
|
curthread->inherited_priority = (*mutex)->m_prio;
|
2003-04-28 23:56:12 +00:00
|
|
|
THR_SCHED_UNLOCK(curthread, curthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Add to the list of owned mutexes: */
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_ASSERT_NOT_OWNED(*mutex);
|
2001-01-24 13:03:38 +00:00
|
|
|
TAILQ_INSERT_TAIL(&curthread->mutexq,
|
|
|
|
(*mutex), m_qe);
|
|
|
|
} else if ((*mutex)->m_owner == curthread)
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = mutex_self_trylock(curthread, *mutex);
|
2001-01-24 13:03:38 +00:00
|
|
|
else
|
|
|
|
/* Return a busy error: */
|
|
|
|
ret = EBUSY;
|
|
|
|
break;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* POSIX priority protection mutex: */
|
|
|
|
case PTHREAD_PRIO_PROTECT:
|
|
|
|
/* Check for a priority ceiling violation: */
|
|
|
|
if (curthread->active_priority > (*mutex)->m_prio)
|
|
|
|
ret = EINVAL;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Check if this mutex is not locked: */
|
|
|
|
else if ((*mutex)->m_owner == NULL) {
|
|
|
|
/* Lock the mutex for the running thread: */
|
|
|
|
(*mutex)->m_owner = curthread;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-28 23:56:12 +00:00
|
|
|
THR_SCHED_LOCK(curthread, curthread);
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Track number of priority mutexes owned: */
|
|
|
|
curthread->priority_mutex_count++;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/*
|
|
|
|
* The running thread inherits the ceiling
|
|
|
|
* priority of the mutex and executes at that
|
|
|
|
* priority.
|
|
|
|
*/
|
|
|
|
curthread->active_priority = (*mutex)->m_prio;
|
|
|
|
(*mutex)->m_saved_prio =
|
|
|
|
curthread->inherited_priority;
|
|
|
|
curthread->inherited_priority =
|
|
|
|
(*mutex)->m_prio;
|
2003-04-28 23:56:12 +00:00
|
|
|
THR_SCHED_UNLOCK(curthread, curthread);
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Add to the list of owned mutexes: */
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_ASSERT_NOT_OWNED(*mutex);
|
2001-01-24 13:03:38 +00:00
|
|
|
TAILQ_INSERT_TAIL(&curthread->mutexq,
|
|
|
|
(*mutex), m_qe);
|
|
|
|
} else if ((*mutex)->m_owner == curthread)
|
2003-04-18 05:04:16 +00:00
|
|
|
ret = mutex_self_trylock(curthread, *mutex);
|
2001-01-24 13:03:38 +00:00
|
|
|
else
|
|
|
|
/* Return a busy error: */
|
|
|
|
ret = EBUSY;
|
|
|
|
break;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Trap invalid mutex types: */
|
|
|
|
default:
|
|
|
|
/* Return an invalid argument error: */
|
|
|
|
ret = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
1996-01-22 00:23:58 +00:00
|
|
|
|
2004-12-18 18:07:37 +00:00
|
|
|
if (ret == 0 && private)
|
|
|
|
THR_CRITICAL_ENTER(curthread);
|
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Unlock the mutex structure: */
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_RELEASE(curthread, &(*mutex)->m_lock);
|
1996-01-22 00:23:58 +00:00
|
|
|
|
|
|
|
/* Return the completion status: */
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
__pthread_mutex_trylock(pthread_mutex_t *mutex)
|
1996-01-22 00:23:58 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
int ret = 0;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
1998-04-04 11:33:01 +00:00
|
|
|
if (mutex == NULL)
|
2001-01-24 13:03:38 +00:00
|
|
|
ret = EINVAL;
|
1998-04-04 11:33:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
|
|
|
* initialization:
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
else if ((*mutex != NULL) ||
|
|
|
|
((ret = init_static(curthread, mutex)) == 0))
|
|
|
|
ret = mutex_trylock_common(curthread, mutex);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_pthread_mutex_trylock(pthread_mutex_t *mutex)
|
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
2001-01-24 13:03:38 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (mutex == NULL)
|
|
|
|
ret = EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
|
|
|
* initialization marking the mutex private (delete safe):
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
else if ((*mutex != NULL) ||
|
|
|
|
((ret = init_static_private(curthread, mutex)) == 0))
|
|
|
|
ret = mutex_trylock_common(curthread, mutex);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-09-04 14:06:43 +00:00
|
|
|
mutex_lock_common(struct pthread *curthread, pthread_mutex_t *m,
|
|
|
|
const struct timespec * abstime)
|
2001-01-24 13:03:38 +00:00
|
|
|
{
|
2004-12-18 18:07:37 +00:00
|
|
|
int private;
|
2001-01-24 13:03:38 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_ASSERT((m != NULL) && (*m != NULL),
|
2001-01-24 13:03:38 +00:00
|
|
|
"Uninitialized mutex in pthread_mutex_trylock_basic");
|
2000-11-09 05:08:26 +00:00
|
|
|
|
2003-09-04 14:06:43 +00:00
|
|
|
if (abstime != NULL && (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
|
|
|
|
abstime->tv_nsec >= 1000000000))
|
|
|
|
return (EINVAL);
|
|
|
|
|
2000-11-09 05:08:26 +00:00
|
|
|
/* Reset the interrupted flag: */
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->interrupted = 0;
|
2003-09-04 14:06:43 +00:00
|
|
|
curthread->timeout = 0;
|
|
|
|
curthread->wakeup_time.tv_sec = -1;
|
2000-11-09 05:08:26 +00:00
|
|
|
|
2004-12-18 18:07:37 +00:00
|
|
|
private = (*m)->m_flags & MUTEX_FLAGS_PRIVATE;
|
|
|
|
|
2000-11-09 05:08:26 +00:00
|
|
|
/*
|
|
|
|
* Enter a loop waiting to become the mutex owner. We need a
|
|
|
|
* loop in case the waiting thread is interrupted by a signal
|
|
|
|
* to execute a signal handler. It is not (currently) possible
|
|
|
|
* to remain in the waiting queue while running a handler.
|
|
|
|
* Instead, the thread is interrupted and backed out of the
|
|
|
|
* waiting queue prior to executing the signal handler.
|
|
|
|
*/
|
2000-11-20 13:12:44 +00:00
|
|
|
do {
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
/* Lock the mutex structure: */
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_ACQUIRE(curthread, &(*m)->m_lock);
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
|
1999-05-23 10:55:33 +00:00
|
|
|
/*
|
|
|
|
* If the mutex was statically allocated, properly
|
|
|
|
* initialize the tail queue.
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
if (((*m)->m_flags & MUTEX_FLAGS_INITED) == 0) {
|
|
|
|
TAILQ_INIT(&(*m)->m_queue);
|
|
|
|
(*m)->m_flags |= MUTEX_FLAGS_INITED;
|
|
|
|
MUTEX_INIT_LINK(*m);
|
1999-05-23 10:55:33 +00:00
|
|
|
}
|
|
|
|
|
1996-08-20 08:22:01 +00:00
|
|
|
/* Process according to mutex type: */
|
2003-04-18 05:04:16 +00:00
|
|
|
switch ((*m)->m_protocol) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Default POSIX mutex: */
|
|
|
|
case PTHREAD_PRIO_NONE:
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((*m)->m_owner == NULL) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Lock the mutex for this thread: */
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_owner = curthread;
|
1996-08-20 08:22:01 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Add to the list of owned mutexes: */
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_ASSERT_NOT_OWNED(*m);
|
2001-01-24 13:03:38 +00:00
|
|
|
TAILQ_INSERT_TAIL(&curthread->mutexq,
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m), m_qe);
|
2004-12-18 18:07:37 +00:00
|
|
|
if (private)
|
|
|
|
THR_CRITICAL_ENTER(curthread);
|
1996-08-20 08:22:01 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
} else if ((*m)->m_owner == curthread) {
|
|
|
|
ret = mutex_self_lock(curthread, *m);
|
|
|
|
|
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
} else {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* Join the queue of threads waiting to lock
|
2003-04-18 05:04:16 +00:00
|
|
|
* the mutex and save a pointer to the mutex.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_queue_enq(*m, curthread);
|
|
|
|
curthread->data.mutex = *m;
|
2004-12-18 18:07:37 +00:00
|
|
|
curthread->sigbackout = mutex_lock_backout;
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* This thread is active and is in a critical
|
|
|
|
* region (holding the mutex lock); we should
|
|
|
|
* be able to safely set the state.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-05-16 19:58:30 +00:00
|
|
|
THR_SCHED_LOCK(curthread, curthread);
|
2006-02-23 21:34:08 +00:00
|
|
|
/* Set the wakeup time: */
|
|
|
|
if (abstime) {
|
|
|
|
curthread->wakeup_time.tv_sec =
|
|
|
|
abstime->tv_sec;
|
|
|
|
curthread->wakeup_time.tv_nsec =
|
|
|
|
abstime->tv_nsec;
|
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_SET_STATE(curthread, PS_MUTEX_WAIT);
|
2003-05-16 19:58:30 +00:00
|
|
|
THR_SCHED_UNLOCK(curthread, curthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Schedule the next thread: */
|
|
|
|
_thr_sched_switch(curthread);
|
2003-09-04 14:06:43 +00:00
|
|
|
|
|
|
|
if (THR_IN_MUTEXQ(curthread)) {
|
|
|
|
THR_LOCK_ACQUIRE(curthread, &(*m)->m_lock);
|
|
|
|
mutex_queue_remove(*m, curthread);
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
}
|
2004-12-18 18:07:37 +00:00
|
|
|
/*
|
|
|
|
* Only clear these after assuring the
|
|
|
|
* thread is dequeued.
|
|
|
|
*/
|
|
|
|
curthread->data.mutex = NULL;
|
|
|
|
curthread->sigbackout = NULL;
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|
1996-08-20 08:22:01 +00:00
|
|
|
break;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/* POSIX priority inheritence mutex: */
|
|
|
|
case PTHREAD_PRIO_INHERIT:
|
|
|
|
/* Check if this mutex is not locked: */
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((*m)->m_owner == NULL) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Lock the mutex for this thread: */
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_owner = curthread;
|
1996-08-20 08:22:01 +00:00
|
|
|
|
2003-04-28 23:56:12 +00:00
|
|
|
THR_SCHED_LOCK(curthread, curthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Track number of priority mutexes owned: */
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->priority_mutex_count++;
|
1996-08-20 08:22:01 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* The mutex takes on attributes of the
|
|
|
|
* running thread when there are no waiters.
|
2003-04-18 05:04:16 +00:00
|
|
|
* Make sure the thread's scheduling lock is
|
|
|
|
* held while priorities are adjusted.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_prio = curthread->active_priority;
|
|
|
|
(*m)->m_saved_prio =
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->inherited_priority;
|
2003-04-18 05:04:16 +00:00
|
|
|
curthread->inherited_priority = (*m)->m_prio;
|
|
|
|
THR_SCHED_UNLOCK(curthread, curthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/* Add to the list of owned mutexes: */
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_ASSERT_NOT_OWNED(*m);
|
2001-01-24 13:03:38 +00:00
|
|
|
TAILQ_INSERT_TAIL(&curthread->mutexq,
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m), m_qe);
|
2004-12-18 18:07:37 +00:00
|
|
|
if (private)
|
|
|
|
THR_CRITICAL_ENTER(curthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
} else if ((*m)->m_owner == curthread) {
|
|
|
|
ret = mutex_self_lock(curthread, *m);
|
|
|
|
|
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
} else {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* Join the queue of threads waiting to lock
|
2003-04-18 05:04:16 +00:00
|
|
|
* the mutex and save a pointer to the mutex.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_queue_enq(*m, curthread);
|
|
|
|
curthread->data.mutex = *m;
|
2004-12-18 18:07:37 +00:00
|
|
|
curthread->sigbackout = mutex_lock_backout;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* This thread is active and is in a critical
|
|
|
|
* region (holding the mutex lock); we should
|
|
|
|
* be able to safely set the state.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
if (curthread->active_priority > (*m)->m_prio)
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Adjust priorities: */
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_priority_adjust(curthread, *m);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-05-16 19:58:30 +00:00
|
|
|
THR_SCHED_LOCK(curthread, curthread);
|
2006-02-23 21:34:08 +00:00
|
|
|
/* Set the wakeup time: */
|
|
|
|
if (abstime) {
|
|
|
|
curthread->wakeup_time.tv_sec =
|
|
|
|
abstime->tv_sec;
|
|
|
|
curthread->wakeup_time.tv_nsec =
|
|
|
|
abstime->tv_nsec;
|
|
|
|
}
|
2003-04-28 23:56:12 +00:00
|
|
|
THR_SET_STATE(curthread, PS_MUTEX_WAIT);
|
2003-05-16 19:58:30 +00:00
|
|
|
THR_SCHED_UNLOCK(curthread, curthread);
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Schedule the next thread: */
|
|
|
|
_thr_sched_switch(curthread);
|
2003-09-04 14:06:43 +00:00
|
|
|
|
|
|
|
if (THR_IN_MUTEXQ(curthread)) {
|
|
|
|
THR_LOCK_ACQUIRE(curthread, &(*m)->m_lock);
|
|
|
|
mutex_queue_remove(*m, curthread);
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
}
|
2004-12-18 18:07:37 +00:00
|
|
|
/*
|
|
|
|
* Only clear these after assuring the
|
|
|
|
* thread is dequeued.
|
|
|
|
*/
|
|
|
|
curthread->data.mutex = NULL;
|
|
|
|
curthread->sigbackout = NULL;
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|
1999-03-23 05:07:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* POSIX priority protection mutex: */
|
|
|
|
case PTHREAD_PRIO_PROTECT:
|
|
|
|
/* Check for a priority ceiling violation: */
|
2003-04-18 05:04:16 +00:00
|
|
|
if (curthread->active_priority > (*m)->m_prio) {
|
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
1999-03-23 05:07:56 +00:00
|
|
|
ret = EINVAL;
|
2003-04-18 05:04:16 +00:00
|
|
|
}
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Check if this mutex is not locked: */
|
2003-04-18 05:04:16 +00:00
|
|
|
else if ((*m)->m_owner == NULL) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* Lock the mutex for the running
|
|
|
|
* thread:
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_owner = curthread;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-28 23:56:12 +00:00
|
|
|
THR_SCHED_LOCK(curthread, curthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Track number of priority mutexes owned: */
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->priority_mutex_count++;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* The running thread inherits the ceiling
|
|
|
|
* priority of the mutex and executes at that
|
2003-04-18 05:04:16 +00:00
|
|
|
* priority. Make sure the thread's
|
|
|
|
* scheduling lock is held while priorities
|
|
|
|
* are adjusted.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
curthread->active_priority = (*m)->m_prio;
|
|
|
|
(*m)->m_saved_prio =
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->inherited_priority;
|
2003-04-18 05:04:16 +00:00
|
|
|
curthread->inherited_priority = (*m)->m_prio;
|
|
|
|
THR_SCHED_UNLOCK(curthread, curthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/* Add to the list of owned mutexes: */
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_ASSERT_NOT_OWNED(*m);
|
2001-01-24 13:03:38 +00:00
|
|
|
TAILQ_INSERT_TAIL(&curthread->mutexq,
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m), m_qe);
|
2004-12-18 18:07:37 +00:00
|
|
|
if (private)
|
|
|
|
THR_CRITICAL_ENTER(curthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
|
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
} else if ((*m)->m_owner == curthread) {
|
|
|
|
ret = mutex_self_lock(curthread, *m);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
} else {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Join the queue of threads waiting to lock
|
|
|
|
* the mutex and save a pointer to the mutex.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_queue_enq(*m, curthread);
|
|
|
|
curthread->data.mutex = *m;
|
2004-12-18 18:07:37 +00:00
|
|
|
curthread->sigbackout = mutex_lock_backout;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/* Clear any previous error: */
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->error = 0;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* This thread is active and is in a critical
|
|
|
|
* region (holding the mutex lock); we should
|
|
|
|
* be able to safely set the state.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-28 23:56:12 +00:00
|
|
|
|
2003-05-16 19:58:30 +00:00
|
|
|
THR_SCHED_LOCK(curthread, curthread);
|
2006-02-23 21:34:08 +00:00
|
|
|
/* Set the wakeup time: */
|
|
|
|
if (abstime) {
|
|
|
|
curthread->wakeup_time.tv_sec =
|
|
|
|
abstime->tv_sec;
|
|
|
|
curthread->wakeup_time.tv_nsec =
|
|
|
|
abstime->tv_nsec;
|
|
|
|
}
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_SET_STATE(curthread, PS_MUTEX_WAIT);
|
2003-05-16 19:58:30 +00:00
|
|
|
THR_SCHED_UNLOCK(curthread, curthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
|
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Schedule the next thread: */
|
|
|
|
_thr_sched_switch(curthread);
|
2003-09-04 14:06:43 +00:00
|
|
|
|
|
|
|
if (THR_IN_MUTEXQ(curthread)) {
|
|
|
|
THR_LOCK_ACQUIRE(curthread, &(*m)->m_lock);
|
|
|
|
mutex_queue_remove(*m, curthread);
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
}
|
2004-12-18 18:07:37 +00:00
|
|
|
/*
|
|
|
|
* Only clear these after assuring the
|
|
|
|
* thread is dequeued.
|
|
|
|
*/
|
|
|
|
curthread->data.mutex = NULL;
|
|
|
|
curthread->sigbackout = NULL;
|
2003-09-04 14:06:43 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* The threads priority may have changed while
|
|
|
|
* waiting for the mutex causing a ceiling
|
|
|
|
* violation.
|
|
|
|
*/
|
2001-01-24 13:03:38 +00:00
|
|
|
ret = curthread->error;
|
|
|
|
curthread->error = 0;
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
1996-08-20 08:22:01 +00:00
|
|
|
break;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
|
|
|
/* Trap invalid mutex types: */
|
1996-08-20 08:22:01 +00:00
|
|
|
default:
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
|
1996-08-20 08:22:01 +00:00
|
|
|
/* Return an invalid argument error: */
|
1997-11-25 01:29:16 +00:00
|
|
|
ret = EINVAL;
|
1996-08-20 08:22:01 +00:00
|
|
|
break;
|
|
|
|
}
|
1996-01-22 00:23:58 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
} while (((*m)->m_owner != curthread) && (ret == 0) &&
|
2003-09-04 14:06:43 +00:00
|
|
|
(curthread->interrupted == 0) && (curthread->timeout == 0));
|
|
|
|
|
2003-12-09 00:52:28 +00:00
|
|
|
if (ret == 0 && (*m)->m_owner != curthread && curthread->timeout)
|
2003-09-04 14:06:43 +00:00
|
|
|
ret = ETIMEDOUT;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* Check to see if this thread was interrupted and
|
|
|
|
* is still in the mutex queue of waiting threads:
|
|
|
|
*/
|
|
|
|
if (curthread->interrupted != 0) {
|
|
|
|
/* Remove this thread from the mutex queue. */
|
|
|
|
THR_LOCK_ACQUIRE(curthread, &(*m)->m_lock);
|
|
|
|
if (THR_IN_SYNCQ(curthread))
|
|
|
|
mutex_queue_remove(*m, curthread);
|
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
|
|
|
|
/* Check for asynchronous cancellation. */
|
|
|
|
if (curthread->continuation != NULL)
|
|
|
|
curthread->continuation((void *) curthread);
|
|
|
|
}
|
2000-11-09 05:08:26 +00:00
|
|
|
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Return the completion status: */
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-04-18 05:04:16 +00:00
|
|
|
__pthread_mutex_lock(pthread_mutex_t *m)
|
2001-01-24 13:03:38 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread *curthread;
|
2001-01-24 13:03:38 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
if (_thr_initial == NULL)
|
|
|
|
_libpthread_init(NULL);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
curthread = _get_curthread();
|
|
|
|
if (m == NULL)
|
2001-01-24 13:03:38 +00:00
|
|
|
ret = EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
|
|
|
* initialization:
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
else if ((*m != NULL) || ((ret = init_static(curthread, m)) == 0))
|
2003-09-04 14:06:43 +00:00
|
|
|
ret = mutex_lock_common(curthread, m, NULL);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2003-05-30 00:21:52 +00:00
|
|
|
__strong_reference(__pthread_mutex_lock, _thr_mutex_lock);
|
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
int
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_lock(pthread_mutex_t *m)
|
2001-01-24 13:03:38 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread *curthread;
|
2001-01-24 13:03:38 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
if (_thr_initial == NULL)
|
|
|
|
_libpthread_init(NULL);
|
|
|
|
curthread = _get_curthread();
|
2001-01-24 13:03:38 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
if (m == NULL)
|
2001-01-24 13:03:38 +00:00
|
|
|
ret = EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
|
|
|
* initialization marking it private (delete safe):
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
else if ((*m != NULL) ||
|
|
|
|
((ret = init_static_private(curthread, m)) == 0))
|
2003-09-04 14:06:43 +00:00
|
|
|
ret = mutex_lock_common(curthread, m, NULL);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
__pthread_mutex_timedlock(pthread_mutex_t *m,
|
|
|
|
const struct timespec *abs_timeout)
|
|
|
|
{
|
|
|
|
struct pthread *curthread;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (_thr_initial == NULL)
|
|
|
|
_libpthread_init(NULL);
|
|
|
|
|
|
|
|
curthread = _get_curthread();
|
|
|
|
if (m == NULL)
|
|
|
|
ret = EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
|
|
|
* initialization:
|
|
|
|
*/
|
|
|
|
else if ((*m != NULL) || ((ret = init_static(curthread, m)) == 0))
|
|
|
|
ret = mutex_lock_common(curthread, m, abs_timeout);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_pthread_mutex_timedlock(pthread_mutex_t *m,
|
|
|
|
const struct timespec *abs_timeout)
|
|
|
|
{
|
|
|
|
struct pthread *curthread;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (_thr_initial == NULL)
|
|
|
|
_libpthread_init(NULL);
|
|
|
|
curthread = _get_curthread();
|
|
|
|
|
|
|
|
if (m == NULL)
|
|
|
|
ret = EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
|
|
|
* initialization marking it private (delete safe):
|
|
|
|
*/
|
|
|
|
else if ((*m != NULL) ||
|
|
|
|
((ret = init_static_private(curthread, m)) == 0))
|
|
|
|
ret = mutex_lock_common(curthread, m, abs_timeout);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-04-18 05:04:16 +00:00
|
|
|
_pthread_mutex_unlock(pthread_mutex_t *m)
|
1996-01-22 00:23:58 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
return (mutex_unlock_common(m, /* add reference */ 0));
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
2003-05-30 00:21:52 +00:00
|
|
|
__strong_reference(_pthread_mutex_unlock, _thr_mutex_unlock);
|
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
int
|
2003-04-18 05:04:16 +00:00
|
|
|
_mutex_cv_unlock(pthread_mutex_t *m)
|
1999-03-23 05:07:56 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
return (mutex_unlock_common(m, /* add reference */ 1));
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-04-18 05:04:16 +00:00
|
|
|
_mutex_cv_lock(pthread_mutex_t *m)
|
1999-03-23 05:07:56 +00:00
|
|
|
{
|
2003-04-28 23:56:12 +00:00
|
|
|
struct pthread *curthread;
|
2000-10-13 22:12:32 +00:00
|
|
|
int ret;
|
2003-04-28 23:56:12 +00:00
|
|
|
|
|
|
|
curthread = _get_curthread();
|
|
|
|
if ((ret = _pthread_mutex_lock(m)) == 0) {
|
|
|
|
THR_LOCK_ACQUIRE(curthread, &(*m)->m_lock);
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_refcount--;
|
2003-04-28 23:56:12 +00:00
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
|
|
|
}
|
1999-03-23 05:07:56 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_self_trylock(struct pthread *curthread, pthread_mutex_t m)
|
1999-03-23 05:07:56 +00:00
|
|
|
{
|
2000-10-13 22:12:32 +00:00
|
|
|
int ret = 0;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
switch (m->m_type) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/* case PTHREAD_MUTEX_DEFAULT: */
|
|
|
|
case PTHREAD_MUTEX_ERRORCHECK:
|
|
|
|
case PTHREAD_MUTEX_NORMAL:
|
2004-10-31 05:03:50 +00:00
|
|
|
ret = EBUSY;
|
1999-03-23 05:07:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PTHREAD_MUTEX_RECURSIVE:
|
|
|
|
/* Increment the lock count: */
|
2003-04-18 05:04:16 +00:00
|
|
|
m->m_count++;
|
1999-03-23 05:07:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Trap invalid mutex types; */
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_self_lock(struct pthread *curthread, pthread_mutex_t m)
|
1999-03-23 05:07:56 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2004-12-18 18:07:37 +00:00
|
|
|
/*
|
|
|
|
* Don't allow evil recursive mutexes for private use
|
|
|
|
* in libc and libpthread.
|
|
|
|
*/
|
|
|
|
if (m->m_flags & MUTEX_FLAGS_PRIVATE)
|
|
|
|
PANIC("Recurse on a private mutex.");
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
switch (m->m_type) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/* case PTHREAD_MUTEX_DEFAULT: */
|
|
|
|
case PTHREAD_MUTEX_ERRORCHECK:
|
|
|
|
/*
|
|
|
|
* POSIX specifies that mutexes should return EDEADLK if a
|
|
|
|
* recursive lock is detected.
|
|
|
|
*/
|
|
|
|
ret = EDEADLK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PTHREAD_MUTEX_NORMAL:
|
|
|
|
/*
|
|
|
|
* What SS2 define as a 'normal' mutex. Intentionally
|
|
|
|
* deadlock on attempts to get a lock you already own.
|
|
|
|
*/
|
2003-04-28 23:56:12 +00:00
|
|
|
|
2003-05-16 19:58:30 +00:00
|
|
|
THR_SCHED_LOCK(curthread, curthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_SET_STATE(curthread, PS_DEADLOCK);
|
2003-05-16 19:58:30 +00:00
|
|
|
THR_SCHED_UNLOCK(curthread, curthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
|
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &m->m_lock);
|
|
|
|
|
|
|
|
/* Schedule the next thread: */
|
|
|
|
_thr_sched_switch(curthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PTHREAD_MUTEX_RECURSIVE:
|
|
|
|
/* Increment the lock count: */
|
2003-04-18 05:04:16 +00:00
|
|
|
m->m_count++;
|
1999-03-23 05:07:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Trap invalid mutex types; */
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (ret);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
static int
|
|
|
|
mutex_unlock_common(pthread_mutex_t *m, int add_reference)
|
1999-03-23 05:07:56 +00:00
|
|
|
{
|
2003-07-18 02:46:30 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
2003-07-23 02:11:07 +00:00
|
|
|
struct kse_mailbox *kmbx = NULL;
|
2003-07-18 02:46:30 +00:00
|
|
|
int ret = 0;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
if (m == NULL || *m == NULL)
|
1997-11-25 01:29:16 +00:00
|
|
|
ret = EINVAL;
|
2003-04-18 05:04:16 +00:00
|
|
|
else {
|
1998-04-29 09:59:34 +00:00
|
|
|
/* Lock the mutex structure: */
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_ACQUIRE(curthread, &(*m)->m_lock);
|
1996-01-22 00:23:58 +00:00
|
|
|
|
1996-08-20 08:22:01 +00:00
|
|
|
/* Process according to mutex type: */
|
2003-04-18 05:04:16 +00:00
|
|
|
switch ((*m)->m_protocol) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Default POSIX mutex: */
|
|
|
|
case PTHREAD_PRIO_NONE:
|
|
|
|
/*
|
|
|
|
* Check if the running thread is not the owner of the
|
|
|
|
* mutex:
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((*m)->m_owner != curthread)
|
2004-01-17 03:09:57 +00:00
|
|
|
ret = EPERM;
|
2003-04-18 05:04:16 +00:00
|
|
|
else if (((*m)->m_type == PTHREAD_MUTEX_RECURSIVE) &&
|
|
|
|
((*m)->m_count > 0))
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Decrement the count: */
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_count--;
|
|
|
|
else {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Clear the count in case this is a recursive
|
1999-03-23 05:07:56 +00:00
|
|
|
* mutex.
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_count = 0;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/* Remove the mutex from the threads queue. */
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_ASSERT_IS_OWNED(*m);
|
|
|
|
TAILQ_REMOVE(&(*m)->m_owner->mutexq,
|
|
|
|
(*m), m_qe);
|
|
|
|
MUTEX_INIT_LINK(*m);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Hand off the mutex to the next waiting
|
|
|
|
* thread:
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-07-23 02:11:07 +00:00
|
|
|
kmbx = mutex_handoff(curthread, *m);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* POSIX priority inheritence mutex: */
|
|
|
|
case PTHREAD_PRIO_INHERIT:
|
1996-08-20 08:22:01 +00:00
|
|
|
/*
|
1999-03-23 05:07:56 +00:00
|
|
|
* Check if the running thread is not the owner of the
|
|
|
|
* mutex:
|
1996-08-20 08:22:01 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((*m)->m_owner != curthread)
|
2004-01-17 03:09:57 +00:00
|
|
|
ret = EPERM;
|
2003-04-18 05:04:16 +00:00
|
|
|
else if (((*m)->m_type == PTHREAD_MUTEX_RECURSIVE) &&
|
|
|
|
((*m)->m_count > 0))
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Decrement the count: */
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_count--;
|
|
|
|
else {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* Clear the count in case this is recursive
|
|
|
|
* mutex.
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_count = 0;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Restore the threads inherited priority and
|
|
|
|
* recompute the active priority (being careful
|
|
|
|
* not to override changes in the threads base
|
|
|
|
* priority subsequent to locking the mutex).
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_SCHED_LOCK(curthread, curthread);
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->inherited_priority =
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_saved_prio;
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->active_priority =
|
|
|
|
MAX(curthread->inherited_priority,
|
|
|
|
curthread->base_priority);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This thread now owns one less priority mutex.
|
|
|
|
*/
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->priority_mutex_count--;
|
2003-04-28 23:56:12 +00:00
|
|
|
THR_SCHED_UNLOCK(curthread, curthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/* Remove the mutex from the threads queue. */
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_ASSERT_IS_OWNED(*m);
|
|
|
|
TAILQ_REMOVE(&(*m)->m_owner->mutexq,
|
|
|
|
(*m), m_qe);
|
|
|
|
MUTEX_INIT_LINK(*m);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Hand off the mutex to the next waiting
|
|
|
|
* thread:
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-07-23 02:11:07 +00:00
|
|
|
kmbx = mutex_handoff(curthread, *m);
|
1996-08-20 08:22:01 +00:00
|
|
|
}
|
|
|
|
break;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/* POSIX priority ceiling mutex: */
|
|
|
|
case PTHREAD_PRIO_PROTECT:
|
|
|
|
/*
|
|
|
|
* Check if the running thread is not the owner of the
|
|
|
|
* mutex:
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((*m)->m_owner != curthread)
|
2004-01-17 03:09:57 +00:00
|
|
|
ret = EPERM;
|
2003-04-18 05:04:16 +00:00
|
|
|
else if (((*m)->m_type == PTHREAD_MUTEX_RECURSIVE) &&
|
|
|
|
((*m)->m_count > 0))
|
1996-08-20 08:22:01 +00:00
|
|
|
/* Decrement the count: */
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_count--;
|
|
|
|
else {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Clear the count in case this is a recursive
|
1999-03-23 05:07:56 +00:00
|
|
|
* mutex.
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_count = 0;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
1998-11-28 23:52:58 +00:00
|
|
|
/*
|
1999-03-23 05:07:56 +00:00
|
|
|
* Restore the threads inherited priority and
|
|
|
|
* recompute the active priority (being careful
|
|
|
|
* not to override changes in the threads base
|
|
|
|
* priority subsequent to locking the mutex).
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_SCHED_LOCK(curthread, curthread);
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->inherited_priority =
|
2003-07-18 02:46:30 +00:00
|
|
|
(*m)->m_saved_prio;
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->active_priority =
|
|
|
|
MAX(curthread->inherited_priority,
|
|
|
|
curthread->base_priority);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This thread now owns one less priority mutex.
|
1998-11-28 23:52:58 +00:00
|
|
|
*/
|
2001-01-24 13:03:38 +00:00
|
|
|
curthread->priority_mutex_count--;
|
2003-07-30 13:28:05 +00:00
|
|
|
THR_SCHED_UNLOCK(curthread, curthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/* Remove the mutex from the threads queue. */
|
2003-04-18 05:04:16 +00:00
|
|
|
MUTEX_ASSERT_IS_OWNED(*m);
|
|
|
|
TAILQ_REMOVE(&(*m)->m_owner->mutexq,
|
|
|
|
(*m), m_qe);
|
|
|
|
MUTEX_INIT_LINK(*m);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Hand off the mutex to the next waiting
|
|
|
|
* thread:
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-07-23 02:11:07 +00:00
|
|
|
kmbx = mutex_handoff(curthread, *m);
|
1996-08-20 08:22:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Trap invalid mutex types: */
|
|
|
|
default:
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Return an invalid argument error: */
|
1997-11-25 01:29:16 +00:00
|
|
|
ret = EINVAL;
|
1996-08-20 08:22:01 +00:00
|
|
|
break;
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((ret == 0) && (add_reference != 0))
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Increment the reference count: */
|
2003-04-18 05:04:16 +00:00
|
|
|
(*m)->m_refcount++;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2004-12-18 18:07:37 +00:00
|
|
|
/* Leave the critical region if this is a private mutex. */
|
|
|
|
if ((ret == 0) && ((*m)->m_flags & MUTEX_FLAGS_PRIVATE))
|
|
|
|
THR_CRITICAL_LEAVE(curthread);
|
|
|
|
|
1998-04-29 09:59:34 +00:00
|
|
|
/* Unlock the mutex structure: */
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_RELEASE(curthread, &(*m)->m_lock);
|
2004-12-18 18:07:37 +00:00
|
|
|
|
2003-07-23 02:11:07 +00:00
|
|
|
if (kmbx != NULL)
|
|
|
|
kse_wakeup(kmbx);
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the completion status: */
|
|
|
|
return (ret);
|
|
|
|
}
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
* This function is called when a change in base priority occurs for
|
|
|
|
* a thread that is holding or waiting for a priority protection or
|
|
|
|
* inheritence mutex. A change in a threads base priority can effect
|
|
|
|
* changes to active priorities of other threads and to the ordering
|
|
|
|
* of mutex locking by waiting threads.
|
1999-03-23 05:07:56 +00:00
|
|
|
*
|
2003-04-18 05:04:16 +00:00
|
|
|
* This must be called without the target thread's scheduling lock held.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
|
|
|
void
|
2003-04-18 05:04:16 +00:00
|
|
|
_mutex_notify_priochange(struct pthread *curthread, struct pthread *pthread,
|
|
|
|
int propagate_prio)
|
1999-03-23 05:07:56 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread_mutex *m;
|
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Adjust the priorites of any owned priority mutexes: */
|
|
|
|
if (pthread->priority_mutex_count > 0) {
|
|
|
|
/*
|
|
|
|
* Rescan the mutexes owned by this thread and correct
|
|
|
|
* their priorities to account for this threads change
|
|
|
|
* in priority. This has the side effect of changing
|
|
|
|
* the threads active priority.
|
2003-04-18 05:04:16 +00:00
|
|
|
*
|
|
|
|
* Be sure to lock the first mutex in the list of owned
|
|
|
|
* mutexes. This acts as a barrier against another
|
|
|
|
* simultaneous call to change the threads priority
|
|
|
|
* and from the owning thread releasing the mutex.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
m = TAILQ_FIRST(&pthread->mutexq);
|
|
|
|
if (m != NULL) {
|
|
|
|
THR_LOCK_ACQUIRE(curthread, &m->m_lock);
|
|
|
|
/*
|
|
|
|
* Make sure the thread still owns the lock.
|
|
|
|
*/
|
|
|
|
if (m == TAILQ_FIRST(&pthread->mutexq))
|
|
|
|
mutex_rescan_owned(curthread, pthread,
|
|
|
|
/* rescan all owned */ NULL);
|
|
|
|
THR_LOCK_RELEASE(curthread, &m->m_lock);
|
|
|
|
}
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this thread is waiting on a priority inheritence mutex,
|
|
|
|
* check for priority adjustments. A change in priority can
|
2003-04-18 05:04:16 +00:00
|
|
|
* also cause a ceiling violation(*) for a thread waiting on
|
1999-03-23 05:07:56 +00:00
|
|
|
* a priority protection mutex; we don't perform the check here
|
|
|
|
* as it is done in pthread_mutex_unlock.
|
|
|
|
*
|
|
|
|
* (*) It should be noted that a priority change to a thread
|
|
|
|
* _after_ taking and owning a priority ceiling mutex
|
|
|
|
* does not affect ownership of that mutex; the ceiling
|
|
|
|
* priority is only checked before mutex ownership occurs.
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
if (propagate_prio != 0) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Lock the thread's scheduling queue. This is a bit
|
|
|
|
* convoluted; the "in synchronization queue flag" can
|
|
|
|
* only be cleared with both the thread's scheduling and
|
|
|
|
* mutex locks held. The thread's pointer to the wanted
|
|
|
|
* mutex is guaranteed to be valid during this time.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_SCHED_LOCK(curthread, pthread);
|
|
|
|
|
|
|
|
if (((pthread->sflags & THR_FLAGS_IN_SYNCQ) == 0) ||
|
|
|
|
((m = pthread->data.mutex) == NULL))
|
|
|
|
THR_SCHED_UNLOCK(curthread, pthread);
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* This thread is currently waiting on a mutex; unlock
|
|
|
|
* the scheduling queue lock and lock the mutex. We
|
|
|
|
* can't hold both at the same time because the locking
|
|
|
|
* order could cause a deadlock.
|
|
|
|
*/
|
|
|
|
THR_SCHED_UNLOCK(curthread, pthread);
|
|
|
|
THR_LOCK_ACQUIRE(curthread, &m->m_lock);
|
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Check to make sure this thread is still in the
|
|
|
|
* same state (the lock above can yield the CPU to
|
|
|
|
* another thread or the thread may be running on
|
|
|
|
* another CPU).
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
if (((pthread->sflags & THR_FLAGS_IN_SYNCQ) != 0) &&
|
|
|
|
(pthread->data.mutex == m)) {
|
|
|
|
/*
|
|
|
|
* Remove and reinsert this thread into
|
|
|
|
* the list of waiting threads to preserve
|
|
|
|
* decreasing priority order.
|
|
|
|
*/
|
|
|
|
mutex_queue_remove(m, pthread);
|
|
|
|
mutex_queue_enq(m, pthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
if (m->m_protocol == PTHREAD_PRIO_INHERIT)
|
|
|
|
/* Adjust priorities: */
|
|
|
|
mutex_priority_adjust(curthread, m);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Unlock the mutex structure: */
|
|
|
|
THR_LOCK_RELEASE(curthread, &m->m_lock);
|
|
|
|
}
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when a new thread is added to the mutex waiting queue or
|
|
|
|
* when a threads priority changes that is already in the mutex
|
|
|
|
* waiting queue.
|
2003-04-18 05:04:16 +00:00
|
|
|
*
|
|
|
|
* This must be called with the mutex locked by the current thread.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
|
|
|
static void
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_priority_adjust(struct pthread *curthread, pthread_mutex_t mutex)
|
1999-03-23 05:07:56 +00:00
|
|
|
{
|
|
|
|
pthread_mutex_t m = mutex;
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread *pthread_next, *pthread = mutex->m_owner;
|
|
|
|
int done, temp_prio;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the mutex priority as the maximum of the highest
|
|
|
|
* active priority of any waiting threads and the owning threads
|
|
|
|
* active priority(*).
|
|
|
|
*
|
|
|
|
* (*) Because the owning threads current active priority may
|
|
|
|
* reflect priority inherited from this mutex (and the mutex
|
|
|
|
* priority may have changed) we must recalculate the active
|
|
|
|
* priority based on the threads saved inherited priority
|
|
|
|
* and its base priority.
|
|
|
|
*/
|
|
|
|
pthread_next = TAILQ_FIRST(&m->m_queue); /* should never be NULL */
|
|
|
|
temp_prio = MAX(pthread_next->active_priority,
|
|
|
|
MAX(m->m_saved_prio, pthread->base_priority));
|
|
|
|
|
|
|
|
/* See if this mutex really needs adjusting: */
|
|
|
|
if (temp_prio == m->m_prio)
|
|
|
|
/* No need to propagate the priority: */
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Set new priority of the mutex: */
|
|
|
|
m->m_prio = temp_prio;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* Don't unlock the mutex passed in as an argument. It is
|
|
|
|
* expected to be locked and unlocked by the caller.
|
|
|
|
*/
|
|
|
|
done = 1;
|
|
|
|
do {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* Save the threads priority before rescanning the
|
|
|
|
* owned mutexes:
|
|
|
|
*/
|
|
|
|
temp_prio = pthread->active_priority;
|
|
|
|
|
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Fix the priorities for all mutexes held by the owning
|
|
|
|
* thread since taking this mutex. This also has a
|
1999-03-23 05:07:56 +00:00
|
|
|
* potential side-effect of changing the threads priority.
|
2003-04-18 05:04:16 +00:00
|
|
|
*
|
|
|
|
* At this point the mutex is locked by the current thread.
|
|
|
|
* The owning thread can't release the mutex until it is
|
|
|
|
* unlocked, so we should be able to safely walk its list
|
|
|
|
* of owned mutexes.
|
|
|
|
*/
|
|
|
|
mutex_rescan_owned(curthread, pthread, m);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this isn't the first time through the loop,
|
|
|
|
* the current mutex needs to be unlocked.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
if (done == 0)
|
|
|
|
THR_LOCK_RELEASE(curthread, &m->m_lock);
|
|
|
|
|
|
|
|
/* Assume we're done unless told otherwise: */
|
|
|
|
done = 1;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the thread is currently waiting on a mutex, check
|
|
|
|
* to see if the threads new priority has affected the
|
|
|
|
* priority of the mutex.
|
|
|
|
*/
|
|
|
|
if ((temp_prio != pthread->active_priority) &&
|
2003-04-18 05:04:16 +00:00
|
|
|
((pthread->sflags & THR_FLAGS_IN_SYNCQ) != 0) &&
|
|
|
|
((m = pthread->data.mutex) != NULL) &&
|
|
|
|
(m->m_protocol == PTHREAD_PRIO_INHERIT)) {
|
|
|
|
/* Lock the mutex structure: */
|
|
|
|
THR_LOCK_ACQUIRE(curthread, &m->m_lock);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Make sure the thread is still waiting on the
|
|
|
|
* mutex:
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
if (((pthread->sflags & THR_FLAGS_IN_SYNCQ) != 0) &&
|
|
|
|
(m == pthread->data.mutex)) {
|
|
|
|
/*
|
|
|
|
* The priority for this thread has changed.
|
|
|
|
* Remove and reinsert this thread into the
|
|
|
|
* list of waiting threads to preserve
|
|
|
|
* decreasing priority order.
|
|
|
|
*/
|
|
|
|
mutex_queue_remove(m, pthread);
|
|
|
|
mutex_queue_enq(m, pthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* Grab the waiting thread with highest
|
|
|
|
* priority:
|
|
|
|
*/
|
|
|
|
pthread_next = TAILQ_FIRST(&m->m_queue);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Calculate the mutex priority as the maximum
|
|
|
|
* of the highest active priority of any
|
|
|
|
* waiting threads and the owning threads
|
|
|
|
* active priority.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
temp_prio = MAX(pthread_next->active_priority,
|
|
|
|
MAX(m->m_saved_prio,
|
|
|
|
m->m_owner->base_priority));
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
if (temp_prio != m->m_prio) {
|
|
|
|
/*
|
|
|
|
* The priority needs to be propagated
|
|
|
|
* to the mutex this thread is waiting
|
|
|
|
* on and up to the owner of that mutex.
|
|
|
|
*/
|
|
|
|
m->m_prio = temp_prio;
|
|
|
|
pthread = m->m_owner;
|
|
|
|
|
|
|
|
/* We're not done yet: */
|
|
|
|
done = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Only release the mutex if we're done: */
|
|
|
|
if (done != 0)
|
|
|
|
THR_LOCK_RELEASE(curthread, &m->m_lock);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
2003-04-18 05:04:16 +00:00
|
|
|
} while (done == 0);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_rescan_owned(struct pthread *curthread, struct pthread *pthread,
|
|
|
|
struct pthread_mutex *mutex)
|
1999-03-23 05:07:56 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread_mutex *m;
|
|
|
|
struct pthread *pthread_next;
|
|
|
|
int active_prio, inherited_prio;
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start walking the mutexes the thread has taken since
|
|
|
|
* taking this mutex.
|
|
|
|
*/
|
|
|
|
if (mutex == NULL) {
|
|
|
|
/*
|
|
|
|
* A null mutex means start at the beginning of the owned
|
|
|
|
* mutex list.
|
|
|
|
*/
|
|
|
|
m = TAILQ_FIRST(&pthread->mutexq);
|
|
|
|
|
|
|
|
/* There is no inherited priority yet. */
|
|
|
|
inherited_prio = 0;
|
2003-04-18 05:04:16 +00:00
|
|
|
} else {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* The caller wants to start after a specific mutex. It
|
|
|
|
* is assumed that this mutex is a priority inheritence
|
|
|
|
* mutex and that its priority has been correctly
|
|
|
|
* calculated.
|
|
|
|
*/
|
|
|
|
m = TAILQ_NEXT(mutex, m_qe);
|
|
|
|
|
|
|
|
/* Start inheriting priority from the specified mutex. */
|
|
|
|
inherited_prio = mutex->m_prio;
|
|
|
|
}
|
|
|
|
active_prio = MAX(inherited_prio, pthread->base_priority);
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
for (; m != NULL; m = TAILQ_NEXT(m, m_qe)) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* We only want to deal with priority inheritence
|
|
|
|
* mutexes. This might be optimized by only placing
|
|
|
|
* priority inheritence mutexes into the owned mutex
|
|
|
|
* list, but it may prove to be useful having all
|
|
|
|
* owned mutexes in this list. Consider a thread
|
|
|
|
* exiting while holding mutexes...
|
|
|
|
*/
|
|
|
|
if (m->m_protocol == PTHREAD_PRIO_INHERIT) {
|
|
|
|
/*
|
|
|
|
* Fix the owners saved (inherited) priority to
|
|
|
|
* reflect the priority of the previous mutex.
|
|
|
|
*/
|
|
|
|
m->m_saved_prio = inherited_prio;
|
|
|
|
|
|
|
|
if ((pthread_next = TAILQ_FIRST(&m->m_queue)) != NULL)
|
|
|
|
/* Recalculate the priority of the mutex: */
|
|
|
|
m->m_prio = MAX(active_prio,
|
|
|
|
pthread_next->active_priority);
|
|
|
|
else
|
|
|
|
m->m_prio = active_prio;
|
|
|
|
|
|
|
|
/* Recalculate new inherited and active priorities: */
|
|
|
|
inherited_prio = m->m_prio;
|
|
|
|
active_prio = MAX(m->m_prio, pthread->base_priority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fix the threads inherited priority and recalculate its
|
|
|
|
* active priority.
|
|
|
|
*/
|
|
|
|
pthread->inherited_priority = inherited_prio;
|
|
|
|
active_prio = MAX(inherited_prio, pthread->base_priority);
|
|
|
|
|
|
|
|
if (active_prio != pthread->active_priority) {
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Lock the thread's scheduling queue: */
|
|
|
|
THR_SCHED_LOCK(curthread, pthread);
|
|
|
|
|
|
|
|
if ((pthread->flags & THR_FLAGS_IN_RUNQ) == 0) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* This thread is not in a run queue. Just set
|
|
|
|
* its active priority.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
pthread->active_priority = active_prio;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* This thread is in a run queue. Remove it from
|
|
|
|
* the queue before changing its priority:
|
|
|
|
*/
|
|
|
|
THR_RUNQ_REMOVE(pthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* POSIX states that if the priority is being
|
|
|
|
* lowered, the thread must be inserted at the
|
|
|
|
* head of the queue for its priority if it owns
|
|
|
|
* any priority protection or inheritence mutexes.
|
|
|
|
*/
|
|
|
|
if ((active_prio < pthread->active_priority) &&
|
|
|
|
(pthread->priority_mutex_count > 0)) {
|
|
|
|
/* Set the new active priority. */
|
|
|
|
pthread->active_priority = active_prio;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_RUNQ_INSERT_HEAD(pthread);
|
|
|
|
} else {
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Set the new active priority. */
|
|
|
|
pthread->active_priority = active_prio;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_RUNQ_INSERT_TAIL(pthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_SCHED_UNLOCK(curthread, pthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-28 05:38:13 +00:00
|
|
|
void
|
|
|
|
_mutex_unlock_private(pthread_t pthread)
|
|
|
|
{
|
|
|
|
struct pthread_mutex *m, *m_next;
|
|
|
|
|
|
|
|
for (m = TAILQ_FIRST(&pthread->mutexq); m != NULL; m = m_next) {
|
|
|
|
m_next = TAILQ_NEXT(m, m_qe);
|
|
|
|
if ((m->m_flags & MUTEX_FLAGS_PRIVATE) != 0)
|
|
|
|
pthread_mutex_unlock(&m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* This is called by the current thread when it wants to back out of a
|
|
|
|
* mutex_lock in order to run a signal handler.
|
|
|
|
*/
|
2004-12-18 18:07:37 +00:00
|
|
|
static void
|
|
|
|
mutex_lock_backout(void *arg)
|
2000-10-13 22:12:32 +00:00
|
|
|
{
|
2004-12-18 18:07:37 +00:00
|
|
|
struct pthread *curthread = (struct pthread *)arg;
|
2003-04-18 05:04:16 +00:00
|
|
|
struct pthread_mutex *m;
|
2000-10-13 22:12:32 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((curthread->sflags & THR_FLAGS_IN_SYNCQ) != 0) {
|
|
|
|
/*
|
|
|
|
* Any other thread may clear the "in sync queue flag",
|
|
|
|
* but only the current thread can clear the pointer
|
|
|
|
* to the mutex. So if the flag is set, we can
|
|
|
|
* guarantee that the pointer to the mutex is valid.
|
|
|
|
* The only problem may be if the mutex is destroyed
|
|
|
|
* out from under us, but that should be considered
|
|
|
|
* an application bug.
|
|
|
|
*/
|
|
|
|
m = curthread->data.mutex;
|
2000-10-13 22:12:32 +00:00
|
|
|
|
|
|
|
/* Lock the mutex structure: */
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_LOCK_ACQUIRE(curthread, &m->m_lock);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to make sure this thread doesn't already own
|
|
|
|
* the mutex. Since mutexes are unlocked with direct
|
|
|
|
* handoffs, it is possible the previous owner gave it
|
|
|
|
* to us after we checked the sync queue flag and before
|
|
|
|
* we locked the mutex structure.
|
|
|
|
*/
|
|
|
|
if (m->m_owner == curthread) {
|
|
|
|
THR_LOCK_RELEASE(curthread, &m->m_lock);
|
|
|
|
mutex_unlock_common(&m, /* add_reference */ 0);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Remove ourselves from the mutex queue and
|
|
|
|
* clear the pointer to the mutex. We may no
|
|
|
|
* longer be in the mutex queue, but the removal
|
|
|
|
* function will DTRT.
|
|
|
|
*/
|
|
|
|
mutex_queue_remove(m, curthread);
|
|
|
|
curthread->data.mutex = NULL;
|
|
|
|
THR_LOCK_RELEASE(curthread, &m->m_lock);
|
|
|
|
}
|
|
|
|
}
|
2004-12-18 18:07:37 +00:00
|
|
|
/* No need to call this again. */
|
|
|
|
curthread->sigbackout = NULL;
|
2003-04-18 05:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dequeue a waiting thread from the head of a mutex queue in descending
|
|
|
|
* priority order.
|
|
|
|
*
|
|
|
|
* In order to properly dequeue a thread from the mutex queue and
|
|
|
|
* make it runnable without the possibility of errant wakeups, it
|
|
|
|
* is necessary to lock the thread's scheduling queue while also
|
|
|
|
* holding the mutex lock.
|
|
|
|
*/
|
2003-07-23 02:11:07 +00:00
|
|
|
static struct kse_mailbox *
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_handoff(struct pthread *curthread, struct pthread_mutex *mutex)
|
|
|
|
{
|
2003-07-23 02:11:07 +00:00
|
|
|
struct kse_mailbox *kmbx = NULL;
|
2003-07-18 02:46:30 +00:00
|
|
|
struct pthread *pthread;
|
2003-04-18 05:04:16 +00:00
|
|
|
|
|
|
|
/* Keep dequeueing until we find a valid thread: */
|
|
|
|
mutex->m_owner = NULL;
|
|
|
|
pthread = TAILQ_FIRST(&mutex->m_queue);
|
|
|
|
while (pthread != NULL) {
|
|
|
|
/* Take the thread's scheduling lock: */
|
|
|
|
THR_SCHED_LOCK(curthread, pthread);
|
2000-10-13 22:12:32 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Remove the thread from the mutex queue: */
|
|
|
|
TAILQ_REMOVE(&mutex->m_queue, pthread, sqe);
|
|
|
|
pthread->sflags &= ~THR_FLAGS_IN_SYNCQ;
|
2000-10-13 22:12:32 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* Only exit the loop if the thread hasn't been
|
|
|
|
* cancelled.
|
|
|
|
*/
|
|
|
|
switch (mutex->m_protocol) {
|
|
|
|
case PTHREAD_PRIO_NONE:
|
|
|
|
/*
|
|
|
|
* Assign the new owner and add the mutex to the
|
|
|
|
* thread's list of owned mutexes.
|
|
|
|
*/
|
|
|
|
mutex->m_owner = pthread;
|
|
|
|
TAILQ_INSERT_TAIL(&pthread->mutexq, mutex, m_qe);
|
|
|
|
break;
|
2000-10-13 22:12:32 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
case PTHREAD_PRIO_INHERIT:
|
|
|
|
/*
|
|
|
|
* Assign the new owner and add the mutex to the
|
|
|
|
* thread's list of owned mutexes.
|
|
|
|
*/
|
|
|
|
mutex->m_owner = pthread;
|
|
|
|
TAILQ_INSERT_TAIL(&pthread->mutexq, mutex, m_qe);
|
|
|
|
|
|
|
|
/* Track number of priority mutexes owned: */
|
|
|
|
pthread->priority_mutex_count++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the priority of the mutex. Since our waiting
|
|
|
|
* threads are in descending priority order, the
|
|
|
|
* priority of the mutex becomes the active priority
|
|
|
|
* of the thread we just dequeued.
|
|
|
|
*/
|
|
|
|
mutex->m_prio = pthread->active_priority;
|
|
|
|
|
|
|
|
/* Save the owning threads inherited priority: */
|
|
|
|
mutex->m_saved_prio = pthread->inherited_priority;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The owning threads inherited priority now becomes
|
|
|
|
* his active priority (the priority of the mutex).
|
|
|
|
*/
|
|
|
|
pthread->inherited_priority = mutex->m_prio;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PTHREAD_PRIO_PROTECT:
|
|
|
|
if (pthread->active_priority > mutex->m_prio) {
|
|
|
|
/*
|
|
|
|
* Either the mutex ceiling priority has
|
|
|
|
* been lowered and/or this threads priority
|
|
|
|
* has been raised subsequent to the thread
|
|
|
|
* being queued on the waiting list.
|
|
|
|
*/
|
|
|
|
pthread->error = EINVAL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* Assign the new owner and add the mutex
|
|
|
|
* to the thread's list of owned mutexes.
|
|
|
|
*/
|
|
|
|
mutex->m_owner = pthread;
|
|
|
|
TAILQ_INSERT_TAIL(&pthread->mutexq,
|
|
|
|
mutex, m_qe);
|
|
|
|
|
|
|
|
/* Track number of priority mutexes owned: */
|
|
|
|
pthread->priority_mutex_count++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the owning threads inherited
|
|
|
|
* priority:
|
|
|
|
*/
|
|
|
|
mutex->m_saved_prio =
|
|
|
|
pthread->inherited_priority;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The owning thread inherits the ceiling
|
|
|
|
* priority of the mutex and executes at
|
|
|
|
* that priority:
|
|
|
|
*/
|
|
|
|
pthread->inherited_priority = mutex->m_prio;
|
|
|
|
pthread->active_priority = mutex->m_prio;
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the thread runnable and unlock the scheduling queue: */
|
2003-07-23 02:11:07 +00:00
|
|
|
kmbx = _thr_setrunnable_unlocked(pthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
|
2003-07-18 02:46:30 +00:00
|
|
|
/* Add a preemption point. */
|
|
|
|
if ((curthread->kseg == pthread->kseg) &&
|
|
|
|
(pthread->active_priority > curthread->active_priority))
|
|
|
|
curthread->critical_yield = 1;
|
|
|
|
|
2004-12-18 18:07:37 +00:00
|
|
|
if (mutex->m_owner == pthread) {
|
2003-04-18 05:04:16 +00:00
|
|
|
/* We're done; a valid owner was found. */
|
2004-12-18 18:07:37 +00:00
|
|
|
if (mutex->m_flags & MUTEX_FLAGS_PRIVATE)
|
|
|
|
THR_CRITICAL_ENTER(pthread);
|
|
|
|
THR_SCHED_UNLOCK(curthread, pthread);
|
2003-04-18 05:04:16 +00:00
|
|
|
break;
|
2004-12-18 18:07:37 +00:00
|
|
|
}
|
|
|
|
THR_SCHED_UNLOCK(curthread, pthread);
|
|
|
|
/* Get the next thread from the waiting queue: */
|
|
|
|
pthread = TAILQ_NEXT(pthread, sqe);
|
2000-10-13 22:12:32 +00:00
|
|
|
}
|
2003-04-18 05:04:16 +00:00
|
|
|
|
|
|
|
if ((pthread == NULL) && (mutex->m_protocol == PTHREAD_PRIO_INHERIT))
|
|
|
|
/* This mutex has no priority: */
|
|
|
|
mutex->m_prio = 0;
|
2003-07-23 02:11:07 +00:00
|
|
|
return (kmbx);
|
2000-10-13 22:12:32 +00:00
|
|
|
}
|
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* Dequeue a waiting thread from the head of a mutex queue in descending
|
|
|
|
* priority order.
|
|
|
|
*/
|
|
|
|
static inline pthread_t
|
2003-04-18 05:04:16 +00:00
|
|
|
mutex_queue_deq(struct pthread_mutex *mutex)
|
1999-03-23 05:07:56 +00:00
|
|
|
{
|
|
|
|
pthread_t pthread;
|
|
|
|
|
1999-11-28 05:38:13 +00:00
|
|
|
while ((pthread = TAILQ_FIRST(&mutex->m_queue)) != NULL) {
|
2000-10-13 22:12:32 +00:00
|
|
|
TAILQ_REMOVE(&mutex->m_queue, pthread, sqe);
|
2003-04-18 05:04:16 +00:00
|
|
|
pthread->sflags &= ~THR_FLAGS_IN_SYNCQ;
|
1999-11-28 05:38:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only exit the loop if the thread hasn't been
|
|
|
|
* cancelled.
|
|
|
|
*/
|
|
|
|
if (pthread->interrupted == 0)
|
|
|
|
break;
|
|
|
|
}
|
1999-03-23 05:07:56 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (pthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a waiting thread from a mutex queue in descending priority order.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
mutex_queue_remove(pthread_mutex_t mutex, pthread_t pthread)
|
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((pthread->sflags & THR_FLAGS_IN_SYNCQ) != 0) {
|
2000-10-13 22:12:32 +00:00
|
|
|
TAILQ_REMOVE(&mutex->m_queue, pthread, sqe);
|
2003-04-18 05:04:16 +00:00
|
|
|
pthread->sflags &= ~THR_FLAGS_IN_SYNCQ;
|
1999-11-28 05:38:13 +00:00
|
|
|
}
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enqueue a waiting thread to a queue in descending priority order.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
mutex_queue_enq(pthread_mutex_t mutex, pthread_t pthread)
|
|
|
|
{
|
|
|
|
pthread_t tid = TAILQ_LAST(&mutex->m_queue, mutex_head);
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
THR_ASSERT_NOT_IN_SYNCQ(pthread);
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* For the common case of all threads having equal priority,
|
|
|
|
* we perform a quick check against the priority of the thread
|
|
|
|
* at the tail of the queue.
|
|
|
|
*/
|
|
|
|
if ((tid == NULL) || (pthread->active_priority <= tid->active_priority))
|
2000-10-13 22:12:32 +00:00
|
|
|
TAILQ_INSERT_TAIL(&mutex->m_queue, pthread, sqe);
|
1999-03-23 05:07:56 +00:00
|
|
|
else {
|
|
|
|
tid = TAILQ_FIRST(&mutex->m_queue);
|
|
|
|
while (pthread->active_priority <= tid->active_priority)
|
2000-10-13 22:12:32 +00:00
|
|
|
tid = TAILQ_NEXT(tid, sqe);
|
|
|
|
TAILQ_INSERT_BEFORE(tid, pthread, sqe);
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|
2003-04-18 05:04:16 +00:00
|
|
|
pthread->sflags |= THR_FLAGS_IN_SYNCQ;
|
1999-03-23 05:07:56 +00:00
|
|
|
}
|