2000-02-18 03:38:33 +08:00
|
|
|
/* thread.h: Locking and threading module definitions
|
|
|
|
|
2000-02-24 10:49:44 +08:00
|
|
|
Copyright 1998, 1999, 2000 Cygnus Solutions.
|
2001-03-21 10:17:58 +08:00
|
|
|
Copyright 2001 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
Written by Marco Fuykschot <marco@ddi.nl>
|
2001-03-21 10:17:58 +08:00
|
|
|
Major update 2001 Robert Collins <rbtcollins@hotmail.com>
|
|
|
|
|
2000-02-18 03:38:33 +08: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. */
|
|
|
|
|
|
|
|
#ifndef _CYGNUS_THREADS_
|
|
|
|
#define _CYGNUS_THREADS_
|
|
|
|
|
|
|
|
#define LOCK_FD_LIST 1
|
|
|
|
#define LOCK_MEMORY_LIST 2
|
|
|
|
#define LOCK_MMAP_LIST 3
|
|
|
|
#define LOCK_DLL_LIST 4
|
|
|
|
|
|
|
|
#define WRITE_LOCK 1
|
|
|
|
#define READ_LOCK 2
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#if defined (_CYG_THREAD_FAILSAFE) && defined (_MT_SAFE)
|
|
|
|
void AssertResourceOwner (int, int);
|
|
|
|
#else
|
2000-10-15 09:37:07 +08:00
|
|
|
# define AssertResourceOwner(i,ii)
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _MT_SAFE
|
|
|
|
|
|
|
|
#define SetResourceLock(i,n,c)
|
|
|
|
#define ReleaseResourceLock(i,n,c)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
//#include <pthread.h>
|
|
|
|
/* FIXME: these are defined in pthread.h, but pthread.h defines symbols it shouldn't -
|
|
|
|
* all the types.
|
|
|
|
*/
|
|
|
|
#define PTHREAD_PROCESS_PRIVATE 0
|
|
|
|
#define PTHREAD_PROCESS_SHARED 1
|
|
|
|
#define PTHREAD_DESTRUCTOR_ITERATIONS 1
|
|
|
|
/* Tls has 64 items for pre win2000 - and we don't want to use them all :]
|
|
|
|
* Before committing discuss this with the list
|
|
|
|
*/
|
|
|
|
#define PTHREAD_KEYS_MAX 32
|
|
|
|
#define PTHREAD_CREATE_DETACHED 1
|
|
|
|
/* the default : joinable */
|
|
|
|
#define PTHREAD_CREATE_JOINABLE 0
|
|
|
|
|
|
|
|
#include <signal.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#define _NOMNTENT_FUNCS
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <mntent.h>
|
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
extern "C"
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-03-21 10:17:58 +08:00
|
|
|
|
|
|
|
struct _winsup_t
|
|
|
|
{
|
2000-02-18 03:38:33 +08:00
|
|
|
/*
|
|
|
|
Needed for the group functions
|
|
|
|
*/
|
2001-03-21 10:17:58 +08:00
|
|
|
struct group _grp;
|
|
|
|
char *_namearray[2];
|
|
|
|
char _linebuf[100];
|
|
|
|
int _grp_pos;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* console.cc */
|
2001-03-21 10:17:58 +08:00
|
|
|
unsigned _rarg;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* dlfcn.cc */
|
2001-03-21 10:17:58 +08:00
|
|
|
int _dl_error;
|
|
|
|
char _dl_buffer[256];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* passwd.cc */
|
2001-03-21 10:17:58 +08:00
|
|
|
struct passwd _res;
|
|
|
|
char _tmpbuf[100];
|
|
|
|
char _pass[_PASSWORD_LEN];
|
|
|
|
int _pw_pos;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* path.cc */
|
2001-03-21 10:17:58 +08:00
|
|
|
struct mntent _ret;
|
|
|
|
int _iteration;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* strerror */
|
2001-03-21 10:17:58 +08:00
|
|
|
char _strerror_buf[20];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* syscalls.cc */
|
2001-03-21 10:17:58 +08:00
|
|
|
char _dacl_buf[1024];
|
|
|
|
char _sacl_buf[1024];
|
|
|
|
char _ownr_buf[1024];
|
|
|
|
char _grp_buf[1024];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* sysloc.cc */
|
2001-03-21 10:17:58 +08:00
|
|
|
char *_process_ident;
|
|
|
|
int _process_logopt;
|
|
|
|
int _process_facility;
|
|
|
|
int _process_logmask;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* times.cc */
|
2001-03-21 10:17:58 +08:00
|
|
|
char _b[20];
|
|
|
|
struct tm _localtime_buf;
|
|
|
|
char _buf1[33];
|
|
|
|
char _buf2[33];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* uinfo.cc */
|
2001-03-21 10:17:58 +08:00
|
|
|
char _username[MAX_USER_NAME];
|
|
|
|
};
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
struct __reent_t
|
|
|
|
{
|
|
|
|
struct _reent *_clib;
|
|
|
|
struct _winsup_t *_winsup;
|
|
|
|
};
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
_reent *_reent_clib ();
|
|
|
|
_winsup_t *_reent_winsup ();
|
|
|
|
void SetResourceLock (int, int, const char *) __attribute__ ((regparm (3)));
|
|
|
|
void ReleaseResourceLock (int, int, const char *)
|
|
|
|
__attribute__ ((regparm (3)));
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
#ifdef _CYG_THREAD_FAILSAFE
|
2001-03-21 10:17:58 +08:00
|
|
|
void AssertResourceOwner (int, int);
|
2000-02-18 03:38:33 +08:00
|
|
|
#else
|
|
|
|
#define AssertResourceOwner(i,ii)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
class per_process;
|
|
|
|
class pinfo;
|
|
|
|
|
|
|
|
class ResourceLocks
|
|
|
|
{
|
|
|
|
public:
|
2001-03-21 10:17:58 +08:00
|
|
|
ResourceLocks ()
|
|
|
|
{
|
|
|
|
};
|
2000-10-15 09:37:07 +08:00
|
|
|
LPCRITICAL_SECTION Lock (int);
|
|
|
|
void Init ();
|
|
|
|
void Delete ();
|
2000-02-18 03:38:33 +08:00
|
|
|
#ifdef _CYG_THREAD_FAILSAFE
|
2000-10-15 09:37:07 +08:00
|
|
|
DWORD owner;
|
|
|
|
DWORD count;
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif
|
2001-03-21 10:17:58 +08:00
|
|
|
private:
|
2000-10-15 09:37:07 +08:00
|
|
|
CRITICAL_SECTION lock;
|
|
|
|
bool inited;
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
#define PTHREAD_MAGIC 0xdf0df045
|
|
|
|
#define PTHREAD_MUTEX_MAGIC PTHREAD_MAGIC+1
|
|
|
|
#define PTHREAD_KEY_MAGIC PTHREAD_MAGIC+2
|
|
|
|
#define PTHREAD_ATTR_MAGIC PTHREAD_MAGIC+3
|
|
|
|
#define PTHREAD_MUTEXATTR_MAGIC PTHREAD_MAGIC+4
|
|
|
|
#define PTHREAD_COND_MAGIC PTHREAD_MAGIC+5
|
|
|
|
#define PTHREAD_CONDATTR_MAGIC PTHREAD_MAGIC+6
|
|
|
|
#define SEM_MAGIC PTHREAD_MAGIC+7
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
class verifyable_object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
long magic;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
verifyable_object (long);
|
|
|
|
~verifyable_object ();
|
|
|
|
};
|
|
|
|
|
|
|
|
int verifyable_object_isvalid (verifyable_object *, long);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
class pthread_attr:public verifyable_object
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-03-21 10:17:58 +08:00
|
|
|
public:
|
|
|
|
int joinable;
|
|
|
|
size_t stacksize;
|
|
|
|
|
|
|
|
pthread_attr ();
|
|
|
|
~pthread_attr ();
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
class pthread:public verifyable_object
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2001-03-21 10:17:58 +08:00
|
|
|
HANDLE win32_obj_id;
|
|
|
|
class pthread_attr attr;
|
|
|
|
void *(*function) (void *);
|
2000-10-15 09:37:07 +08:00
|
|
|
void *arg;
|
|
|
|
void *return_ptr;
|
|
|
|
bool suspended;
|
2001-03-21 10:17:58 +08:00
|
|
|
int joinable;
|
|
|
|
DWORD GetThreadId ()
|
|
|
|
{
|
|
|
|
return thread_id;
|
|
|
|
}
|
|
|
|
void setThreadIdtoCurrent ()
|
|
|
|
{
|
|
|
|
thread_id = GetCurrentThreadId ();
|
|
|
|
}
|
2000-10-15 09:37:07 +08:00
|
|
|
|
|
|
|
/* signal handling */
|
|
|
|
struct sigaction *sigs;
|
|
|
|
sigset_t *sigmask;
|
|
|
|
LONG *sigtodo;
|
2001-03-21 10:17:58 +08:00
|
|
|
void create (void *(*)(void *), pthread_attr *, void *);
|
|
|
|
|
|
|
|
pthread ();
|
|
|
|
~pthread ();
|
|
|
|
|
|
|
|
private:
|
|
|
|
DWORD thread_id;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
class pthread_mutexattr:public verifyable_object
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2001-03-21 10:17:58 +08:00
|
|
|
|
|
|
|
pthread_mutexattr ();
|
|
|
|
~pthread_mutexattr ();
|
|
|
|
};
|
|
|
|
|
|
|
|
class pthread_mutex:public verifyable_object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HANDLE win32_obj_id;
|
|
|
|
LONG condwaits;
|
|
|
|
|
2000-10-15 09:37:07 +08:00
|
|
|
int Lock ();
|
|
|
|
int TryLock ();
|
|
|
|
int UnLock ();
|
2001-03-21 10:17:58 +08:00
|
|
|
|
|
|
|
pthread_mutex (pthread_mutexattr *);
|
|
|
|
~pthread_mutex ();
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
class pthread_key:public verifyable_object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
DWORD dwTlsIndex;
|
|
|
|
int set (const void *);
|
|
|
|
void *get ();
|
|
|
|
|
|
|
|
pthread_key ();
|
|
|
|
~pthread_key ();
|
|
|
|
};
|
|
|
|
|
|
|
|
class pthread_condattr:public verifyable_object
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2000-10-15 09:37:07 +08:00
|
|
|
int shared;
|
2001-03-21 10:17:58 +08:00
|
|
|
|
|
|
|
pthread_condattr ();
|
|
|
|
~pthread_condattr ();
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
class pthread_cond:public verifyable_object
|
2001-03-17 09:14:58 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
int shared;
|
|
|
|
LONG waiting;
|
2001-03-21 10:17:58 +08:00
|
|
|
pthread_mutex *mutex;
|
|
|
|
HANDLE win32_obj_id;
|
2001-03-17 09:14:58 +08:00
|
|
|
int TimedWait (DWORD dwMilliseconds);
|
2001-03-21 10:17:58 +08:00
|
|
|
void BroadCast ();
|
|
|
|
void Signal ();
|
|
|
|
|
|
|
|
pthread_cond (pthread_condattr *);
|
|
|
|
~pthread_cond ();
|
2001-03-17 09:14:58 +08:00
|
|
|
};
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
/* shouldn't be here */
|
|
|
|
class semaphore:public verifyable_object
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-03-21 10:17:58 +08:00
|
|
|
public:
|
|
|
|
HANDLE win32_obj_id;
|
|
|
|
int shared;
|
|
|
|
void Wait ();
|
|
|
|
void Post ();
|
|
|
|
int TryWait ();
|
|
|
|
|
|
|
|
semaphore (int, unsigned int);
|
|
|
|
~semaphore ();
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef class pthread *pthread_t;
|
|
|
|
typedef class pthread_mutex *pthread_mutex_t;
|
|
|
|
/* sem routines belong in semaphore.cc */
|
|
|
|
typedef class semaphore *sem_t;
|
|
|
|
|
|
|
|
typedef class pthread_key *pthread_key_t;
|
|
|
|
typedef class pthread_attr *pthread_attr_t;
|
|
|
|
typedef class pthread_mutexattr *pthread_mutexattr_t;
|
|
|
|
typedef class pthread_condattr *pthread_condattr_t;
|
|
|
|
typedef class pthread_cond *pthread_cond_t;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
class MTinterface
|
|
|
|
{
|
|
|
|
public:
|
2000-10-15 09:37:07 +08:00
|
|
|
// General
|
|
|
|
DWORD reent_index;
|
2001-03-21 10:17:58 +08:00
|
|
|
DWORD thread_self_dwTlsIndex;
|
|
|
|
/* we may get 0 for the Tls index.. grrr */
|
|
|
|
int indexallocated;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-15 09:37:07 +08:00
|
|
|
// Used for main thread data, and sigproc thread
|
|
|
|
struct __reent_t reents;
|
|
|
|
struct _winsup_t winsup_reent;
|
2001-03-21 10:17:58 +08:00
|
|
|
pthread mainthread;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
void Init (int);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
MTinterface ():reent_index (0), indexallocated (0)
|
|
|
|
{
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
void *thread_init_wrapper (void *);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* ThreadCreation */
|
2001-03-21 10:17:58 +08:00
|
|
|
int __pthread_create (pthread_t * thread, const pthread_attr_t * attr,
|
|
|
|
void *(*start_routine) (void *), void *arg);
|
|
|
|
int __pthread_attr_init (pthread_attr_t * attr);
|
|
|
|
int __pthread_attr_destroy (pthread_attr_t * attr);
|
|
|
|
int __pthread_attr_setdetachstate (pthread_attr_t *, int);
|
|
|
|
int __pthread_attr_getdetachstate (const pthread_attr_t *, int *);
|
|
|
|
int __pthread_attr_setstacksize (pthread_attr_t * attr, size_t size);
|
|
|
|
int __pthread_attr_getstacksize (pthread_attr_t * attr, size_t * size);
|
2000-02-18 03:38:33 +08:00
|
|
|
/*
|
|
|
|
__pthread_attr_setstackaddr(...);
|
|
|
|
__pthread_attr_getstackaddr(...);
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Thread Exit */
|
2001-03-21 10:17:58 +08:00
|
|
|
void __pthread_exit (void *value_ptr);
|
|
|
|
int __pthread_join (pthread_t * thread, void **return_val);
|
|
|
|
int __pthread_detach (pthread_t * thread);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Thread suspend */
|
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
int __pthread_suspend (pthread_t * thread);
|
|
|
|
int __pthread_continue (pthread_t * thread);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-21 10:17:58 +08:00
|
|
|
unsigned long __pthread_getsequence_np (pthread_t * thread);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Thread SpecificData */
|
2001-03-21 10:17:58 +08:00
|
|
|
int __pthread_key_create (pthread_key_t * key, void (*destructor) (void *));
|
|
|
|
int __pthread_key_delete (pthread_key_t * key);
|
|
|
|
int __pthread_setspecific (pthread_key_t key, const void *value);
|
|
|
|
void *__pthread_getspecific (pthread_key_t key);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-17 09:14:58 +08:00
|
|
|
/* Thead synchroniation */
|
2001-03-21 10:17:58 +08:00
|
|
|
int __pthread_cond_destroy (pthread_cond_t * cond);
|
|
|
|
int __pthread_cond_init (pthread_cond_t * cond,
|
|
|
|
const pthread_condattr_t * attr);
|
|
|
|
int __pthread_cond_signal (pthread_cond_t * cond);
|
|
|
|
int __pthread_cond_broadcast (pthread_cond_t * cond);
|
|
|
|
int __pthread_cond_timedwait (pthread_cond_t * cond,
|
|
|
|
pthread_mutex_t * mutex,
|
|
|
|
const struct timespec *abstime);
|
|
|
|
int __pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex);
|
|
|
|
int __pthread_condattr_init (pthread_condattr_t * condattr);
|
|
|
|
int __pthread_condattr_destroy (pthread_condattr_t * condattr);
|
|
|
|
int __pthread_condattr_getpshared (const pthread_condattr_t * attr,
|
|
|
|
int *pshared);
|
|
|
|
int __pthread_condattr_setpshared (pthread_condattr_t * attr, int pshared);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Thread signal */
|
2001-03-21 10:17:58 +08:00
|
|
|
int __pthread_kill (pthread_t * thread, int sig);
|
|
|
|
int __pthread_sigmask (int operation, const sigset_t * set,
|
|
|
|
sigset_t * old_set);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* ID */
|
2001-03-21 10:17:58 +08:00
|
|
|
pthread_t __pthread_self ();
|
|
|
|
int __pthread_equal (pthread_t * t1, pthread_t * t2);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Mutexes */
|
2001-03-21 10:17:58 +08:00
|
|
|
int __pthread_mutex_init (pthread_mutex_t *, const pthread_mutexattr_t *);
|
|
|
|
int __pthread_mutex_lock (pthread_mutex_t *);
|
|
|
|
int __pthread_mutex_trylock (pthread_mutex_t *);
|
|
|
|
int __pthread_mutex_unlock (pthread_mutex_t *);
|
|
|
|
int __pthread_mutex_destroy (pthread_mutex_t *);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Semaphores */
|
2001-03-21 10:17:58 +08:00
|
|
|
int __sem_init (sem_t * sem, int pshared, unsigned int value);
|
|
|
|
int __sem_destroy (sem_t * sem);
|
|
|
|
int __sem_wait (sem_t * sem);
|
|
|
|
int __sem_trywait (sem_t * sem);
|
|
|
|
int __sem_post (sem_t * sem);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
#endif // MT_SAFE
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
#endif // _CYGNUS_THREADS_
|