1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-15 10:17:20 +00:00

Implement compile time debug support instead of tracking file name and

line number every time a file descriptor is locked.

This looks like a big change but it isn't. It should reduce the size
of libc_r and make it run slightly faster.
This commit is contained in:
John Birrell 1998-06-09 23:21:05 +00:00
parent a675022188
commit ddc8afd422
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=36830
62 changed files with 342 additions and 321 deletions

View File

@ -45,7 +45,7 @@ accept(int fd, struct sockaddr * name, int *namelen)
int ret;
/* Lock the file descriptor: */
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Enter a loop to wait for a connection request: */
while ((ret = _thread_sys_accept(fd, name, namelen)) < 0) {
/* Check if the socket is to block: */
@ -100,7 +100,7 @@ accept(int fd, struct sockaddr * name, int *namelen)
_thread_fd_table[ret]->flags &= ~O_NONBLOCK;
/* Unlock the file descriptor: */
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
/* Return the socket file descriptor or -1 on error: */
return (ret);

View File

@ -41,9 +41,9 @@ bind(int fd, const struct sockaddr * name, int namelen)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
ret = _thread_sys_bind(fd, name, namelen);
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -47,9 +47,9 @@ close(int fd)
struct stat sb;
/* Lock the file descriptor while the file is closed: */
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Get file descriptor status. */
fstat(fd, &sb);
_thread_sys_fstat(fd, &sb);
/*
* Check if the file should be left as blocking.

View File

@ -32,6 +32,7 @@
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
#include "pthread_private.h"
@ -85,7 +86,7 @@ pthread_cond_init(pthread_cond_t * cond, const pthread_condattr_t * cond_attr)
_thread_queue_init(&pcond->c_queue);
pcond->c_flags |= COND_FLAGS_INITED;
pcond->c_type = type;
pcond->access_lock = 0;
memset(&pcond->lock,0,sizeof(pcond->lock));
*cond = pcond;
}
}
@ -103,7 +104,7 @@ pthread_cond_destroy(pthread_cond_t * cond)
rval = EINVAL;
else {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/*
* Free the memory allocated for the condition
@ -137,7 +138,7 @@ pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
else if (*cond != NULL ||
(rval = pthread_cond_init(cond,NULL)) == 0) {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -156,14 +157,14 @@ pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
_thread_run->wakeup_time.tv_sec = -1;
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
/* Schedule the next thread: */
_thread_kern_sched_state(PS_COND_WAIT,
__FILE__, __LINE__);
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Lock the mutex: */
rval = pthread_mutex_lock(mutex);
@ -177,7 +178,7 @@ pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */
@ -201,7 +202,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
else if (*cond != NULL ||
(rval = pthread_cond_init(cond,NULL)) == 0) {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -230,14 +231,14 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
_thread_queue_deq(&(*cond)->c_queue);
} else {
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
/* Schedule the next thread: */
_thread_kern_sched_state(PS_COND_WAIT,
__FILE__, __LINE__);
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Lock the mutex: */
if ((rval = pthread_mutex_lock(mutex)) != 0) {
@ -258,7 +259,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */
@ -276,7 +277,7 @@ pthread_cond_signal(pthread_cond_t * cond)
rval = EINVAL;
else {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -297,7 +298,7 @@ pthread_cond_signal(pthread_cond_t * cond)
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */
@ -315,7 +316,7 @@ pthread_cond_broadcast(pthread_cond_t * cond)
rval = EINVAL;
else {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -340,7 +341,7 @@ pthread_cond_broadcast(pthread_cond_t * cond)
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */

View File

@ -44,7 +44,7 @@ connect(int fd, const struct sockaddr * name, int namelen)
struct sockaddr tmpname;
int ret, tmpnamelen;
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
if ((ret = _thread_sys_connect(fd, name, namelen)) < 0) {
if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) &&
((errno == EWOULDBLOCK) || (errno == EINPROGRESS) ||
@ -69,7 +69,7 @@ connect(int fd, const struct sockaddr * name, int namelen)
ret = -1;
}
}
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -41,7 +41,7 @@ dup(int fd)
int ret;
/* Lock the file descriptor: */
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Perform the 'dup' syscall: */
if ((ret = _thread_sys_dup(fd)) < 0) {
}
@ -61,7 +61,7 @@ dup(int fd)
}
/* Unlock the file descriptor: */
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
/* Return the completion status: */
return (ret);

View File

@ -41,9 +41,9 @@ dup2(int fd, int newfd)
int ret;
/* Lock the file descriptor: */
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Lock the file descriptor: */
if ((ret = _thread_fd_lock(newfd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(newfd, FD_RDWR, NULL)) == 0) {
/* Perform the 'dup2' syscall: */
if ((ret = _thread_sys_dup2(fd, newfd)) < 0) {
}
@ -63,10 +63,10 @@ dup2(int fd, int newfd)
}
/* Unlock the file descriptor: */
_thread_fd_unlock(newfd, FD_RDWR);
_FD_UNLOCK(newfd, FD_RDWR);
}
/* Unlock the file descriptor: */
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
/* Return the completion status: */
return (ret);

View File

@ -41,9 +41,9 @@ fchmod(int fd, mode_t mode)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
ret = _thread_sys_fchmod(fd, mode);
_thread_fd_unlock(fd, FD_WRITE);
_FD_UNLOCK(fd, FD_WRITE);
}
return (ret);
}

View File

@ -42,9 +42,9 @@ fchown(int fd, uid_t owner, gid_t group)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
ret = _thread_sys_fchown(fd, owner, group);
_thread_fd_unlock(fd, FD_WRITE);
_FD_UNLOCK(fd, FD_WRITE);
}
return (ret);
}

View File

@ -47,7 +47,7 @@ fcntl(int fd, int cmd,...)
va_list ap;
/* Lock the file descriptor: */
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Initialise the variable argument list: */
va_start(ap, cmd);
@ -80,8 +80,11 @@ fcntl(int fd, int cmd,...)
}
break;
case F_SETFD:
flags = va_arg(ap, int);
ret = _thread_sys_fcntl(fd, cmd, flags);
break;
case F_GETFD:
ret = _thread_sys_fcntl(fd, cmd, 0);
break;
case F_GETFL:
ret = _thread_fd_table[fd]->flags;
@ -102,7 +105,7 @@ fcntl(int fd, int cmd,...)
va_end(ap);
/* Unlock the file descriptor: */
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
/* Return the completion status: */

View File

