ObjFW  Diff

Differences From Artifact [e8b813b448]:

To Artifact [16b08dae34]:

  • File src/thread_pthread.m — part of check-in [77780c7596] at 2019-09-01 15:29:31 on branch trunk — OFThread: Allow specifying a name before starting

    This allows specifying a name before the thread gets started, so that
    the name can be decided by whoever starts the thread, rather than just
    by the thread itself once it's running.

    This is especially useful as some operating systems do not support
    changing the name of the thread once it's running. (user: js, size: 4075) [annotate] [blame] [check-ins using]


28
29
30
31
32
33
34

35
36
37
38
39
40
41
#import "macros.h"

static int minPrio = 0, maxPrio = 0, normalPrio = 0;

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.
 */
OF_CONSTRUCTOR()







>







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#import "macros.h"

static int minPrio = 0, maxPrio = 0, normalPrio = 0;

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

/*
 * This is done here to make sure this is done as early as possible in the main
 * thread.
 */
OF_CONSTRUCTOR()
68
69
70
71
72
73
74



75
76
77
78
79
80
81
}

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




	pthread_cleanup_push(free, data);

	ctx->function(ctx->object);

	pthread_cleanup_pop(1);
	return NULL;
}







>
>
>







69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
}

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

	if (ctx->name != NULL)
		of_thread_set_name(ctx->name);

	pthread_cleanup_push(free, data);

	ctx->function(ctx->object);

	pthread_cleanup_pop(1);
	return NULL;
}
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
		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;








|
|







101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
		pthread_attr_destroy(&pattr);
	}

	return true;
}

bool
of_thread_new(of_thread_t *thread, const char *name, 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;

148
149
150
151
152
153
154

155
156
157
158
159
160
161
		if ((ctx = malloc(sizeof(*ctx))) == NULL) {
			errno = ENOMEM;
			return false;
		}

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


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








>







152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
		if ((ctx = malloc(sizeof(*ctx))) == NULL) {
			errno = ENOMEM;
			return false;
		}

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

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