* thread.h (verifyable_object_state verifyable_object_isvalid): Delete function

declaration that should have been static.
* thread.cc (verifyable_object_state verifyable_object_isvalid): Make inline
static.
(pthread*::is_good_object): Move to directly after verifyable_object_state
verifyable_object_isvalid and make inline.
This commit is contained in:
Christopher Faylor 2005-07-03 03:25:19 +00:00
parent 893ac8e03c
commit 4c157aa9e5
3 changed files with 215 additions and 214 deletions

View File

@ -1,3 +1,12 @@
2005-07-02 Christopher Faylor <cgf@timesys.com>
* thread.h (verifyable_object_state verifyable_object_isvalid): Delete
function declaration that should have been static.
* thread.cc (verifyable_object_state verifyable_object_isvalid): Make
inline static.
(pthread*::is_good_object): Move to directly after
verifyable_object_state verifyable_object_isvalid and make inline.
2005-07-02 Christopher Faylor <cgf@timesys.com> 2005-07-02 Christopher Faylor <cgf@timesys.com>
Replace valid memory checks with new myfault class "exception Replace valid memory checks with new myfault class "exception

View File

@ -48,6 +48,11 @@ details. */
extern "C" void __fp_lock_all (); extern "C" void __fp_lock_all ();
extern "C" void __fp_unlock_all (); extern "C" void __fp_unlock_all ();
static inline verifyable_object_state
verifyable_object_isvalid (void const * objectptr, long magic,
void *static_ptr1 = NULL,
void *static_ptr2 = NULL,
void *static_ptr3 = NULL);
extern int threadsafe; extern int threadsafe;
@ -107,7 +112,201 @@ __cygwin_lock_unlock (_LOCK_T *lock)
} }
} }
inline LPCRITICAL_SECTION static inline verifyable_object_state
verifyable_object_isvalid (void const * objectptr, long magic, void *static_ptr1,
void *static_ptr2, void *static_ptr3)
{
verifyable_object **object = (verifyable_object **) objectptr;
myfault efault;
if (efault.faulted ())
return INVALID_OBJECT;
if ((static_ptr1 && *object == static_ptr1) ||
(static_ptr2 && *object == static_ptr2) ||
(static_ptr3 && *object == static_ptr3))
return VALID_STATIC_OBJECT;
if ((*object)->magic != magic)
return INVALID_OBJECT;
return VALID_OBJECT;
}
/* static members */
inline bool
pthread_attr::is_good_object (pthread_attr_t const *attr)
{
if (verifyable_object_isvalid (attr, PTHREAD_ATTR_MAGIC) != VALID_OBJECT)
return false;
return true;
}
inline bool
pthread_condattr::is_good_object (pthread_condattr_t const *attr)
{
if (verifyable_object_isvalid (attr, PTHREAD_CONDATTR_MAGIC) != VALID_OBJECT)
return false;
return true;
}
inline bool
pthread_rwlockattr::is_good_object (pthread_rwlockattr_t const *attr)
{
if (verifyable_object_isvalid (attr, PTHREAD_RWLOCKATTR_MAGIC) != VALID_OBJECT)
return false;
return true;
}
inline bool
pthread_key::is_good_object (pthread_key_t const *key)
{
if (verifyable_object_isvalid (key, PTHREAD_KEY_MAGIC) != VALID_OBJECT)
return false;
return true;
}
inline bool
pthread_mutex::is_good_object (pthread_mutex_t const *mutex)
{
if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC) != VALID_OBJECT)
return false;
return true;
}
inline bool
pthread_mutex::is_good_initializer (pthread_mutex_t const *mutex)
{
if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC,
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
PTHREAD_NORMAL_MUTEX_INITIALIZER_NP,
PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) != VALID_STATIC_OBJECT)
return false;
return true;
}
inline bool
pthread_mutex::is_good_initializer_or_object (pthread_mutex_t const *mutex)
{
if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC,
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
PTHREAD_NORMAL_MUTEX_INITIALIZER_NP,
PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) == INVALID_OBJECT)
return false;
return true;
}
inline bool
pthread_mutex::is_good_initializer_or_bad_object (pthread_mutex_t const *mutex)
{
if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC,
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
PTHREAD_NORMAL_MUTEX_INITIALIZER_NP,
PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) == VALID_OBJECT)
return false;
return true;
}
inline bool
pthread_mutex::can_be_unlocked (pthread_mutex_t const *mutex)
{
pthread_t self = pthread::self ();
if (!is_good_object (mutex))
return false;
/*
* Check if the mutex is owned by the current thread and can be unlocked
*/
return ((*mutex)->recursion_counter == 1 && pthread::equal ((*mutex)->owner, self));
}
inline bool
pthread_mutexattr::is_good_object (pthread_mutexattr_t const * attr)
{
if (verifyable_object_isvalid (attr, PTHREAD_MUTEXATTR_MAGIC) != VALID_OBJECT)
return false;
return true;
}
inline bool __attribute__ ((used))
pthread::is_good_object (pthread_t const *thread)
{
if (verifyable_object_isvalid (thread, PTHREAD_MAGIC) != VALID_OBJECT)
return false;
return true;
}
/* Thread synchronisation */
inline bool
pthread_cond::is_good_object (pthread_cond_t const *cond)
{
if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC) != VALID_OBJECT)
return false;
return true;
}
inline bool
pthread_cond::is_good_initializer (pthread_cond_t const *cond)
{
if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC, PTHREAD_COND_INITIALIZER) != VALID_STATIC_OBJECT)
return false;
return true;
}
inline bool
pthread_cond::is_good_initializer_or_object (pthread_cond_t const *cond)
{
if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC, PTHREAD_COND_INITIALIZER) == INVALID_OBJECT)
return false;
return true;
}
inline bool
pthread_cond::is_good_initializer_or_bad_object (pthread_cond_t const *cond)
{
if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC, PTHREAD_COND_INITIALIZER) == VALID_OBJECT)
return false;
return true;
}
/* RW locks */
inline bool
pthread_rwlock::is_good_object (pthread_rwlock_t const *rwlock)
{
if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC) != VALID_OBJECT)
return false;
return true;
}
inline bool
pthread_rwlock::is_good_initializer (pthread_rwlock_t const *rwlock)
{
if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC, PTHREAD_RWLOCK_INITIALIZER) != VALID_STATIC_OBJECT)
return false;
return true;
}
inline bool
pthread_rwlock::is_good_initializer_or_object (pthread_rwlock_t const *rwlock)
{
if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC, PTHREAD_RWLOCK_INITIALIZER) == INVALID_OBJECT)
return false;
return true;
}
inline bool
pthread_rwlock::is_good_initializer_or_bad_object (pthread_rwlock_t const *rwlock)
{
if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC, PTHREAD_RWLOCK_INITIALIZER) == VALID_OBJECT)
return false;
return true;
}
inline bool
semaphore::is_good_object (sem_t const * sem)
{
if (verifyable_object_isvalid (sem, SEM_MAGIC) != VALID_OBJECT)
return false;
return true;
}
LPCRITICAL_SECTION
ResourceLocks::Lock (int _resid) ResourceLocks::Lock (int _resid)
{ {
return &lock; return &lock;
@ -777,15 +976,6 @@ pthread::resume ()
ResumeThread (win32_obj_id); ResumeThread (win32_obj_id);
} }
/* static members */
bool
pthread_attr::is_good_object (pthread_attr_t const *attr)
{
if (verifyable_object_isvalid (attr, PTHREAD_ATTR_MAGIC) != VALID_OBJECT)
return false;
return true;
}
/* instance members */ /* instance members */
pthread_attr::pthread_attr ():verifyable_object (PTHREAD_ATTR_MAGIC), pthread_attr::pthread_attr ():verifyable_object (PTHREAD_ATTR_MAGIC),
@ -799,14 +989,6 @@ pthread_attr::~pthread_attr ()
{ {
} }
bool
pthread_condattr::is_good_object (pthread_condattr_t const *attr)
{
if (verifyable_object_isvalid (attr, PTHREAD_CONDATTR_MAGIC) != VALID_OBJECT)
return false;
return true;
}
pthread_condattr::pthread_condattr ():verifyable_object pthread_condattr::pthread_condattr ():verifyable_object
(PTHREAD_CONDATTR_MAGIC), shared (PTHREAD_PROCESS_PRIVATE) (PTHREAD_CONDATTR_MAGIC), shared (PTHREAD_PROCESS_PRIVATE)
{ {
@ -1016,14 +1198,6 @@ pthread_cond::_fixup_after_fork ()
api_fatal ("pthread_cond::_fixup_after_fork () failed to recreate win32 semaphore"); api_fatal ("pthread_cond::_fixup_after_fork () failed to recreate win32 semaphore");
} }
bool
pthread_rwlockattr::is_good_object (pthread_rwlockattr_t const *attr)
{
if (verifyable_object_isvalid (attr, PTHREAD_RWLOCKATTR_MAGIC) != VALID_OBJECT)
return false;
return true;
}
pthread_rwlockattr::pthread_rwlockattr ():verifyable_object pthread_rwlockattr::pthread_rwlockattr ():verifyable_object
(PTHREAD_RWLOCKATTR_MAGIC), shared (PTHREAD_PROCESS_PRIVATE) (PTHREAD_RWLOCKATTR_MAGIC), shared (PTHREAD_PROCESS_PRIVATE)
{ {
@ -1347,14 +1521,6 @@ pthread_rwlock::_fixup_after_fork ()
/* This stores pthread_key information across fork() boundaries */ /* This stores pthread_key information across fork() boundaries */
List<pthread_key> pthread_key::keys; List<pthread_key> pthread_key::keys;
bool
pthread_key::is_good_object (pthread_key_t const *key)
{
if (verifyable_object_isvalid (key, PTHREAD_KEY_MAGIC) != VALID_OBJECT)
return false;
return true;
}
/* non-static members */ /* non-static members */
pthread_key::pthread_key (void (*aDestructor) (void *)):verifyable_object (PTHREAD_KEY_MAGIC), destructor (aDestructor) pthread_key::pthread_key (void (*aDestructor) (void *)):verifyable_object (PTHREAD_KEY_MAGIC), destructor (aDestructor)
@ -1430,59 +1596,6 @@ pthread_key::run_destructor ()
Isn't duplicated, it's reopened. */ Isn't duplicated, it's reopened. */
/* static members */ /* static members */
bool
pthread_mutex::is_good_object (pthread_mutex_t const *mutex)
{
if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC) != VALID_OBJECT)
return false;
return true;
}
bool
pthread_mutex::is_good_initializer (pthread_mutex_t const *mutex)
{
if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC,
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
PTHREAD_NORMAL_MUTEX_INITIALIZER_NP,
PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) != VALID_STATIC_OBJECT)
return false;
return true;
}
bool
pthread_mutex::is_good_initializer_or_object (pthread_mutex_t const *mutex)
{
if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC,
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
PTHREAD_NORMAL_MUTEX_INITIALIZER_NP,
PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) == INVALID_OBJECT)
return false;
return true;
}
bool
pthread_mutex::is_good_initializer_or_bad_object (pthread_mutex_t const *mutex)
{
if (verifyable_object_isvalid (mutex, PTHREAD_MUTEX_MAGIC,
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
PTHREAD_NORMAL_MUTEX_INITIALIZER_NP,
PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP) == VALID_OBJECT)
return false;
return true;
}
bool
pthread_mutex::can_be_unlocked (pthread_mutex_t const *mutex)
{
pthread_t self = pthread::self ();
if (!is_good_object (mutex))
return false;
/*
* Check if the mutex is owned by the current thread and can be unlocked
*/
return ((*mutex)->recursion_counter == 1 && pthread::equal ((*mutex)->owner, self));
}
List<pthread_mutex> pthread_mutex::mutexes; List<pthread_mutex> pthread_mutex::mutexes;
@ -1630,14 +1743,6 @@ pthread_mutex::_fixup_after_fork ()
condwaits = 0; condwaits = 0;
} }
bool
pthread_mutexattr::is_good_object (pthread_mutexattr_t const * attr)
{
if (verifyable_object_isvalid (attr, PTHREAD_MUTEXATTR_MAGIC) != VALID_OBJECT)
return false;
return true;
}
pthread_mutexattr::pthread_mutexattr ():verifyable_object (PTHREAD_MUTEXATTR_MAGIC), pthread_mutexattr::pthread_mutexattr ():verifyable_object (PTHREAD_MUTEXATTR_MAGIC),
pshared (PTHREAD_PROCESS_PRIVATE), mutextype (PTHREAD_MUTEX_ERRORCHECK) pshared (PTHREAD_PROCESS_PRIVATE), mutextype (PTHREAD_MUTEX_ERRORCHECK)
{ {
@ -1848,35 +1953,6 @@ verifyable_object::~verifyable_object ()
magic = 0; magic = 0;
} }
/* Generic memory acccess routine - where should it live ? */
int __stdcall
check_valid_pointer (void const *pointer)
{
if (!pointer || IsBadWritePtr ((void *) pointer, sizeof (verifyable_object)))
return EFAULT;
return 0;
}
verifyable_object_state
verifyable_object_isvalid (void const * objectptr, long magic, void *static_ptr1,
void *static_ptr2, void *static_ptr3)
{
verifyable_object **object = (verifyable_object **)objectptr;
if (check_valid_pointer (object))
return INVALID_OBJECT;
if ((static_ptr1 && *object == static_ptr1) ||
(static_ptr2 && *object == static_ptr2) ||
(static_ptr3 && *object == static_ptr3))
return VALID_STATIC_OBJECT;
if (!*object)
return INVALID_OBJECT;
if (check_valid_pointer (*object))
return INVALID_OBJECT;
if ((*object)->magic != magic)
return INVALID_OBJECT;
return VALID_OBJECT;
}
DWORD WINAPI DWORD WINAPI
pthread::thread_init_wrapper (void *arg) pthread::thread_init_wrapper (void *arg)
{ {
@ -1901,14 +1977,6 @@ pthread::thread_init_wrapper (void *arg)
return 0; // just for show. Never returns. return 0; // just for show. Never returns.
} }
bool
pthread::is_good_object (pthread_t const *thread)
{
if (verifyable_object_isvalid (thread, PTHREAD_MAGIC) != VALID_OBJECT)
return false;
return true;
}
unsigned long unsigned long
pthread::getsequence_np () pthread::getsequence_np ()
{ {
@ -2448,39 +2516,6 @@ pthread_getspecific (pthread_key_t key)
} }
/* Thread synchronisation */
bool
pthread_cond::is_good_object (pthread_cond_t const *cond)
{
if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC) != VALID_OBJECT)
return false;
return true;
}
bool
pthread_cond::is_good_initializer (pthread_cond_t const *cond)
{
if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC, PTHREAD_COND_INITIALIZER) != VALID_STATIC_OBJECT)
return false;
return true;
}
bool
pthread_cond::is_good_initializer_or_object (pthread_cond_t const *cond)
{
if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC, PTHREAD_COND_INITIALIZER) == INVALID_OBJECT)
return false;
return true;
}
bool
pthread_cond::is_good_initializer_or_bad_object (pthread_cond_t const *cond)
{
if (verifyable_object_isvalid (cond, PTHREAD_COND_MAGIC, PTHREAD_COND_INITIALIZER) == VALID_OBJECT)
return false;
return true;
}
extern "C" int extern "C" int
pthread_cond_destroy (pthread_cond_t *cond) pthread_cond_destroy (pthread_cond_t *cond)
{ {
@ -2579,12 +2614,13 @@ pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex,
{ {
struct timeval tv; struct timeval tv;
long waitlength; long waitlength;
myfault efault;
if (efault.faulted ())
return EINVAL;
pthread_testcancel (); pthread_testcancel ();
if (check_valid_pointer (abstime))
return EINVAL;
gettimeofday (&tv, NULL); gettimeofday (&tv, NULL);
waitlength = abstime->tv_sec * 1000 + abstime->tv_nsec / (1000 * 1000); waitlength = abstime->tv_sec * 1000 + abstime->tv_nsec / (1000 * 1000);
waitlength -= tv.tv_sec * 1000 + tv.tv_usec / 1000; waitlength -= tv.tv_sec * 1000 + tv.tv_usec / 1000;
@ -2650,39 +2686,6 @@ pthread_condattr_destroy (pthread_condattr_t *condattr)
return 0; return 0;
} }
/* RW locks */
bool
pthread_rwlock::is_good_object (pthread_rwlock_t const *rwlock)
{
if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC) != VALID_OBJECT)
return false;
return true;
}
bool
pthread_rwlock::is_good_initializer (pthread_rwlock_t const *rwlock)
{
if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC, PTHREAD_RWLOCK_INITIALIZER) != VALID_STATIC_OBJECT)
return false;
return true;
}
bool
pthread_rwlock::is_good_initializer_or_object (pthread_rwlock_t const *rwlock)
{
if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC, PTHREAD_RWLOCK_INITIALIZER) == INVALID_OBJECT)
return false;
return true;
}
bool
pthread_rwlock::is_good_initializer_or_bad_object (pthread_rwlock_t const *rwlock)
{
if (verifyable_object_isvalid (rwlock, PTHREAD_RWLOCK_MAGIC, PTHREAD_RWLOCK_INITIALIZER) == VALID_OBJECT)
return false;
return true;
}
extern "C" int extern "C" int
pthread_rwlock_destroy (pthread_rwlock_t *rwlock) pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
{ {
@ -2883,7 +2886,8 @@ pthread_mutex::init (pthread_mutex_t *mutex,
{ {
pthread_mutex_t new_mutex; pthread_mutex_t new_mutex;
if (attr && !pthread_mutexattr::is_good_object (attr) || check_valid_pointer (mutex)) myfault efault;
if (attr && !pthread_mutexattr::is_good_object (attr) || efault.faulted ())
return EINVAL; return EINVAL;
mutex_initialization_lock.lock (); mutex_initialization_lock.lock ();
@ -3111,13 +3115,6 @@ pthread_mutexattr_settype (pthread_mutexattr_t *attr, int type)
/* Semaphores */ /* Semaphores */
/* static members */ /* static members */
bool
semaphore::is_good_object (sem_t const * sem)
{
if (verifyable_object_isvalid (sem, SEM_MAGIC) != VALID_OBJECT)
return false;
return true;
}
int int
semaphore::init (sem_t *sem, int pshared, unsigned int value) semaphore::init (sem_t *sem, int pshared, unsigned int value)

View File

@ -137,11 +137,6 @@ typedef enum
VALID_STATIC_OBJECT VALID_STATIC_OBJECT
} verifyable_object_state; } verifyable_object_state;
verifyable_object_state verifyable_object_isvalid (void const * objectptr, long magic,
void *static_ptr1 = NULL,
void *static_ptr2 = NULL,
void *static_ptr3 = NULL);
template <class list_node> inline void template <class list_node> inline void
List_insert (list_node *&head, list_node *node) List_insert (list_node *&head, list_node *node)
{ {