@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_file.c,v 1.2 1998/04/11 07:47:20 jb Exp $
* $Id: uthread_file.c,v 1.3 1998/04/29 09:58:47 jb Exp $
*
* POSIX stdio FILE locking functions. These assume that the locking
* is only required at FILE structure level, not at file descriptor
@ -98,7 +98,7 @@ struct static_file_lock {
static int init_done = 0;
/* Lock for accesses to the hash table: */
static long hash_lock = 0;
static spinlock_t hash_lock = _SPINLOCK_INITIALIZER;
/*
* Find a lock structure for a FILE, return NULL if the file is
@ -189,7 +189,7 @@ _flockfile_debug(FILE * fp, char *fname, int lineno)
/* Check if this is a real file: */
if (fp->_file >= 0) {
/* Lock the hash table: */
_spinlock(&hash_lock);
_SPINLOCK(&hash_lock);
/* Check if the static array has not been initialised: */
if (!init_done) {
@ -209,7 +209,7 @@ _flockfile_debug(FILE * fp, char *fname, int lineno)
p = do_lock(idx, fp);
/* Unlock the hash table: */
_atomic_unlock(&hash_lock);
_SPINUNLOCK(&hash_lock);
/*
* The file is already locked, so check if the
@ -225,7 +225,7 @@ _flockfile_debug(FILE * fp, char *fname, int lineno)
p->count++;
/* Unlock the hash table: */
_atomic_unlock(&hash_lock);
_SPINUNLOCK(&hash_lock);
} else {
/*
* The file is locked for another thread.
@ -235,7 +235,7 @@ _flockfile_debug(FILE * fp, char *fname, int lineno)
TAILQ_INSERT_TAIL(&p->l_head,_thread_run,qe);
/* Unlock the hash table: */
_atomic_unlock(&hash_lock);
_SPINUNLOCK(&hash_lock);
/* Wait on the FILE lock: */
_thread_kern_sched_state(PS_FILE_WAIT, fname, lineno);
@ -262,7 +262,7 @@ _ftrylockfile(FILE * fp)
/* Check if this is a real file: */
if (fp->_file >= 0) {
/* Lock the hash table: */
_spinlock(&hash_lock);
_SPINLOCK(&hash_lock);
/* Get a pointer to any existing lock for the file: */
if ((p = find_lock(idx, fp)) == NULL) {
@ -298,7 +298,7 @@ _ftrylockfile(FILE * fp)
ret = 0;
/* Unlock the hash table: */
_atomic_unlock(&hash_lock);
_SPINUNLOCK(&hash_lock);
}
return (ret);
@ -314,7 +314,7 @@ _funlockfile(FILE * fp)
/* Check if this is a real file: */
if (fp->_file >= 0) {
/* Lock the hash table: */
_spinlock(&hash_lock);
_SPINLOCK(&hash_lock);
/*
* Get a pointer to the lock for the file and check that
@ -358,7 +358,7 @@ _funlockfile(FILE * fp)
}
/* Unlock the hash table: */
_atomic_unlock(&hash_lock);
_SPINUNLOCK(&hash_lock);
}
return;
}

View File

@ -40,9 +40,9 @@ flock(int fd, int operation)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
ret = _thread_sys_flock(fd, operation);
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -46,11 +46,11 @@ fstat(int fd, struct stat * buf)
int ret;
/* Lock the file descriptor for read: */
if ((ret = _thread_fd_lock(fd, FD_READ, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
/* Get the file status: */
ret = _thread_sys_fstat(fd, buf);
/* Unlock the file descriptor: */
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return (ret);
}

View File

@ -46,11 +46,11 @@ fstatfs(int fd, struct statfs * buf)
int ret;
/* Lock the file descriptor for read: */
if ((ret = _thread_fd_lock(fd, FD_READ, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
/* Get the file system status: */
ret = _thread_sys_fstatfs(fd, buf);
/* Unlock the file descriptor: */
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return (ret);
}

View File

@ -40,9 +40,9 @@ fsync(int fd)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
ret = _thread_sys_fsync(fd);
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -41,9 +41,9 @@ getdirentries(int fd, char *buf, int nbytes, long *basep)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
ret = _thread_sys_getdirentries(fd, buf, nbytes, basep);
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -41,9 +41,9 @@ getpeername(int fd, struct sockaddr * peer, int *paddrlen)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_READ, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
ret = _thread_sys_getpeername(fd, peer, paddrlen);
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return ret;
}

View File

@ -41,9 +41,9 @@ getsockname(int s, struct sockaddr * name, int *namelen)
{
int ret;
if ((ret = _thread_fd_lock(s, FD_READ, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(s, FD_READ, NULL)) == 0) {
ret = _thread_sys_getsockname(s, name, namelen);
_thread_fd_unlock(s, FD_READ);
_FD_UNLOCK(s, FD_READ);
}
return ret;
}

View File

@ -41,9 +41,9 @@ getsockopt(int fd, int level, int optname, void *optval, int *optlen)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
ret = _thread_sys_getsockopt(fd, level, optname, optval, optlen);
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return ret;
}

View File

@ -154,7 +154,7 @@ _thread_dump_info(void)
}
/* Output a header for file descriptors: */
strcpy(s, "\n\n=============\nFILE DESCRIPTOR TABLE\n\n");
snprintf(s, sizeof(s), "\n\n=============\nFILE DESCRIPTOR TABLE (table size %d)\n\n",_thread_dtablesize);
_thread_sys_write(fd, s, strlen(s));
/* Enter a loop to report file descriptor lock usage: */

View File

@ -45,7 +45,7 @@ ioctl(int fd, unsigned long request,...)
va_list ap;
/* Lock the file descriptor: */
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Initialise the variable argument list: */
va_start(ap, request);
@ -69,7 +69,7 @@ ioctl(int fd, unsigned long request,...)
va_end(ap);
/* Unlock the file descriptor: */
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
/* Return the completion status: */

View File

@ -41,9 +41,9 @@ listen(int fd, int backlog)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
ret = _thread_sys_listen(fd, backlog);
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -32,6 +32,7 @@
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
#include "pthread_private.h"
@ -94,7 +95,8 @@ pthread_mutex_init(pthread_mutex_t * mutex,
pmutex->m_flags |= MUTEX_FLAGS_INITED;
pmutex->m_owner = NULL;
pmutex->m_type = type;
pmutex->access_lock = 0;
memset(&pmutex->lock, 0,
sizeof(pmutex->lock));
*mutex = pmutex;
} else {
free(pmutex);
@ -116,7 +118,7 @@ pthread_mutex_destroy(pthread_mutex_t * mutex)
ret = EINVAL;
else {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/*
* Free the memory allocated for the mutex
@ -150,7 +152,7 @@ pthread_mutex_trylock(pthread_mutex_t * mutex)
else if (*mutex != NULL ||
(ret = pthread_mutex_init(mutex,NULL)) == 0) {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@ -195,7 +197,7 @@ pthread_mutex_trylock(pthread_mutex_t * mutex)
}
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
}
/* Return the completion status: */
@ -217,7 +219,7 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
else if (*mutex != NULL ||
(ret = pthread_mutex_init(mutex,NULL)) == 0) {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@ -240,13 +242,13 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
_thread_queue_enq(&(*mutex)->m_queue, _thread_run);
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
/* Block signals: */
_thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__);
/* Lock the mutex again: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
}
}
break;
@ -273,13 +275,13 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
_thread_queue_enq(&(*mutex)->m_queue, _thread_run);
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
/* Block signals: */
_thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__);
/* Lock the mutex again: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
}
}
@ -295,7 +297,7 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
}
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
}
/* Return the completion status: */
@ -311,7 +313,7 @@ pthread_mutex_unlock(pthread_mutex_t * mutex)
ret = EINVAL;
} else {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@ -362,7 +364,7 @@ pthread_mutex_unlock(pthread_mutex_t * mutex)
}
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
}
/* Return the completion status: */

View File

@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_read.c,v 1.3 1997/04/01 22:44:15 jb Exp $
* $Id: uthread_read.c,v 1.4 1998/04/29 09:59:10 jb Exp $
*
*/
#include <sys/types.h>
@ -46,9 +46,12 @@ read(int fd, void *buf, size_t nbytes)
{
int ret;
/* POSIX says to do just this: */
if (nbytes == 0)
return (0);
/* Lock the file descriptor for read: */
if ((ret = _thread_fd_lock(fd, FD_READ, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
/* Perform a non-blocking read syscall: */
while ((ret = _thread_sys_read(fd, buf, nbytes)) < 0) {
if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 &&
@ -75,7 +78,7 @@ read(int fd, void *buf, size_t nbytes)
break;
}
}
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return (ret);
}

View File

@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_readv.c,v 1.3 1997/04/01 22:44:16 jb Exp $
* $Id: uthread_readv.c,v 1.4 1998/04/29 09:59:11 jb Exp $
*
*/
#include <sys/types.h>
@ -47,8 +47,7 @@ readv(int fd, const struct iovec * iov, int iovcnt)
int ret;
/* Lock the file descriptor for read: */
if ((ret = _thread_fd_lock(fd, FD_READ, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
/* Perform a non-blocking readv syscall: */
while ((ret = _thread_sys_readv(fd, iov, iovcnt)) < 0) {
if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 &&
@ -75,7 +74,7 @@ readv(int fd, const struct iovec * iov, int iovcnt)
break;
}
}
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return (ret);
}

View File

@ -43,7 +43,7 @@ recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr * from, int *
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_READ, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
while ((ret = _thread_sys_recvfrom(fd, buf, len, flags, from, from_len)) < 0) {
if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) {
_thread_run->data.fd.fd = fd;
@ -65,7 +65,7 @@ recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr * from, int *
break;
}
}
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return (ret);
}

View File

@ -43,7 +43,7 @@ recvmsg(int fd, struct msghdr *msg, int flags)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_READ, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
while ((ret = _thread_sys_recvmsg(fd, msg, flags)) < 0) {
if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) {
_thread_run->data.fd.fd = fd;
@ -65,7 +65,7 @@ recvmsg(int fd, struct msghdr *msg, int flags)
break;
}
}
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return (ret);
}

View File

@ -73,13 +73,13 @@ select(int numfds, fd_set * readfds, fd_set * writefds,
for (i = 0; i < numfds; i++) {
if ((readfds && (FD_ISSET(i, readfds))) || (exceptfds && FD_ISSET(i, exceptfds))) {
if (writefds && FD_ISSET(i, writefds)) {
if ((ret = _thread_fd_lock(i, FD_RDWR, NULL, __FILE__, __LINE__)) != 0) {
if ((ret = _FD_LOCK(i, FD_RDWR, NULL)) != 0) {
got_all_locks = 0;
break;
}
FD_SET(i, &rdwr_locks);
} else {
if ((ret = _thread_fd_lock(i, FD_READ, NULL, __FILE__, __LINE__)) != 0) {
if ((ret = _FD_LOCK(i, FD_READ, NULL)) != 0) {
got_all_locks = 0;
break;
}
@ -87,7 +87,7 @@ select(int numfds, fd_set * readfds, fd_set * writefds,
}
} else {
if (writefds && FD_ISSET(i, writefds)) {
if ((ret = _thread_fd_lock(i, FD_WRITE, NULL, __FILE__, __LINE__)) != 0) {
if ((ret = _FD_LOCK(i, FD_WRITE, NULL)) != 0) {
got_all_locks = 0;
break;
}
@ -137,13 +137,13 @@ select(int numfds, fd_set * readfds, fd_set * writefds,
/* clean up the locks */
for (i = 0; i < numfds; i++)
if (FD_ISSET(i, &read_locks))
_thread_fd_unlock(i, FD_READ);
_FD_UNLOCK(i, FD_READ);
for (i = 0; i < numfds; i++)
if (FD_ISSET(i, &rdwr_locks))
_thread_fd_unlock(i, FD_RDWR);
_FD_UNLOCK(i, FD_RDWR);
for (i = 0; i < numfds; i++)
if (FD_ISSET(i, &write_locks))
_thread_fd_unlock(i, FD_WRITE);
_FD_UNLOCK(i, FD_WRITE);
if (ret > 0) {
if (readfds != NULL) {

View File

@ -43,7 +43,7 @@ sendmsg(int fd, const struct msghdr *msg, int flags)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
while ((ret = _thread_sys_sendmsg(fd, msg, flags)) < 0) {
if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) {
_thread_run->data.fd.fd = fd;
@ -64,7 +64,7 @@ sendmsg(int fd, const struct msghdr *msg, int flags)
break;
}
}
_thread_fd_unlock(fd, FD_WRITE);
_FD_UNLOCK(fd, FD_WRITE);
}
return (ret);
}

View File

@ -43,7 +43,7 @@ sendto(int fd, const void *msg, size_t len, int flags, const struct sockaddr * t
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
while ((ret = _thread_sys_sendto(fd, msg, len, flags, to, to_len)) < 0) {
if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) {
_thread_run->data.fd.fd = fd;
@ -64,7 +64,7 @@ sendto(int fd, const void *msg, size_t len, int flags, const struct sockaddr * t
break;
}
}
_thread_fd_unlock(fd, FD_WRITE);
_FD_UNLOCK(fd, FD_WRITE);
}
return (ret);
}

View File

@ -41,9 +41,9 @@ setsockopt(int fd, int level, int optname, const void *optval, int optlen)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
ret = _thread_sys_setsockopt(fd, level, optname, optval, optlen);
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return ret;
}

View File

@ -44,21 +44,21 @@ shutdown(int fd, int how)
switch (how) {
case 0:
if ((ret = _thread_fd_lock(fd, FD_READ, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
ret = _thread_sys_shutdown(fd, how);
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
break;
case 1:
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
ret = _thread_sys_shutdown(fd, how);
_thread_fd_unlock(fd, FD_WRITE);
_FD_UNLOCK(fd, FD_WRITE);
}
break;
case 2:
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
ret = _thread_sys_shutdown(fd, how);
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
break;
default:

View File

@ -39,17 +39,17 @@
#include "pthread_private.h"
/* Static variables: */
static int volatile yield_on_unlock_dead = 0;
static int volatile yield_on_unlock_thread = 0;
static long volatile thread_dead_lock = 0;
static long volatile thread_link_list_lock = 0;
static int volatile yield_on_unlock_dead = 0;
static int volatile yield_on_unlock_thread = 0;
static spinlock_t thread_dead_lock = _SPINLOCK_INITIALIZER;
static spinlock_t thread_link_list_lock = _SPINLOCK_INITIALIZER;
/* Lock the thread list: */
void
_lock_thread_list()
{
/* Lock the thread list: */
_spinlock(&thread_link_list_lock);
_SPINLOCK(&thread_link_list_lock);
}
/* Lock the dead thread list: */
@ -57,7 +57,7 @@ void
_lock_dead_thread_list()
{
/* Lock the dead thread list: */
_spinlock(&thread_dead_lock);
_SPINLOCK(&thread_dead_lock);
}
/* Lock the thread list: */
@ -65,7 +65,7 @@ void
_unlock_thread_list()
{
/* Unlock the thread list: */
_atomic_unlock(&thread_link_list_lock);
_SPINUNLOCK(&thread_link_list_lock);
/*
* Check if a scheduler interrupt occurred while the thread
@ -85,7 +85,7 @@ void
_unlock_dead_thread_list()
{
/* Unlock the dead thread list: */
_atomic_unlock(&thread_dead_lock);
_SPINUNLOCK(&thread_dead_lock);
/*
* Check if a scheduler interrupt occurred while the dead
@ -137,7 +137,7 @@ _thread_sig_handler(int sig, int code, struct sigcontext * scp)
* unfortunate time which one of the threads is
* modifying the thread list:
*/
if (thread_link_list_lock)
if (thread_link_list_lock.access_lock)
/*
* Set a flag so that the thread that has
* the lock yields when it unlocks the
@ -149,7 +149,7 @@ _thread_sig_handler(int sig, int code, struct sigcontext * scp)
* unfortunate time which one of the threads is
* modifying the dead thread list:
*/
if (thread_dead_lock)
if (thread_dead_lock.access_lock)
/*
* Set a flag so that the thread that has
* the lock yields when it unlocks the

View File

@ -46,19 +46,19 @@ pthread_key_create(pthread_key_t * key, void (*destructor) (void *))
{
for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) {
/* Lock the key table entry: */
_spinlock(&key_table[*key].access_lock);
_SPINLOCK(&key_table[*key].lock);
if (key_table[(*key)].allocated == 0) {
key_table[(*key)].allocated = 1;
key_table[(*key)].destructor = destructor;
/* Unlock the key table entry: */
_atomic_unlock(&key_table[*key].access_lock);
_SPINUNLOCK(&key_table[*key].lock);
return (0);
}
/* Unlock the key table entry: */
_atomic_unlock(&key_table[*key].access_lock);
_SPINUNLOCK(&key_table[*key].lock);
}
return (EAGAIN);
}
@ -70,7 +70,7 @@ pthread_key_delete(pthread_key_t key)
if (key < PTHREAD_KEYS_MAX) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
_SPINLOCK(&key_table[key].lock);
if (key_table[key].allocated)
key_table[key].allocated = 0;
@ -78,7 +78,7 @@ pthread_key_delete(pthread_key_t key)
ret = EINVAL;
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
_SPINUNLOCK(&key_table[key].lock);
} else
ret = EINVAL;
return (ret);
@ -90,25 +90,33 @@ _thread_cleanupspecific(void)
void *data;
int key;
int itr;
void (*destructor)( void *);
for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) {
for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
if (_thread_run->specific_data_count) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
_SPINLOCK(&key_table[key].lock);
destructor = NULL;
if (key_table[key].allocated) {
if (_thread_run->specific_data[key]) {
data = (void *) _thread_run->specific_data[key];
_thread_run->specific_data[key] = NULL;
_thread_run->specific_data_count--;
if (key_table[key].destructor)
key_table[key].destructor(data);
destructor = key_table[key].destructor;
}
}
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
_SPINUNLOCK(&key_table[key].lock);
/*
* If there is a destructore, call it
* with the key table entry unlocked:
*/
if (destructor)
destructor(data);
} else {
free(_thread_run->specific_data);
return;
@ -140,9 +148,6 @@ pthread_setspecific(pthread_key_t key, const void *value)
if ((pthread->specific_data) ||
(pthread->specific_data = pthread_key_allocate_data())) {
if (key < PTHREAD_KEYS_MAX) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
if (key_table[key].allocated) {
if (pthread->specific_data[key] == NULL) {
if (value != NULL)
@ -155,10 +160,6 @@ pthread_setspecific(pthread_key_t key, const void *value)
ret = 0;
} else
ret = EINVAL;
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
} else
ret = EINVAL;
} else
@ -177,9 +178,6 @@ pthread_getspecific(pthread_key_t key)
/* Check if there is specific data: */
if (pthread->specific_data != NULL && key < PTHREAD_KEYS_MAX) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
/* Check if this key has been used before: */
if (key_table[key].allocated) {
/* Return the value: */
@ -191,9 +189,6 @@ pthread_getspecific(pthread_key_t key)
*/
data = NULL;
}
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
} else
/* No specific data has been created, so just return NULL: */
data = NULL;

View File

@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_write.c,v 1.5 1998/04/29 09:59:33 jb Exp $
* $Id: uthread_write.c,v 1.6 1998/05/25 21:45:50 jb Exp $
*
*/
#include <sys/types.h>
@ -50,9 +50,12 @@ write(int fd, const void *buf, size_t nbytes)
ssize_t num = 0;
ssize_t ret;
/* POSIX says to do just this: */
if (nbytes == 0)
return (0);
/* Lock the file descriptor for write: */
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
/* Check if file operations are to block */
blocking = ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0);
@ -113,7 +116,7 @@ write(int fd, const void *buf, size_t nbytes)
/* Return the number of bytes written: */
ret = num;
}
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -29,13 +29,15 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_writev.c,v 1.6 1998/05/25 21:45:52 jb Exp $
* $Id: uthread_writev.c,v 1.7 1998/05/27 00:44:58 jb Exp $
*
*/
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/uio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
@ -68,8 +70,7 @@ writev(int fd, const struct iovec * iov, int iovcnt)
memcpy(p_iov,iov,iovcnt * sizeof(struct iovec));
/* Lock the file descriptor for write: */
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
/* Check if file operations are to block */
blocking = ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0);
@ -166,7 +167,7 @@ writev(int fd, const struct iovec * iov, int iovcnt)
/* Return the number of bytes written: */
ret = num;
}
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
/* If memory was allocated for the array, free it: */

View File

@ -47,9 +47,9 @@ close(int fd)
struct stat sb;
/* Lock the file descriptor while the file is closed: */
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Get file descriptor status. */
fstat(fd, &sb);
_thread_sys_fstat(fd, &sb);
/*
* Check if the file should be left as blocking.

View File

@ -32,6 +32,7 @@
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
#include "pthread_private.h"
@ -85,7 +86,7 @@ pthread_cond_init(pthread_cond_t * cond, const pthread_condattr_t * cond_attr)
_thread_queue_init(&pcond->c_queue);
pcond->c_flags |= COND_FLAGS_INITED;
pcond->c_type = type;
pcond->access_lock = 0;
memset(&pcond->lock,0,sizeof(pcond->lock));
*cond = pcond;
}
}
@ -103,7 +104,7 @@ pthread_cond_destroy(pthread_cond_t * cond)
rval = EINVAL;
else {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/*
* Free the memory allocated for the condition
@ -137,7 +138,7 @@ pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
else if (*cond != NULL ||
(rval = pthread_cond_init(cond,NULL)) == 0) {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -156,14 +157,14 @@ pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
_thread_run->wakeup_time.tv_sec = -1;
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
/* Schedule the next thread: */
_thread_kern_sched_state(PS_COND_WAIT,
__FILE__, __LINE__);
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Lock the mutex: */
rval = pthread_mutex_lock(mutex);
@ -177,7 +178,7 @@ pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */
@ -201,7 +202,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
else if (*cond != NULL ||
(rval = pthread_cond_init(cond,NULL)) == 0) {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -230,14 +231,14 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
_thread_queue_deq(&(*cond)->c_queue);
} else {
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
/* Schedule the next thread: */
_thread_kern_sched_state(PS_COND_WAIT,
__FILE__, __LINE__);
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Lock the mutex: */
if ((rval = pthread_mutex_lock(mutex)) != 0) {
@ -258,7 +259,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */
@ -276,7 +277,7 @@ pthread_cond_signal(pthread_cond_t * cond)
rval = EINVAL;
else {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -297,7 +298,7 @@ pthread_cond_signal(pthread_cond_t * cond)
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */
@ -315,7 +316,7 @@ pthread_cond_broadcast(pthread_cond_t * cond)
rval = EINVAL;
else {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -340,7 +341,7 @@ pthread_cond_broadcast(pthread_cond_t * cond)
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */

View File

@ -47,7 +47,7 @@ fcntl(int fd, int cmd,...)
va_list ap;
/* Lock the file descriptor: */
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Initialise the variable argument list: */
va_start(ap, cmd);
@ -80,8 +80,11 @@ fcntl(int fd, int cmd,...)
}
break;
case F_SETFD:
flags = va_arg(ap, int);
ret = _thread_sys_fcntl(fd, cmd, flags);
break;
case F_GETFD:
ret = _thread_sys_fcntl(fd, cmd, 0);
break;
case F_GETFL:
ret = _thread_fd_table[fd]->flags;
@ -102,7 +105,7 @@ fcntl(int fd, int cmd,...)
va_end(ap);
/* Unlock the file descriptor: */
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
/* Return the completion status: */

View File

@ -40,9 +40,9 @@ fsync(int fd)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
ret = _thread_sys_fsync(fd);
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -154,7 +154,7 @@ _thread_dump_info(void)
}
/* Output a header for file descriptors: */
strcpy(s, "\n\n=============\nFILE DESCRIPTOR TABLE\n\n");
snprintf(s, sizeof(s), "\n\n=============\nFILE DESCRIPTOR TABLE (table size %d)\n\n",_thread_dtablesize);
_thread_sys_write(fd, s, strlen(s));
/* Enter a loop to report file descriptor lock usage: */

View File

@ -32,6 +32,7 @@
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
#include "pthread_private.h"
@ -94,7 +95,8 @@ pthread_mutex_init(pthread_mutex_t * mutex,
pmutex->m_flags |= MUTEX_FLAGS_INITED;
pmutex->m_owner = NULL;
pmutex->m_type = type;
pmutex->access_lock = 0;
memset(&pmutex->lock, 0,
sizeof(pmutex->lock));
*mutex = pmutex;
} else {
free(pmutex);
@ -116,7 +118,7 @@ pthread_mutex_destroy(pthread_mutex_t * mutex)
ret = EINVAL;
else {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/*
* Free the memory allocated for the mutex
@ -150,7 +152,7 @@ pthread_mutex_trylock(pthread_mutex_t * mutex)
else if (*mutex != NULL ||
(ret = pthread_mutex_init(mutex,NULL)) == 0) {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@ -195,7 +197,7 @@ pthread_mutex_trylock(pthread_mutex_t * mutex)
}
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
}
/* Return the completion status: */
@ -217,7 +219,7 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
else if (*mutex != NULL ||
(ret = pthread_mutex_init(mutex,NULL)) == 0) {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@ -240,13 +242,13 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
_thread_queue_enq(&(*mutex)->m_queue, _thread_run);
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
/* Block signals: */
_thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__);
/* Lock the mutex again: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
}
}
break;
@ -273,13 +275,13 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
_thread_queue_enq(&(*mutex)->m_queue, _thread_run);
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
/* Block signals: */
_thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__);
/* Lock the mutex again: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
}
}
@ -295,7 +297,7 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
}
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
}
/* Return the completion status: */
@ -311,7 +313,7 @@ pthread_mutex_unlock(pthread_mutex_t * mutex)
ret = EINVAL;
} else {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@ -362,7 +364,7 @@ pthread_mutex_unlock(pthread_mutex_t * mutex)
}
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
}
/* Return the completion status: */

View File

@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_read.c,v 1.3 1997/04/01 22:44:15 jb Exp $
* $Id: uthread_read.c,v 1.4 1998/04/29 09:59:10 jb Exp $
*
*/
#include <sys/types.h>
@ -46,9 +46,12 @@ read(int fd, void *buf, size_t nbytes)
{
int ret;
/* POSIX says to do just this: */
if (nbytes == 0)
return (0);
/* Lock the file descriptor for read: */
if ((ret = _thread_fd_lock(fd, FD_READ, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
/* Perform a non-blocking read syscall: */
while ((ret = _thread_sys_read(fd, buf, nbytes)) < 0) {
if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 &&
@ -75,7 +78,7 @@ read(int fd, void *buf, size_t nbytes)
break;
}
}
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return (ret);
}

View File

@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_readv.c,v 1.3 1997/04/01 22:44:16 jb Exp $
* $Id: uthread_readv.c,v 1.4 1998/04/29 09:59:11 jb Exp $
*
*/
#include <sys/types.h>
@ -47,8 +47,7 @@ readv(int fd, const struct iovec * iov, int iovcnt)
int ret;
/* Lock the file descriptor for read: */
if ((ret = _thread_fd_lock(fd, FD_READ, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
/* Perform a non-blocking readv syscall: */
while ((ret = _thread_sys_readv(fd, iov, iovcnt)) < 0) {
if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 &&
@ -75,7 +74,7 @@ readv(int fd, const struct iovec * iov, int iovcnt)
break;
}
}
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return (ret);
}

View File

@ -73,13 +73,13 @@ select(int numfds, fd_set * readfds, fd_set * writefds,
for (i = 0; i < numfds; i++) {
if ((readfds && (FD_ISSET(i, readfds))) || (exceptfds && FD_ISSET(i, exceptfds))) {
if (writefds && FD_ISSET(i, writefds)) {
if ((ret = _thread_fd_lock(i, FD_RDWR, NULL, __FILE__, __LINE__)) != 0) {
if ((ret = _FD_LOCK(i, FD_RDWR, NULL)) != 0) {
got_all_locks = 0;
break;
}
FD_SET(i, &rdwr_locks);
} else {
if ((ret = _thread_fd_lock(i, FD_READ, NULL, __FILE__, __LINE__)) != 0) {
if ((ret = _FD_LOCK(i, FD_READ, NULL)) != 0) {
got_all_locks = 0;
break;
}
@ -87,7 +87,7 @@ select(int numfds, fd_set * readfds, fd_set * writefds,
}
} else {
if (writefds && FD_ISSET(i, writefds)) {
if ((ret = _thread_fd_lock(i, FD_WRITE, NULL, __FILE__, __LINE__)) != 0) {
if ((ret = _FD_LOCK(i, FD_WRITE, NULL)) != 0) {
got_all_locks = 0;
break;
}
@ -137,13 +137,13 @@ select(int numfds, fd_set * readfds, fd_set * writefds,
/* clean up the locks */
for (i = 0; i < numfds; i++)
if (FD_ISSET(i, &read_locks))
_thread_fd_unlock(i, FD_READ);
_FD_UNLOCK(i, FD_READ);
for (i = 0; i < numfds; i++)
if (FD_ISSET(i, &rdwr_locks))
_thread_fd_unlock(i, FD_RDWR);
_FD_UNLOCK(i, FD_RDWR);
for (i = 0; i < numfds; i++)
if (FD_ISSET(i, &write_locks))
_thread_fd_unlock(i, FD_WRITE);
_FD_UNLOCK(i, FD_WRITE);
if (ret > 0) {
if (readfds != NULL) {

View File

@ -39,17 +39,17 @@
#include "pthread_private.h"
/* Static variables: */
static int volatile yield_on_unlock_dead = 0;
static int volatile yield_on_unlock_thread = 0;
static long volatile thread_dead_lock = 0;
static long volatile thread_link_list_lock = 0;
static int volatile yield_on_unlock_dead = 0;
static int volatile yield_on_unlock_thread = 0;
static spinlock_t thread_dead_lock = _SPINLOCK_INITIALIZER;
static spinlock_t thread_link_list_lock = _SPINLOCK_INITIALIZER;
/* Lock the thread list: */
void
_lock_thread_list()
{
/* Lock the thread list: */
_spinlock(&thread_link_list_lock);
_SPINLOCK(&thread_link_list_lock);
}
/* Lock the dead thread list: */
@ -57,7 +57,7 @@ void
_lock_dead_thread_list()
{
/* Lock the dead thread list: */
_spinlock(&thread_dead_lock);
_SPINLOCK(&thread_dead_lock);
}
/* Lock the thread list: */
@ -65,7 +65,7 @@ void
_unlock_thread_list()
{
/* Unlock the thread list: */
_atomic_unlock(&thread_link_list_lock);
_SPINUNLOCK(&thread_link_list_lock);
/*
* Check if a scheduler interrupt occurred while the thread
@ -85,7 +85,7 @@ void
_unlock_dead_thread_list()
{
/* Unlock the dead thread list: */
_atomic_unlock(&thread_dead_lock);
_SPINUNLOCK(&thread_dead_lock);
/*
* Check if a scheduler interrupt occurred while the dead
@ -137,7 +137,7 @@ _thread_sig_handler(int sig, int code, struct sigcontext * scp)
* unfortunate time which one of the threads is
* modifying the thread list:
*/
if (thread_link_list_lock)
if (thread_link_list_lock.access_lock)
/*
* Set a flag so that the thread that has
* the lock yields when it unlocks the
@ -149,7 +149,7 @@ _thread_sig_handler(int sig, int code, struct sigcontext * scp)
* unfortunate time which one of the threads is
* modifying the dead thread list:
*/
if (thread_dead_lock)
if (thread_dead_lock.access_lock)
/*
* Set a flag so that the thread that has
* the lock yields when it unlocks the

View File

@ -46,19 +46,19 @@ pthread_key_create(pthread_key_t * key, void (*destructor) (void *))
{
for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) {
/* Lock the key table entry: */
_spinlock(&key_table[*key].access_lock);
_SPINLOCK(&key_table[*key].lock);
if (key_table[(*key)].allocated == 0) {
key_table[(*key)].allocated = 1;
key_table[(*key)].destructor = destructor;
/* Unlock the key table entry: */
_atomic_unlock(&key_table[*key].access_lock);
_SPINUNLOCK(&key_table[*key].lock);
return (0);
}
/* Unlock the key table entry: */
_atomic_unlock(&key_table[*key].access_lock);
_SPINUNLOCK(&key_table[*key].lock);
}
return (EAGAIN);
}
@ -70,7 +70,7 @@ pthread_key_delete(pthread_key_t key)
if (key < PTHREAD_KEYS_MAX) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
_SPINLOCK(&key_table[key].lock);
if (key_table[key].allocated)
key_table[key].allocated = 0;
@ -78,7 +78,7 @@ pthread_key_delete(pthread_key_t key)
ret = EINVAL;
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
_SPINUNLOCK(&key_table[key].lock);
} else
ret = EINVAL;
return (ret);
@ -90,25 +90,33 @@ _thread_cleanupspecific(void)
void *data;
int key;
int itr;
void (*destructor)( void *);
for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) {
for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
if (_thread_run->specific_data_count) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
_SPINLOCK(&key_table[key].lock);
destructor = NULL;
if (key_table[key].allocated) {
if (_thread_run->specific_data[key]) {
data = (void *) _thread_run->specific_data[key];
_thread_run->specific_data[key] = NULL;
_thread_run->specific_data_count--;
if (key_table[key].destructor)
key_table[key].destructor(data);
destructor = key_table[key].destructor;
}
}
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
_SPINUNLOCK(&key_table[key].lock);
/*
* If there is a destructore, call it
* with the key table entry unlocked:
*/
if (destructor)
destructor(data);
} else {
free(_thread_run->specific_data);
return;
@ -140,9 +148,6 @@ pthread_setspecific(pthread_key_t key, const void *value)
if ((pthread->specific_data) ||
(pthread->specific_data = pthread_key_allocate_data())) {
if (key < PTHREAD_KEYS_MAX) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
if (key_table[key].allocated) {
if (pthread->specific_data[key] == NULL) {
if (value != NULL)
@ -155,10 +160,6 @@ pthread_setspecific(pthread_key_t key, const void *value)
ret = 0;
} else
ret = EINVAL;
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
} else
ret = EINVAL;
} else
@ -177,9 +178,6 @@ pthread_getspecific(pthread_key_t key)
/* Check if there is specific data: */
if (pthread->specific_data != NULL && key < PTHREAD_KEYS_MAX) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
/* Check if this key has been used before: */
if (key_table[key].allocated) {
/* Return the value: */
@ -191,9 +189,6 @@ pthread_getspecific(pthread_key_t key)
*/
data = NULL;
}
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
} else
/* No specific data has been created, so just return NULL: */
data = NULL;

View File

@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_write.c,v 1.5 1998/04/29 09:59:33 jb Exp $
* $Id: uthread_write.c,v 1.6 1998/05/25 21:45:50 jb Exp $
*
*/
#include <sys/types.h>
@ -50,9 +50,12 @@ write(int fd, const void *buf, size_t nbytes)
ssize_t num = 0;
ssize_t ret;
/* POSIX says to do just this: */
if (nbytes == 0)
return (0);
/* Lock the file descriptor for write: */
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
/* Check if file operations are to block */
blocking = ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0);
@ -113,7 +116,7 @@ write(int fd, const void *buf, size_t nbytes)
/* Return the number of bytes written: */
ret = num;
}
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -29,13 +29,15 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_writev.c,v 1.6 1998/05/25 21:45:52 jb Exp $
* $Id: uthread_writev.c,v 1.7 1998/05/27 00:44:58 jb Exp $
*
*/
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/uio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
@ -68,8 +70,7 @@ writev(int fd, const struct iovec * iov, int iovcnt)
memcpy(p_iov,iov,iovcnt * sizeof(struct iovec));
/* Lock the file descriptor for write: */
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
/* Check if file operations are to block */
blocking = ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0);
@ -166,7 +167,7 @@ writev(int fd, const struct iovec * iov, int iovcnt)
/* Return the number of bytes written: */
ret = num;
}
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
/* If memory was allocated for the array, free it: */

View File

@ -47,9 +47,9 @@ close(int fd)
struct stat sb;
/* Lock the file descriptor while the file is closed: */
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Get file descriptor status. */
fstat(fd, &sb);
_thread_sys_fstat(fd, &sb);
/*
* Check if the file should be left as blocking.

View File

@ -32,6 +32,7 @@
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
#include "pthread_private.h"
@ -85,7 +86,7 @@ pthread_cond_init(pthread_cond_t * cond, const pthread_condattr_t * cond_attr)
_thread_queue_init(&pcond->c_queue);
pcond->c_flags |= COND_FLAGS_INITED;
pcond->c_type = type;
pcond->access_lock = 0;
memset(&pcond->lock,0,sizeof(pcond->lock));
*cond = pcond;
}
}
@ -103,7 +104,7 @@ pthread_cond_destroy(pthread_cond_t * cond)
rval = EINVAL;
else {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/*
* Free the memory allocated for the condition
@ -137,7 +138,7 @@ pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
else if (*cond != NULL ||
(rval = pthread_cond_init(cond,NULL)) == 0) {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -156,14 +157,14 @@ pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
_thread_run->wakeup_time.tv_sec = -1;
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
/* Schedule the next thread: */
_thread_kern_sched_state(PS_COND_WAIT,
__FILE__, __LINE__);
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Lock the mutex: */
rval = pthread_mutex_lock(mutex);
@ -177,7 +178,7 @@ pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */
@ -201,7 +202,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
else if (*cond != NULL ||
(rval = pthread_cond_init(cond,NULL)) == 0) {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -230,14 +231,14 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
_thread_queue_deq(&(*cond)->c_queue);
} else {
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
/* Schedule the next thread: */
_thread_kern_sched_state(PS_COND_WAIT,
__FILE__, __LINE__);
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Lock the mutex: */
if ((rval = pthread_mutex_lock(mutex)) != 0) {
@ -258,7 +259,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */
@ -276,7 +277,7 @@ pthread_cond_signal(pthread_cond_t * cond)
rval = EINVAL;
else {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -297,7 +298,7 @@ pthread_cond_signal(pthread_cond_t * cond)
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */
@ -315,7 +316,7 @@ pthread_cond_broadcast(pthread_cond_t * cond)
rval = EINVAL;
else {
/* Lock the condition variable structure: */
_spinlock(&(*cond)->access_lock);
_SPINLOCK(&(*cond)->lock);
/* Process according to condition variable type: */
switch ((*cond)->c_type) {
@ -340,7 +341,7 @@ pthread_cond_broadcast(pthread_cond_t * cond)
}
/* Unlock the condition variable structure: */
_atomic_unlock(&(*cond)->access_lock);
_SPINUNLOCK(&(*cond)->lock);
}
/* Return the completion status: */

View File

@ -47,7 +47,7 @@ fcntl(int fd, int cmd,...)
va_list ap;
/* Lock the file descriptor: */
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Initialise the variable argument list: */
va_start(ap, cmd);
@ -80,8 +80,11 @@ fcntl(int fd, int cmd,...)
}
break;
case F_SETFD:
flags = va_arg(ap, int);
ret = _thread_sys_fcntl(fd, cmd, flags);
break;
case F_GETFD:
ret = _thread_sys_fcntl(fd, cmd, 0);
break;
case F_GETFL:
ret = _thread_fd_table[fd]->flags;
@ -102,7 +105,7 @@ fcntl(int fd, int cmd,...)
va_end(ap);
/* Unlock the file descriptor: */
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
/* Return the completion status: */

View File

@ -40,9 +40,9 @@ fsync(int fd)
{
int ret;
if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
ret = _thread_sys_fsync(fd);
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -154,7 +154,7 @@ _thread_dump_info(void)
}
/* Output a header for file descriptors: */
strcpy(s, "\n\n=============\nFILE DESCRIPTOR TABLE\n\n");
snprintf(s, sizeof(s), "\n\n=============\nFILE DESCRIPTOR TABLE (table size %d)\n\n",_thread_dtablesize);
_thread_sys_write(fd, s, strlen(s));
/* Enter a loop to report file descriptor lock usage: */

View File

@ -32,6 +32,7 @@
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
#include "pthread_private.h"
@ -94,7 +95,8 @@ pthread_mutex_init(pthread_mutex_t * mutex,
pmutex->m_flags |= MUTEX_FLAGS_INITED;
pmutex->m_owner = NULL;
pmutex->m_type = type;
pmutex->access_lock = 0;
memset(&pmutex->lock, 0,
sizeof(pmutex->lock));
*mutex = pmutex;
} else {
free(pmutex);
@ -116,7 +118,7 @@ pthread_mutex_destroy(pthread_mutex_t * mutex)
ret = EINVAL;
else {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/*
* Free the memory allocated for the mutex
@ -150,7 +152,7 @@ pthread_mutex_trylock(pthread_mutex_t * mutex)
else if (*mutex != NULL ||
(ret = pthread_mutex_init(mutex,NULL)) == 0) {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@ -195,7 +197,7 @@ pthread_mutex_trylock(pthread_mutex_t * mutex)
}
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
}
/* Return the completion status: */
@ -217,7 +219,7 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
else if (*mutex != NULL ||
(ret = pthread_mutex_init(mutex,NULL)) == 0) {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@ -240,13 +242,13 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
_thread_queue_enq(&(*mutex)->m_queue, _thread_run);
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
/* Block signals: */
_thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__);
/* Lock the mutex again: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
}
}
break;
@ -273,13 +275,13 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
_thread_queue_enq(&(*mutex)->m_queue, _thread_run);
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
/* Block signals: */
_thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__);
/* Lock the mutex again: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
}
}
@ -295,7 +297,7 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
}
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
}
/* Return the completion status: */
@ -311,7 +313,7 @@ pthread_mutex_unlock(pthread_mutex_t * mutex)
ret = EINVAL;
} else {
/* Lock the mutex structure: */
_spinlock(&(*mutex)->access_lock);
_SPINLOCK(&(*mutex)->lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@ -362,7 +364,7 @@ pthread_mutex_unlock(pthread_mutex_t * mutex)
}
/* Unlock the mutex structure: */
_atomic_unlock(&(*mutex)->access_lock);
_SPINUNLOCK(&(*mutex)->lock);
}
/* Return the completion status: */

View File

@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_read.c,v 1.3 1997/04/01 22:44:15 jb Exp $
* $Id: uthread_read.c,v 1.4 1998/04/29 09:59:10 jb Exp $
*
*/
#include <sys/types.h>
@ -46,9 +46,12 @@ read(int fd, void *buf, size_t nbytes)
{
int ret;
/* POSIX says to do just this: */
if (nbytes == 0)
return (0);
/* Lock the file descriptor for read: */
if ((ret = _thread_fd_lock(fd, FD_READ, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
/* Perform a non-blocking read syscall: */
while ((ret = _thread_sys_read(fd, buf, nbytes)) < 0) {
if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 &&
@ -75,7 +78,7 @@ read(int fd, void *buf, size_t nbytes)
break;
}
}
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return (ret);
}

View File

@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_readv.c,v 1.3 1997/04/01 22:44:16 jb Exp $
* $Id: uthread_readv.c,v 1.4 1998/04/29 09:59:11 jb Exp $
*
*/
#include <sys/types.h>
@ -47,8 +47,7 @@ readv(int fd, const struct iovec * iov, int iovcnt)
int ret;
/* Lock the file descriptor for read: */
if ((ret = _thread_fd_lock(fd, FD_READ, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
/* Perform a non-blocking readv syscall: */
while ((ret = _thread_sys_readv(fd, iov, iovcnt)) < 0) {
if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 &&
@ -75,7 +74,7 @@ readv(int fd, const struct iovec * iov, int iovcnt)
break;
}
}
_thread_fd_unlock(fd, FD_READ);
_FD_UNLOCK(fd, FD_READ);
}
return (ret);
}

View File

@ -73,13 +73,13 @@ select(int numfds, fd_set * readfds, fd_set * writefds,
for (i = 0; i < numfds; i++) {
if ((readfds && (FD_ISSET(i, readfds))) || (exceptfds && FD_ISSET(i, exceptfds))) {
if (writefds && FD_ISSET(i, writefds)) {
if ((ret = _thread_fd_lock(i, FD_RDWR, NULL, __FILE__, __LINE__)) != 0) {
if ((ret = _FD_LOCK(i, FD_RDWR, NULL)) != 0) {
got_all_locks = 0;
break;
}
FD_SET(i, &rdwr_locks);
} else {
if ((ret = _thread_fd_lock(i, FD_READ, NULL, __FILE__, __LINE__)) != 0) {
if ((ret = _FD_LOCK(i, FD_READ, NULL)) != 0) {
got_all_locks = 0;
break;
}
@ -87,7 +87,7 @@ select(int numfds, fd_set * readfds, fd_set * writefds,
}
} else {
if (writefds && FD_ISSET(i, writefds)) {
if ((ret = _thread_fd_lock(i, FD_WRITE, NULL, __FILE__, __LINE__)) != 0) {
if ((ret = _FD_LOCK(i, FD_WRITE, NULL)) != 0) {
got_all_locks = 0;
break;
}
@ -137,13 +137,13 @@ select(int numfds, fd_set * readfds, fd_set * writefds,
/* clean up the locks */
for (i = 0; i < numfds; i++)
if (FD_ISSET(i, &read_locks))
_thread_fd_unlock(i, FD_READ);
_FD_UNLOCK(i, FD_READ);
for (i = 0; i < numfds; i++)
if (FD_ISSET(i, &rdwr_locks))
_thread_fd_unlock(i, FD_RDWR);
_FD_UNLOCK(i, FD_RDWR);
for (i = 0; i < numfds; i++)
if (FD_ISSET(i, &write_locks))
_thread_fd_unlock(i, FD_WRITE);
_FD_UNLOCK(i, FD_WRITE);
if (ret > 0) {
if (readfds != NULL) {

View File

@ -39,17 +39,17 @@
#include "pthread_private.h"
/* Static variables: */
static int volatile yield_on_unlock_dead = 0;
static int volatile yield_on_unlock_thread = 0;
static long volatile thread_dead_lock = 0;
static long volatile thread_link_list_lock = 0;
static int volatile yield_on_unlock_dead = 0;
static int volatile yield_on_unlock_thread = 0;
static spinlock_t thread_dead_lock = _SPINLOCK_INITIALIZER;
static spinlock_t thread_link_list_lock = _SPINLOCK_INITIALIZER;
/* Lock the thread list: */
void
_lock_thread_list()
{
/* Lock the thread list: */
_spinlock(&thread_link_list_lock);
_SPINLOCK(&thread_link_list_lock);
}
/* Lock the dead thread list: */
@ -57,7 +57,7 @@ void
_lock_dead_thread_list()
{
/* Lock the dead thread list: */
_spinlock(&thread_dead_lock);
_SPINLOCK(&thread_dead_lock);
}
/* Lock the thread list: */
@ -65,7 +65,7 @@ void
_unlock_thread_list()
{
/* Unlock the thread list: */
_atomic_unlock(&thread_link_list_lock);
_SPINUNLOCK(&thread_link_list_lock);
/*
* Check if a scheduler interrupt occurred while the thread
@ -85,7 +85,7 @@ void
_unlock_dead_thread_list()
{
/* Unlock the dead thread list: */
_atomic_unlock(&thread_dead_lock);
_SPINUNLOCK(&thread_dead_lock);
/*
* Check if a scheduler interrupt occurred while the dead
@ -137,7 +137,7 @@ _thread_sig_handler(int sig, int code, struct sigcontext * scp)
* unfortunate time which one of the threads is
* modifying the thread list:
*/
if (thread_link_list_lock)
if (thread_link_list_lock.access_lock)
/*
* Set a flag so that the thread that has
* the lock yields when it unlocks the
@ -149,7 +149,7 @@ _thread_sig_handler(int sig, int code, struct sigcontext * scp)
* unfortunate time which one of the threads is
* modifying the dead thread list:
*/
if (thread_dead_lock)
if (thread_dead_lock.access_lock)
/*
* Set a flag so that the thread that has
* the lock yields when it unlocks the

View File

@ -46,19 +46,19 @@ pthread_key_create(pthread_key_t * key, void (*destructor) (void *))
{
for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) {
/* Lock the key table entry: */
_spinlock(&key_table[*key].access_lock);
_SPINLOCK(&key_table[*key].lock);
if (key_table[(*key)].allocated == 0) {
key_table[(*key)].allocated = 1;
key_table[(*key)].destructor = destructor;
/* Unlock the key table entry: */
_atomic_unlock(&key_table[*key].access_lock);
_SPINUNLOCK(&key_table[*key].lock);
return (0);
}
/* Unlock the key table entry: */
_atomic_unlock(&key_table[*key].access_lock);
_SPINUNLOCK(&key_table[*key].lock);
}
return (EAGAIN);
}
@ -70,7 +70,7 @@ pthread_key_delete(pthread_key_t key)
if (key < PTHREAD_KEYS_MAX) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
_SPINLOCK(&key_table[key].lock);
if (key_table[key].allocated)
key_table[key].allocated = 0;
@ -78,7 +78,7 @@ pthread_key_delete(pthread_key_t key)
ret = EINVAL;
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
_SPINUNLOCK(&key_table[key].lock);
} else
ret = EINVAL;
return (ret);
@ -90,25 +90,33 @@ _thread_cleanupspecific(void)
void *data;
int key;
int itr;
void (*destructor)( void *);
for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) {
for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
if (_thread_run->specific_data_count) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
_SPINLOCK(&key_table[key].lock);
destructor = NULL;
if (key_table[key].allocated) {
if (_thread_run->specific_data[key]) {
data = (void *) _thread_run->specific_data[key];
_thread_run->specific_data[key] = NULL;
_thread_run->specific_data_count--;
if (key_table[key].destructor)
key_table[key].destructor(data);
destructor = key_table[key].destructor;
}
}
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
_SPINUNLOCK(&key_table[key].lock);
/*
* If there is a destructore, call it
* with the key table entry unlocked:
*/
if (destructor)
destructor(data);
} else {
free(_thread_run->specific_data);
return;
@ -140,9 +148,6 @@ pthread_setspecific(pthread_key_t key, const void *value)
if ((pthread->specific_data) ||
(pthread->specific_data = pthread_key_allocate_data())) {
if (key < PTHREAD_KEYS_MAX) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
if (key_table[key].allocated) {
if (pthread->specific_data[key] == NULL) {
if (value != NULL)
@ -155,10 +160,6 @@ pthread_setspecific(pthread_key_t key, const void *value)
ret = 0;
} else
ret = EINVAL;
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
} else
ret = EINVAL;
} else
@ -177,9 +178,6 @@ pthread_getspecific(pthread_key_t key)
/* Check if there is specific data: */
if (pthread->specific_data != NULL && key < PTHREAD_KEYS_MAX) {
/* Lock the key table entry: */
_spinlock(&key_table[key].access_lock);
/* Check if this key has been used before: */
if (key_table[key].allocated) {
/* Return the value: */
@ -191,9 +189,6 @@ pthread_getspecific(pthread_key_t key)
*/
data = NULL;
}
/* Unlock the key table entry: */
_atomic_unlock(&key_table[key].access_lock);
} else
/* No specific data has been created, so just return NULL: */
data = NULL;

View File

@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_write.c,v 1.5 1998/04/29 09:59:33 jb Exp $
* $Id: uthread_write.c,v 1.6 1998/05/25 21:45:50 jb Exp $
*
*/
#include <sys/types.h>
@ -50,9 +50,12 @@ write(int fd, const void *buf, size_t nbytes)
ssize_t num = 0;
ssize_t ret;
/* POSIX says to do just this: */
if (nbytes == 0)
return (0);
/* Lock the file descriptor for write: */
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
/* Check if file operations are to block */
blocking = ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0);
@ -113,7 +116,7 @@ write(int fd, const void *buf, size_t nbytes)
/* Return the number of bytes written: */
ret = num;
}
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
return (ret);
}

View File

@ -29,13 +29,15 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: uthread_writev.c,v 1.6 1998/05/25 21:45:52 jb Exp $
* $Id: uthread_writev.c,v 1.7 1998/05/27 00:44:58 jb Exp $
*
*/
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/uio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
@ -68,8 +70,7 @@ writev(int fd, const struct iovec * iov, int iovcnt)
memcpy(p_iov,iov,iovcnt * sizeof(struct iovec));
/* Lock the file descriptor for write: */
if ((ret = _thread_fd_lock(fd, FD_WRITE, NULL,
__FILE__, __LINE__)) == 0) {
if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) {
/* Check if file operations are to block */
blocking = ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0);
@ -166,7 +167,7 @@ writev(int fd, const struct iovec * iov, int iovcnt)
/* Return the number of bytes written: */
ret = num;
}
_thread_fd_unlock(fd, FD_RDWR);
_FD_UNLOCK(fd, FD_RDWR);
}
/* If memory was allocated for the array, free it: */