ObjFW  Diff

Differences From Artifact [706256fda3]:

To Artifact [22a0bb4722]:


22
23
24
25
26
27
28


29
30
31
32






















33
34
35
36
37
38
39

#import "macros.h"

#ifdef OF_HAIKU
# include <kernel/OS.h>
#endif



struct thread_ctx {
	void (*function)(id object);
	id object;
};























static void*
function_wrapper(void *data)
{
	struct thread_ctx *ctx = data;

	pthread_cleanup_push(free, data);







>
>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#import "macros.h"

#ifdef OF_HAIKU
# include <kernel/OS.h>
#endif

static int minPrio, maxPrio, normalPrio;

struct thread_ctx {
	void (*function)(id object);
	id object;
};

/*
 * This is done here to make sure this is done as early as possible in the main
 * thread.
 */
static void __attribute__((constructor))
init(void)
{
	pthread_attr_t pattr;
	int policy;
	struct sched_param param;

	OF_ENSURE(pthread_attr_init(&pattr) == 0);
	OF_ENSURE(pthread_attr_getschedpolicy(&pattr, &policy) == 0);
	OF_ENSURE((minPrio = sched_get_priority_min(policy)) != -1);
	OF_ENSURE((maxPrio = sched_get_priority_max(policy)) != -1);
	OF_ENSURE(pthread_attr_getschedparam(&pattr, &param) == 0);

	normalPrio = param.sched_priority;

	pthread_attr_destroy(&pattr);
}

static void*
function_wrapper(void *data)
{
	struct thread_ctx *ctx = data;

	pthread_cleanup_push(free, data);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{
	pthread_attr_t pattr;

	if (pthread_attr_init(&pattr) != 0)
		return false;

	@try {
		int policy, minPrio, maxPrio;
		struct sched_param param;

		if (pthread_attr_getschedpolicy(&pattr, &policy) != 0)
			return false;

		minPrio = sched_get_priority_min(policy);
		maxPrio = sched_get_priority_max(policy);

		if (pthread_attr_getschedparam(&pattr, &param) != 0)
			return false;

		/* Prevent possible division by zero */
		if (minPrio != maxPrio)
			attr->priority = (float)(param.sched_priority -
			    minPrio) / (maxPrio - minPrio);
		else
			attr->priority = 0;

		if (pthread_attr_getstacksize(&pattr, &attr->stackSize) != 0)
			return false;
	} @finally {
		pthread_attr_destroy(&pattr);
	}








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|







73
74
75
76
77
78
79

















80
81
82
83
84
85
86
87
{
	pthread_attr_t pattr;

	if (pthread_attr_init(&pattr) != 0)
		return false;

	@try {

















		attr->priority = 0;

		if (pthread_attr_getstacksize(&pattr, &attr->stackSize) != 0)
			return false;
	} @finally {
		pthread_attr_destroy(&pattr);
	}

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118








119
120
121
122
123
124
125
	if (pthread_attr_init(&pattr) != 0)
		return false;

	@try {
		struct thread_ctx *ctx;

		if (attr != NULL) {
			int policy, minPrio, maxPrio;
			struct sched_param param;

			if (attr->priority < 0 || attr->priority > 1)
				return false;

			if (pthread_attr_getschedpolicy(&pattr, &policy) != 0)
				return false;

			minPrio = sched_get_priority_min(policy);
			maxPrio = sched_get_priority_max(policy);

			param.sched_priority = (float)minPrio +
			    attr->priority * (maxPrio - minPrio);

#ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
			if (pthread_attr_setinheritsched(&pattr,
			    PTHREAD_EXPLICIT_SCHED) != 0)
				return false;
#endif









			if (pthread_attr_setschedparam(&pattr, &param) != 0)
				return false;

			if (attr->stackSize > 0) {
				if (pthread_attr_setstacksize(&pattr,
				    attr->stackSize) != 0)
					return false;







<


|

<
<
<
<
<
<
<
<
<







>
>
>
>
>
>
>
>







98
99
100
101
102
103
104

105
106
107
108









109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
	if (pthread_attr_init(&pattr) != 0)
		return false;

	@try {
		struct thread_ctx *ctx;

		if (attr != NULL) {

			struct sched_param param;

			if (attr->priority < -1 || attr->priority > 1)
				return false;










#ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
			if (pthread_attr_setinheritsched(&pattr,
			    PTHREAD_EXPLICIT_SCHED) != 0)
				return false;
#endif

			if (attr->priority < 0) {
				param.sched_priority = minPrio +
				    (1.0 + attr->priority) *
				    (normalPrio - minPrio);
			} else
				param.sched_priority = normalPrio +
				    attr->priority * (maxPrio - normalPrio);

			if (pthread_attr_setschedparam(&pattr, &param) != 0)
				return false;

			if (attr->stackSize > 0) {
				if (pthread_attr_setstacksize(&pattr,
				    attr->stackSize) != 0)
					return false;