2010-09-21 14:22:38 +02:00
|
|
|
#include <minix/mthread.h>
|
|
|
|
#include "global.h"
|
2010-09-30 15:44:13 +02:00
|
|
|
#include "proto.h"
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static struct __mthread_attr *va_front, *va_rear;
|
|
|
|
static void mthread_attr_add(mthread_attr_t *a);
|
|
|
|
static void mthread_attr_remove(mthread_attr_t *a);
|
|
|
|
static int mthread_attr_valid(mthread_attr_t *a);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_init_valid_attributes *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void mthread_init_valid_attributes(void)
|
2010-09-21 14:22:38 +02:00
|
|
|
{
|
|
|
|
/* Initialize list of valid attributs */
|
|
|
|
va_front = va_rear = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_add *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static void mthread_attr_add(a)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *a;
|
|
|
|
{
|
|
|
|
/* Add attribute to list of valid, initialized attributes */
|
|
|
|
|
|
|
|
if (va_front == NULL) { /* Empty list */
|
|
|
|
va_front = *a;
|
2011-03-18 11:29:54 +01:00
|
|
|
(*a)->ma_prev = NULL;
|
2010-09-21 14:22:38 +02:00
|
|
|
} else {
|
2011-03-18 11:29:54 +01:00
|
|
|
va_rear->ma_next = *a;
|
|
|
|
(*a)->ma_prev = va_rear;
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
(*a)->ma_next = NULL;
|
2010-09-21 14:22:38 +02:00
|
|
|
va_rear = *a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_destroy *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_attr_destroy(attr)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *attr;
|
|
|
|
{
|
|
|
|
/* Invalidate attribute and deallocate resources. */
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (attr == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_attr_valid(attr))
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/* Valide attribute; invalidate it */
|
|
|
|
mthread_attr_remove(attr);
|
|
|
|
free(*attr);
|
|
|
|
*attr = NULL;
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_init *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_attr_init(attr)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *attr; /* Attribute */
|
|
|
|
{
|
|
|
|
/* Initialize the attribute to a known state. */
|
|
|
|
struct __mthread_attr *a;
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (attr == NULL)
|
|
|
|
return(EAGAIN);
|
|
|
|
else if (mthread_attr_valid(attr))
|
|
|
|
return(EBUSY);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
if ((a = malloc(sizeof(struct __mthread_attr))) == NULL)
|
|
|
|
return(-1);
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
a->ma_detachstate = MTHREAD_CREATE_JOINABLE;
|
|
|
|
a->ma_stackaddr = NULL;
|
|
|
|
a->ma_stacksize = (size_t) 0;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
*attr = (mthread_attr_t) a;
|
|
|
|
mthread_attr_add(attr); /* Validate attribute: attribute now in use */
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_getdetachstate *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_attr_getdetachstate(attr, detachstate)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *attr;
|
|
|
|
int *detachstate;
|
|
|
|
{
|
|
|
|
/* Get detachstate of a thread attribute */
|
|
|
|
struct __mthread_attr *a;
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (attr == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
a = (struct __mthread_attr *) *attr;
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_attr_valid(attr))
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
*detachstate = a->ma_detachstate;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_setdetachstate *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_attr_setdetachstate(attr, detachstate)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *attr;
|
|
|
|
int detachstate;
|
|
|
|
{
|
|
|
|
/* Set detachstate of a thread attribute */
|
|
|
|
struct __mthread_attr *a;
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (attr == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
a = (struct __mthread_attr *) *attr;
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_attr_valid(attr))
|
|
|
|
return(EINVAL);
|
|
|
|
else if(detachstate != MTHREAD_CREATE_JOINABLE &&
|
|
|
|
detachstate != MTHREAD_CREATE_DETACHED)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
a->ma_detachstate = detachstate;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_getstack *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_attr_getstack(attr, stackaddr, stacksize)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *attr;
|
|
|
|
void **stackaddr;
|
|
|
|
size_t *stacksize;
|
|
|
|
{
|
|
|
|
/* Get stack attribute */
|
|
|
|
struct __mthread_attr *a;
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (attr == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
a = (struct __mthread_attr *) *attr;
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_attr_valid(attr))
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
*stackaddr = a->ma_stackaddr;
|
|
|
|
*stacksize = a->ma_stacksize;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_getstacksize *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_attr_getstacksize(attr, stacksize)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *attr;
|
|
|
|
size_t *stacksize;
|
|
|
|
{
|
|
|
|
/* Get stack size attribute */
|
|
|
|
struct __mthread_attr *a;
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (attr == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
a = (struct __mthread_attr *) *attr;
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_attr_valid(attr))
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
*stacksize = a->ma_stacksize;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_setstack *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_attr_setstack(attr, stackaddr, stacksize)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *attr;
|
|
|
|
void *stackaddr;
|
|
|
|
size_t stacksize;
|
|
|
|
{
|
|
|
|
/* Set stack attribute */
|
|
|
|
struct __mthread_attr *a;
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (attr == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
a = (struct __mthread_attr *) *attr;
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_attr_valid(attr) || stacksize < MTHREAD_STACK_MIN)
|
|
|
|
return(EINVAL);
|
|
|
|
|
2010-09-21 14:22:38 +02:00
|
|
|
/* We don't care about address alignment (POSIX standard). The ucontext
|
|
|
|
* system calls will make sure that the provided stack will be aligned (at
|
|
|
|
* the cost of some memory if needed).
|
|
|
|
*/
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
a->ma_stackaddr = stackaddr;
|
|
|
|
a->ma_stacksize = stacksize;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_setstacksize *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_attr_setstacksize(attr, stacksize)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *attr;
|
|
|
|
size_t stacksize;
|
|
|
|
{
|
|
|
|
/* Set stack size attribute */
|
|
|
|
struct __mthread_attr *a;
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (attr == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
a = (struct __mthread_attr *) *attr;
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_attr_valid(attr) || stacksize < MTHREAD_STACK_MIN)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
a->ma_stacksize = stacksize;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_remove *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static void mthread_attr_remove(a)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *a;
|
|
|
|
{
|
|
|
|
/* Remove attribute from list of valid, initialized attributes */
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
if ((*a)->ma_prev == NULL)
|
|
|
|
va_front = (*a)->ma_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
else
|
2011-03-18 11:29:54 +01:00
|
|
|
(*a)->ma_prev->ma_next = (*a)->ma_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
if ((*a)->ma_next == NULL)
|
|
|
|
va_rear = (*a)->ma_prev;
|
2010-09-21 14:22:38 +02:00
|
|
|
else
|
2011-03-18 11:29:54 +01:00
|
|
|
(*a)->ma_next->ma_prev = (*a)->ma_prev;
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_valid *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static int mthread_attr_valid(a)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_attr_t *a;
|
|
|
|
{
|
|
|
|
/* Check to see if attribute is on the list of valid attributes */
|
|
|
|
struct __mthread_attr *loopitem;
|
|
|
|
|
|
|
|
loopitem = va_front;
|
|
|
|
|
|
|
|
while (loopitem != NULL) {
|
|
|
|
if (loopitem == *a)
|
|
|
|
return(1);
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
loopitem = loopitem->ma_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_attr_verify *
|
|
|
|
*===========================================================================*/
|
|
|
|
#ifdef MDEBUG
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_attr_verify(void)
|
2010-09-21 14:22:38 +02:00
|
|
|
{
|
|
|
|
/* Return true when no attributes are in use */
|
|
|
|
struct __mthread_attr *loopitem;
|
|
|
|
|
|
|
|
loopitem = va_front;
|
|
|
|
|
|
|
|
while (loopitem != NULL) {
|
2011-03-18 11:29:54 +01:00
|
|
|
loopitem = loopitem->ma_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-14 13:48:43 +01:00
|
|
|
/* pthread compatibility layer. */
|
|
|
|
__weak_alias(pthread_attr_destroy, mthread_attr_destroy)
|
|
|
|
__weak_alias(pthread_attr_getdetachstate, mthread_attr_getdetachstate)
|
|
|
|
__weak_alias(pthread_attr_getstack, mthread_attr_getstack)
|
|
|
|
__weak_alias(pthread_attr_getstacksize, mthread_attr_getstacksize)
|
|
|
|
__weak_alias(pthread_attr_init, mthread_attr_init)
|
|
|
|
__weak_alias(pthread_attr_setdetachstate, mthread_attr_setdetachstate)
|
|
|
|
__weak_alias(pthread_attr_setstack, mthread_attr_setstack)
|
|
|
|
__weak_alias(pthread_attr_setstacksize, mthread_attr_setstacksize)
|
2010-09-21 14:22:38 +02:00
|
|
|
|