File: DKEndpointManager.m

package info (click to toggle)
dbuskit 0.1.1-14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,956 kB
  • sloc: objc: 10,543; sh: 9,463; ansic: 200; makefile: 32
file content (1013 lines) | stat: -rw-r--r-- 27,830 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
/** Implemenation of the DKEndpointManager class that manages D-Bus endpoints.
   Copyright (C) 2011 Free Software Foundation, Inc.

   Written by:  Niels Grewe <niels.grewe@halbordnung.de>
   Created: January 2011

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02111 USA.
   */

#import "DKArgument.h"
#import "DKEndpointManager.h"
#import "DKEndpoint.h"
#import "DKIntrospectionParserDelegate.h"
#import "DKMethodCall.h"
#import "DKObjectPathNode.h"
#import "DKProxy+Private.h"
#import "DKSignal.h"

#import "DBusKit/DKProxy.h"

#import <Foundation/NSDate.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSInvocation.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSMapTable.h>
#import <Foundation/NSRunLoop.h>
#import <Foundation/NSThread.h>
#import <Foundation/NSTimer.h>
#import <Foundation/NSValue.h>

#include <sched.h>

/*
 * Phony interfaces to make the compiler aware of the fact that the private
 * classes here inherit from NSObject, so that we can call +alloc -init on them
 * in order to initialize them before starting the worker thread.
 */
@interface GSStackTrace : NSObject @end
@interface DKWatcher : NSObject <RunLoopEvents> @end

@interface DKEndpoint (Private)
- (void)_mergeInfo: (NSDictionary*)info;
@end

@interface NSObject (DKContextPrivateMethods)
- (void)monitorForEvents;
- (void)unmonitorForEvents;
- (void)handleTimeout: (NSTimer*)timer;
@end

static DKEndpointManager *sharedManager;

#define DKTheManager getManager(managerClass, getManagerSelector)
#define DKManagerThread managerThread
#define DKPerformOnManagerThread(target,payloadSelector,object) performOnThread(target,\
  perfonOnThreadSelector,\
  payloadSelector, \
  DKManagerThread,\
  object,\
  NO)



/* Definitions for the ring buffer, designwise inspired by EtoileThread */

// Needs to be 2^n
#define DKRingSize 32

#define DKRingMask (DKRingSize - 1)

#define DKRingSpace (DKRingSize - (producerCounter - consumerCounter))

#define DKRingFull (DKRingSpace == 0)
#define DKRingEmpty ((producerCounter - consumerCounter) == 0)
#define DKMaskIndex(index) ((index) & DKRingMask)

#define DKRingSchedule \
if (NO == DKRingEmpty)\
{\
  if (0 == initializeRefCount)\
  {\
    if (__sync_bool_compare_and_swap(&threadStarted, 0, 1))\
    {\
      [workerThread start];\
    }\
  }\
  NSDebugMLog(@"Stuff in buffer: Scheduling buffer draining.");\
  [self performSelector: @selector(drainBuffer:)\
               onThread: workerThread\
	     withObject: nil\
          waitUntilDone: NO];\
}

/*
 * This works the following way:
 * 1.  Start the worker thread if it is not yet running.
 * 2.  Check whether the buffer is full
 * 3.  If so, spin for a short while to allow it to drain or yield to other
 *     threads if it's taking to long.
 * 4.  Lock the producer lock since multiple threads might want to write to the
 *     buffer.
 * 5.  Check again if the buffer hasn't filled in the meantime.
 * 6.  Retain the target for its trip to the other thread.
 * 7.  Insert the new request into the buffer.
 * 8.  Increment the producer counter.
 * 9.  Unlock the producer lock.
 */
#define DKRingInsert(x) do {\
  NSUInteger count = 0; \
  while (DKRingFull)\
  {\
    if ((++count % 16) == 0)\
    {\
      sched_yield();\
    }\
  }\
  [producerLock lock];\
    while (DKRingFull)\
    {\
      if ((++count % 16) == 0)\
      {\
	sched_yield();\
      }\
    }\
  [x.target retain];\
  ringBuffer[DKMaskIndex(producerCounter)] = x;\
  __sync_fetch_and_add(&producerCounter, 1);\
  [producerLock unlock];\
  NSDebugMLog(@"Inserting into ringbuffer (remaining capacity: %lu).",\
    DKRingSpace);\
} while (0)


