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;
|
mthread_thread_t r;
|
||||||
{
|
{
|
||||||
/* Compare two thread ids */
|
/* Compare two thread ids */
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
return(l == r);
|
return(l == r);
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ void *arg;
|
||||||
/* Register procedure proc for execution in a thread. */
|
/* Register procedure proc for execution in a thread. */
|
||||||
mthread_thread_t thread;
|
mthread_thread_t thread;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (proc == NULL)
|
if (proc == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -85,7 +85,7 @@ mthread_thread_t detach;
|
||||||
* this thread are automatically freed.
|
* this thread are automatically freed.
|
||||||
*/
|
*/
|
||||||
mthread_tcb_t *tcb;
|
mthread_tcb_t *tcb;
|
||||||
mthread_init(); /* Make sure libmthread is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||||
|
|
||||||
if (!isokthreadid(detach))
|
if (!isokthreadid(detach))
|
||||||
return(ESRCH);
|
return(ESRCH);
|
||||||
|
@ -113,7 +113,7 @@ void *value;
|
||||||
/* Make a thread stop running and store the result value. */
|
/* Make a thread stop running and store the result value. */
|
||||||
mthread_tcb_t *tcb;
|
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);
|
tcb = mthread_find_tcb(current_thread);
|
||||||
|
|
||||||
|
@ -229,11 +229,12 @@ void mthread_init(void)
|
||||||
* threads.
|
* threads.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (!initialized) {
|
if (initialized) return;
|
||||||
|
|
||||||
no_threads = 0;
|
no_threads = 0;
|
||||||
used_threads = 0;
|
used_threads = 0;
|
||||||
need_reset = 0;
|
need_reset = 0;
|
||||||
running_main_thread = 1;/* mthread_init can only be called from the
|
running_main_thread = 1; /* mthread_init can only be called from the
|
||||||
* main thread. Calling it from a thread will
|
* main thread. Calling it from a thread will
|
||||||
* not enter this clause.
|
* not enter this clause.
|
||||||
*/
|
*/
|
||||||
|
@ -249,7 +250,6 @@ void mthread_init(void)
|
||||||
mthread_init_scheduler();
|
mthread_init_scheduler();
|
||||||
|
|
||||||
initialized = 1;
|
initialized = 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -264,7 +264,7 @@ void **value;
|
||||||
|
|
||||||
mthread_tcb_t *tcb;
|
mthread_tcb_t *tcb;
|
||||||
|
|
||||||
mthread_init(); /* Make sure libmthread is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||||
|
|
||||||
if (!isokthreadid(join))
|
if (!isokthreadid(join))
|
||||||
return(ESRCH);
|
return(ESRCH);
|
||||||
|
@ -320,7 +320,7 @@ void (*proc)(void);
|
||||||
{
|
{
|
||||||
/* Run procedure proc just once */
|
/* 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)
|
if (once == NULL || proc == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -338,7 +338,7 @@ mthread_thread_t mthread_self(void)
|
||||||
{
|
{
|
||||||
/* Return the thread id of the thread calling this function. */
|
/* 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);
|
return(current_thread);
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ mthread_attr_t *attr;
|
||||||
{
|
{
|
||||||
/* Invalidate attribute and deallocate resources. */
|
/* Invalidate attribute and deallocate resources. */
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (attr == NULL)
|
if (attr == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -72,7 +72,7 @@ mthread_attr_t *attr; /* Attribute */
|
||||||
/* Initialize the attribute to a known state. */
|
/* Initialize the attribute to a known state. */
|
||||||
struct __mthread_attr *a;
|
struct __mthread_attr *a;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (attr == NULL)
|
if (attr == NULL)
|
||||||
return(EAGAIN);
|
return(EAGAIN);
|
||||||
|
@ -102,7 +102,7 @@ int *detachstate;
|
||||||
/* Get detachstate of a thread attribute */
|
/* Get detachstate of a thread attribute */
|
||||||
struct __mthread_attr *a;
|
struct __mthread_attr *a;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (attr == NULL)
|
if (attr == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -127,7 +127,7 @@ int detachstate;
|
||||||
/* Set detachstate of a thread attribute */
|
/* Set detachstate of a thread attribute */
|
||||||
struct __mthread_attr *a;
|
struct __mthread_attr *a;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (attr == NULL)
|
if (attr == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -156,7 +156,7 @@ size_t *stacksize;
|
||||||
/* Get stack attribute */
|
/* Get stack attribute */
|
||||||
struct __mthread_attr *a;
|
struct __mthread_attr *a;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (attr == NULL)
|
if (attr == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -182,7 +182,7 @@ size_t *stacksize;
|
||||||
/* Get stack size attribute */
|
/* Get stack size attribute */
|
||||||
struct __mthread_attr *a;
|
struct __mthread_attr *a;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (attr == NULL)
|
if (attr == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -208,7 +208,7 @@ size_t stacksize;
|
||||||
/* Set stack attribute */
|
/* Set stack attribute */
|
||||||
struct __mthread_attr *a;
|
struct __mthread_attr *a;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (attr == NULL)
|
if (attr == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -239,7 +239,7 @@ size_t stacksize;
|
||||||
/* Set stack size attribute */
|
/* Set stack size attribute */
|
||||||
struct __mthread_attr *a;
|
struct __mthread_attr *a;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (attr == NULL)
|
if (attr == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -283,7 +283,7 @@ mthread_attr_t *a;
|
||||||
/* Check to see if attribute is on the list of valid attributes */
|
/* Check to see if attribute is on the list of valid attributes */
|
||||||
struct __mthread_attr *loopitem;
|
struct __mthread_attr *loopitem;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
loopitem = va_front;
|
loopitem = va_front;
|
||||||
|
|
||||||
|
@ -307,7 +307,7 @@ int mthread_attr_verify(void)
|
||||||
/* Return true when no attributes are in use */
|
/* Return true when no attributes are in use */
|
||||||
struct __mthread_attr *loopitem;
|
struct __mthread_attr *loopitem;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
loopitem = va_front;
|
loopitem = va_front;
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,7 @@ mthread_cond_t *cond;
|
||||||
mthread_thread_t t;
|
mthread_thread_t t;
|
||||||
mthread_tcb_t *tcb;
|
mthread_tcb_t *tcb;
|
||||||
|
|
||||||
mthread_init(); /* Make sure libmthread is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||||
|
|
||||||
if (cond == NULL)
|
if (cond == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -89,7 +89,7 @@ mthread_cond_t *cond;
|
||||||
mthread_thread_t t;
|
mthread_thread_t t;
|
||||||
mthread_tcb_t *tcb;
|
mthread_tcb_t *tcb;
|
||||||
|
|
||||||
mthread_init(); /* Make sure libmthread is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||||
|
|
||||||
if (cond == NULL)
|
if (cond == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -126,7 +126,7 @@ mthread_condattr_t *cattr;
|
||||||
/* Initialize condition variable to a known state. cattr is ignored */
|
/* Initialize condition variable to a known state. cattr is ignored */
|
||||||
struct __mthread_cond *c;
|
struct __mthread_cond *c;
|
||||||
|
|
||||||
mthread_init(); /* Make sure libmthread is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||||
|
|
||||||
if (cond == NULL)
|
if (cond == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -181,7 +181,7 @@ mthread_cond_t *cond;
|
||||||
mthread_thread_t t;
|
mthread_thread_t t;
|
||||||
mthread_tcb_t *tcb;
|
mthread_tcb_t *tcb;
|
||||||
|
|
||||||
mthread_init(); /* Make sure libmthread is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||||
|
|
||||||
if (cond == NULL)
|
if (cond == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -214,7 +214,7 @@ mthread_cond_t *c;
|
||||||
/* Check to see if cond is on the list of valid conditions */
|
/* Check to see if cond is on the list of valid conditions */
|
||||||
struct __mthread_cond *loopitem;
|
struct __mthread_cond *loopitem;
|
||||||
|
|
||||||
mthread_init();
|
MTHREAD_CHECK_INIT();
|
||||||
|
|
||||||
loopitem = vc_front;
|
loopitem = vc_front;
|
||||||
|
|
||||||
|
@ -237,7 +237,7 @@ int mthread_cond_verify(void)
|
||||||
{
|
{
|
||||||
/* Return true in case no condition variables are in use. */
|
/* 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);
|
return(vc_front == NULL);
|
||||||
}
|
}
|
||||||
|
@ -256,7 +256,7 @@ mthread_mutex_t *mutex;
|
||||||
struct __mthread_cond *c;
|
struct __mthread_cond *c;
|
||||||
struct __mthread_mutex *m;
|
struct __mthread_mutex *m;
|
||||||
|
|
||||||
mthread_init(); /* Make sure libmthread is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||||
|
|
||||||
if (cond == NULL || mutex == NULL)
|
if (cond == NULL || mutex == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
|
|
@ -34,7 +34,7 @@ int mthread_key_create(mthread_key_t *key, void (*destructor)(void *))
|
||||||
*/
|
*/
|
||||||
mthread_key_t k;
|
mthread_key_t k;
|
||||||
|
|
||||||
mthread_init(); /* Make sure libmthread is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
||||||
keys_used = 1;
|
keys_used = 1;
|
||||||
|
|
||||||
/* We do not yet allocate storage space for the values here, because we can
|
/* 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.
|
/* 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)
|
if (key < 0 || key >= MTHREAD_KEYS_MAX || !keys[key].used)
|
||||||
return(EINVAL);
|
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.
|
/* 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)
|
if (key < 0 || key >= MTHREAD_KEYS_MAX || !keys[key].used)
|
||||||
return(NULL);
|
return(NULL);
|
||||||
|
@ -109,7 +109,7 @@ int mthread_setspecific(mthread_key_t key, void *value)
|
||||||
*/
|
*/
|
||||||
void **p;
|
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)
|
if (key < 0 || key >= MTHREAD_KEYS_MAX || !keys[key].used)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
|
|
@ -56,7 +56,7 @@ mthread_mutex_t *mutex;
|
||||||
mthread_thread_t t;
|
mthread_thread_t t;
|
||||||
mthread_tcb_t *tcb;
|
mthread_tcb_t *tcb;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (mutex == NULL)
|
if (mutex == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -95,7 +95,7 @@ mthread_mutexattr_t *mattr; /* Mutex attribute */
|
||||||
|
|
||||||
struct __mthread_mutex *m;
|
struct __mthread_mutex *m;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (mutex == NULL)
|
if (mutex == NULL)
|
||||||
return(EAGAIN);
|
return(EAGAIN);
|
||||||
|
@ -127,7 +127,7 @@ mthread_mutex_t *mutex; /* Mutex that is to be locked */
|
||||||
|
|
||||||
struct __mthread_mutex *m;
|
struct __mthread_mutex *m;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (mutex == NULL)
|
if (mutex == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -180,7 +180,7 @@ mthread_mutex_t *mutex; /* Mutex that is to be locked */
|
||||||
|
|
||||||
struct __mthread_mutex *m;
|
struct __mthread_mutex *m;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (mutex == NULL)
|
if (mutex == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -210,7 +210,7 @@ mthread_mutex_t *mutex; /* Mutex that is to be unlocked */
|
||||||
|
|
||||||
struct __mthread_mutex *m;
|
struct __mthread_mutex *m;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
if (mutex == NULL)
|
if (mutex == NULL)
|
||||||
return(EINVAL);
|
return(EINVAL);
|
||||||
|
@ -237,7 +237,7 @@ mthread_mutex_t *m;
|
||||||
/* Check to see if mutex is on the list of valid mutexes */
|
/* Check to see if mutex is on the list of valid mutexes */
|
||||||
struct __mthread_mutex *loopitem;
|
struct __mthread_mutex *loopitem;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
loopitem = vm_front;
|
loopitem = vm_front;
|
||||||
|
|
||||||
|
@ -262,7 +262,7 @@ int mthread_mutex_verify(void)
|
||||||
int r = 1;
|
int r = 1;
|
||||||
struct __mthread_mutex *loopitem;
|
struct __mthread_mutex *loopitem;
|
||||||
|
|
||||||
mthread_init(); /* Make sure mthreads is initialized */
|
MTHREAD_CHECK_INIT(); /* Make sure mthreads is initialized */
|
||||||
|
|
||||||
#ifdef MTHREAD_STRICT
|
#ifdef MTHREAD_STRICT
|
||||||
loopitem = vm_front;
|
loopitem = vm_front;
|
||||||
|
|
|
@ -38,8 +38,10 @@ void mthread_init_valid_mutexes(void);
|
||||||
|
|
||||||
#ifdef MTHREAD_STRICT
|
#ifdef MTHREAD_STRICT
|
||||||
int mthread_mutex_valid(mthread_mutex_t *mutex);
|
int mthread_mutex_valid(mthread_mutex_t *mutex);
|
||||||
|
# define MTHREAD_CHECK_INIT() mthread_init()
|
||||||
#else
|
#else
|
||||||
# define mthread_mutex_valid(x) ((*x)->mm_magic == MTHREAD_INIT_MAGIC)
|
# define mthread_mutex_valid(x) ((*x)->mm_magic == MTHREAD_INIT_MAGIC)
|
||||||
|
# define MTHREAD_CHECK_INIT()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef MDEBUG
|
#ifdef MDEBUG
|
||||||
|
|
|
@ -38,7 +38,7 @@ void mthread_schedule(void)
|
||||||
mthread_tcb_t *new_tcb, *old_tcb;
|
mthread_tcb_t *new_tcb, *old_tcb;
|
||||||
ucontext_t *new_ctx, *old_ctx;
|
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;
|
old_thread = current_thread;
|
||||||
|
|
||||||
|
@ -154,7 +154,7 @@ int mthread_yield(void)
|
||||||
mthread_tcb_t *tcb;
|
mthread_tcb_t *tcb;
|
||||||
mthread_thread_t t;
|
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
|
/* Detached threads cannot clean themselves up. This is a perfect moment to
|
||||||
* do it */
|
* do it */
|
||||||
|
@ -192,7 +192,7 @@ void mthread_yield_all(void)
|
||||||
* this function will lead to a deadlock.
|
* 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");
|
if (yield_all) mthread_panic("Deadlock: two threads trying to yield_all");
|
||||||
yield_all = 1;
|
yield_all = 1;
|
||||||
|
|
Loading…
Reference in a new issue