summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Schleifer <js@heap.zone>2019-09-01 17:29:31 +0200
committerJonathan Schleifer <js@heap.zone>2019-09-01 17:29:31 +0200
commit3d48375ec2f7e044fae1bec5a3ed27b95792339b (patch)
tree0ac76a96636ba8f7e14681b3535ffd1287d825f0
parentdfd7a1e97a13793d5c7b9d0439385dde66b5cc48 (diff)
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.
-rw-r--r--src/OFThread.m4
-rw-r--r--src/thread.h4
-rw-r--r--src/thread_amiga.m6
-rw-r--r--src/thread_pthread.m9
-rw-r--r--src/thread_winapi.m4
5 files changed, 18 insertions, 9 deletions
diff --git a/src/OFThread.m b/src/OFThread.m
index b3dac55b..997bc83c 100644
--- a/src/OFThread.m
+++ b/src/OFThread.m
@@ -415,7 +415,9 @@ static OFDNSResolver *DNSResolver;
_running = OF_THREAD_RUNNING;
- if (!of_thread_new(&_thread, callMain, self, &_attr)) {
+ if (!of_thread_new(&_thread,
+ [_name cStringWithEncoding: [OFLocale encoding]], callMain, self,
+ &_attr)) {
[self release];
@throw [OFThreadStartFailedException
exceptionWithThread: self
diff --git a/src/thread.h b/src/thread.h
index ce140c16..c9de585d 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -66,8 +66,8 @@ extern of_thread_t of_thread_current(void);
extern "C" {
#endif
extern bool of_thread_attr_init(of_thread_attr_t *attr);
-extern bool of_thread_new(of_thread_t *thread, void (*function)(id), id object,
- const of_thread_attr_t *attr);
+extern bool of_thread_new(of_thread_t *thread, const char *name,
+ void (*function)(id), id object, const of_thread_attr_t *attr);
extern void of_thread_set_name(const char *name);
extern bool of_thread_join(of_thread_t thread);
extern bool of_thread_detach(of_thread_t thread);
diff --git a/src/thread_amiga.m b/src/thread_amiga.m
index 06f99b5f..1608c275 100644
--- a/src/thread_amiga.m
+++ b/src/thread_amiga.m
@@ -72,8 +72,8 @@ of_thread_attr_init(of_thread_attr_t *attr)
}
bool
-of_thread_new(of_thread_t *thread, void (*function)(id), id object,
- const of_thread_attr_t *attr)
+of_thread_new(of_thread_t *thread, const char *name, void (*function)(id),
+ id object, const of_thread_attr_t *attr)
{
OFMutableData *tags = nil;
@@ -106,6 +106,8 @@ of_thread_new(of_thread_t *thread, void (*function)(id), id object,
#ifdef OF_MORPHOS
ADD_TAG(NP_CodeType, CODETYPE_PPC);
#endif
+ if (name != NULL)
+ ADD_TAG(NP_Name, (ULONG)name);
ADD_TAG(NP_Input, ((struct Process *)FindTask(NULL))->pr_CIS)
ADD_TAG(NP_Output, ((struct Process *)FindTask(NULL))->pr_COS)
diff --git a/src/thread_pthread.m b/src/thread_pthread.m
index 4dfa546b..94d6e392 100644
--- a/src/thread_pthread.m
+++ b/src/thread_pthread.m
@@ -32,6 +32,7 @@ static int minPrio = 0, maxPrio = 0, normalPrio = 0;
struct thread_ctx {
void (*function)(id object);
id object;
+ const char *name;
};
/*
@@ -72,6 +73,9 @@ 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);
@@ -101,8 +105,8 @@ of_thread_attr_init(of_thread_attr_t *attr)
}
bool
-of_thread_new(of_thread_t *thread, void (*function)(id), id object,
- const of_thread_attr_t *attr)
+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;
@@ -152,6 +156,7 @@ of_thread_new(of_thread_t *thread, void (*function)(id), id object,
ctx->function = function;
ctx->object = object;
+ ctx->name = name;
ret = (pthread_create(thread, &pattr,
functionWrapper, ctx) == 0);
diff --git a/src/thread_winapi.m b/src/thread_winapi.m
index 08ba2c19..05d5ff25 100644
--- a/src/thread_winapi.m
+++ b/src/thread_winapi.m
@@ -29,8 +29,8 @@ of_thread_attr_init(of_thread_attr_t *attr)
}
bool
-of_thread_new(of_thread_t *thread, void (*function)(id), id object,
- const of_thread_attr_t *attr)
+of_thread_new(of_thread_t *thread, const char *name, void (*function)(id),
+ id object, const of_thread_attr_t *attr)
{
*thread = CreateThread(NULL, (attr != NULL ? attr->stackSize : 0),
(LPTHREAD_START_ROUTINE)function, (void *)object, 0, NULL);