Use the pta_magic field in pthread attribute

Set PT_ATTR_DEAD on pthread_attr_destroy().
Check pta_magic before using pthread_attr_t in a bunch of other functions.
This commit is contained in:
kamil 2020-01-29 13:47:31 +00:00
parent 8265fdce60
commit b3401c13c3
1 changed files with 74 additions and 2 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: pthread_attr.c,v 1.18 2017/08/01 12:31:45 martin Exp $ */
/* $NetBSD: pthread_attr.c,v 1.19 2020/01/29 13:47:31 kamil Exp $ */
/*-
* Copyright (c) 2001, 2002, 2003, 2008 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: pthread_attr.c,v 1.18 2017/08/01 12:31:45 martin Exp $");
__RCSID("$NetBSD: pthread_attr.c,v 1.19 2020/01/29 13:47:31 kamil Exp $");
#include <errno.h>
#include <stdio.h>
@ -86,9 +86,14 @@ pthread_attr_destroy(pthread_attr_t *attr)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if ((p = attr->pta_private) != NULL)
free(p);
attr->pta_magic = PT_ATTR_DEAD;
return 0;
}
@ -98,6 +103,9 @@ pthread_attr_get_np(pthread_t thread, pthread_attr_t *attr)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
p = pthread__attr_init_private(attr);
if (p == NULL)
return ENOMEM;
@ -117,6 +125,9 @@ int
pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
{
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if (attr->pta_flags & PT_FLAG_DETACHED)
*detachstate = PTHREAD_CREATE_DETACHED;
else
@ -130,6 +141,9 @@ int
pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
{
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
switch (detachstate) {
case PTHREAD_CREATE_JOINABLE:
attr->pta_flags &= ~PT_FLAG_DETACHED;
@ -150,6 +164,9 @@ pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guard)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if ((p = attr->pta_private) == NULL)
*guard = pthread__guardsize;
else
@ -164,6 +181,9 @@ pthread_attr_setguardsize(pthread_attr_t *attr, size_t guard)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
p = pthread__attr_init_private(attr);
if (p == NULL)
return ENOMEM;
@ -178,6 +198,9 @@ int
pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
{
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if (attr->pta_flags & PT_FLAG_EXPLICIT_SCHED)
*inherit = PTHREAD_EXPLICIT_SCHED;
else
@ -191,6 +214,9 @@ int
pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
{
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
switch (inherit) {
case PTHREAD_INHERIT_SCHED:
attr->pta_flags &= ~PT_FLAG_EXPLICIT_SCHED;
@ -210,6 +236,9 @@ int
pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
{
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if (attr->pta_flags & PT_FLAG_SCOPE_SYSTEM)
*scope = PTHREAD_SCOPE_SYSTEM;
else
@ -223,6 +252,9 @@ int
pthread_attr_setscope(pthread_attr_t *attr, int scope)
{
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
switch (scope) {
case PTHREAD_SCOPE_PROCESS:
attr->pta_flags &= ~PT_FLAG_SCOPE_SYSTEM;
@ -245,6 +277,9 @@ pthread_attr_setschedparam(pthread_attr_t *attr,
struct pthread_attr_private *p;
int error;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if (param == NULL)
return EINVAL;
p = pthread__attr_init_private(attr);
@ -263,6 +298,9 @@ pthread_attr_getschedparam(const pthread_attr_t *attr,
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if (param == NULL)
return EINVAL;
p = attr->pta_private;
@ -279,6 +317,8 @@ pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
switch (policy) {
case SCHED_OTHER:
@ -300,6 +340,9 @@ pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
p = attr->pta_private;
if (p == NULL) {
*policy = SCHED_OTHER;
@ -315,6 +358,9 @@ pthread_attr_getstack(const pthread_attr_t *attr, void **addr, size_t *size)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if ((p = attr->pta_private) == NULL) {
*addr = NULL;
*size = pthread__stacksize;
@ -332,6 +378,9 @@ pthread_attr_setstack(pthread_attr_t *attr, void *addr, size_t size)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
p = pthread__attr_init_private(attr);
if (p == NULL)
return ENOMEM;
@ -348,6 +397,9 @@ pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *size)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if ((p = attr->pta_private) == NULL)
*size = pthread__stacksize;
else
@ -362,6 +414,9 @@ pthread_attr_setstacksize(pthread_attr_t *attr, size_t size)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if (size < (size_t)sysconf(_SC_THREAD_STACK_MIN))
return EINVAL;
@ -380,6 +435,9 @@ pthread_attr_getstackaddr(const pthread_attr_t *attr, void **addr)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if ((p = attr->pta_private) == NULL)
*addr = NULL;
else
@ -394,6 +452,9 @@ pthread_attr_setstackaddr(pthread_attr_t *attr, void *addr)
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
p = pthread__attr_init_private(attr);
if (p == NULL)
return ENOMEM;
@ -410,6 +471,9 @@ pthread_attr_getname_np(const pthread_attr_t *attr, char *name, size_t len,
{
struct pthread_attr_private *p;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
if ((p = attr->pta_private) == NULL) {
name[0] = '\0';
if (argp != NULL)
@ -430,6 +494,9 @@ pthread_attr_setname_np(pthread_attr_t *attr, const char *name, void *arg)
struct pthread_attr_private *p;
int namelen;
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
p = pthread__attr_init_private(attr);
if (p == NULL)
return ENOMEM;
@ -447,6 +514,10 @@ pthread_attr_setname_np(pthread_attr_t *attr, const char *name, void *arg)
int
pthread_attr_setcreatesuspend_np(pthread_attr_t *attr)
{
pthread__error(EINVAL, "Invalid attribute",
attr->pta_magic == PT_ATTR_MAGIC);
attr->pta_flags |= PT_FLAG_SUSPENDED;
return 0;
}
@ -455,6 +526,7 @@ int
pthread_getattr_np(pthread_t thread, pthread_attr_t *attr)
{
int error;
if ((error = pthread_attr_init(attr)) != 0)
return error;
if ((error = pthread_attr_get_np(thread, attr)) != 0) {