/*
 * If the buffer is not empty, remove an element and process it.
 */
#define DKRingRemove(x) do {\
  if (NO == DKRingEmpty)\
  {\
    NSDebugMLog(@"Removing element at %lu from ring buffer", DKMaskIndex(consumerCounter));\
    x = ringBuffer[DKMaskIndex(consumerCounter)];\
    ringBuffer[DKMaskIndex(consumerCounter)] = (DKRingBufferElement){nil, NULL, nil, NULL};\
    [x.target autorelease];\
    __sync_fetch_and_add(&consumerCounter, 1);\
  }\
  NSDebugMLog(@"(new capacity: %lu).",\
    DKRingSpace);\
} while (0)

@implementation DKEndpointManager

+ (void)initialize
{
  if ([DKEndpointManager class] != self)
  {
    return;
  }

  /*
   * It might be smart to put DBus into thread-safe mode by default because
   * there is a fair chance of missing NSWillBecomeMultiThreadedNotification.
   * (This code might be executed pretty late in the application lifecycle.)
   * Note: We could define our own hooks and use NSLock and friends, but that's
   * pretty pointless because DBus will use pthreads itself, just as NSLock
   * would.
   */
   dbus_threads_init_default();

  /*
   * To sidestep the limitation of handling of +initialize by the gcc and
   * gnustep runtimes (which use a global lock to protect against multiple calls
   * to the same +initialize), we make sure that we initialize all classes that
   * are used on the run loop of the worker thread. This way, we will not
   * deadlock when user code uses DBusKit objects in +initialize.
   * NOTE: This only works properly if we send instance methods (maybe
   * because the meta-class gets initialized otherwise?).
   */
  [[[GSStackTrace alloc] init] release];
  [[[NSException alloc] init] release];
  [[[NSTimer alloc] init] release];
  [[[DKWatcher alloc] init] release];
  [[[DKSignal alloc] init] release];

  // NOTE: DKArgument initializes its own children.
  [[[DKArgument alloc] init] release];
  [[[DKProxyStandin alloc] init] release];
  [[[DKIntrospectionParserDelegate alloc] init] release];
  [[[DKMethodCall alloc] init] release];

  sharedManager = [[DKEndpointManager alloc] init];
  [sharedManager enterInitialize];
  // Preload the bus objects:
  [DKDBus sessionBus];
  [DKDBus systemBus];
  [sharedManager leaveInitialize];
}

+ (id)sharedEndpointManager
{
  return sharedManager;
}


+ (id)allocWithZone: (NSZone*)zone
{
  if (nil != sharedManager)
  {
    return nil;
  }
  return [super allocWithZone: zone];
}

- (id)init
{
  if (nil != sharedManager)
  {
    [self release];
    return nil;
  }

  if (nil == (self = [super init]))
  {
    return nil;
  }

   activeConnections = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks,
     NSNonRetainedObjectMapValueCallBacks,
     3);
   connectionStateLock = [NSRecursiveLock new];
   workerThread = [[NSThread alloc] initWithTarget: self
                                          selector: @selector(start:)
                                            object: nil];
   [workerThread setName: @"DBusKit worker thread"];
   /*
    * We set this up with a refcout of 1 because we want to start in
    * non-threaded mode. Otherwise people will get bitten by synchronisation
    * issues from +initialize.
    */
   initializeRefCount = 1;
   ringBuffer = calloc(sizeof(DKRingBufferElement), DKRingSize);
   producerLock = [NSLock new];

   synchronizationStateLock = [NSRecursiveLock new];
   syncedWatchers = [[NSMapTable alloc] initWithKeyOptions: NSMapTableStrongMemory
                                              valueOptions: NSMapTableStrongMemory
                                                  capacity: 5];
   syncedTimers = [[NSMapTable alloc] initWithKeyOptions: NSMapTableStrongMemory
                                            valueOptions: NSMapTableStrongMemory
                                                capacity: 5];
   if (NO == (activeConnections && connectionStateLock
     && workerThread && ringBuffer && producerLock && synchronizationStateLock
     && syncedWatchers && syncedTimers))
   {
     [self release];
     return nil;
   }
   return self;
}


