ObjFW  Diff

Differences From Artifact [7ca1a9fd57]:

To Artifact [967081c426]:

  • File src/threading_pthread.m — part of check-in [52e02c06ca] at 2014-08-01 12:27:29 on branch trunk — Change return type for thread main

    This changes the return type to void, as the return type of a thread's
    main depends on the threading implementation used. For pthreads, it adds
    a wrapper function which returns NULL to avoid problems with bogus
    return values. For WinAPI threads, the function is just casted, as bogus
    return values don't seem to matter there. (user: js, size: 4486) [annotate] [blame] [check-ins using]


9
10
11
12
13
14
15


















16
17
18
19
20
21
22
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */



















bool
of_thread_attr_init(of_thread_attr_t *attr)
{
	pthread_attr_t pattr;

	if (pthread_attr_init(&pattr) != 0)







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







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

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);

	ctx->function(ctx->object);

	pthread_cleanup_pop(1);
	return NULL;
}

bool
of_thread_attr_init(of_thread_attr_t *attr)
{
	pthread_attr_t pattr;

	if (pthread_attr_init(&pattr) != 0)
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64


65
66
67
68
69
70
71
		pthread_attr_destroy(&pattr);
	}

	return true;
}

bool
of_thread_new(of_thread_t *thread, id (*function)(id), id data,
    const of_thread_attr_t *attr)
{
	bool ret;
	pthread_attr_t pattr;

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

	@try {


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

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








|









>
>







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
		pthread_attr_destroy(&pattr);
	}

	return true;
}

bool
of_thread_new(of_thread_t *thread, void (*function)(id), id object,
    const of_thread_attr_t *attr)
{
	bool ret;
	pthread_attr_t pattr;

	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;

85
86
87
88
89
90
91
92






93
94
95
96
97
98
99
100
101
			if (pthread_attr_setschedparam(&pattr, &param) != 0)
				return false;

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







		ret = (pthread_create(thread, &pattr,
		    (void*(*)(void*))function, (__bridge void*)data) == 0);
	} @finally {
		pthread_attr_destroy(&pattr);
	}

	return ret;
}









>
>
>
>
>
>

|







105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
			if (pthread_attr_setschedparam(&pattr, &param) != 0)
				return false;

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

		if ((ctx = malloc(sizeof(*ctx))) == NULL)
			return false;

		ctx->function = function;
		ctx->object = object;

		ret = (pthread_create(thread, &pattr,
		    function_wrapper, ctx) == 0);
	} @finally {
		pthread_attr_destroy(&pattr);
	}

	return ret;
}