Skip to content

Commit 1575d60

Browse files
committed
mprove separation of system dependent code form main run loop code
1 parent 4d43825 commit 1575d60

6 files changed

Lines changed: 55 additions & 38 deletions

File tree

Headers/Foundation/NSRunLoop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ GS_EXPORT_CLASS
5252
NSMapTable *_contextMap;
5353
NSMutableArray *_contextStack;
5454
NSMutableArray *_timedPerformers;
55-
void *_extra;
55+
void *_internal;
5656
#endif
5757
}
5858

Source/GSRunLoopCtxt.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,13 @@
4040

4141
#include "GNUstepBase/GSIArray.h"
4242

43-
#ifdef HAVE_POLL
44-
typedef struct{
45-
int limit;
46-
short *index;
47-
}pollextra;
48-
#endif
49-
5043
@class NSString;
5144
@class GSRunLoopWatcher;
5245

5346
@interface GSRunLoopCtxt : NSObject
5447
{
5548
@public
56-
void *extra; /** Copy of the RunLoop ivar. */
49+
void *extra; /** Common data for contexts in a loop */
5750
NSString *mode; /** The mode for this context. */
5851
GSIArray performers; /** The actions to perform regularly. */
5952
unsigned maxPerformers;
@@ -84,7 +77,7 @@ typedef struct{
8477
for: (GSRunLoopWatcher*)watcher;
8578

8679
- (void) endPoll;
87-
- (id) initWithMode: (NSString*)theMode extra: (void*)e;
80+
- (id) initWithMode: (NSString*)theMode extra: (void**)e;
8881
- (BOOL) pollUntil: (int)milliseconds within: (NSArray*)contexts;
8982

9083
/* Callbacks for a map table whose values are watchers.

Source/GSRunLoopCtxt.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ - (void) endPoll
130130
completed = YES;
131131
}
132132

133-
- (id) initWithMode: (NSString*)theMode extra: (void*)e
133+
- (id) initWithMode: (NSString*)theMode extra: (void**)e
134134
{
135135
self = [super init];
136136
if (self != nil)
137137
{
138138
NSZone *z;
139139

140140
mode = [theMode copy];
141-
extra = e;
141+
extra = *e;
142142
z = [self zone];
143143
timers = [[GSMinHeap alloc] initWithCapacity: 100 andComparator: NULL];
144144
performers = NSZoneMalloc(z, sizeof(GSIArray_t));

Source/NSRunLoop.m

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@
5555
#ifdef HAVE_SYS_TIME_H
5656
#include <sys/time.h>
5757
#endif
58-
#ifdef HAVE_POLL_F
59-
#include <poll.h>
60-
#endif
6158
#include <math.h>
6259
#include <time.h>
6360

@@ -72,10 +69,6 @@
7269

7370
static NSDate *theFuture = nil;
7471

75-
@interface NSObject (OptionalPortRunLoop)
76-
- (void) getFds: (NSInteger*)fds count: (NSInteger*)count;
77-
@end
78-
7972

8073

8174
/*
@@ -436,6 +429,13 @@ - (void) receivedEvent: (void*)data
436429

437430
#endif
438431

432+
typedef struct {
433+
void *sharedContextInfo;
434+
} RunLoopInternal;
435+
436+
#define internal ((RunLoopInternal*)_internal)
437+
438+
439439
@interface NSRunLoop (Private)
440440

441441
- (void) _addWatcher: (GSRunLoopWatcher*)item
@@ -464,7 +464,8 @@ - (void) _addWatcher: (GSRunLoopWatcher*) item forMode: (NSString*)mode
464464
context = NSMapGet(_contextMap, mode);
465465
if (context == nil)
466466
{
467-
context = [[GSRunLoopCtxt alloc] initWithMode: mode extra: _extra];
467+
context = [[GSRunLoopCtxt alloc] initWithMode: mode
468+
extra: &internal->sharedContextInfo];
468469
NSMapInsert(_contextMap, context->mode, context);
469470
RELEASE(context);
470471
}
@@ -594,17 +595,14 @@ - (GSRunLoopWatcher*) _getWatcher: (void*)data
594595

595596
- (id) _init
596597
{
597-
self = [super init];
598-
if (self != nil)
598+
if (nil != (self = [super init]))
599599
{
600600
_contextStack = [NSMutableArray new];
601601
_contextMap = NSCreateMapTable (NSNonRetainedObjectMapKeyCallBacks,
602602
NSObjectMapValueCallBacks, 0);
603603
_timedPerformers = [[NSMutableArray alloc] initWithCapacity: 8];
604-
#ifdef HAVE_POLL_F
605-
_extra = NSZoneMalloc(NSDefaultMallocZone(), sizeof(pollextra));
606-
memset(_extra, '\0', sizeof(pollextra));
607-
#endif
604+
_internal = (RunLoopInternal*)
605+
NSZoneCalloc(NSDefaultMallocZone(), 1, sizeof(RunLoopInternal));
608606
}
609607
return self;
610608
}
@@ -850,15 +848,11 @@ - (id) init
850848

851849
- (void) dealloc
852850
{
853-
#ifdef HAVE_POLL_F
854-
if (_extra != 0)
855-
{
856-
pollextra *e = (pollextra*)_extra;
857-
if (e->index != 0)
858-
NSZoneFree(NSDefaultMallocZone(), e->index);
859-
NSZoneFree(NSDefaultMallocZone(), e);
851+
if (internal)
852+
{
853+
NSZoneFree(NSDefaultMallocZone(), internal);
854+
_internal = NULL;
860855
}
861-
#endif
862856
RELEASE(_contextStack);
863857
if (_contextMap != 0)
864858
{
@@ -924,7 +918,8 @@ - (void) addTimer: (NSTimer*)timer
924918
context = NSMapGet(_contextMap, mode);
925919
if (context == nil)
926920
{
927-
context = [[GSRunLoopCtxt alloc] initWithMode: mode extra: _extra];
921+
context = [[GSRunLoopCtxt alloc] initWithMode: mode
922+
extra: &internal->sharedContextInfo];
928923
NSMapInsert(_contextMap, context->mode, context);
929924
RELEASE(context);
930925
}
@@ -1550,7 +1545,7 @@ - (void) performSelector: (SEL)aSelector
15501545
if (context == nil)
15511546
{
15521547
context = [[GSRunLoopCtxt alloc] initWithMode: mode
1553-
extra: _extra];
1548+
extra: &internal->sharedContextInfo];
15541549
NSMapInsert(_contextMap, context->mode, context);
15551550
RELEASE(context);
15561551
}

Source/unix/GSRunLoopCtxt.m

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
#include <poll.h>
2525
#endif
2626

27+
#ifdef HAVE_POLL
28+
typedef struct {
29+
int limit;
30+
short *index;
31+
unsigned refcount;
32+
} pollextra;
33+
#endif
34+
2735
#define FDCOUNT 1024
2836

2937
@interface GSRunLoopCtxtUnix: GSRunLoopCtxt
@@ -60,6 +68,19 @@ - (void) dealloc
6068
{
6169
NSZoneFree(NSDefaultMallocZone(), pollfds);
6270
}
71+
if (extra)
72+
{
73+
pollextra *pe = (pollextra*)extra;
74+
75+
if (--pe->refcount == 0)
76+
{
77+
if (pe->index != 0)
78+
{
79+
NSZoneFree(NSDefaultMallocZone(), pe->index);
80+
}
81+
NSZoneFree(NSDefaultMallocZone(), pe);
82+
}
83+
}
6384
#endif
6485
DEALLOC
6586
}
@@ -112,7 +133,7 @@ - (void) endEvent: (void*)data
112133
}
113134
}
114135

115-
- (id) initWithMode: (NSString*)theMode extra: (void*)e
136+
- (id) initWithMode: (NSString*)theMode extra: (void**)e
116137
{
117138
if (nil != (self = [super initWithMode: theMode extra: e]))
118139
{
@@ -122,6 +143,14 @@ - (id) initWithMode: (NSString*)theMode extra: (void*)e
122143
[self watcherCallbacks], 0);
123144
_wfdMap = NSCreateMapTable (NSIntegerMapKeyCallBacks,
124145
[self watcherCallbacks], 0);
146+
#ifdef HAVE_POLL_F
147+
if (NULL == *e)
148+
{
149+
*e = extra
150+
= NSZoneCalloc(NSDefaultMallocZone(), 1, sizeof(pollextra));
151+
}
152+
((pollextra*)extra)->refcount++;
153+
#endif
125154
}
126155
return self;
127156
}

Source/win32/GSRunLoopCtxt.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ - (id) init
7878
return nil;
7979
}
8080

81-
- (id) initWithMode: (NSString*)theMode extra: (void*)e
81+
- (id) initWithMode: (NSString*)theMode extra: (void**)e
8282
{
8383
if (nil != (self = [super initWithMode: theMode extra: e]))
8484
{

0 commit comments

Comments
 (0)