- (void)enableThread
{
  if (__sync_bool_compare_and_swap(&threadEnabled, 0, 1))
  {
    [self leaveInitialize];
  }
}

- (NSThread*)workerThread
{
  return workerThread;
}

- (id)endpointForDBusConnection: (DBusConnection*)connection
                    mergingInfo: (NSDictionary*)info
{
  DKEndpoint *endpoint = nil;
  [connectionStateLock lock];
  NS_DURING
  {
    // Check whether we can reuse an old connection:
    endpoint = NSMapGet(activeConnections, (void*)connection);
    if (nil != endpoint)
    {
      NSDebugMLog(@"Will reuse old connection");
      /*
       * We want to retain the endpoint because the map table only weakly
       * references it and we want to pass ownership of this endpoint to the
       * caller at the end of the function (we will autorelease it there).
       */
      [endpoint retain];
      NS_DURING
      {
	[endpoint _mergeInfo: info];
      }
      NS_HANDLER
      {
	[endpoint release];
	[localException raise];
      }
      NS_ENDHANDLER
    }
  }

  /* We couldn't find a preexisting endpoint, so we create a new one: */
  if (nil == endpoint)
  {
    endpoint = [[DKEndpoint alloc] initWithConnection: connection
                                                 info: info];
  }

  if (nil != endpoint)
  {
    NSMapInsert(activeConnections, connection, endpoint);
  }
  else
  {
    NSDebugMLog(@"Could not create endpoint!");
  }
  NS_HANDLER
  {
    [connectionStateLock unlock];
    [localException raise];
  }
  NS_ENDHANDLER
  [connectionStateLock unlock];
  return [endpoint autorelease];
}

/**
 * For use with non-well-known buses, not exteremly useful, but generic.
 */
- (DKEndpoint*)endpointForConnectionTo: (NSString*)endpointName
{
  DBusError err;
  /* Note: dbus_connection_open_private() would be an option here, but would
   * require us to take care of the connections ourselves. Right now, this does
   * not seem to be worth the effort, so we let D-Bus do this for us (hence we
   * call dbus_connection_unref() and not dbus_connection_close() in -cleanup).
   */
  DBusConnection *conn = NULL;
  NSDictionary *theInfo = [[NSDictionary alloc] initWithObjectsAndKeys: endpointName,
    @"address", nil];
  DKEndpoint *endpoint = nil;
  dbus_error_init(&err);

  conn = dbus_connection_open([endpointName UTF8String], &err);
  if (NULL == conn)
  {
    [theInfo release];
    NSWarnMLog(@"Could not open D-Bus connection. Error: %s. (%s)",
      err.name,
      err.message);
    dbus_error_free(&err);
    return nil;
  }
  dbus_error_free(&err);

  NS_DURING
  {
    endpoint = [self endpointForDBusConnection: conn
                                   mergingInfo: theInfo];
  }
  NS_HANDLER
  {
    [theInfo release];
    dbus_connection_unref(conn);
    [localException raise];
  }
  NS_ENDHANDLER

  [theInfo release];

  // -_initWithConnection did increase the refcount, we release ownership of the
  // connection:
  dbus_connection_unref(conn);
  return endpoint;
}

