forked from Lephenixnoir/gint
99 lines
2.6 KiB
C
99 lines
2.6 KiB
C
//---
|
|
// gint:thread:attributes - Thread attribute helper
|
|
//---
|
|
#include <gint/thread.h>
|
|
#include <gint/std/string.h>
|
|
|
|
//---
|
|
// User interface
|
|
//---
|
|
/* thread_attr_init(): Initialize thread attribute object */
|
|
int thread_attr_init(thread_attr_t *attr)
|
|
{
|
|
if (attr == NULL)
|
|
return (-1);
|
|
thread_atomic_start();
|
|
memset(attr, 0x00, sizeof(struct thread_attr_s));
|
|
attr->state.detach = THREAD_ATTR_JOINABLE;
|
|
attr->state.longjmp = THREAD_ATTR_LONGJMP_DISABLE;
|
|
attr->private. watermark = THREAD_ATTR_WATERMARK;
|
|
thread_atomic_stop();
|
|
return (0);
|
|
}
|
|
|
|
/* thread_attr_setdetachstate() and thread_attr_getdetachstate()
|
|
|
|
The "thread_attr_setdetachstate()" function sets the detach state attribute
|
|
of the thread attributes object referred to by attr to the value specified
|
|
in detachstate.
|
|
|
|
The "thread_attr_getdetachstate()" returns the detach state attribute of
|
|
the thread attributes object attr in the buffer pointed to by detachstate.
|
|
*/
|
|
int thread_attr_setdetachstate(thread_attr_t *attr, int detachstate)
|
|
{
|
|
if (attr == NULL || attr->private.watermark != THREAD_ATTR_WATERMARK)
|
|
return (-1);
|
|
switch (detachstate) {
|
|
case THREAD_ATTR_JOINABLE:
|
|
case THREAD_ATTR_DETACHED:
|
|
break;
|
|
default:
|
|
return (-1);
|
|
}
|
|
thread_atomic_start();
|
|
attr->state.detach = detachstate;
|
|
thread_atomic_stop();
|
|
return (0);
|
|
}
|
|
int thread_attr_getdetachstate(thread_attr_t *attr, int *detachstate)
|
|
{
|
|
if (attr == NULL || attr->private.watermark != THREAD_ATTR_WATERMARK)
|
|
return (-1);
|
|
thread_atomic_start();
|
|
*detachstate = attr->state.detach;
|
|
thread_atomic_stop();
|
|
return (0);
|
|
}
|
|
|
|
|
|
/* thread_attr_enablelongjmp() thread_attr_getlongjmpstatus()
|
|
Enable / disable the custom longjmp feature */
|
|
int thread_attr_enablelongjmp(thread_attr_t *attr, int status)
|
|
{
|
|
if (attr == NULL || attr->private.watermark != THREAD_ATTR_WATERMARK)
|
|
return (-1);
|
|
switch (status) {
|
|
case THREAD_ATTR_LONGJMP_ENABLE:
|
|
case THREAD_ATTR_LONGJMP_DISABLE:
|
|
break;
|
|
default:
|
|
return (-1);
|
|
}
|
|
thread_atomic_start();
|
|
attr->state.longjmp = status;
|
|
thread_atomic_stop();
|
|
return (0);
|
|
}
|
|
int thread_attr_getlongjmpstatus(thread_attr_t *attr, int *status)
|
|
{
|
|
if (attr == NULL || attr->private.watermark != THREAD_ATTR_WATERMARK)
|
|
return (-1);
|
|
thread_atomic_start();
|
|
*status = attr->state.longjmp;
|
|
thread_atomic_stop();
|
|
return (0);
|
|
}
|
|
|
|
/* thread_attr_destroy(): Destroy thread attribute object */
|
|
int thread_attr_destroy(thread_attr_t *attr)
|
|
{
|
|
if (attr == NULL || attr->private.watermark != THREAD_ATTR_WATERMARK)
|
|
return (-1);
|
|
|
|
thread_atomic_start();
|
|
memset(attr, 0x00, sizeof(struct thread_attr_s));
|
|
thread_atomic_stop();
|
|
return (0);
|
|
}
|