2000-02-17 20:38:33 +01:00
|
|
|
/* pthread.cc: posix pthread interface for Cygwin
|
|
|
|
|
2003-01-10 13:32:49 +01:00
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-03-18 22:11:25 +01:00
|
|
|
Originally written by Marco Fuykschot <marco@ddi.nl>
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "thread.h"
|
2003-10-27 12:48:29 +01:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/fcntl.h>
|
2005-07-03 04:40:30 +02:00
|
|
|
#include "cygtls.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-03-21 03:17:58 +01:00
|
|
|
extern "C"
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-03-21 03:17:58 +01:00
|
|
|
/* ThreadCreation */
|
2001-04-09 02:44:25 +02:00
|
|
|
int
|
2001-04-12 06:04:53 +02:00
|
|
|
pthread_create (pthread_t * thread, const pthread_attr_t * attr,
|
|
|
|
void *(*start_routine) (void *), void *arg)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return pthread::create (thread, attr, start_routine, arg);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
int
|
|
|
|
pthread_once (pthread_once_t * once_control, void (*init_routine) (void))
|
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return pthread::once (once_control, init_routine);
|
2001-04-12 06:04:53 +02:00
|
|
|
}
|
|
|
|
|
2001-04-13 17:28:20 +02:00
|
|
|
int
|
2002-09-22 05:38:57 +02:00
|
|
|
pthread_atfork (void (*prepare)(void), void (*parent)(void), void (*child)(void))
|
2001-04-13 17:28:20 +02:00
|
|
|
{
|
2002-09-22 05:38:57 +02:00
|
|
|
return pthread::atfork (prepare, parent, child);
|
2001-04-13 17:28:20 +02:00
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Thread Exit */
|
2001-04-12 06:04:53 +02:00
|
|
|
void
|
|
|
|
pthread_exit (void *value_ptr)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-22 05:38:57 +02:00
|
|
|
return pthread::self ()->exit (value_ptr);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
int
|
|
|
|
pthread_join (pthread_t thread, void **return_val)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return pthread::join (&thread, (void **) return_val);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
int
|
|
|
|
pthread_detach (pthread_t thread)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return pthread::detach (&thread);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
|
|
|
|
/* This isn't a posix call... should we keep it? */
|
|
|
|
int
|
|
|
|
pthread_suspend (pthread_t thread)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return pthread::suspend (&thread);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
/* same */
|
|
|
|
int
|
|
|
|
pthread_continue (pthread_t thread)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return pthread::resume (&thread);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
unsigned long
|
|
|
|
pthread_getsequence_np (pthread_t * thread)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2003-03-27 20:52:20 +01:00
|
|
|
if (!pthread::is_good_object (thread))
|
2002-09-16 12:53:29 +02:00
|
|
|
return EINVAL;
|
2002-09-22 05:38:57 +02:00
|
|
|
return (*thread)->getsequence_np ();
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* ID */
|
|
|
|
|
2001-04-09 02:44:25 +02:00
|
|
|
pthread_t pthread_self ()
|
|
|
|
{
|
2002-09-22 05:38:57 +02:00
|
|
|
return pthread::self ();
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Mutexes */
|
2001-04-09 02:44:25 +02:00
|
|
|
int
|
2001-04-12 06:04:53 +02:00
|
|
|
pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * attr)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-30 01:47:45 +02:00
|
|
|
return pthread_mutex::init (mutex, attr);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2001-03-17 02:14:58 +01:00
|
|
|
/* Synchronisation */
|
2001-04-09 02:44:25 +02:00
|
|
|
int
|
2001-04-12 06:04:53 +02:00
|
|
|
pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * attr)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2003-01-09 21:40:44 +01:00
|
|
|
return pthread_cond::init (cond, attr);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
* cygwin.din: Add pthread_rwlock_destroy, pthread_rwlock_init,
pthread_rwlock_rdlock, pthread_rwlock_tryrdlock,
pthread_rwlock_wrlock, pthread_rwlock_trywrlock,
pthread_rwlock_unlock, pthread_rwlockattr_init,
pthread_rwlockattr_getpshared, pthread_rwlockattr_setpshared,
and pthread_rwlockattr_destroy.
* include/cygwin/version.h: Bump API minor number.
* include/pthread.h (PTHREAD_RWLOCK_INITIALIZER): Define a
reasonable value.
Add prototypes for pthread_rwlock_destroy, pthread_rwlock_init,
pthread_rwlock_rdlock, pthread_rwlock_tryrdlock,
pthread_rwlock_wrlock, pthread_rwlock_trywrlock,
pthread_rwlock_unlock, pthread_rwlockattr_init,
pthread_rwlockattr_getpshared, pthread_rwlockattr_setpshared,
and pthread_rwlockattr_destroy.
* thread.h (PTHREAD_ONCE_MAGIC): Remove superflous semicolon.
(PTHREAD_RWLOCK_MAGIC): New define.
(PTHREAD_RWLOCKATTR_MAGIC): Ditto.
(pthread_rwlockattr): New class.
(pthread_rwlock): Ditto.
(MTinterface::rwlocks): New member.
(MTinterface::MTinterface): Initialize rwlocks.
Add prototypes for __pthread_rwlock_destroy,
__pthread_rwlock_wrlock, __pthread_rwlock_trywrlock,
__pthread_rwlock_unlock, __pthread_rwlockattr_init,
__pthread_rwlockattr_getpshared, __pthread_rwlockattr_setpshared,
and __pthread_rwlockattr_destroy.
* thread.cc (MTinterface::Init): Initialize rwlock internal mutex.
(MTinterface::fixup_after_fork): Fixup rwlocks after fork.
(pthread_rwlockattr::isGoodObject): Implement.
(pthread_rwlockattr::pthread_rwlockattr): Ditto.
(pthread_rwlockattr::~pthread_rwlockattr): Ditto.
(pthread_rwlock::initMutex): Ditto.
(pthread_rwlock::pthread_rwlock): Ditto.
(pthread_rwlock::~pthread_rwlock): Ditto.
(pthread_rwlock::RdLock): Ditto.
(pthread_rwlock::TryRdLock): Ditto.
(pthread_rwlock::WrLock): Ditto.
(pthread_rwlock::TryWrLock): Ditto.
(pthread_rwlock::UnLock): Ditto.
(pthread_rwlock::addReader): Ditto.
(pthread_rwlock::removeReader): Ditto.
(pthread_rwlock::lookupReader): Ditto.
(pthread_rwlock::RdLockCleanup): Ditto.
(pthread_rwlock::WrLockCleanup): Ditto.
(pthread_rwlock::fixup_after_fork): Ditto.
(pthread_rwlock::isGoodObject): Ditto.
(pthread_rwlock::isGoodInitializer): Ditto.
(pthread_rwlock::isGoodInitializerOrObject): Ditto.
(pthread_rwlock::isGoodInitializerOrBadObject): Ditto.
(__pthread_rwlock_destroy): Ditto.
(pthread_rwlock::init): Ditto.
(__pthread_rwlock_rdlock): Ditto.
(__pthread_rwlock_tryrdlock): Ditto.
(__pthread_rwlock_wrlock): Ditto.
(__pthread_rwlock_trywrlock): Ditto.
2003-03-18 21:01:07 +01:00
|
|
|
/* RW Locks */
|
|
|
|
int
|
|
|
|
pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
|
|
|
|
{
|
|
|
|
return pthread_rwlock::init (rwlock, attr);
|
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
/* Cancelability */
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_cancel (pthread_t thread)
|
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return pthread::cancel (thread);
|
2001-04-12 06:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_setcancelstate (int state, int *oldstate)
|
|
|
|
{
|
2002-09-22 05:38:57 +02:00
|
|
|
return pthread::self ()->setcancelstate (state, oldstate);
|
2001-04-12 06:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_setcanceltype (int type, int *oldtype)
|
|
|
|
{
|
2002-09-22 05:38:57 +02:00
|
|
|
return pthread::self ()->setcanceltype (type, oldtype);
|
2001-04-12 06:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-07-05 05:16:46 +02:00
|
|
|
pthread_testcancel ()
|
2001-04-12 06:04:53 +02:00
|
|
|
{
|
2002-09-22 05:38:57 +02:00
|
|
|
pthread::self ()->testcancel ();
|
2001-04-12 06:04:53 +02:00
|
|
|
}
|
|
|
|
|
2002-06-10 03:10:45 +02:00
|
|
|
void
|
|
|
|
_pthread_cleanup_push (__pthread_cleanup_handler *handler)
|
|
|
|
{
|
2002-09-22 05:38:57 +02:00
|
|
|
pthread::self ()->push_cleanup_handler (handler);
|
2002-06-10 03:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_pthread_cleanup_pop (int execute)
|
|
|
|
{
|
2002-09-22 05:38:57 +02:00
|
|
|
pthread::self ()->pop_cleanup_handler (execute);
|
2002-06-10 03:10:45 +02:00
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Semaphores */
|
2001-04-12 06:04:53 +02:00
|
|
|
int
|
|
|
|
sem_init (sem_t * sem, int pshared, unsigned int value)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return semaphore::init (sem, pshared, value);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
int
|
|
|
|
sem_destroy (sem_t * sem)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return semaphore::destroy (sem);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2003-10-27 12:48:29 +01:00
|
|
|
/* Mangle semaphore name to follow windows naming rules. Prepend "Global\"
|
|
|
|
if running on terminal service aware machine. Substitute invalid backslash
|
|
|
|
by forward slash characters, hoping not to collide. */
|
|
|
|
static bool
|
|
|
|
mangle_sem_name (char *mangled, const char *name)
|
|
|
|
{
|
2005-07-03 04:40:30 +02:00
|
|
|
myfault efault;
|
|
|
|
if (efault.faulted (EFAULT))
|
2003-10-27 12:48:29 +01:00
|
|
|
return false;
|
2005-07-03 04:40:30 +02:00
|
|
|
if (!*name)
|
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return false;
|
|
|
|
}
|
2003-10-27 12:48:29 +01:00
|
|
|
int len = strlen (name);
|
2005-04-03 10:45:21 +02:00
|
|
|
if (len >= CYG_MAX_PATH
|
|
|
|
|| (wincap.has_terminal_services () && len >= CYG_MAX_PATH - 7))
|
2003-10-27 12:48:29 +01:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
strcpy (mangled, wincap.has_terminal_services () ? "Global\\" : "");
|
|
|
|
char *d = mangled + strlen (mangled);
|
|
|
|
const char *s = name;
|
|
|
|
while (*s)
|
|
|
|
*d++ = (*s == '\\') ? '/' : *s++;
|
|
|
|
*d = '\0';
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
sem_t *
|
|
|
|
sem_open (const char *name, int oflag, ...)
|
|
|
|
{
|
|
|
|
mode_t mode = 0;
|
|
|
|
unsigned int value = 0;
|
|
|
|
if (oflag & O_CREAT)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, oflag);
|
|
|
|
mode = va_arg (ap, mode_t);
|
|
|
|
value = va_arg (ap, unsigned int);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
2005-04-03 10:45:21 +02:00
|
|
|
char mangled_name[CYG_MAX_PATH];
|
2003-10-27 12:48:29 +01:00
|
|
|
if (!mangle_sem_name (mangled_name, name))
|
|
|
|
return NULL;
|
|
|
|
return semaphore::open (mangled_name, oflag, mode, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sem_close (sem_t * sem)
|
|
|
|
{
|
|
|
|
return semaphore::destroy (sem);
|
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
int
|
|
|
|
sem_wait (sem_t * sem)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return semaphore::wait (sem);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
int
|
|
|
|
sem_trywait (sem_t * sem)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return semaphore::trywait (sem);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
|
|
|
|
2003-10-27 12:48:29 +01:00
|
|
|
int
|
|
|
|
sem_timedwait (sem_t * sem, const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
return semaphore::timedwait (sem, abstime);
|
|
|
|
}
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
int
|
|
|
|
sem_post (sem_t * sem)
|
2001-04-09 02:44:25 +02:00
|
|
|
{
|
2002-09-21 05:59:58 +02:00
|
|
|
return semaphore::post (sem);
|
2001-04-09 02:44:25 +02:00
|
|
|
}
|
2001-04-12 06:04:53 +02:00
|
|
|
|
2003-10-27 12:48:29 +01:00
|
|
|
int
|
|
|
|
sem_getvalue (sem_t * sem, int *sval)
|
|
|
|
{
|
|
|
|
return semaphore::getvalue (sem, sval);
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|