- (DKEndpoint*)endpointForWellKnownBus: (DBusBusType)type
{
  DBusError err;
  DBusConnection *conn = NULL;
  NSDictionary *theInfo = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt: type],
    @"wellKnownBus", nil];
  DKEndpoint *endpoint = nil;
  dbus_error_init(&err);
  conn = dbus_bus_get(type, &err);
  if (NULL == conn)
  {
    [theInfo release];
    NSWarnMLog(@"Could not open D-Bus connection. Error: %s. (%s)",
      err.name,
      err.message);
    dbus_error_free(&err);
    return nil;
  }
  dbus_error_free(&err);

  /*
   * dbus_bus_get() will cause _exit() to be called when the bus goes away.
   * Since we are library code, we don't want to confuse the user with that.
   *
   * TODO: Instead, we will need to watch for the "Disconnected" signal from
   * DBUS_PATH_LOCAL in DBUS_INTERFACE_LOCAL and invalidate all DBus ports.
   */
  dbus_connection_set_exit_on_disconnect(conn, NO);

  NS_DURING
  {
  endpoint = [self endpointForDBusConnection: conn
                                 mergingInfo: theInfo];
  }
  NS_HANDLER
  {
    [theInfo release];
    dbus_connection_unref(conn);
  }
  NS_ENDHANDLER

  [theInfo release];
  // -initWithConnection did increase the refcount, we release ownership of the
  // connection:
  dbus_connection_unref(conn);
  return endpoint;
}

- (void)removeEndpointForDBusConnection: (DBusConnection*)connection
{
  [connectionStateLock lock];
  NS_DURING
  {
    NSMapRemove(activeConnections, connection);
  }
  NS_HANDLER
  {
    [connectionStateLock unlock];
    [localException raise];
  }
  NS_ENDHANDLER

  [connectionStateLock unlock];
}

- (void)distantFutureReached: (id)ignored
{
  //Won't happen.
}

- (void)start: (id)ignored
{
  NSAutoreleasePool *arp = [NSAutoreleasePool new];
  // We schedule a timer to make sure that the run loop actually runs:
  [NSTimer scheduledTimerWithTimeInterval: [[NSDate distantFuture] timeIntervalSinceNow]
                                   target: self
                                 selector: @selector(distantFutureReached:)
                                 userInfo: nil
                                  repeats: NO];
  [[NSRunLoop currentRunLoop] run];
  [arp release];
}
- (void)_performRecovery: (NSTimer*)timer
{
  NSDictionary *userInfo = [timer userInfo];
  DKDBusBusType busType = [(NSNumber*)[userInfo objectForKey: @"busType"] integerValue];
  DKEndpoint *newEndpoint = [self endpointForWellKnownBus: busType];
  if (nil != newEndpoint)
  {
    [(DKDBus*)[userInfo objectForKey: @"proxy"] _reconnectedWithEndpoint: newEndpoint];
    [timer invalidate];
  }
}

- (void)attemptRecoveryForEndpoint: (DKEndpoint*)endpoint
                             proxy: (DKProxy*)proxy
{
  DBusConnection *connection = NULL;
  NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt: [endpoint DBusBusType]], @"busType",
    proxy, @"proxy", nil];
  NSTimer *timer = nil;
  [connectionStateLock lock];
  connection = [endpoint DBusConnection];
  if (NULL != connection)
  {
    NSMapRemove(activeConnections, connection);
  }
  [connectionStateLock unlock];
  timer = [NSTimer timerWithTimeInterval: 0.5
                                  target: self
				selector: @selector(_performRecovery:)
				userInfo: infoDict
				 repeats: YES];
  if (0 != initializeRefCount)
  {
    [[NSRunLoop currentRunLoop] addTimer: timer
                                 forMode: NSDefaultRunLoopMode];
  }
  else
  {
    [self performSelector: @selector(_injectTimer:)
                 onThread: workerThread
 	       withObject: timer
	    waitUntilDone: NO];
  }
}

- (void)invokeRequest: (const DKRingBufferElement)request
{
  NSMethodSignature *sig = nil;
  NSInvocation *inv = nil;
  id data = request.object;
  // We don't handle incomplete requests:
  if ((nil == request.target) || (0 == request.selector))
  {
    return;
  }

  /*
   * Special case for when we cannot use the ring buffer for some reason.
   * This might mean that we are in the worker thread and the buffer is
   * full. In this case, we must wrap the call in an NSInvocation
   * object and dispatch the call via the run loop.
   */
  sig = [request.target methodSignatureForSelector: request.selector];
  inv = [NSInvocation invocationWithMethodSignature: sig];
  [inv setSelector: request.selector];
  [inv setArgument: &data
           atIndex: 2];
  [[NSRunLoop currentRunLoop] performSelector: @selector(invokeWithTarget:)
                                       target: inv
                                     argument: request.target
                                        order: 0
                                        modes: [NSArray arrayWithObject: NSDefaultRunLoopMode]];
}

