Skip to content

Commit

Permalink
NSThread: Implement +detatchThreadWithBlock: and -initWithBlock: (#454)
Browse files Browse the repository at this point in the history
* NSThread: Implement +detatchThreadWithBlock: and -initWithBlock:

* Remove extraneous include

* NSThread: Define GSThreadBlock so that GCC stub impl does not fail

* Add missing spaces - trivial style fixup

---------

Co-authored-by: rfm <[email protected]>
  • Loading branch information
hmelder and rfm authored Oct 30, 2024
1 parent cfc28d8 commit fd5ec29
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Headers/Foundation/NSThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
extern "C" {
#endif

#if OS_API_VERSION(MAC_OS_X_VERSION_10_12, GS_API_LATEST)
#import <GNUstepBase/GSBlocks.h>
DEFINE_BLOCK_TYPE_NO_ARGS(GSThreadBlock, void);
#endif

/**
* This class encapsulates OpenStep threading. See [NSLock] and its
* subclasses for handling synchronisation between threads.<br />
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion Source/NSThread.m
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,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
Expand Down Expand Up @@ -2414,6 +2422,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

/**
* <p>
* This function is provided to let threads started by some other
Expand Down

0 comments on commit fd5ec29

Please sign in to comment.