VLCEventManager.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*****************************************************************************
  2. * VLCEventManager.h: VLC.framework VLCEventManager implementation
  3. * This is used to communicate in a sound way accross threads.
  4. *****************************************************************************
  5. * Copyright (C) 2007 Pierre d'Herbemont
  6. * Copyright (C) 2007 the VideoLAN team
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24. *****************************************************************************/
  25. #import "VLCEventManager.h"
  26. #import <pthread.h>
  27. /**
  28. * Defines the type of interthread message on the queue.
  29. */
  30. typedef enum
  31. {
  32. VLCNotification, //< Standard NSNotification.
  33. VLCObjectMethodWithObjectArg, //< Method with an object argument.
  34. VLCObjectMethodWithArrayArg //< Method with an array argument.
  35. } message_type_t;
  36. /**
  37. * Data structured used to enqueue messages onto the queue.
  38. */
  39. typedef struct {
  40. id target; //< Target object that should receive the message (retained until method is called).
  41. SEL sel; //< A selector that identifies the message to be sent to the target.
  42. union u //< Object could either be a VLCNotification or other.
  43. {
  44. NSString * name; //< Name to be used for NSNotification
  45. id object; //< Object argument to pass to the target via the selector.
  46. } u;
  47. message_type_t type; //< Type of queued message.
  48. } message_t;
  49. @interface VLCEventManager (Private)
  50. - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data;
  51. - (void)callObjectMethodWithArgs:(NSData*)data;
  52. - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName:(NSString *)aNotificationName;
  53. - (pthread_cond_t *)signalData;
  54. - (pthread_mutex_t *)queueLock;
  55. - (NSMutableArray *)messageQueue;
  56. @end
  57. /**
  58. * Provides a function for the main entry point for the dispatch thread. It dispatches any messages that is queued.
  59. * \param user_data Pointer to the VLCEventManager instance that instiated this thread.
  60. */
  61. static void * EventDispatcherMainLoop(void * user_data)
  62. {
  63. VLCEventManager * self = user_data;
  64. for(;;)
  65. {
  66. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  67. message_t * message, * message_newer = NULL;
  68. NSData * dataMessage;
  69. int i;
  70. /* Sleep a bit not to flood the interface */
  71. usleep(300);
  72. /* Wait for some data */
  73. pthread_mutex_lock( [self queueLock] );
  74. /* Wait until we have something on the queue */
  75. while( [[self messageQueue] count] <= 0 )
  76. {
  77. pthread_cond_wait( [self signalData], [self queueLock] );
  78. }
  79. //if( [[self messageQueue] count] % 100 == 0 || [[self messageQueue] count] < 100 )
  80. // NSLog(@"[EVENT_MANAGER] On the stack we have %d elements", [[self messageQueue] count]);
  81. /* Get the first object off the queue. */
  82. dataMessage = [[[self messageQueue] lastObject] retain]; // Released in 'call'
  83. [[self messageQueue] removeLastObject];
  84. message = (message_t *)[dataMessage bytes];
  85. /* Remove duplicate notifications. */
  86. if( message->type == VLCNotification )
  87. {
  88. for( i = [[self messageQueue] count]-1; i >= 0; i-- )
  89. {
  90. message_newer = (message_t *)[(NSData *)[[self messageQueue] objectAtIndex:i] bytes];
  91. if( message_newer->type == VLCNotification &&
  92. message_newer->target == message->target &&
  93. [message_newer->u.name isEqualToString:message->u.name] )
  94. {
  95. [message_newer->target release];
  96. [message_newer->u.name release];
  97. [[self messageQueue] removeObjectAtIndex:i];
  98. }
  99. }
  100. }
  101. else if( message->type == VLCObjectMethodWithArrayArg )
  102. {
  103. NSMutableArray * newArg = nil;
  104. /* Collapse messages that takes array arg by sending one bigger array */
  105. for( i = [[self messageQueue] count]-1; i >= 0; i-- )
  106. {
  107. message_newer = (message_t *)[(NSData *)[[self messageQueue] objectAtIndex: i] bytes];
  108. if( message_newer->type == VLCObjectMethodWithArrayArg &&
  109. message_newer->target == message->target &&
  110. message_newer->sel == message->sel )
  111. {
  112. if(!newArg)
  113. {
  114. newArg = [NSMutableArray arrayWithArray:message->u.object];
  115. [message->u.object release];
  116. }
  117. [newArg addObjectsFromArray:message_newer->u.object];
  118. [message_newer->target release];
  119. [message_newer->u.object release];
  120. [[self messageQueue] removeObjectAtIndex:i];
  121. }
  122. /* It should be a good idea not to collapse event, with other kind of event in-between
  123. * Ignore for now only if target is the same */
  124. else if( message_newer->target != message->target )
  125. break;
  126. }
  127. if( newArg )
  128. message->u.object = [newArg retain];
  129. }
  130. pthread_mutex_unlock( [self queueLock] );
  131. if( message->type == VLCNotification )
  132. [self performSelectorOnMainThread:@selector(callDelegateOfObjectAndSendNotificationWithArgs:)
  133. withObject:dataMessage
  134. waitUntilDone: NO];
  135. else
  136. [self performSelectorOnMainThread:@selector(callObjectMethodWithArgs:)
  137. withObject:dataMessage
  138. waitUntilDone: YES];
  139. [pool release];
  140. }
  141. return nil;
  142. }
  143. @implementation VLCEventManager
  144. + (id)sharedManager
  145. {
  146. static VLCEventManager * defaultManager = NULL;
  147. /* We do want a lock here to avoid leaks */
  148. if ( !defaultManager )
  149. {
  150. defaultManager = [[VLCEventManager alloc] init];
  151. }
  152. return defaultManager;
  153. }
  154. - (void)dummy
  155. {
  156. /* Put Cocoa in multithreaded mode by calling a dummy function */
  157. }
  158. - (id)init
  159. {
  160. if( self = [super init] )
  161. {
  162. if(![NSThread isMultiThreaded])
  163. {
  164. [NSThread detachNewThreadSelector:@selector(dummy) toTarget:self withObject:nil];
  165. NSAssert([NSThread isMultiThreaded], @"Can't put Cocoa in multithreaded mode");
  166. }
  167. pthread_mutex_init( &queueLock, NULL );
  168. pthread_cond_init( &signalData, NULL );
  169. pthread_create( &dispatcherThread, NULL, EventDispatcherMainLoop, self );
  170. messageQueue = [[NSMutableArray alloc] initWithCapacity:10];
  171. }
  172. return self;
  173. }
  174. - (void)dealloc
  175. {
  176. pthread_kill( dispatcherThread, SIGKILL );
  177. pthread_join( dispatcherThread, NULL );
  178. [messageQueue release];
  179. [super dealloc];
  180. }
  181. - (void)callOnMainThreadDelegateOfObject:(id)aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
  182. {
  183. // NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  184. message_t message =
  185. {
  186. [aTarget retain],
  187. aSelector,
  188. [aNotificationName retain],
  189. VLCNotification
  190. };
  191. // if( [NSThread isMainThread] )
  192. // {
  193. [self callDelegateOfObjectAndSendNotificationWithArgs:[[NSData dataWithBytes:&message length:sizeof(message_t)] retain] /* released in the call */];
  194. // }
  195. // else
  196. // {
  197. // pthread_mutex_lock( [self queueLock] );
  198. // [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(message_t)] atIndex:0];
  199. // pthread_cond_signal( [self signalData] );
  200. // pthread_mutex_unlock( [self queueLock] );
  201. // }
  202. [pool release];
  203. }
  204. - (void)callOnMainThreadObject:(id)aTarget withMethod:(SEL)aSelector withArgumentAsObject: (id)arg
  205. {
  206. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  207. message_t message =
  208. {
  209. [aTarget retain],
  210. aSelector,
  211. [arg retain],
  212. [arg isKindOfClass:[NSArray class]] ? VLCObjectMethodWithArrayArg : VLCObjectMethodWithObjectArg
  213. };
  214. pthread_mutex_lock( [self queueLock] );
  215. [[self messageQueue] insertObject:[NSData dataWithBytes:&message length:sizeof(message_t)] atIndex:0];
  216. pthread_cond_signal( [self signalData] );
  217. pthread_mutex_unlock( [self queueLock] );
  218. [pool release];
  219. }
  220. @end
  221. @implementation VLCEventManager (Private)
  222. - (void)callDelegateOfObjectAndSendNotificationWithArgs:(NSData*)data
  223. {
  224. message_t * message = (message_t *)[data bytes];
  225. [self callDelegateOfObject:message->target withDelegateMethod:message->sel withNotificationName:message->u.name];
  226. [message->u.name release];
  227. [message->target release];
  228. [data release];
  229. }
  230. - (void)callObjectMethodWithArgs:(NSData*)data
  231. {
  232. message_t * message = (message_t *)[data bytes];
  233. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[message->target methodForSelector: message->sel];
  234. method( message->target, message->sel, message->u.object);
  235. [message->u.object release];
  236. [message->target release];
  237. [data release];
  238. }
  239. - (void)callDelegateOfObject:(id) aTarget withDelegateMethod:(SEL)aSelector withNotificationName: (NSString *)aNotificationName
  240. {
  241. // [[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:aNotificationName object:aTarget]];
  242. if (![aTarget delegate] || ![[aTarget delegate] respondsToSelector:aSelector])
  243. return;
  244. void (*method)(id, SEL, id) = (void (*)(id, SEL, id))[[aTarget delegate] methodForSelector: aSelector];
  245. method( [aTarget delegate], aSelector, [NSNotification notificationWithName:aNotificationName object:aTarget]);
  246. }
  247. - (NSMutableArray *)messageQueue
  248. {
  249. return messageQueue;
  250. }
  251. - (pthread_cond_t *)signalData
  252. {
  253. return &signalData;
  254. }
  255. - (pthread_mutex_t *)queueLock
  256. {
  257. return &queueLock;
  258. }
  259. @end