- (BOOL)boolReturnForPerformingSelector: (SEL)selector
                                 target: (id)target
		 		   data: (void*)data
                          waitForReturn: (BOOL)doWait
{
  /*
   * Setup the returnValue with -1 to signify that the call has not been
   * completed.
   */
  volatile NSInteger retVal = -1;
  NSInteger *retValPointer = NULL;
  NSUInteger count = 0;
  DKRingBufferElement request;
  BOOL performSynchronized = NO;
  BOOL workerThreadIsCurrent = [workerThread isEqual: [NSThread currentThread]];
  if (doWait)
  {
    // If we are waiting for the return value, we pass the return value pointer.
    retValPointer = (NSInteger*)&retVal;
  }
  else
  {
    //Otherwise we pass NULL and set the return value to 1.
    retVal = 1;
  }

  request = (DKRingBufferElement){target, selector, (id)data, retValPointer};

  /*
   * Under two conditions we want to execute the request directly: a) we are
   * being called from within the worker thread and are supposed to wait for the
   * result. b) We are being called from within an +initialize method and thus
   * cannot use the worker thread.
   */

  performSynchronized = (0 != initializeRefCount);
  if (performSynchronized)
  {
    [synchronizationStateLock lock];
    if (0 != initializeRefCount)
    {
      performSynchronized = YES;
    }
    else
    {
      performSynchronized = NO;
      [synchronizationStateLock unlock];
    }
  }
  // Note the following if statement will be executed under lock if
  // preformSynchronized == YES
  if (workerThreadIsCurrent || (YES == performSynchronized))
  {
    IMP performRequest = [target methodForSelector: selector];
    NSDebugMLog(@"Performing on current thread");
    NSAssert2(performRequest, @"Could not perform selector %@ on %@",
      selector,
      target);
    if (YES == doWait)
    {
      retVal = (BOOL)(intptr_t)performRequest(target, selector, data);
      if (performSynchronized)
      {
        [synchronizationStateLock unlock];
      }
    }
    else if (performSynchronized)
    {
      [self invokeRequest: request];
      retVal = YES;
      [synchronizationStateLock unlock];
    }
    else if (YES == DKRingFull)
    {
      NSWarnMLog(@"Warning, ring buffer full when called from within worker thread. Will handle call through NSInvocation.");
      [self invokeRequest: request];
      return YES;
    }

    if (doWait || performSynchronized)
    {
      return retVal;
    }
  }

  /*
   * Otherwise, we insert the request and spin until the worker thread completes
   * the request.
   */

  DKRingInsert(request);
  DKRingSchedule;
  while ((-1 == retVal) && (YES == doWait))
  {
    if (0 == (++count % 16))
    {
      sched_yield();
    }
  }
  return (BOOL)retVal;
}

- (void)drainBuffer: (id)ignored
{
  DKRingBufferElement element = {nil, NULL, nil, NULL};
  NSInteger *returnPointer = NULL;
  NSDebugMLog(@"Started draining buffer");
  DKRingRemove(element);

  if (nil != element.target)
  {
    IMP performRequest = [element.target methodForSelector: element.selector];
    returnPointer = element.returnPointer;
    NSAssert2(performRequest, @"Could not perform selector %@ on %@",
      NSStringFromSelector(element.selector),
      element.target);
    if (NULL != returnPointer)
    {
      NS_DURING
      {
        *returnPointer = (NSInteger)performRequest(element.target,
          element.selector,
          element.object);
      }
      NS_HANDLER
      {
        // Set the pointer to 0 so that the requesting thread does not
        // continue to wait for the result.
        *returnPointer = 0;
        [localException raise];
      }
      NS_ENDHANDLER
    }
    else
    {
      // If no return pointer is set, the other thread is not waiting for
      // completion.
      performRequest(element.target, element.selector, element.object);
    }
  }
  else
  {
    // If there was no object:
    if (NULL != returnPointer)
    {
      *returnPointer = 0;
    }
  }
}

