From 3ad63ce57d91ea8537f91dac49ddabb243c5b2ad Mon Sep 17 00:00:00 2001 From: hmelder Date: Fri, 25 Oct 2024 21:06:53 +0200 Subject: [PATCH 1/6] NSThread: Implement +detatchThreadWithBlock: and -initWithBlock: --- Headers/Foundation/NSThread.h | 29 ++++++++++++++++++++++++++++ Source/NSThread.m | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Headers/Foundation/NSThread.h b/Headers/Foundation/NSThread.h index 6f003e2b6..da8c5fedb 100644 --- a/Headers/Foundation/NSThread.h +++ b/Headers/Foundation/NSThread.h @@ -43,6 +43,11 @@ extern "C" { #endif +#if OS_API_VERSION(MAC_OS_X_VERSION_10_12, GS_API_LATEST) +#import +DEFINE_BLOCK_TYPE(GSThreadBlock, void, void); +#endif + /** * This class encapsulates OpenStep threading. See [NSLock] and its * subclasses for handling synchronisation between threads.
@@ -67,6 +72,7 @@ GS_EXPORT_CLASS BOOL _cancelled; BOOL _active; BOOL _finished; + BOOL _targetIsBlock; NSHandler *_exception_handler; // Not retained. NSMutableDictionary *_thread_dictionary; struct autorelease_thread_vars _autorelease_vars; @@ -398,6 +404,29 @@ GS_EXPORT void GSUnregisterCurrentThread (void); #endif +/** + * This category contains convenience + * initialisers and methods for executing + * blocks in different threads and creating + * NSThread objects with a block as entry point. + */ +@interface NSThread (BlockAdditions) + +#if OS_API_VERSION(MAC_OS_X_VERSION_10_12, GS_API_LATEST) +/** + * Detaches a new thread with block as its entry point. + */ ++ (void)detachNewThreadWithBlock: (GSThreadBlock)block; + +/** + * Initialises a NSThread object with block as the + * entry point. + */ +- (instancetype)initWithBlock: (GSThreadBlock)block; +#endif // OS_API_VERSION + +@end + /* * Notification Strings. * NSBecomingMultiThreaded and NSThreadExiting are defined for strict diff --git a/Source/NSThread.m b/Source/NSThread.m index f3ec8d720..af7067f71 100644 --- a/Source/NSThread.m +++ b/Source/NSThread.m @@ -30,6 +30,7 @@ NSThread class reference */ +#include "GNUstepBase/GSBlocks.h" #import "common.h" #import "GSPThread.h" @@ -1337,7 +1338,15 @@ - (void) main NSStringFromSelector(_cmd)]; } - [_target performSelector: _selector withObject: _arg]; + if (_targetIsBlock) + { + GSThreadBlock block = (GSThreadBlock)_target; + CALL_BLOCK_NO_ARGS(block); + } + else + { + [_target performSelector: _selector withObject: _arg]; + } } - (NSString*) name @@ -2414,6 +2423,31 @@ - (void) performSelectorInBackground: (SEL)aSelector @end +@implementation NSThread (BlockAdditions) + ++ (void)detachNewThreadWithBlock: (GSThreadBlock)block +{ + NSThread *thread; + + thread = [[NSThread alloc] initWithBlock: block]; + [thread start]; + + RELEASE(thread); +} + +- (instancetype)initWithBlock: (GSThreadBlock)block +{ + if (nil != (self = [self init])) + { + _targetIsBlock = YES; + /* Copy block to heap */ + _target = _Block_copy(block); + } + return self; +} + +@end + /** *

* This function is provided to let threads started by some other From 27b5946f1f3479d8f4df1a52a18cf6b71fa5c712 Mon Sep 17 00:00:00 2001 From: hmelder Date: Fri, 25 Oct 2024 21:13:15 +0200 Subject: [PATCH 2/6] Remove extraneous include --- Source/NSThread.m | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/NSThread.m b/Source/NSThread.m index af7067f71..5a4776d07 100644 --- a/Source/NSThread.m +++ b/Source/NSThread.m @@ -30,7 +30,6 @@ NSThread class reference */ -#include "GNUstepBase/GSBlocks.h" #import "common.h" #import "GSPThread.h" From ea3112442603fb14003e04ef5d8223be8c721d71 Mon Sep 17 00:00:00 2001 From: hmelder Date: Fri, 25 Oct 2024 21:16:28 +0200 Subject: [PATCH 3/6] NSThread: Define GSThreadBlock so that GCC stub impl does not fail --- Headers/Foundation/NSThread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Headers/Foundation/NSThread.h b/Headers/Foundation/NSThread.h index da8c5fedb..5e47601e9 100644 --- a/Headers/Foundation/NSThread.h +++ b/Headers/Foundation/NSThread.h @@ -45,7 +45,7 @@ extern "C" { #if OS_API_VERSION(MAC_OS_X_VERSION_10_12, GS_API_LATEST) #import -DEFINE_BLOCK_TYPE(GSThreadBlock, void, void); +DEFINE_BLOCK_TYPE_NO_ARGS(GSThreadBlock, void); #endif /** From ca3a343c356de724abae6ba7d9459c0471cedcd8 Mon Sep 17 00:00:00 2001 From: hmelder Date: Fri, 25 Oct 2024 21:17:46 +0200 Subject: [PATCH 4/6] Fix indentation --- Headers/Foundation/NSThread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Headers/Foundation/NSThread.h b/Headers/Foundation/NSThread.h index 5e47601e9..8d47018ab 100644 --- a/Headers/Foundation/NSThread.h +++ b/Headers/Foundation/NSThread.h @@ -72,7 +72,7 @@ GS_EXPORT_CLASS BOOL _cancelled; BOOL _active; BOOL _finished; - BOOL _targetIsBlock; + BOOL _targetIsBlock; NSHandler *_exception_handler; // Not retained. NSMutableDictionary *_thread_dictionary; struct autorelease_thread_vars _autorelease_vars; From 2604992adcfcb18ebf67d2d5f47b82d0d9f6977c Mon Sep 17 00:00:00 2001 From: hmelder Date: Fri, 25 Oct 2024 21:18:16 +0200 Subject: [PATCH 5/6] Fix indentation --- Headers/Foundation/NSThread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Headers/Foundation/NSThread.h b/Headers/Foundation/NSThread.h index 8d47018ab..48d8f6121 100644 --- a/Headers/Foundation/NSThread.h +++ b/Headers/Foundation/NSThread.h @@ -72,7 +72,7 @@ GS_EXPORT_CLASS BOOL _cancelled; BOOL _active; BOOL _finished; - BOOL _targetIsBlock; + BOOL _targetIsBlock; NSHandler *_exception_handler; // Not retained. NSMutableDictionary *_thread_dictionary; struct autorelease_thread_vars _autorelease_vars; From 2c55f76e1901fae3cbde710f7321731a8109f873 Mon Sep 17 00:00:00 2001 From: rfm Date: Wed, 30 Oct 2024 08:34:40 +0000 Subject: [PATCH 6/6] add missing spaces - trivial style fixup --- Headers/Foundation/NSThread.h | 4 ++-- Source/NSThread.m | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Headers/Foundation/NSThread.h b/Headers/Foundation/NSThread.h index 48d8f6121..498d78592 100644 --- a/Headers/Foundation/NSThread.h +++ b/Headers/Foundation/NSThread.h @@ -416,13 +416,13 @@ GS_EXPORT void GSUnregisterCurrentThread (void); /** * Detaches a new thread with block as its entry point. */ -+ (void)detachNewThreadWithBlock: (GSThreadBlock)block; ++ (void) detachNewThreadWithBlock: (GSThreadBlock)block; /** * Initialises a NSThread object with block as the * entry point. */ -- (instancetype)initWithBlock: (GSThreadBlock)block; +- (instancetype) initWithBlock: (GSThreadBlock)block; #endif // OS_API_VERSION @end diff --git a/Source/NSThread.m b/Source/NSThread.m index 5a4776d07..860e63876 100644 --- a/Source/NSThread.m +++ b/Source/NSThread.m @@ -2424,7 +2424,7 @@ - (void) performSelectorInBackground: (SEL)aSelector @implementation NSThread (BlockAdditions) -+ (void)detachNewThreadWithBlock: (GSThreadBlock)block ++ (void) detachNewThreadWithBlock: (GSThreadBlock)block { NSThread *thread; @@ -2434,7 +2434,7 @@ + (void)detachNewThreadWithBlock: (GSThreadBlock)block RELEASE(thread); } -- (instancetype)initWithBlock: (GSThreadBlock)block +- (instancetype) initWithBlock: (GSThreadBlock)block { if (nil != (self = [self init])) {