newlib/winsup/cygwin/thread.h

309 lines
5.9 KiB
C
Raw Normal View History

2000-02-17 20:38:33 +01:00
/* thread.h: Locking and threading module definitions
Copyright 1998, 1999, 2000 Cygnus Solutions.
2000-02-17 20:38:33 +01:00
Written by Marco Fuykschot <marco@ddi.nl>
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 LOCK_THREAD_LIST 5
#define LOCK_MUTEX_LIST 6
#define LOCK_SEM_LIST 7
#define WRITE_LOCK 1
#define READ_LOCK 2
extern "C"
{
#if defined (_CYG_THREAD_FAILSAFE) && defined (_MT_SAFE)
void AssertResourceOwner (int, int);
#else
#define AssertResourceOwner(i,ii)
#endif
}
#ifndef _MT_SAFE
#define SetResourceLock(i,n,c)
#define ReleaseResourceLock(i,n,c)
#else
#include <pthread.h>
#include <pwd.h>
#include <grp.h>
#define _NOMNTENT_FUNCS
2000-02-17 20:38:33 +01:00
#include <mntent.h>
extern "C" {
struct _winsup_t
{
/*
Needed for the group functions
*/
struct group _grp;
char *_namearray[2];
char _linebuf[100];
int _grp_pos;
/* console.cc */
unsigned _rarg;
char _my_title_buf[TITLESIZE + 1];
/* dlfcn.cc */
int _dl_error;
char _dl_buffer[256];
/* passwd.cc */
struct passwd _res;
char _tmpbuf[100];
char _pass[_PASSWORD_LEN];
int _pw_pos;
/* path.cc */
struct mntent _ret;
int _iteration;
/* strerror */
char _strerror_buf[20];
/* syscalls.cc */
char _dacl_buf[1024];
char _sacl_buf[1024];
char _ownr_buf[1024];
char _grp_buf[1024];
/* sysloc.cc */
char *_process_ident;
int _process_logopt;
int _process_facility;
int _process_logmask;
/* times.cc */
char _b[20];
struct tm _localtime_buf;
char _buf1[33];
char _buf2[33];
/* uinfo.cc */
char _username[MAX_USER_NAME];
};
struct __reent_t
{
struct _reent *_clib;
struct _winsup_t *_winsup;
};
_reent *_reent_clib ();
_winsup_t *_reent_winsup ();
void SetResourceLock (int, int, const char *);
void ReleaseResourceLock (int, int, const char *);
#ifdef _CYG_THREAD_FAILSAFE
void AssertResourceOwner (int, int);
#else
#define AssertResourceOwner(i,ii)
#endif
}
class per_process;
class pinfo;
class ResourceLocks
{
public:
ResourceLocks ():inited (false) {};
LPCRITICAL_SECTION Lock (int);
void Init ();
void Delete ();
#ifdef _CYG_THREAD_FAILSAFE
DWORD owner;
DWORD count;
#endif
private:
CRITICAL_SECTION lock;
bool inited;
};
#define MT_MAX_ITEMS 128
// thread classes\lists
class MTitem
{
public:
HANDLE win32_obj_id;
UINT return_value;
bool used;
char joinable; // for thread only
bool HandleOke () {return win32_obj_id;};
virtual void Destroy ();
virtual int Id () {return (int) win32_obj_id;};
};
class ThreadItem:public MTitem
{
public:
pthread_attr_t attr;
TFD (function);
void *arg;
void *return_ptr;
bool suspended;
DWORD thread_id;
DWORD GetThreadId () {return thread_id;};
/* signal handling */
struct sigaction *sigs;
sigset_t *sigmask;
LONG *sigtodo;
};
class MutexItem:public MTitem
{
public:
int Lock ();
int TryLock ();
int UnLock ();
};
class SemaphoreItem:public MTitem
{
public:
int shared;
int Wait ();
int Post ();
int TryWait ();
};
typedef struct
{
MTitem *items[MT_MAX_ITEMS];
int index;
}
MTList;
class MTinterface
{
public:
// General
DWORD reent_index;
DWORD thread_key;
// Used for main thread data, and sigproc thread
struct __reent_t reents;
struct _winsup_t winsup_reent;
ThreadItem mainthread;
void Init0 ();
void Init1 ();
void ClearReent ();
void ReleaseItem (MTitem *);
// Thread functions
ThreadItem *CreateThread (pthread_t *, TFD (func), void *, pthread_attr_t);
ThreadItem *GetCallingThread ();
ThreadItem *GetThread (pthread_t *);
// Mutex functions
MutexItem *CreateMutex (pthread_mutex_t *);
MutexItem *GetMutex (pthread_mutex_t *);
// Semaphore functions
SemaphoreItem *CreateSemaphore (sem_t *, int, int);
SemaphoreItem *GetSemaphore (sem_t * t);
private:
// General Administration
MTitem * Find (void *, int (*compare) (void *, void *), int &, MTList *);
MTitem *GetItem (int, MTList *);
MTitem *SetItem (int, MTitem *, MTList *);
int Find (MTitem &, MTList *);
int FindNextUnused (MTList *);
MTList threadlist;
MTList mutexlist;
MTList semalist;
};
extern "C"
{
void *thread_init_wrapper (void *);
/* ThreadCreation */
int __pthread_create (pthread_t * thread, const pthread_attr_t * attr, TFD (start_routine), void *arg);
int __pthread_attr_init (pthread_attr_t * attr);
int __pthread_attr_destroy (pthread_attr_t * attr);
int __pthread_attr_setstacksize (pthread_attr_t * attr, size_t size);
int __pthread_attr_getstacksize (pthread_attr_t * attr, size_t * size);
/*
__pthread_attr_setstackaddr(...);
__pthread_attr_getstackaddr(...);
*/
/* Thread Exit */
int __pthread_exit (void *value_ptr);
int __pthread_join(pthread_t *thread, void **return_val);
int __pthread_detach(pthread_t *thread);
/* Thread suspend */
int __pthread_suspend(pthread_t *thread);
int __pthread_continue(pthread_t *thread);
unsigned long __pthread_getsequence_np (pthread_t * thread);
/* Thread SpecificData */
int __pthread_key_create (pthread_key_t * key);
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);
/* Thread signal */
int __pthread_kill (pthread_t * thread, int sig);
int __pthread_sigmask (int operation, const sigset_t * set, sigset_t * old_set);
/* ID */
pthread_t __pthread_self ();
int __pthread_equal (pthread_t * t1, pthread_t * t2);
/* Mutexes */
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 *);
/* Semaphores */
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);
};
* Makefile.in: Add cygheap.o. * child_info.h: Add specific exec class. * cygheap.h: New file. Contains declarations for cygwin heap. * cygheap.cc: New file. Implements cygwin heap functions. * dcrt0.cc (quoted): Simplify due to new method for passing arguments between cygwin programs. (alloc_stack_hard_way): Attempt to handle overlapped stack. (dll_crt0_1): Move child_info processing here. Accomodate new method for passing arguments between cygwin programs. Initialize cygwin heap. Establish __argc and __argv variables. (_dll_crt0): Move most of child_info processing to dll_crt0_1. (cygwin_dll_init): Remove duplication. * dtable.cc (dtable::extend): Allocate dtable using cygwin heap. (dtable::build_fhandler): Ditto for fhandler type being constructed. (dtable::dup_worker): Free new fhandler from cygwin heap on error. (dtable::select_*): Don't assume that this == fdtab. (dtable::linearize_fd_array): Delete. (dtable::delinearize_fd_array): Delete. (dtable::fixup_after_exec): New file. (dtable::vfork_child_dup): Use cygwin heap. (dtable::vfork_parent_restore): Ditto. * dtable.h: Remove obsolete methods. Add new method. * environ.cc (posify): Eliminate already_posix parameter and logic. (envsize): New function. (_addenv): Use envsize. (environ_init): Accept an argument pointing to an existing environment list. If supplied, allocate space for this in the the program's heap. * fhandler.cc (fhandler_base::operator =): Move here from fhandler.h. Use cygwin heap to allocate filenames. (fhandler_base::set_name): Allocate/free names from cygwin heap. (fhandler_base::linearize): Delete. (fhandler_base::de_linearize): Delete. (fhandler_base::operator delete): Free from cygwin heap. (fhandler_base::~fhandler_base): Ditto. * fhandler.h: Accomodate elimination of *linearize and other changes above. * fhandler_console.cc (fhandler_console::fixup_after_exec): Rename from de_linearize. * heap.h: New file. * fhandler_tty.cc (fhandler_tty_slave::fhandler_tty_slave): Use cygwin heap for name. fhandler_tty::fixup_after_exec): Rename from de_linearize. * fork.cc (fork): Call cygheap_fixup_in_child. * heap.cc: Use declarations in heap.h. * malloc.cc: Sprinkle assertions throughout to catch attempts to free/realloc something from the cygwin heap. * path.cc: Throughout, eliminate use of per-thread cache for cwd. Use cwd_* functions rather than cwd_* variables to access cwd_win32 and cwd_posix. (cwd_win32): New function. (cwd_posix): New function. (cwd_hash): New function. (cwd_fixup_after_exec): New function. * path.h: Accomodate path.cc changes. * pinfo.cc (pinfo_init): Accept a pointer to an environment table. Pass this to environ_init. Eliminate old 'title' tests. * pinfo.h: Accomodate above change in argument. * spawn.cc (struct av): New method for building argv list. (av::unshift): New method. (spawn_guts): Allocate everything that the child process needs in the cygwin heap and pass a pointer to this to the child. Build argv list using new method. Eliminate delinearize stuff. * thread.h: Eliminate _cwd_win32 and _cwd_posix buffers. * winsup.h: Eliminate obsolete functions. Add envsize() declaration.
2000-09-03 06:16:35 +02:00
#endif // MT_SAFE
2000-02-17 20:38:33 +01:00
* Makefile.in: Add cygheap.o. * child_info.h: Add specific exec class. * cygheap.h: New file. Contains declarations for cygwin heap. * cygheap.cc: New file. Implements cygwin heap functions. * dcrt0.cc (quoted): Simplify due to new method for passing arguments between cygwin programs. (alloc_stack_hard_way): Attempt to handle overlapped stack. (dll_crt0_1): Move child_info processing here. Accomodate new method for passing arguments between cygwin programs. Initialize cygwin heap. Establish __argc and __argv variables. (_dll_crt0): Move most of child_info processing to dll_crt0_1. (cygwin_dll_init): Remove duplication. * dtable.cc (dtable::extend): Allocate dtable using cygwin heap. (dtable::build_fhandler): Ditto for fhandler type being constructed. (dtable::dup_worker): Free new fhandler from cygwin heap on error. (dtable::select_*): Don't assume that this == fdtab. (dtable::linearize_fd_array): Delete. (dtable::delinearize_fd_array): Delete. (dtable::fixup_after_exec): New file. (dtable::vfork_child_dup): Use cygwin heap. (dtable::vfork_parent_restore): Ditto. * dtable.h: Remove obsolete methods. Add new method. * environ.cc (posify): Eliminate already_posix parameter and logic. (envsize): New function. (_addenv): Use envsize. (environ_init): Accept an argument pointing to an existing environment list. If supplied, allocate space for this in the the program's heap. * fhandler.cc (fhandler_base::operator =): Move here from fhandler.h. Use cygwin heap to allocate filenames. (fhandler_base::set_name): Allocate/free names from cygwin heap. (fhandler_base::linearize): Delete. (fhandler_base::de_linearize): Delete. (fhandler_base::operator delete): Free from cygwin heap. (fhandler_base::~fhandler_base): Ditto. * fhandler.h: Accomodate elimination of *linearize and other changes above. * fhandler_console.cc (fhandler_console::fixup_after_exec): Rename from de_linearize. * heap.h: New file. * fhandler_tty.cc (fhandler_tty_slave::fhandler_tty_slave): Use cygwin heap for name. fhandler_tty::fixup_after_exec): Rename from de_linearize. * fork.cc (fork): Call cygheap_fixup_in_child. * heap.cc: Use declarations in heap.h. * malloc.cc: Sprinkle assertions throughout to catch attempts to free/realloc something from the cygwin heap. * path.cc: Throughout, eliminate use of per-thread cache for cwd. Use cwd_* functions rather than cwd_* variables to access cwd_win32 and cwd_posix. (cwd_win32): New function. (cwd_posix): New function. (cwd_hash): New function. (cwd_fixup_after_exec): New function. * path.h: Accomodate path.cc changes. * pinfo.cc (pinfo_init): Accept a pointer to an environment table. Pass this to environ_init. Eliminate old 'title' tests. * pinfo.h: Accomodate above change in argument. * spawn.cc (struct av): New method for building argv list. (av::unshift): New method. (spawn_guts): Allocate everything that the child process needs in the cygwin heap and pass a pointer to this to the child. Build argv list using new method. Eliminate delinearize stuff. * thread.h: Eliminate _cwd_win32 and _cwd_posix buffers. * winsup.h: Eliminate obsolete functions. Add envsize() declaration.
2000-09-03 06:16:35 +02:00
#endif // _CYGNUS_THREADS_