- (void)enterInitialize
{
  if (0 == initializeRefCount)
  {
    [synchronizationStateLock lock];
     __sync_fetch_and_add(&initializeRefCount, 1);
     [synchronizationStateLock unlock];
  }
  else
  {
     __sync_fetch_and_add(&initializeRefCount, 1);
  }
}
- (void)_transferWatchersToWorkerThread
{
  // Note: The caller obtains the synchronizationStateLock
  NSMapEnumerator theEnum = NSEnumerateMapTable(syncedWatchers);
  NS_DURING
  {
    // Set up enumerator and associated variables:
    id thisWatcher = nil;
    NSThread *thisThread = nil;

    // First, we iterate over all watchers:
    while (NSNextMapEnumeratorPair(&theEnum, (void**)&thisWatcher, (void**)&thisThread))
    {
      if ([thisThread isExecuting])
      {
        /*
         * Remove them from the thread they were created in (if it is still
         * running).
         */
        [thisWatcher performSelector: @selector(unmonitorForEvents)
	                    onThread: thisThread
	                  withObject: nil
	               waitUntilDone: YES];
      }
      /*
       * Schedule it for monitoring the fd on the worker thread.
       */
      [thisWatcher performSelector: @selector(monitorForEvents)
	                  onThread: workerThread
	                withObject: nil
	             waitUntilDone: NO];
    }
  }
  NS_HANDLER
  {
    NSEndMapTableEnumeration(&theEnum);
    [localException raise];
  }
  NS_ENDHANDLER
  NSEndMapTableEnumeration(&theEnum);
  NSResetMapTable(syncedWatchers);
  // Note: The caller unlocks the synchronizationStateLock
}

- (void)_injectTimer: (NSTimer*)timer
{
  if (nil == timer)
  {
    // It's silly to inject non-existant timers.
    return;
  }

  if (NO == [workerThread isEqual: [NSThread currentThread]])
  {
    // We only inject timers into the worker thread;
    return;
  }

  [[NSRunLoop currentRunLoop] addTimer: timer
                               forMode: NSDefaultRunLoopMode];

}

- (void)_transferTimersToWorkerThread
{
  // Note: The caller obtains the synchronizationStateLock
  NSMapEnumerator theEnum = NSEnumerateMapTable(syncedTimers);
  NS_DURING
  {
    // Set up enumerator and associated variables:
    NSTimer *thisTimer = nil;
    NSDictionary *metadata = nil;

    // First, we iterate over all watchers:
    while (NSNextMapEnumeratorPair(&theEnum, (void**)&thisTimer, (void**)&metadata))
    {
      // Set up variables:
      id userInfo = nil;
      NSDate *fireDate = nil;
      const NSTimeInterval timeInterval = [thisTimer timeInterval];
      id target = nil;
      NSThread *thisThread = nil;
      NSTimer *newTimer = nil;

      if (NO == [thisTimer isValid])
      {
	// Don't do anything with invalid timers
	continue;
      }

      // Collect info about the timer in order to reschedule it on the worker
      // thread:
      userInfo = [thisTimer userInfo];
      fireDate = [thisTimer fireDate];
      target = [metadata objectForKey: @"context"];
      newTimer = [NSTimer timerWithTimeInterval: timeInterval
                                         target: target
                                       selector: @selector(handleTimeout:)
                                       userInfo: userInfo
                                        repeats: YES];
      [newTimer setFireDate: fireDate];

      thisThread = [metadata objectForKey: @"thread"];
      if ([thisThread isExecuting])
      {
	/*
	 * If the thread the timer was scheduled for is still running,
	 * invalidate the timer.
	 */
	[thisTimer performSelector: @selector(invalidate)
	                  onThread: thisThread
	                withObject: nil
	             waitUntilDone: YES];
      }
      /*
       * Inject the timer to the worker thread:
       */
      [self performSelector: @selector(_injectTimer:)
                   onThread: workerThread
		 withObject: newTimer
	      waitUntilDone: NO];
    }
  }
  NS_HANDLER
  {
    NSEndMapTableEnumeration(&theEnum);
    [localException raise];
  }
  NS_ENDHANDLER
  NSEndMapTableEnumeration(&theEnum);
  NSResetMapTable(syncedTimers);
  // Note: The caller unlocks the synchronizationStateLock
}

