newlib/winsup/cygwin/thread.h
Christopher Faylor 2d1d1eb1e4 * cygtls.h (_threadinfo::call): Remove regparm declaration to work around
compiler bug.
* autoload.cc (TryEnterCriticalSection): Remove.
* dcrt0.cc (dll_crt0_0): Delete inappropriate setting of _my_tls.stackptr to
NULL since it has really bad consequences.  Make 'si' an automatic variable.
* cygtls.cc (_threadinfo::init_thread): Correct thinko which caused thread list
to be allocated every time.
* cygtls.h (CYGTLS_PADSIZE): Define as const int.
* sync.h: Make multiple inclusion safe.
(muto::next): Eliminate.
(muto::exiting_thread): New variable.
(muto::set_exiting_thread): New function.
(new_muto): Change to use different section for mutos since c++ give
inexplicable warning in some cases otherwise.
(new_muto1): Ditto.
* dcrt0.cc (do_exit): Call muto::set_exiting_thread here.
* sync.cc (muto_start): Eliminate.
(muto::acquire): Always give exiting thread a lock.  Never give thread a lock
if exiting.
(muto::release): Ditto for releasing.
* dtable.cc (dtable::init_lock): Unline function and define here.
* dtable.h (lock_cs): Define as a muto since critical sections seem to work
oddly on Windows Me.
(lock): Accommodate switch to muto.
(unlock): Ditto.
* exceptions.cc (setup_handler): Don't worry about acquiring mutos since that
hasn't mattered for a long time.
(signal_exit): Ditto: muto stuff will be handled automatically on exit now.
* Makefile.in (DLL_IMPORTS): Link advapi32 to ensure proper DLL initialization.
* autoload.cc (RegCloseKey): Arbitrarily choose this function as a "seed" to
pull the advapi32 link library in.  So, comment out the autoloading.
* cygtls.cc (_threadinfo::init_thread): Just clear CYGTLS_PADSIZE.
(_threadinfo::remove): Add debugging.
(_threadinfo::find_tls): Ditto.
* cygtls.h (_threadinfo::padding): Make zero length (for now?).
* dcrt0.cc (dll_crt0_0): Move more initialization here from dll_crt0_1.
(dll_crt0_1): See above.
* dtable.h (dtable::lock): Remove commented out critical section locking.
* dtable.h (dtable::init_lock): Remove commented out critical section locking.
* dtable.h (dtable::unlock): Remove commented out critical section locking.
* exceptions.cc (interruptible): bool'ize.
* init.cc (threadfunc_fe): Revert to storing threadfunc at stack bottom.
(munge_threadfunc): Ditto.  Avoid adding overhead to calibration_thread.
(prime_threads): Don't initialize tls stuff.
(dll_entry): Make minor change to initialization order.
* tlsoffsets.h: Regenerate.
* sigproc.cc (wait_sig): Add sanity check for end of process thread exit.
* select.h: Make minor formatting change.
* Makefile.in: Add still more -fomit-frame-pointer functions.
* dtable.h (dtable::lock): New function.
(dtable::unlock): New function.
(dtable::init_lock): New function.
* cygheap.h (HEAP_TLS): Declare new enum value.
(init_cygheap::threadlist): Declare new array.
(init_cygheap::sthreads): Declare new variable.
(cygheap_fdmanip::~cygheap_fdmanip): Use new dtable lock/unlock functions.
(cygheap_fdnew::cygheap_fdnew): Ditto.
(cygheap_fdget::cygheap_fdget): Ditto.
* dtable.cc (dtable_init): Initialize fdtab critical section.
(dtable::fixup_after_fork): Ditto.
(dtable::fixup_after_exec): Ditto.
(dtable::dup2): Use lock/unlock calls to protect access to fdtab.
(dtable::find_fifo): Ditto.
(dtable::fixup_before_fork): Ditto.
(dtable::fixup_before_exec): Ditto.
(dtable::set_file_pointers_for_exec): Ditto.
(dtable::vfork_child_dup): Ditto.
(dtable::vfork_parent_restore): Ditto.
* syscalls.cc (close_all_files): Ditto.
* sync.h (muto::acquired): Declare new function.
(new_muto1): Declare new macro used to specify name of muto storage.
* sync.cc (muto::acquired): Define new function.
* cygthread.cc (cygthread::stub): Remove signal chain removal call since it is
handled during initialization now.
* cygthread.cc (cygthread::simplestub): Remove signal chain removal call since
it is handled during initialization now.
* cygtls.cc (sentry): New class used for locking.  Use throughout.
(_threadinfo::reset_exception): Don't pop stack.
(_threadinfo::find_tls): Move from exceptions.cc.
(_threadinfo::init_thread): Initialize array of threads rather than linked
list.  Take second argument indicating thread function for this thread.
(_threadinfo::remove): Search thread array rather than linked list.  Use sentry
to lock.  Only unlock if we got the lock.
(_threadinfo::find_tls): Ditto for first two.
(handle_threadlist_exception): Handle exceptions when manipulating the thread
list in case of premature thread termination.
(_threadinfo::init_threadlist_exceptions): Ditto.
* cygtls.h (TLS_STACK_SIZE): Decrease size.
(_threadinfo::padding): Add element to avoid overwriting lower part of stack.
(_threadinfo::remove): Add a "wait" argument to control how long we wait for a
lock before removing.
* exceptions.cc (init_exception_handler): Make global.  Take argument to
control exception handler being set.
(ctrl_c_handler): Wait forever when removing self from signal chain.
(_threadinfo::find_tls): Move to cygtls.cc.
(sig_handle): Reorganize detection for thread-specific signals.
* heap.cc (heap_init): Rework slightly.  Make fatal error more verbose.  Remove
malloc initialization since it can't happen during dll attach.
* init.cc (search_for): Move address to search for on stack here.
(threadfunc_ix): Ditto for stack offset.  Make shared so that stack walk
potentially only has to be done once when cygwin processes are running.
(threadfunc_fe): Use standard tls to store thread function (may change back
later).
(calibration_thread): New function.  Potentially called to find threadfunc_ix.
(munge_threadfunc): Search for "search_for" value on stack.  Output warning
when thread func not found on stack.  Use standard tls to store thread
function.
(prime_threads): New function.  Called to prime thread front end.
(dll_entry): Call dll_crt0_0 here when DLL_PROCESS_ATTACH.  Call prime_threads
here.  Try to remove thread from signal list here.
* sigproc.cc (wait_sig): Initialize threadlist exception stuff here.
* thread.cc (pthread::exit): Pass argument to signal list remove function.
* thread.h: Remove obsolete *ResourceLock defines.
* tlsoffsets.h: Regenerate.
* winsup.h (spf): Define temporary debug macro to be deleted later.
* dcrt0.cc (dll_crt0_0): New function, called during DLL initialization.
Mainly consists of code pulled from dll_crt0_1.
(dll_crt0_1): See above.
(_dll_crt0): Wait for initial calibration thread to complete, if appropriate.
Move some stuff to dll_crt0_0.
(initialize_main_tls): Accommodate argument change to
_thread_info::init_thread.
* fork.cc (fork_child): Ditto.
(sync_with_child): Fix debug message.
* external.cc (cygwin_internal): Remove special considerations for
uninitialized dll since initialization happens during dll attach now.
* dlfcn.cc (dlopen): Remove obsolete *ResourceLock calls.
(dlclose): Ditto.
* cygheap.h (init_cygheap::close_ctty): Declare new function.
* cygheap.cc (init_cygheap::close_ctty): Define new function.
* syscalls.cc (close_all_files): Use close_ctty.
(setsid): Ditto.
* cygthread.cc (cygthread::stub): Remove exception initialization.
* cygthread.cc (cygthread::stub): Remove exception initialization.
(cygthread::simplestub): Ditto.
* thread.cc (pthread::thread_init_wrapper): Ditto.
* cygtls.cc (_last_thread): Make static.
(_threadinfo::call2): Initialize exception handler here.
(_threadinfo::find_tls): Move here.
* exceptions.cc (_threadinfo::find_tls): Move.
* dcrt0.cc (__api_fatal): Add prefix info to message here rather than including
it in every call to function.
* winsup.h (api_fatal): Accommodate above change.
* debug.cc (add_handle): Don't do anything if cygheap not around.
(mark_closed): Ditto.
* dll_init.cc (dll_list::detach): Fix debug output.
* fork.cc (sync_with_child): Ditto.
(vfork): Improve debug output.
* heap.cc (heap_init): Ditto.
* exceptions.cc (try_to_debug): Clarify message when debugger attaches.
2004-01-14 15:45:37 +00:00

