libmthread: don't always verify library initialization
This can be turned back on when the library is compiled with -DMTHREAD_STRICT (which enables more sanity checks). However, always performing this check shows up in system profiling results.
This commit is contained in:
parent
068d443d12
commit
76092ddf33
7 changed files with 59 additions and 57 deletions
|
@ -33,7 +33,7 @@ mthread_thread_t l;
|
|||
mthread_thread_t r;
|
||||
{
|
||||
/* Compare two thread ids */
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
return(l == r);
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ void *arg;
|
|||
/* Register procedure proc for execution in a thread. */
|
||||
mthread_thread_t thread;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (proc == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -85,7 +85,7 @@ mthread_thread_t detach;
|
|||
* this thread are automatically freed.
|
||||
*/
|
||||
mthread_tcb_t *tcb;
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (!isokthreadid(detach))
|
||||
return(ESRCH);
|
||||
|
@ -113,7 +113,7 @@ void *value;
|
|||
/* Make a thread stop running and store the result value. */
|
||||
mthread_tcb_t *tcb;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
tcb = mthread_find_tcb(current_thread);
|
||||
|
||||
|
@ -229,27 +229,27 @@ void mthread_init(void)
|
|||
* threads.
|
||||
*/
|
||||
|
||||
if (!initialized) {
|
||||
no_threads = 0;
|
||||
used_threads = 0;
|
||||
need_reset = 0;
|
||||
running_main_thread = 1;/* mthread_init can only be called from the
|
||||
* main thread. Calling it from a thread will
|
||||
* not enter this clause.
|
||||
*/
|
||||
if (initialized) return;
|
||||
|
||||
if (mthread_getcontext(&(mainthread.m_context)) == -1)
|
||||
mthread_panic("Couldn't save state for main thread");
|
||||
current_thread = MAIN_THREAD;
|
||||
no_threads = 0;
|
||||
used_threads = 0;
|
||||
need_reset = 0;
|
||||
running_main_thread = 1; /* mthread_init can only be called from the
|
||||
* main thread. Calling it from a thread will
|
||||
* not enter this clause.
|
||||
*/
|
||||
|
||||
mthread_init_valid_mutexes();
|
||||
mthread_init_valid_conditions();
|
||||
mthread_init_valid_attributes();
|
||||
mthread_init_keys();
|
||||
mthread_init_scheduler();
|
||||
if (mthread_getcontext(&(mainthread.m_context)) == -1)
|
||||
mthread_panic("Couldn't save state for main thread");
|
||||
current_thread = MAIN_THREAD;
|
||||
|
||||
initialized = 1;
|
||||
}
|
||||
mthread_init_valid_mutexes();
|
||||
mthread_init_valid_conditions();
|
||||
mthread_init_valid_attributes();
|
||||
mthread_init_keys();
|
||||
mthread_init_scheduler();
|
||||
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
|
||||
|
@ -264,7 +264,7 @@ void **value;
|
|||
|
||||
mthread_tcb_t *tcb;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (!isokthreadid(join))
|
||||
return(ESRCH);
|
||||
|
@ -320,7 +320,7 @@ void (*proc)(void);
|
|||
{
|
||||
/* Run procedure proc just once */
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (once == NULL || proc == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -338,7 +338,7 @@ mthread_thread_t mthread_self(void)
|
|||
{
|
||||
/* Return the thread id of the thread calling this function. */
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
return(current_thread);
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ mthread_attr_t *attr;
|
|||
{
|
||||
/* Invalidate attribute and deallocate resources. */
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (attr == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -72,7 +72,7 @@ mthread_attr_t *attr; /* Attribute */
|
|||
/* Initialize the attribute to a known state. */
|
||||
struct __mthread_attr *a;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (attr == NULL)
|
||||
return(EAGAIN);
|
||||
|
@ -102,7 +102,7 @@ int *detachstate;
|
|||
/* Get detachstate of a thread attribute */
|
||||
struct __mthread_attr *a;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (attr == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -127,7 +127,7 @@ int detachstate;
|
|||
/* Set detachstate of a thread attribute */
|
||||
struct __mthread_attr *a;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (attr == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -156,7 +156,7 @@ size_t *stacksize;
|
|||
/* Get stack attribute */
|
||||
struct __mthread_attr *a;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (attr == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -182,7 +182,7 @@ size_t *stacksize;
|
|||
/* Get stack size attribute */
|
||||
struct __mthread_attr *a;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (attr == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -208,7 +208,7 @@ size_t stacksize;
|
|||
/* Set stack attribute */
|
||||
struct __mthread_attr *a;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (attr == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -239,7 +239,7 @@ size_t stacksize;
|
|||
/* Set stack size attribute */
|
||||
struct __mthread_attr *a;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (attr == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -283,7 +283,7 @@ mthread_attr_t *a;
|
|||
/* Check to see if attribute is on the list of valid attributes */
|
||||
struct __mthread_attr *loopitem;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
loopitem = va_front;
|
||||
|
||||
|
@ -307,7 +307,7 @@ int mthread_attr_verify(void)
|
|||
/* Return true when no attributes are in use */
|
||||
struct __mthread_attr *loopitem;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
loopitem = va_front;
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ mthread_cond_t *cond;
|
|||
mthread_thread_t t;
|
||||
mthread_tcb_t *tcb;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (cond == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -89,7 +89,7 @@ mthread_cond_t *cond;
|
|||
mthread_thread_t t;
|
||||
mthread_tcb_t *tcb;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (cond == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -126,7 +126,7 @@ mthread_condattr_t *cattr;
|
|||
/* Initialize condition variable to a known state. cattr is ignored */
|
||||
struct __mthread_cond *c;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (cond == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -181,7 +181,7 @@ mthread_cond_t *cond;
|
|||
mthread_thread_t t;
|
||||
mthread_tcb_t *tcb;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (cond == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -214,7 +214,7 @@ mthread_cond_t *c;
|
|||
/* Check to see if cond is on the list of valid conditions */
|
||||
struct __mthread_cond *loopitem;
|
||||
|
||||
mthread_init();
|
||||
MTHREAD_CHECK_INIT();
|
||||
|
||||
loopitem = vc_front;
|
||||
|
||||
|
@ -237,7 +237,7 @@ int mthread_cond_verify(void)
|
|||
{
|
||||
/* Return true in case no condition variables are in use. */
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
return(vc_front == NULL);
|
||||
}
|
||||
|
@ -256,7 +256,7 @@ mthread_mutex_t *mutex;
|
|||
struct __mthread_cond *c;
|
||||
struct __mthread_mutex *m;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (cond == NULL || mutex == NULL)
|
||||
return(EINVAL);
|
||||
|
|
|
@ -34,7 +34,7 @@ int mthread_key_create(mthread_key_t *key, void (*destructor)(void *))
|
|||
*/
|
||||
mthread_key_t k;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
keys_used = 1;
|
||||
|
||||
/* We do not yet allocate storage space for the values here, because we can
|
||||
|
@ -65,7 +65,7 @@ int mthread_key_delete(mthread_key_t key)
|
|||
/* Free up a key, as well as any associated storage space.
|
||||
*/
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (key < 0 || key >= MTHREAD_KEYS_MAX || !keys[key].used)
|
||||
return(EINVAL);
|
||||
|
@ -85,7 +85,7 @@ void *mthread_getspecific(mthread_key_t key)
|
|||
/* Get this thread's local value for the given key. The default is NULL.
|
||||
*/
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (key < 0 || key >= MTHREAD_KEYS_MAX || !keys[key].used)
|
||||
return(NULL);
|
||||
|
@ -109,7 +109,7 @@ int mthread_setspecific(mthread_key_t key, void *value)
|
|||
*/
|
||||
void **p;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (key < 0 || key >= MTHREAD_KEYS_MAX || !keys[key].used)
|
||||
return(EINVAL);
|
||||
|
|
|
@ -12,7 +12,7 @@ static void mthread_mutex_remove(mthread_mutex_t *m);
|
|||
#endif
|
||||
|
||||
/*===========================================================================*
|
||||
* mthread_init_valid_mutexes *
|
||||
* mthread_init_valid_mutexes *
|
||||
*===========================================================================*/
|
||||
void mthread_init_valid_mutexes(void)
|
||||
{
|
||||
|
@ -56,7 +56,7 @@ mthread_mutex_t *mutex;
|
|||
mthread_thread_t t;
|
||||
mthread_tcb_t *tcb;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (mutex == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -95,7 +95,7 @@ mthread_mutexattr_t *mattr; /* Mutex attribute */
|
|||
|
||||
struct __mthread_mutex *m;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (mutex == NULL)
|
||||
return(EAGAIN);
|
||||
|
@ -127,7 +127,7 @@ mthread_mutex_t *mutex; /* Mutex that is to be locked */
|
|||
|
||||
struct __mthread_mutex *m;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (mutex == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -180,7 +180,7 @@ mthread_mutex_t *mutex; /* Mutex that is to be locked */
|
|||
|
||||
struct __mthread_mutex *m;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (mutex == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -210,7 +210,7 @@ mthread_mutex_t *mutex; /* Mutex that is to be unlocked */
|
|||
|
||||
struct __mthread_mutex *m;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
if (mutex == NULL)
|
||||
return(EINVAL);
|
||||
|
@ -237,7 +237,7 @@ mthread_mutex_t *m;
|
|||
/* Check to see if mutex is on the list of valid mutexes */
|
||||
struct __mthread_mutex *loopitem;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
loopitem = vm_front;
|
||||
|
||||
|
@ -262,7 +262,7 @@ int mthread_mutex_verify(void)
|
|||
int r = 1;
|
||||
struct __mthread_mutex *loopitem;
|
||||
|
||||
mthread_init(); /* Make sure mthreads is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||
|
||||
#ifdef MTHREAD_STRICT
|
||||
loopitem = vm_front;
|
||||
|
|
|
@ -38,8 +38,10 @@ void mthread_init_valid_mutexes(void);
|
|||
|
||||
#ifdef MTHREAD_STRICT
|
||||
int mthread_mutex_valid(mthread_mutex_t *mutex);
|
||||
# define MTHREAD_CHECK_INIT() mthread_init()
|
||||
#else
|
||||
# define mthread_mutex_valid(x) ((*x)->mm_magic == MTHREAD_INIT_MAGIC)
|
||||
# define MTHREAD_CHECK_INIT()
|
||||
#endif
|
||||
|
||||
#ifdef MDEBUG
|
||||
|
|
|
@ -38,7 +38,7 @@ void mthread_schedule(void)
|
|||
mthread_tcb_t *new_tcb, *old_tcb;
|
||||
ucontext_t *new_ctx, *old_ctx;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
old_thread = current_thread;
|
||||
|
||||
|
@ -154,7 +154,7 @@ int mthread_yield(void)
|
|||
mthread_tcb_t *tcb;
|
||||
mthread_thread_t t;
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
/* Detached threads cannot clean themselves up. This is a perfect moment to
|
||||
* do it */
|
||||
|
@ -192,7 +192,7 @@ void mthread_yield_all(void)
|
|||
* this function will lead to a deadlock.
|
||||
*/
|
||||
|
||||
mthread_init(); /* Make sure libmthread is initialized */
|
||||
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||
|
||||
if (yield_all) mthread_panic("Deadlock: two threads trying to yield_all");
|
||||
yield_all = 1;
|
||||
|
|
Loading…
Reference in a new issue