2000-02-17 20:38:33 +01:00
|
|
|
/* thread.h: Locking and threading module definitions
|
|
|
|
|
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
|
|
|
|
|
|
|
Written by Marco Fuykschot <marco@ddi.nl>
|
2001-03-21 03:17:58 +01:00
|
|
|
Major update 2001 Robert Collins <rbtcollins@hotmail.com>
|
2001-06-26 16:47:48 +02:00
|
|
|
|
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. */
|
|
|
|
|
|
|
|
#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)
|
2001-04-12 06:04:53 +02:00
|
|
|
void AssertResourceOwner (int, int);
|
2000-02-17 20:38:33 +01:00
|
|
|
#else
|
2001-04-01 05:06:02 +02:00
|
|
|
#define AssertResourceOwner(i,ii)
|
2000-02-17 20:38:33 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _MT_SAFE
|
|
|
|
|
|
|
|
#define SetResourceLock(i,n,c)
|
|
|
|
#define ReleaseResourceLock(i,n,c)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
#include <pthread.h>
|
2001-03-21 03:17:58 +01:00
|
|
|
#include <signal.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2000-08-22 07:10:20 +02:00
|
|
|
#define _NOMNTENT_FUNCS
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <mntent.h>
|
|
|
|
|
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
|
|
|
|
2001-04-01 05:06:02 +02:00
|
|
|
struct _winsup_t
|
|
|
|
{
|
|
|
|
/*
|
2001-04-12 06:04:53 +02:00
|
|
|
Needed for the group functions
|
|
|
|
*/
|
* (child_info.h, cygheap.h, dcrt0.cc, dir.cc, fhandler.cc, fhandler.h,
fhandler_clipboard.cc, fhandler_disk_file.cc, fhandler_dsp.cc,
fhandler_floppy.cc, fhandler_mem.cc, fhandler_random.cc,
fhandler_tape.cc, fhandler_zero.cc, grp.cc, mmap.cc, passwd.cc,
pinfo.cc, pinfo.h, pipe.cc, sec_acl.cc, sec_helper.cc, security.cc,
security.h, spawn.cc, syscalls.cc, thread.h, uinfo.cc, winsup.h):
Change usage of uid_t to __uid16_t, gid_t to __gid16_t and
off_t to __off32_t throughout. Use INVALID_UID, INVALID_GID and
INVALID_SEEK instead casting -1 to the appropriate type.
* winsup.h: Define INVALID_UID, INVALID_GID and INVALID_SEEK.
* include/cygwin/acl.h: Define internal __aclent16_t and __aclent32_t
types. Don't declare acl functions when compiling Cygwin.
* include/cygwin/grp.h: Declare getgrgid() and getgrnam() with
correct types for internal usage.
2002-02-10 14:38:51 +01:00
|
|
|
struct __group16 _grp;
|
2001-04-01 05:06:02 +02:00
|
|
|
char *_namearray[2];
|
|
|
|
int _grp_pos;
|
|
|
|
|
|
|
|
/* console.cc */
|
|
|
|
unsigned _rarg;
|
|
|
|
|
|
|
|
/* dlfcn.cc */
|
|
|
|
int _dl_error;
|
|
|
|
char _dl_buffer[256];
|
|
|
|
|
|
|
|
/* passwd.cc */
|
|
|
|
struct passwd _res;
|
|
|
|
char _pass[_PASSWORD_LEN];
|
|
|
|
int _pw_pos;
|
|
|
|
|
|
|
|
/* path.cc */
|
|
|
|
struct mntent mntbuf;
|
|
|
|
int _iteration;
|
|
|
|
DWORD available_drives;
|
2001-04-02 06:27:12 +02:00
|
|
|
char mnt_type[80];
|
|
|
|
char mnt_opts[80];
|
|
|
|
char mnt_fsname[MAX_PATH];
|
|
|
|
char mnt_dir[MAX_PATH];
|
2001-04-01 05:06:02 +02:00
|
|
|
|
|
|
|
/* strerror */
|
|
|
|
char _strerror_buf[20];
|
|
|
|
|
|
|
|
/* sysloc.cc */
|
|
|
|
char *_process_ident;
|
|
|
|
int _process_logopt;
|
|
|
|
int _process_facility;
|
|
|
|
int _process_logmask;
|
|
|
|
|
|
|
|
/* times.cc */
|
|
|
|
char timezone_buf[20];
|
|
|
|
struct tm _localtime_buf;
|
|
|
|
|
|
|
|
/* uinfo.cc */
|
2001-04-30 20:21:48 +02:00
|
|
|
char _username[UNLEN + 1];
|
2001-08-22 23:51:48 +02:00
|
|
|
|
|
|
|
/* net.cc */
|
|
|
|
char *_ntoa_buf;
|
|
|
|
struct protoent *_protoent_buf;
|
|
|
|
struct servent *_servent_buf;
|
|
|
|
struct hostent *_hostent_buf;
|
2001-04-01 05:06:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct __reent_t
|
|
|
|
{
|
|
|
|
struct _reent *_clib;
|
|
|
|
struct _winsup_t *_winsup;
|
|
|
|
};
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-04-01 05:06:02 +02: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-17 20:38:33 +01:00
|
|
|
|
|
|
|
#ifdef _CYG_THREAD_FAILSAFE
|
2001-04-01 05:06:02 +02:00
|
|
|
void AssertResourceOwner (int, int);
|
2000-02-17 20:38:33 +01:00
|
|
|
#else
|
|
|
|
#define AssertResourceOwner(i,ii)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-01-09 21:40:44 +01:00
|
|
|
class nativeMutex
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool init ();
|
|
|
|
bool lock ();
|
|
|
|
void unlock ();
|
|
|
|
private:
|
|
|
|
HANDLE theHandle;
|
|
|
|
};
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
class per_process;
|
|
|
|
class pinfo;
|
|
|
|
|
|
|
|
class ResourceLocks
|
|
|
|
{
|
|
|
|
public:
|
2001-04-12 06:04:53 +02:00
|
|
|
ResourceLocks ()
|
|
|
|
{
|
|
|
|
}
|
2000-10-15 03:37:07 +02:00
|
|
|
LPCRITICAL_SECTION Lock (int);
|
|
|
|
void Init ();
|
|
|
|
void Delete ();
|
2000-02-17 20:38:33 +01:00
|
|
|
#ifdef _CYG_THREAD_FAILSAFE
|
2000-10-15 03:37:07 +02:00
|
|
|
DWORD owner;
|
|
|
|
DWORD count;
|
2000-02-17 20:38:33 +01:00
|
|
|
#endif
|
2001-03-21 03:17:58 +01:00
|
|
|
private:
|
2000-10-15 03:37:07 +02:00
|
|
|
CRITICAL_SECTION lock;
|
|
|
|
bool inited;
|
2000-02-17 20:38:33 +01:00
|
|
|
};
|
|
|
|
|
2001-03-21 03:17:58 +01: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
|
2001-04-12 06:04:53 +02:00
|
|
|
#define PTHREAD_ONCE_MAGIC PTHREAD_MAGIC+8;
|
|
|
|
|
2003-01-09 21:50:23 +01:00
|
|
|
#define MUTEX_LOCK_COUNTER_INITIAL (-1)
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
/* verifyable_object should not be defined here - it's a general purpose class */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-03-21 03:17:58 +01:00
|
|
|
class verifyable_object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
long magic;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
verifyable_object (long);
|
|
|
|
virtual ~verifyable_object ();
|
2001-03-21 03:17:58 +01:00
|
|
|
};
|
|
|
|
|
2001-11-05 07:09:15 +01:00
|
|
|
typedef enum
|
2001-09-29 11:01:01 +02:00
|
|
|
{
|
|
|
|
VALID_OBJECT,
|
|
|
|
INVALID_OBJECT,
|
|
|
|
VALID_STATIC_OBJECT
|
|
|
|
} verifyable_object_state;
|
|
|
|
|
|
|
|
verifyable_object_state verifyable_object_isvalid (void const *, long);
|
|
|
|
verifyable_object_state verifyable_object_isvalid (void const *, long, void *);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-09-21 05:20:27 +02:00
|
|
|
/* interface */
|
|
|
|
template <class ListNode> class List {
|
|
|
|
public:
|
|
|
|
List();
|
|
|
|
void Insert (ListNode *aNode);
|
|
|
|
ListNode *Remove ( ListNode *aNode);
|
|
|
|
ListNode *Pop ();
|
|
|
|
void forEach (void (*)(ListNode *aNode));
|
|
|
|
protected:
|
|
|
|
ListNode *head;
|
|
|
|
};
|
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
class pthread_key:public verifyable_object
|
|
|
|
{
|
|
|
|
public:
|
2002-09-21 00:34:05 +02:00
|
|
|
static bool isGoodObject (pthread_key_t const *);
|
2002-09-21 05:20:27 +02:00
|
|
|
static void runAllDestructors ();
|
2001-04-12 06:04:53 +02:00
|
|
|
|
|
|
|
DWORD dwTlsIndex;
|
2002-09-17 11:12:36 +02:00
|
|
|
|
2001-04-12 06:04:53 +02:00
|
|
|
int set (const void *);
|
2002-09-21 03:59:46 +02:00
|
|
|
void *get () const;
|
2001-04-12 06:04:53 +02:00
|
|
|
|
2002-09-21 03:59:46 +02:00
|
|
|
pthread_key (void (*)(void *));
|
2002-09-29 04:19:35 +02:00
|
|
|
~pthread_key ();
|
2002-09-17 11:12:36 +02:00
|
|
|
static void fixup_before_fork();
|
|
|
|
static void fixup_after_fork();
|
2002-09-21 05:20:27 +02:00
|
|
|
|
|
|
|
/* List support calls */
|
|
|
|
class pthread_key *next;
|
2002-09-17 11:12:36 +02:00
|
|
|
private:
|
|
|
|
// lists of objects. USE THREADSAFE INSERTS AND DELETES.
|
2002-09-21 05:20:27 +02:00
|
|
|
static List<pthread_key> keys;
|
|
|
|
static void saveAKey (pthread_key *);
|
|
|
|
static void restoreAKey (pthread_key *);
|
|
|
|
static void destroyAKey (pthread_key *);
|
2002-09-17 11:12:36 +02:00
|
|
|
void saveKeyToBuffer ();
|
|
|
|
void recreateKeyFromBuffer ();
|
2002-09-21 03:59:46 +02:00
|
|
|
void (*destructor) (void *);
|
2002-09-27 17:08:50 +02:00
|
|
|
void run_destructor ();
|
2002-09-21 05:20:27 +02:00
|
|
|
void *fork_buf;
|
2001-04-12 06:04:53 +02:00
|
|
|
};
|
|
|
|
|
2002-09-21 03:59:46 +02:00
|
|
|
/* implementation */
|
2002-09-21 05:20:27 +02:00
|
|
|
template <class ListNode>
|
|
|
|
List<ListNode>::List<ListNode> () : head(NULL)
|
|
|
|
{
|
|
|
|
}
|
2002-09-21 03:59:46 +02:00
|
|
|
template <class ListNode> void
|
|
|
|
List<ListNode>::Insert (ListNode *aNode)
|
|
|
|
{
|
|
|
|
if (!aNode)
|
2002-09-29 04:19:35 +02:00
|
|
|
return;
|
2002-09-21 05:20:27 +02:00
|
|
|
aNode->next = (ListNode *) InterlockedExchangePointer (&head, aNode);
|
2002-09-21 03:59:46 +02:00
|
|
|
}
|
|
|
|
template <class ListNode> ListNode *
|
|
|
|
List<ListNode>::Remove ( ListNode *aNode)
|
|
|
|
{
|
|
|
|
if (!aNode)
|
2002-09-29 04:19:35 +02:00
|
|
|
return NULL;
|
2002-09-21 03:59:46 +02:00
|
|
|
if (!head)
|
2002-09-29 04:19:35 +02:00
|
|
|
return NULL;
|
2002-09-21 03:59:46 +02:00
|
|
|
if (aNode == head)
|
2002-09-29 04:19:35 +02:00
|
|
|
return Pop ();
|
2002-09-21 03:59:46 +02:00
|
|
|
ListNode *resultPrev = head;
|
2002-09-21 05:20:27 +02:00
|
|
|
while (resultPrev && resultPrev->next && !(aNode == resultPrev->next))
|
2002-09-29 04:19:35 +02:00
|
|
|
resultPrev = resultPrev->next;
|
2002-09-21 03:59:46 +02:00
|
|
|
if (resultPrev)
|
2002-09-29 04:19:35 +02:00
|
|
|
return (ListNode *)InterlockedExchangePointer (&resultPrev->next, resultPrev->next->next);
|
2002-09-21 03:59:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
template <class ListNode> ListNode *
|
|
|
|
List<ListNode>::Pop ()
|
|
|
|
{
|
2002-09-21 05:20:27 +02:00
|
|
|
return (ListNode *) InterlockedExchangePointer (&head, head->next);
|
2002-09-21 03:59:46 +02:00
|
|
|
}
|
2002-09-21 05:20:27 +02:00
|
|
|
/* poor mans generic programming. */
|
|
|
|
template <class ListNode> void
|
|
|
|
List<ListNode>::forEach (void (*callback)(ListNode *))
|
2001-04-12 06:04:53 +02:00
|
|
|
{
|
2002-09-21 05:20:27 +02:00
|
|
|
ListNode *aNode = head;
|
2002-09-30 04:51:22 +02:00
|
|
|
while (aNode)
|
2002-09-29 04:19:35 +02:00
|
|
|
{
|
|
|
|
callback (aNode);
|
|
|
|
aNode = aNode->next;
|
|
|
|
}
|
2002-09-21 05:20:27 +02:00
|
|
|
}
|
2001-04-12 06:04:53 +02:00
|
|
|
|
2001-03-21 03:17:58 +01:00
|
|
|
class pthread_attr:public verifyable_object
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-03-21 03:17:58 +01:00
|
|
|
public:
|
2002-09-21 00:34:05 +02:00
|
|
|
static bool isGoodObject(pthread_attr_t const *);
|
2001-03-21 03:17:58 +01:00
|
|
|
int joinable;
|
2001-04-12 06:04:53 +02:00
|
|
|
int contentionscope;
|
|
|
|
int inheritsched;
|
|
|
|
struct sched_param schedparam;
|
2001-03-21 03:17:58 +01:00
|
|
|
size_t stacksize;
|
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
pthread_attr ();
|
|
|
|
~pthread_attr ();
|
2000-02-17 20:38:33 +01:00
|
|
|
};
|
|
|
|
|
2002-06-10 04:40:13 +02:00
|
|
|
class pthread_mutexattr:public verifyable_object
|
|
|
|
{
|
|
|
|
public:
|
2002-09-21 00:34:05 +02:00
|
|
|
static bool isGoodObject(pthread_mutexattr_t const *);
|
2002-06-10 04:40:13 +02:00
|
|
|
int pshared;
|
|
|
|
int mutextype;
|
2002-09-29 04:19:35 +02:00
|
|
|
pthread_mutexattr ();
|
|
|
|
~pthread_mutexattr ();
|
2002-06-10 04:40:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class pthread_mutex:public verifyable_object
|
|
|
|
{
|
|
|
|
public:
|
2003-01-09 21:40:44 +01:00
|
|
|
static bool isGoodObject (pthread_mutex_t const *);
|
|
|
|
static bool isGoodInitializer (pthread_mutex_t const *);
|
|
|
|
static bool isGoodInitializerOrObject (pthread_mutex_t const *);
|
2002-10-17 10:44:18 +02:00
|
|
|
static bool isGoodInitializerOrBadObject (pthread_mutex_t const *mutex);
|
2002-09-30 01:47:45 +02:00
|
|
|
static void initMutex ();
|
|
|
|
static int init (pthread_mutex_t *, const pthread_mutexattr_t *);
|
2002-09-30 04:51:22 +02:00
|
|
|
|
2003-01-09 21:50:23 +01:00
|
|
|
LONG lock_counter;
|
2002-06-10 04:40:13 +02:00
|
|
|
HANDLE win32_obj_id;
|
2003-01-09 21:50:23 +01:00
|
|
|
unsigned int recursion_counter;
|
2002-06-10 04:40:13 +02:00
|
|
|
LONG condwaits;
|
2003-01-09 21:50:23 +01:00
|
|
|
pthread_t owner;
|
|
|
|
int type;
|
2002-06-10 04:40:13 +02:00
|
|
|
int pshared;
|
|
|
|
class pthread_mutex * next;
|
|
|
|
|
|
|
|
int Lock ();
|
|
|
|
int TryLock ();
|
|
|
|
int UnLock ();
|
2003-01-09 21:50:23 +01:00
|
|
|
int Destroy ();
|
|
|
|
void SetOwner ();
|
|
|
|
int LockRecursive ();
|
2002-06-10 04:40:13 +02:00
|
|
|
void fixup_after_fork ();
|
|
|
|
|
|
|
|
pthread_mutex (pthread_mutexattr * = NULL);
|
|
|
|
pthread_mutex (pthread_mutex_t *, pthread_mutexattr *);
|
|
|
|
~pthread_mutex ();
|
2003-01-09 21:40:44 +01:00
|
|
|
|
2002-09-30 01:47:45 +02:00
|
|
|
private:
|
2002-09-30 13:43:43 +02:00
|
|
|
static nativeMutex mutexInitializationLock;
|
2002-06-10 04:40:13 +02:00
|
|
|
};
|
|
|
|
|
2003-01-09 21:57:54 +01:00
|
|
|
#define WAIT_CANCELED (WAIT_OBJECT_0 + 1)
|
|
|
|
|
2001-03-21 03:17:58 +01:00
|
|
|
class pthread:public verifyable_object
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
public:
|
2001-03-21 03:17:58 +01:00
|
|
|
HANDLE win32_obj_id;
|
|
|
|
class pthread_attr attr;
|
|
|
|
void *(*function) (void *);
|
2000-10-15 03:37:07 +02:00
|
|
|
void *arg;
|
|
|
|
void *return_ptr;
|
|
|
|
bool suspended;
|
2001-04-12 06:04:53 +02:00
|
|
|
int cancelstate, canceltype;
|
2002-07-04 16:17:30 +02:00
|
|
|
HANDLE cancel_event;
|
2002-06-05 14:39:55 +02:00
|
|
|
pthread_t joiner;
|
2001-04-12 06:04:53 +02:00
|
|
|
// int joinable;
|
|
|
|
|
2000-10-15 03:37:07 +02:00
|
|
|
/* signal handling */
|
|
|
|
struct sigaction *sigs;
|
|
|
|
sigset_t *sigmask;
|
|
|
|
LONG *sigtodo;
|
2002-09-16 12:53:29 +02:00
|
|
|
virtual void create (void *(*)(void *), pthread_attr *, void *);
|
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
pthread ();
|
|
|
|
virtual ~pthread ();
|
2001-03-21 03:17:58 +01:00
|
|
|
|
2002-11-24 14:54:14 +01:00
|
|
|
static void initMainThread (bool);
|
2002-09-29 04:19:35 +02:00
|
|
|
static bool isGoodObject(pthread_t const *);
|
|
|
|
static void atforkprepare();
|
|
|
|
static void atforkparent();
|
|
|
|
static void atforkchild();
|
2001-03-21 03:17:58 +01:00
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
/* API calls */
|
|
|
|
static int cancel (pthread_t);
|
|
|
|
static int join (pthread_t * thread, void **return_val);
|
|
|
|
static int detach (pthread_t * thread);
|
|
|
|
static int create (pthread_t * thread, const pthread_attr_t * attr,
|
2002-09-21 05:59:58 +02:00
|
|
|
void *(*start_routine) (void *), void *arg);
|
2002-09-29 04:19:35 +02:00
|
|
|
static int once (pthread_once_t *, void (*)(void));
|
|
|
|
static int atfork(void (*)(void), void (*)(void), void (*)(void));
|
|
|
|
static int suspend (pthread_t * thread);
|
|
|
|
static int resume (pthread_t * thread);
|
2002-09-21 05:59:58 +02:00
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
virtual void exit (void *value_ptr);
|
2002-07-04 16:17:30 +02:00
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
virtual int cancel ();
|
2002-09-30 04:51:22 +02:00
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
virtual void testcancel ();
|
|
|
|
static void static_cancel_self ();
|
2002-07-04 16:17:30 +02:00
|
|
|
|
2003-01-09 21:57:54 +01:00
|
|
|
static DWORD cancelable_wait (HANDLE object, DWORD timeout, const bool do_cancel = true);
|
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
virtual int setcancelstate (int state, int *oldstate);
|
|
|
|
virtual int setcanceltype (int type, int *oldtype);
|
2002-07-04 16:17:30 +02:00
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
virtual void push_cleanup_handler (__pthread_cleanup_handler *handler);
|
|
|
|
virtual void pop_cleanup_handler (int const execute);
|
2002-06-10 03:10:45 +02:00
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
static pthread* self ();
|
|
|
|
static void *thread_init_wrapper (void *);
|
2002-06-10 03:10:45 +02:00
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
virtual unsigned long getsequence_np();
|
2002-09-16 12:53:29 +02:00
|
|
|
|
2001-03-21 03:17:58 +01:00
|
|
|
private:
|
2002-09-29 04:19:35 +02:00
|
|
|
DWORD thread_id;
|
|
|
|
__pthread_cleanup_handler *cleanup_stack;
|
|
|
|
pthread_mutex mutex;
|
|
|
|
|
|
|
|
void pop_all_cleanup_handlers (void);
|
|
|
|
void precreate (pthread_attr *);
|
|
|
|
void postcreate ();
|
2002-11-24 14:54:14 +01:00
|
|
|
void setThreadIdtoCurrent ();
|
|
|
|
static void setTlsSelfPointer (pthread *);
|
|
|
|
static pthread *getTlsSelfPointer ();
|
2002-09-29 04:19:35 +02:00
|
|
|
void cancel_self ();
|
|
|
|
DWORD getThreadId ();
|
2002-11-24 14:54:14 +01:00
|
|
|
void initCurrentThread ();
|
2002-09-16 12:53:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class pthreadNull : public pthread
|
|
|
|
{
|
2002-09-16 18:09:54 +02:00
|
|
|
public:
|
2002-09-29 04:19:35 +02:00
|
|
|
static pthread *getNullpthread();
|
|
|
|
~pthreadNull();
|
|
|
|
|
|
|
|
/* From pthread These should never get called
|
|
|
|
* as the ojbect is not verifyable
|
|
|
|
*/
|
|
|
|
void create (void *(*)(void *), pthread_attr *, void *);
|
|
|
|
void exit (void *value_ptr);
|
|
|
|
int cancel ();
|
|
|
|
void testcancel ();
|
|
|
|
int setcancelstate (int state, int *oldstate);
|
|
|
|
int setcanceltype (int type, int *oldtype);
|
|
|
|
void push_cleanup_handler (__pthread_cleanup_handler *handler);
|
|
|
|
void pop_cleanup_handler (int const execute);
|
|
|
|
unsigned long getsequence_np();
|
2002-09-16 12:53:29 +02:00
|
|
|
|
|
|
|
private:
|
2002-09-29 04:19:35 +02:00
|
|
|
pthreadNull ();
|
|
|
|
static pthreadNull _instance;
|
2000-02-17 20:38:33 +01:00
|
|
|
};
|
|
|
|
|
2001-03-21 03:17:58 +01:00
|
|
|
class pthread_condattr:public verifyable_object
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
public:
|
2002-09-21 01:46:12 +02:00
|
|
|
static bool isGoodObject(pthread_condattr_t const *);
|
2000-10-15 03:37:07 +02:00
|
|
|
int shared;
|
2001-03-21 03:17:58 +01:00
|
|
|
|
2001-06-26 16:47:48 +02:00
|
|
|
pthread_condattr ();
|
|
|
|
~pthread_condattr ();
|
2000-02-17 20:38:33 +01:00
|
|
|
};
|
|
|
|
|
2001-03-21 03:17:58 +01:00
|
|
|
class pthread_cond:public verifyable_object
|
2001-03-17 02:14:58 +01:00
|
|
|
{
|
|
|
|
public:
|
2003-01-09 21:40:44 +01:00
|
|
|
static bool isGoodObject (pthread_cond_t const *);
|
|
|
|
static bool isGoodInitializer (pthread_cond_t const *);
|
|
|
|
static bool isGoodInitializerOrObject (pthread_cond_t const *);
|
|
|
|
static bool isGoodInitializerOrBadObject (pthread_cond_t const *);
|
|
|
|
static void initMutex ();
|
|
|
|
static int init (pthread_cond_t *, const pthread_condattr_t *);
|
|
|
|
|
2001-03-17 02:14:58 +01:00
|
|
|
int shared;
|
|
|
|
LONG waiting;
|
2001-12-26 13:46:26 +01:00
|
|
|
LONG ExitingWait;
|
2001-03-21 03:17:58 +01:00
|
|
|
pthread_mutex *mutex;
|
2001-05-07 00:23:43 +02:00
|
|
|
/* to allow atomic behaviour for cond_broadcast */
|
|
|
|
pthread_mutex_t cond_access;
|
2001-03-21 03:17:58 +01:00
|
|
|
HANDLE win32_obj_id;
|
2001-09-11 10:15:39 +02:00
|
|
|
class pthread_cond * next;
|
2001-03-17 02:14:58 +01:00
|
|
|
int TimedWait (DWORD dwMilliseconds);
|
2001-03-21 03:17:58 +01:00
|
|
|
void BroadCast ();
|
|
|
|
void Signal ();
|
2001-09-11 10:15:39 +02:00
|
|
|
void fixup_after_fork ();
|
2001-03-21 03:17:58 +01:00
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
pthread_cond (pthread_condattr *);
|
|
|
|
~pthread_cond ();
|
2003-01-09 21:40:44 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
static nativeMutex condInitializationLock;
|
2001-04-12 06:04:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class pthread_once
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
int state;
|
2001-03-17 02:14:58 +01:00
|
|
|
};
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-03-21 03:17:58 +01:00
|
|
|
/* shouldn't be here */
|
|
|
|
class semaphore:public verifyable_object
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-03-21 03:17:58 +01:00
|
|
|
public:
|
2002-09-21 01:46:12 +02:00
|
|
|
static bool isGoodObject(sem_t const *);
|
2002-09-21 05:59:58 +02:00
|
|
|
/* API calls */
|
|
|
|
static int init (sem_t * sem, int pshared, unsigned int value);
|
|
|
|
static int destroy (sem_t * sem);
|
|
|
|
static int wait (sem_t * sem);
|
|
|
|
static int trywait (sem_t * sem);
|
|
|
|
static int post (sem_t * sem);
|
2002-09-30 04:51:22 +02:00
|
|
|
|
2001-03-21 03:17:58 +01:00
|
|
|
HANDLE win32_obj_id;
|
2001-09-11 10:15:39 +02:00
|
|
|
class semaphore * next;
|
2001-03-21 03:17:58 +01:00
|
|
|
int shared;
|
2001-09-11 10:15:39 +02:00
|
|
|
long currentvalue;
|
2001-03-21 03:17:58 +01:00
|
|
|
void Wait ();
|
|
|
|
void Post ();
|
|
|
|
int TryWait ();
|
2001-09-11 10:15:39 +02:00
|
|
|
void fixup_after_fork ();
|
2001-03-21 03:17:58 +01:00
|
|
|
|
2002-09-29 04:19:35 +02:00
|
|
|
semaphore (int, unsigned int);
|
|
|
|
~semaphore ();
|
2001-03-21 03:17:58 +01:00
|
|
|
};
|
|
|
|
|
2001-04-13 17:28:20 +02:00
|
|
|
class callback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void (*cb)(void);
|
|
|
|
class callback * next;
|
|
|
|
};
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
class MTinterface
|
|
|
|
{
|
|
|
|
public:
|
2000-10-15 03:37:07 +02:00
|
|
|
// General
|
2001-04-12 06:04:53 +02:00
|
|
|
int concurrency;
|
2001-04-14 09:06:02 +02:00
|
|
|
long int threadcount;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-10-15 03:37:07 +02:00
|
|
|
// Used for main thread data, and sigproc thread
|
|
|
|
struct __reent_t reents;
|
|
|
|
struct _winsup_t winsup_reent;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-04-13 17:28:20 +02:00
|
|
|
callback *pthread_prepare;
|
|
|
|
callback *pthread_child;
|
|
|
|
callback *pthread_parent;
|
2001-04-12 06:04:53 +02:00
|
|
|
|
2002-09-17 11:12:36 +02:00
|
|
|
// lists of pthread objects. USE THREADSAFE INSERTS AND DELETES.
|
2001-09-10 00:39:35 +02:00
|
|
|
class pthread_mutex * mutexs;
|
2001-09-11 10:15:39 +02:00
|
|
|
class pthread_cond * conds;
|
|
|
|
class semaphore * semaphores;
|
2001-04-21 16:23:47 +02:00
|
|
|
|
2002-11-24 14:54:14 +01:00
|
|
|
pthread_key reent_key;
|
|
|
|
pthread_key thread_self_key;
|
|
|
|
|
2000-10-17 01:55:58 +02:00
|
|
|
void Init (int);
|
2002-09-17 11:12:36 +02:00
|
|
|
void fixup_before_fork (void);
|
2001-09-11 10:15:39 +02:00
|
|
|
void fixup_after_fork (void);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-11-24 14:54:14 +01:00
|
|
|
MTinterface () :
|
|
|
|
concurrency (0), threadcount (1),
|
|
|
|
pthread_prepare (NULL), pthread_child (NULL), pthread_parent (NULL),
|
|
|
|
mutexs (NULL), conds (NULL), semaphores (NULL),
|
|
|
|
reent_key (NULL), thread_self_key (NULL)
|
2002-09-29 04:19:35 +02:00
|
|
|
{
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
};
|
|
|
|
|
2002-11-24 14:54:14 +01:00
|
|
|
#define MT_INTERFACE user_data->threadinterface
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
extern "C"
|
|
|
|
{
|
2001-04-12 06:04:53 +02:00
|
|
|
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 (const pthread_attr_t * attr, size_t * size);
|
|
|
|
|
|
|
|
int __pthread_attr_getinheritsched (const pthread_attr_t *, int *);
|
|
|
|
int __pthread_attr_getschedparam (const pthread_attr_t *,
|
|
|
|
struct sched_param *);
|
|
|
|
int __pthread_attr_getschedpolicy (const pthread_attr_t *, int *);
|
|
|
|
int __pthread_attr_getscope (const pthread_attr_t *, int *);
|
|
|
|
int __pthread_attr_getstackaddr (const pthread_attr_t *, void **);
|
|
|
|
int __pthread_attr_setinheritsched (pthread_attr_t *, int);
|
|
|
|
int __pthread_attr_setschedparam (pthread_attr_t *,
|
|
|
|
const struct sched_param *);
|
|
|
|
int __pthread_attr_setschedpolicy (pthread_attr_t *, int);
|
|
|
|
int __pthread_attr_setscope (pthread_attr_t *, int);
|
|
|
|
int __pthread_attr_setstackaddr (pthread_attr_t *, void *);
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Thread SpecificData */
|
2001-04-12 06:04:53 +02: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-17 20:38:33 +01:00
|
|
|
|
2001-03-17 02:14:58 +01:00
|
|
|
/* Thead synchroniation */
|
2001-04-12 06:04:53 +02:00
|
|
|
int __pthread_cond_destroy (pthread_cond_t * cond);
|
|
|
|
int __pthread_cond_signal (pthread_cond_t * cond);
|
|
|
|
int __pthread_cond_broadcast (pthread_cond_t * cond);
|
|
|
|
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-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Thread signal */
|
2001-04-12 06:04:53 +02:00
|
|
|
int __pthread_kill (pthread_t thread, int sig);
|
|
|
|
int __pthread_sigmask (int operation, const sigset_t * set,
|
|
|
|
sigset_t * old_set);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* ID */
|
2001-04-12 06:04:53 +02:00
|
|
|
int __pthread_equal (pthread_t * t1, pthread_t * t2);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Mutexes */
|
2001-04-12 06:04:53 +02:00
|
|
|
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 *);
|
|
|
|
int __pthread_mutex_setprioceiling (pthread_mutex_t * mutex,
|
|
|
|
int prioceiling, int *old_ceiling);
|
|
|
|
int __pthread_mutex_getprioceiling (const pthread_mutex_t * mutex,
|
|
|
|
int *prioceiling);
|
|
|
|
|
|
|
|
|
|
|
|
int __pthread_mutexattr_destroy (pthread_mutexattr_t *);
|
|
|
|
int __pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *, int *);
|
|
|
|
int __pthread_mutexattr_getprotocol (const pthread_mutexattr_t *, int *);
|
|
|
|
int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *, int *);
|
|
|
|
int __pthread_mutexattr_gettype (const pthread_mutexattr_t *, int *);
|
|
|
|
int __pthread_mutexattr_init (pthread_mutexattr_t *);
|
|
|
|
int __pthread_mutexattr_setprioceiling (pthread_mutexattr_t *, int);
|
|
|
|
int __pthread_mutexattr_setprotocol (pthread_mutexattr_t *, int);
|
|
|
|
int __pthread_mutexattr_setpshared (pthread_mutexattr_t *, int);
|
|
|
|
int __pthread_mutexattr_settype (pthread_mutexattr_t *, int);
|
|
|
|
|
|
|
|
|
|
|
|
/* Scheduling */
|
|
|
|
int __pthread_getconcurrency (void);
|
|
|
|
int __pthread_setconcurrency (int new_level);
|
|
|
|
int __pthread_getschedparam (pthread_t thread, int *policy,
|
|
|
|
struct sched_param *param);
|
|
|
|
int __pthread_setschedparam (pthread_t thread, int policy,
|
|
|
|
const struct sched_param *param);
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
};
|
2000-09-03 06:16:35 +02:00
|
|
|
#endif // MT_SAFE
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
#endif // _CYGNUS_THREADS_
|