- (void)leaveInitialize
{
  if (1 == initializeRefCount)
  {
    [synchronizationStateLock lock];
    NS_DURING
    {
      if (1 == initializeRefCount)
      {
        // Start the worker thread if necessary:
        if (__sync_bool_compare_and_swap(&threadStarted, 0, 1))
        {
          [workerThread start];
	  NSDebugMLog(@"Worker thread started.");
        }

        // Move the watchers to the worker thread
        [self _transferWatchersToWorkerThread];
        // Move the timers as well:
        [self _transferTimersToWorkerThread];
      }
    }
    NS_HANDLER
    {
      __sync_fetch_and_sub(&initializeRefCount, 1);
      [synchronizationStateLock unlock];
      [localException raise];
    }
    NS_ENDHANDLER
    __sync_fetch_and_sub(&initializeRefCount, 1);
    [synchronizationStateLock unlock];
  }
  else
  {
    __sync_fetch_and_sub(&initializeRefCount, 1);
  }
}

- (BOOL)isSynchronizing
{
  return (0 != initializeRefCount);
}

/**
 * If the endpoint manger is in synchronized mode, this method will register
 * objects scheduled on the runloop by libdbus so that they can be savely
 * moved to the worker thread later on.
 */
- (void)_registerObject: (id)object
                inTable: (NSMapTable*)table
           withMetadata: (id)meta
{
  if (0 != initializeRefCount)
  {
    [synchronizationStateLock lock];
    if (0 != initializeRefCount)
    {
      NS_DURING
      {
	if (nil == meta)
	{
	  meta = [NSThread currentThread];
	}
	NSMapInsert(table, object, meta);
      }
      NS_HANDLER
      {
	[synchronizationStateLock unlock];
	[localException raise];
      }
      NS_ENDHANDLER
    }
    [synchronizationStateLock unlock];
  }
}


/**
 * If the endpoint manger is in synchronized mode, this method will unregister
 * objects that previously were scheduled on the local runloop. This makes sure
 * that no left-over objects get moved around when leaving synchronized mode.
 */
- (void)_unregisterObject: (id)object
                fromTable: (NSMapTable*)table
{
  if (0 != initializeRefCount)
  {
    [synchronizationStateLock lock];
    if (0 != initializeRefCount)
    {
      NS_DURING
      {
	NSMapRemove(table, object);
      }
      NS_HANDLER
      {
	[synchronizationStateLock unlock];
	[localException raise];
      }
      NS_ENDHANDLER
    }
    [synchronizationStateLock unlock];
  }
}


- (void)registerTimer: (id)timer
          fromContext: (id)context;
{
  // Create a dictionary to store metadata about the timer:
  NSDictionary *meta = [[NSDictionary alloc] initWithObjectsAndKeys: [NSThread currentThread], @"thread",
    context, @"context", nil];
  NS_DURING
  {
    [self _registerObject: timer
                  inTable: syncedTimers
	     withMetadata: meta];
  }
  NS_HANDLER
  {
    [meta release];
    [localException raise];
  }
  NS_ENDHANDLER
  [meta release];
}

- (void)registerWatcher: (id)watcher
{
  [self _registerObject: watcher
                inTable: syncedWatchers
	   withMetadata: nil];
}

- (void)unregisterTimer: (id)timer
{
  [self _unregisterObject: timer
                fromTable: syncedTimers];
}

- (void)unregisterWatcher: (id)watcher
{
  [self _unregisterObject: watcher
                fromTable: syncedWatchers];
}

- (void)dealloc
{
  [connectionStateLock lock];
  [synchronizationStateLock lock];
  [producerLock lock];
  [workerThread release];
  NSFreeMapTable(activeConnections);
  NSFreeMapTable(syncedWatchers);
  NSFreeMapTable(syncedTimers);
  free(ringBuffer);
  [producerLock unlock];
  [synchronizationStateLock unlock];
  [connectionStateLock unlock];
  [producerLock release];
  [synchronizationStateLock release];
  [connectionStateLock release];
  [super dealloc];
}
@end