692 lines
15 KiB
C++

/* thread.h: Locking and threading module definitions
Copyright 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
Written by Marco Fuykschot <marco@ddi.nl>
Major update 2001 Robert Collins <rbtcollins@hotmail.com>
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_MMAP_LIST 1
#define WRITE_LOCK 1
#define READ_LOCK 2
#include <pthread.h>
#include <limits.h>
#include <security.h>
#include <errno.h>
extern "C"
{
void SetResourceLock (int, int, const char *) __attribute__ ((regparm (3)));
void ReleaseResourceLock (int, int, const char *)
__attribute__ ((regparm (3)));
}
class fast_mutex
{
public:
fast_mutex () :
lock_counter (0), win32_obj_id (0)
{
}
~fast_mutex ()
{
if(win32_obj_id)
CloseHandle (win32_obj_id);
}
bool init ()
{
lock_counter = 0;
win32_obj_id = ::CreateSemaphore (&sec_none_nih, 0, LONG_MAX, NULL);
if (!win32_obj_id)
{
debug_printf ("CreateSemaphore failed. %E");
return false;
}
return true;
}
void lock ()
{
if (InterlockedIncrement ((long *)&lock_counter) != 1)
WaitForSingleObject (win32_obj_id, INFINITE);
}
void unlock ()
{
if (InterlockedDecrement ((long *)&lock_counter))
::ReleaseSemaphore (win32_obj_id, 1, NULL);
}
private:
unsigned long lock_counter;
HANDLE win32_obj_id;
};
class per_process;
class pinfo;
class ResourceLocks
{
public:
LPCRITICAL_SECTION Lock (int);
void Init ();
void Delete ();
private:
CRITICAL_SECTION lock;
bool inited;
};
#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
#define PTHREAD_ONCE_MAGIC PTHREAD_MAGIC+8
#define PTHREAD_RWLOCK_MAGIC PTHREAD_MAGIC+9
#define PTHREAD_RWLOCKATTR_MAGIC PTHREAD_MAGIC+10
#define MUTEX_OWNER_ANONYMOUS ((pthread_t) -1)
/* verifyable_object should not be defined here - it's a general purpose class */
class verifyable_object
{
public:
long magic;
verifyable_object (long);
virtual ~verifyable_object ();
};
typedef enum
{
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 *);
template <class list_node> inline void
List_insert (list_node *&head, list_node *node)
{
if (!node)
return;
do
node->next = head;
while (InterlockedCompareExchangePointer (&head, node, node->next) != node->next);
}
template <class list_node> inline void
List_remove (fast_mutex &mx, list_node *&head, list_node *node)
{
if (!node)
return;
mx.lock ();
if (head)
{
if (InterlockedCompareExchangePointer (&head, node->next, node) != node)
{
list_node *cur = head;
while (cur->next && node != cur->next)
cur = cur->next;
if (node == cur->next)
cur->next = cur->next->next;
}
}
mx.unlock ();
}
template <class list_node> class List
{
public:
List() : head(NULL)
{
mx_init ();
}
~List()
{
}
void fixup_after_fork ()
{
mx_init ();
}
void insert (list_node *node)
{
List_insert (head, node);
}
void remove (list_node *node)
{
List_remove (mx, head, node);
}
void for_each (void (list_node::*callback) ())
{
mx.lock ();
list_node *cur = head;
while (cur)
{
(cur->*callback) ();
cur = cur->next;
}
mx.unlock ();
}
protected:
void mx_init ()
{
if (!mx.init ())
api_fatal ("Could not create mutex for list synchronisation.");
}
fast_mutex mx;
list_node *head;
};
class pthread_key: public verifyable_object
{
public:
static bool is_good_object (pthread_key_t const *);
DWORD tls_index;
int set (const void *);
void *get () const;
pthread_key (void (*)(void *));
~pthread_key ();
static void fixup_before_fork ()
{
keys.for_each (&pthread_key::_fixup_before_fork);
}
static void fixup_after_fork ()
{
keys.fixup_after_fork ();
keys.for_each (&pthread_key::_fixup_after_fork);
}
static void run_all_destructors ()
{
keys.for_each (&pthread_key::run_destructor);
}
/* List support calls */
class pthread_key *next;
private:
static List<pthread_key> keys;
void _fixup_before_fork ();
void _fixup_after_fork ();
void (*destructor) (void *);
void run_destructor ();
void *fork_buf;
};
class pthread_attr: public verifyable_object
{
public:
static bool is_good_object(pthread_attr_t const *);
int joinable;
int contentionscope;
int inheritsched;
struct sched_param schedparam;
size_t stacksize;
pthread_attr ();
~pthread_attr ();
};
class pthread_mutexattr: public verifyable_object
{
public:
static bool is_good_object(pthread_mutexattr_t const *);
int pshared;
int mutextype;
pthread_mutexattr ();
~pthread_mutexattr ();
};
class pthread_mutex: public verifyable_object
{
public:
static bool is_good_object (pthread_mutex_t const *);
static bool is_good_initializer (pthread_mutex_t const *);
static bool is_good_initializer_or_object (pthread_mutex_t const *);
static bool is_good_initializer_or_bad_object (pthread_mutex_t const *mutex);
static bool can_be_unlocked (pthread_mutex_t const *mutex);
static void init_mutex ();
static int init (pthread_mutex_t *, const pthread_mutexattr_t *);
unsigned long lock_counter;
HANDLE win32_obj_id;
unsigned int recursion_counter;
LONG condwaits;
pthread_t owner;
int type;
int pshared;
pthread_t get_pthread_self () const
{
return PTHREAD_MUTEX_NORMAL == type ? MUTEX_OWNER_ANONYMOUS :
::pthread_self ();
}
int lock ()
{
return _lock (get_pthread_self ());
}
int trylock ()
{
return _trylock (get_pthread_self ());
}
int unlock ()
{
return _unlock (get_pthread_self ());
}
int destroy ()
{
return _destroy (get_pthread_self ());
}
void set_owner (pthread_t self)
{
recursion_counter = 1;
owner = self;
}
int lock_recursive ()
{
if (UINT_MAX == recursion_counter)
return EAGAIN;
++recursion_counter;
return 0;
}
pthread_mutex (pthread_mutexattr * = NULL);
pthread_mutex (pthread_mutex_t *, pthread_mutexattr *);
~pthread_mutex ();
class pthread_mutex * next;
static void fixup_after_fork ()
{
mutexes.fixup_after_fork ();
mutexes.for_each (&pthread_mutex::_fixup_after_fork);
}
private:
int _lock (pthread_t self);
int _trylock (pthread_t self);
int _unlock (pthread_t self);
int _destroy (pthread_t self);
void _fixup_after_fork ();
static List<pthread_mutex> mutexes;
static fast_mutex mutex_initialization_lock;
};
#define WAIT_CANCELED (WAIT_OBJECT_0 + 1)
class _threadinfo;
class pthread: public verifyable_object
{
public:
HANDLE win32_obj_id;
class pthread_attr attr;
void *(*function) (void *);
void *arg;
void *return_ptr;
bool valid;
bool suspended;
int cancelstate, canceltype;
_threadinfo *cygtls;
HANDLE cancel_event;
pthread_t joiner;
virtual void create (void *(*)(void *), pthread_attr *, void *);
pthread ();
virtual ~pthread ();
static void init_mainthread ();
static bool is_good_object(pthread_t const *);
static void atforkprepare();
static void atforkparent();
static void atforkchild();
/* 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,
void *(*start_routine) (void *), void *arg);
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);
virtual void exit (void *value_ptr) __attribute__ ((noreturn));
virtual int cancel ();
virtual void testcancel ();
static void static_cancel_self ();
static DWORD cancelable_wait (HANDLE object, DWORD timeout, const bool do_cancel = true);
virtual int setcancelstate (int state, int *oldstate);
virtual int setcanceltype (int type, int *oldtype);
virtual void push_cleanup_handler (__pthread_cleanup_handler *handler);
virtual void pop_cleanup_handler (int const execute);
static pthread* self ();
static DWORD WINAPI thread_init_wrapper (void *);
virtual unsigned long getsequence_np();
static int equal (pthread_t t1, pthread_t t2)
{
return t1 == t2;
}
/* List support calls */
class pthread *next;
static void fixup_after_fork ()
{
threads.fixup_after_fork ();
threads.for_each (&pthread::_fixup_after_fork);
}
static void suspend_all_except_self ()
{
threads.for_each (&pthread::suspend_except_self);
}
static void resume_all ()
{
threads.for_each (&pthread::resume);
}
private:
static List<pthread> threads;
DWORD thread_id;
__pthread_cleanup_handler *cleanup_stack;
pthread_mutex mutex;
void suspend_except_self ();
void resume ();
void _fixup_after_fork ();
void pop_all_cleanup_handlers (void);
void precreate (pthread_attr *);
void postcreate ();
void set_tls_self_pointer ();
bool create_cancel_event ();
static pthread *get_tls_self_pointer ();
void cancel_self ();
DWORD get_thread_id ();
};
class pthread_null : public pthread
{
public:
static pthread *get_null_pthread();
~pthread_null();
/* 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) __attribute__ ((noreturn));
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();
private:
pthread_null ();
static pthread_null _instance;
};
class pthread_condattr: public verifyable_object
{
public:
static bool is_good_object(pthread_condattr_t const *);
int shared;
pthread_condattr ();
~pthread_condattr ();
};
class pthread_cond: public verifyable_object
{
public:
static bool is_good_object (pthread_cond_t const *);
static bool is_good_initializer (pthread_cond_t const *);
static bool is_good_initializer_or_object (pthread_cond_t const *);
static bool is_good_initializer_or_bad_object (pthread_cond_t const *);
static void init_mutex ();
static int init (pthread_cond_t *, const pthread_condattr_t *);
int shared;
unsigned long waiting;
unsigned long pending;
HANDLE sem_wait;
pthread_mutex mtx_in;
pthread_mutex mtx_out;
pthread_mutex_t mtx_cond;
void unblock (const bool all);
int wait (pthread_mutex_t mutex, DWORD dwMilliseconds = INFINITE);
pthread_cond (pthread_condattr *);
~pthread_cond ();
class pthread_cond * next;
static void fixup_after_fork ()
{
conds.fixup_after_fork ();
conds.for_each (&pthread_cond::_fixup_after_fork);
}
private:
void _fixup_after_fork ();
static List<pthread_cond> conds;
static fast_mutex cond_initialization_lock;
};
class pthread_rwlockattr: public verifyable_object
{
public:
static bool is_good_object(pthread_rwlockattr_t const *);
int shared;
pthread_rwlockattr ();
~pthread_rwlockattr ();
};
class pthread_rwlock: public verifyable_object
{
public:
static bool is_good_object (pthread_rwlock_t const *);
static bool is_good_initializer (pthread_rwlock_t const *);
static bool is_good_initializer_or_object (pthread_rwlock_t const *);
static bool is_good_initializer_or_bad_object (pthread_rwlock_t const *);
static void init_mutex ();
static int init (pthread_rwlock_t *, const pthread_rwlockattr_t *);
int shared;
unsigned long waiting_readers;
unsigned long waiting_writers;
pthread_t writer;
struct RWLOCK_READER
{
struct RWLOCK_READER *next;
pthread_t thread;
} *readers;
fast_mutex readers_mx;
int rdlock ();
int tryrdlock ();
int wrlock ();
int trywrlock ();
int unlock ();
pthread_mutex mtx;
pthread_cond cond_readers;
pthread_cond cond_writers;
pthread_rwlock (pthread_rwlockattr *);
~pthread_rwlock ();
class pthread_rwlock * next;
static void fixup_after_fork ()
{
rwlocks.fixup_after_fork ();
rwlocks.for_each (&pthread_rwlock::_fixup_after_fork);
}
private:
static List<pthread_rwlock> rwlocks;
void add_reader (struct RWLOCK_READER *rd);
void remove_reader (struct RWLOCK_READER *rd);
struct RWLOCK_READER *lookup_reader (pthread_t thread);
void release ()
{
if (waiting_writers)
{
if (!readers)
cond_writers.unblock (false);
}
else if (waiting_readers)
cond_readers.unblock (true);
}
static void rdlock_cleanup (void *arg);
static void wrlock_cleanup (void *arg);
void _fixup_after_fork ();
static fast_mutex rwlock_initialization_lock;
};
class pthread_once
{
public:
pthread_mutex_t mutex;
int state;
};
/* shouldn't be here */
class semaphore: public verifyable_object
{
public:
static bool is_good_object(sem_t const *);
/* API calls */
static int init (sem_t * sem, int pshared, unsigned int value);
static int destroy (sem_t * sem);
static sem_t *open (const char *name, int oflag, mode_t mode,
unsigned int value);
static int wait (sem_t * sem);
static int post (sem_t * sem);
static int getvalue (sem_t * sem, int *sval);
static int trywait (sem_t * sem);
static int timedwait (sem_t * sem, const struct timespec *abstime);
HANDLE win32_obj_id;
int shared;
long currentvalue;
char *name;
semaphore (int, unsigned int);
semaphore (const char *name, int oflag, mode_t mode, unsigned int value);
~semaphore ();
class semaphore * next;
static void fixup_after_fork ()
{
semaphores.fixup_after_fork ();
semaphores.for_each (&semaphore::_fixup_after_fork);
}
private:
void _wait ();
void _post ();
int _getvalue (int *sval);
int _trywait ();
int _timedwait (const struct timespec *abstime);
void _fixup_after_fork ();
static List<semaphore> semaphores;
};
class callback
{
public:
void (*cb)(void);
class callback * next;
};
struct MTinterface
{
// General
int concurrency;
long int threadcount;
callback *pthread_prepare;
callback *pthread_child;
callback *pthread_parent;
void Init ();
void fixup_before_fork (void);
void fixup_after_fork (void);
#if 0 // avoid initialization since zero is implied and
MTinterface () :
concurrency (0), threadcount (0),
pthread_prepare (NULL), pthread_child (NULL), pthread_parent (NULL)
{
}
#endif
};
#define MT_INTERFACE user_data->threadinterface
#endif // _CYGNUS_THREADS_