xref: /haiku/src/system/libroot/posix/pthread/pthread_attr.c (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
1 /*
2 ** Copyright 2006, Jérôme Duval. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
5 
6 
7 #include <pthread.h>
8 #include "pthread_private.h"
9 
10 #include <stdlib.h>
11 
12 
13 int
14 pthread_attr_init(pthread_attr_t *_attr)
15 {
16 	pthread_attr *attr;
17 
18 	if (_attr == NULL)
19 		return B_BAD_VALUE;
20 
21 	attr = (pthread_attr *)malloc(sizeof(pthread_attr));
22 	if (attr == NULL)
23 		return B_NO_MEMORY;
24 
25 	attr->detach_state = PTHREAD_CREATE_JOINABLE;
26 	attr->sched_priority = B_NORMAL_PRIORITY;
27 
28 	*_attr = attr;
29 	return B_OK;
30 }
31 
32 
33 int
34 pthread_attr_destroy(pthread_attr_t *_attr)
35 {
36 	pthread_attr *attr;
37 
38 	if (_attr == NULL || (attr = *_attr) == NULL)
39 		return B_BAD_VALUE;
40 
41 	*_attr = NULL;
42 	free(attr);
43 
44 	return B_OK;
45 }
46 
47 
48 int
49 pthread_attr_getdetachstate(const pthread_attr_t *_attr, int *state)
50 {
51 	pthread_attr *attr;
52 
53 	if (_attr == NULL || (attr = *_attr) == NULL || state == NULL)
54 		return B_BAD_VALUE;
55 
56 	*state = attr->detach_state;
57 
58 	return B_OK;
59 }
60 
61 
62 int
63 pthread_attr_setdetachstate(pthread_attr_t *_attr, int state)
64 {
65 	pthread_attr *attr;
66 
67 	if (_attr == NULL || (attr = *_attr) == NULL)
68 		return B_BAD_VALUE;
69 
70 	if (state != PTHREAD_CREATE_JOINABLE && state != PTHREAD_CREATE_DETACHED)
71 		return B_BAD_VALUE;
72 
73 	attr->detach_state = state;
74 
75 	return B_OK;
76 }
77 
78