VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/threads/plevent.h

Last change on this file was 106112, checked in by vboxsync, 4 months ago

libs/xpcom: Fix building code using the XPCOM SDK bindings outside of VirtualBox, ticketref:22714 bugref:10773

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.8 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org Code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/**********************************************************************
39NSPL Events
40
41Defining Events
42---------------
43
44Events are essentially structures that represent argument lists for a
45function that will run on another thread. All event structures you
46define must include a PLEvent struct as their first field:
47
48 typedef struct MyEventType {
49 PLEvent e;
50 // arguments follow...
51 int x;
52 char* y;
53 } MyEventType;
54
55It is also essential that you establish a model of ownership for each
56argument passed in an event record, i.e. whether particular arguments
57will be deleted by the event destruction callback, or whether they
58only loaned to the event handler callback, and guaranteed to persist
59until the time at which the handler is called.
60
61Sending Events
62--------------
63
64Events are initialized by PL_InitEvent and can be sent via
65PL_PostEvent or PL_PostSynchronousEvent. Events can also have an
66owner. The owner of an event can revoke all the events in a given
67event-queue by calling PL_RevokeEvents. An owner might want
68to do this if, for instance, it is being destroyed, and handling the
69events after the owner's destruction would cause an error (e.g. an
70MWContext).
71
72Since the act of initializing and posting an event must be coordinated
73with it's possible revocation, it is essential that the event-queue's
74monitor be entered surrounding the code that constructs, initializes
75and posts the event:
76
77 void postMyEvent(MyOwner* owner, int x, char* y)
78 {
79 MyEventType* event;
80
81 PL_ENTER_EVENT_QUEUE_MONITOR(myQueue);
82
83 // construct
84 event = PR_NEW(MyEventType);
85 if (event == NULL) goto done;
86
87 // initialize
88 PL_InitEvent(event, owner,
89 (PLHandleEventProc)handleMyEvent,
90 (PLDestroyEventProc)destroyMyEvent);
91 event->x = x;
92 event->y = strdup(y);
93
94 // post
95 PL_PostEvent(myQueue, &event->e);
96
97 done:
98 PL_EXIT_EVENT_QUEUE_MONITOR(myQueue);
99 }
100
101If you don't call PL_InitEvent and PL_PostEvent within the
102event-queue's monitor, you'll get a big red assert.
103
104Handling Events
105---------------
106
107To handle an event you must write a callback that is passed the event
108record you defined containing the event's arguments:
109
110 void* handleMyEvent(MyEventType* event)
111 {
112 doit(event->x, event->y);
113 return NULL; // you could return a value for a sync event
114 }
115
116Similarly for the destruction callback:
117
118 void destroyMyEvent(MyEventType* event)
119 {
120 free(event->y); // created by strdup
121 free(event);
122 }
123
124Processing Events in Your Event Loop
125------------------------------------
126
127If your main loop only processes events delivered to the event queue,
128things are rather simple. You just get the next event (which may
129block), and then handle it:
130
131 while (1) {
132 event = PL_GetEvent(myQueue);
133 PL_HandleEvent(event);
134 }
135
136However, if other things must be waited on, you'll need to obtain a
137file-descriptor that represents your event queue, and hand it to select:
138
139 fd = PL_GetEventQueueSelectFD(myQueue);
140 ...add fd to select set...
141 while (select(...)) {
142 if (...fd...) {
143 PL_ProcessPendingEvents(myQueue);
144 }
145 ...
146 }
147
148Of course, with Motif and Windows it's more complicated than that, and
149on Mac it's completely different, but you get the picture.
150
151Revoking Events
152---------------
153If at any time an owner of events is about to be destroyed, you must
154take steps to ensure that no one tries to use the event queue after
155the owner is gone (or a crash may result). You can do this by either
156processing all the events in the queue before destroying the owner:
157
158 {
159 ...
160 PL_ENTER_EVENT_QUEUE_MONITOR(myQueue);
161 PL_ProcessPendingEvents(myQueue);
162 DestroyMyOwner(owner);
163 PL_EXIT_EVENT_QUEUE_MONITOR(myQueue);
164 ...
165 }
166
167or by revoking the events that are in the queue for that owner. This
168removes them from the queue and calls their destruction callback:
169
170 {
171 ...
172 PL_ENTER_EVENT_QUEUE_MONITOR(myQueue);
173 PL_RevokeEvents(myQueue, owner);
174 DestroyMyOwner(owner);
175 PL_EXIT_EVENT_QUEUE_MONITOR(myQueue);
176 ...
177 }
178
179In either case it is essential that you be in the event-queue's monitor
180to ensure that all events are removed from the queue for that owner,
181and to ensure that no more events will be delivered for that owner.
182**********************************************************************/
183
184#ifndef plevent_h___
185#define plevent_h___
186
187#include "prtypes.h"
188#include "prmon.h"
189
190#ifdef VBOX
191# include <iprt/critsect.h>
192# include <iprt/list.h>
193# include <iprt/semaphore.h>
194# include <iprt/thread.h>
195#endif
196
197#ifdef VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
198#define PL_DestroyEvent VBoxNsplPL_DestroyEvent
199#define PL_HandleEvent VBoxNsplPL_HandleEvent
200#define PL_InitEvent VBoxNsplPL_InitEvent
201#define PL_CreateEventQueue VBoxNsplPL_CreateEventQueue
202#define PL_CreateMonitoredEventQueue VBoxNsplPL_CreateMonitoredEventQueue
203#define PL_CreateNativeEventQueue VBoxNsplPL_CreateNativeEventQueue
204#define PL_DequeueEvent VBoxNsplPL_DequeueEvent
205#define PL_DestroyEventQueue VBoxNsplPL_DestroyEventQueue
206#define PL_EventAvailable VBoxNsplPL_EventAvailable
207#define PL_EventLoop VBoxNsplPL_EventLoop
208#define PL_GetEvent VBoxNsplPL_GetEvent
209#define PL_GetEventOwner VBoxNsplPL_GetEventOwner
210#define PL_GetEventQueueMonitor VBoxNsplPL_GetEventQueueMonitor
211#define PL_GetEventQueueSelectFD VBoxNsplPL_GetEventQueueSelectFD
212#define PL_MapEvents VBoxNsplPL_MapEvents
213#define PL_PostEvent VBoxNsplPL_PostEvent
214#define PL_PostSynchronousEvent VBoxNsplPL_PostSynchronousEvent
215#define PL_ProcessEventsBeforeID VBoxNsplPL_ProcessEventsBeforeID
216#define PL_ProcessPendingEvents VBoxNsplPL_ProcessPendingEvents
217#define PL_RegisterEventIDFunc VBoxNsplPL_RegisterEventIDFunc
218#define PL_RevokeEvents VBoxNsplPL_RevokeEvents
219#define PL_UnregisterEventIDFunc VBoxNsplPL_UnregisterEventIDFunc
220#define PL_WaitForEvent VBoxNsplPL_WaitForEvent
221#define PL_IsQueueNative VBoxNsplPL_IsQueueNative
222#define PL_IsQueueOnCurrentThread VBoxNsplPL_IsQueueOnCurrentThread
223#define PL_FavorPerformanceHint VBoxNsplPL_FavorPerformanceHint
224#endif /* VBOX_WITH_XPCOM_NAMESPACE_CLEANUP */
225
226PR_BEGIN_EXTERN_C
227
228/* Typedefs */
229
230typedef struct PLEvent PLEvent;
231typedef struct PLEventQueue PLEventQueue;
232
233#ifdef VBOX
234
235/*******************************************************************************
236 * Event Queue Operations
237 ******************************************************************************/
238
239/*
240** Creates a new event queue. Returns NULL on failure.
241*/
242PR_EXTERN(PLEventQueue*)
243PL_CreateEventQueue(const char* name, RTTHREAD handlerThread);
244
245
246/* -----------------------------------------------------------------------
247** FUNCTION: PL_CreateNativeEventQueue()
248**
249** DESCRIPTION:
250** PL_CreateNativeEventQueue() creates an event queue that
251** uses platform specific notify mechanisms.
252**
253** For Unix, the platform specific notify mechanism provides
254** an FD that may be extracted using the function
255** PL_GetEventQueueSelectFD(). The FD returned may be used in
256** a select() function call.
257**
258** For Windows, the platform specific notify mechanism
259** provides an event receiver window that is called by
260** Windows to process the event using the windows message
261** pump engine.
262**
263** INPUTS:
264** name: A name, as a diagnostic aid.
265**
266** handlerThread: A pointer to the IPRT thread structure for
267** the thread that will "handle" events posted to this event
268** queue.
269**
270** RETURNS:
271** A pointer to a PLEventQueue structure or NULL.
272**
273*/
274PR_EXTERN(PLEventQueue *)
275 PL_CreateNativeEventQueue(
276 const char *name,
277 RTTHREAD handlerThread
278 );
279
280/* -----------------------------------------------------------------------
281** FUNCTION: PL_CreateMonitoredEventQueue()
282**
283** DESCRIPTION:
284** PL_CreateMonitoredEventQueue() creates an event queue. No
285** platform specific notify mechanism is created with the
286** event queue.
287**
288** Users of this type of event queue must explicitly poll the
289** event queue to retreive and process events.
290**
291**
292** INPUTS:
293** name: A name, as a diagnostic aid.
294**
295** handlerThread: A pointer to the IPRT thread structure for
296** the thread that will "handle" events posted to this event
297** queue.
298**
299** RETURNS:
300** A pointer to a PLEventQueue structure or NULL.
301**
302*/
303PR_EXTERN(PLEventQueue *)
304 PL_CreateMonitoredEventQueue(
305 const char *name,
306 RTTHREAD handlerThread
307 );
308
309/*
310** Destroys an event queue.
311*/
312PR_EXTERN(void)
313PL_DestroyEventQueue(PLEventQueue* self);
314
315/*
316** Returns the monitor associated with an event queue. This monitor is
317** selectable. The monitor should be entered to protect against anyone
318** calling PL_RevokeEvents while the event is trying to be constructed
319** and delivered.
320*/
321PR_EXTERN(PRMonitor*)
322PL_GetEventQueueMonitor(PLEventQueue* self);
323
324#define PL_ENTER_EVENT_QUEUE_MONITOR(queue) \
325 PR_EnterMonitor(PL_GetEventQueueMonitor(queue))
326
327#define PL_EXIT_EVENT_QUEUE_MONITOR(queue) \
328 PR_ExitMonitor(PL_GetEventQueueMonitor(queue))
329
330/*
331** Posts an event to an event queue, waking up any threads waiting for an
332** event. If event is NULL, notification still occurs, but no event will
333** be available.
334**
335** Any events delivered by this routine will be destroyed by PL_HandleEvent
336** when it is called (by the event-handling thread).
337*/
338PR_EXTERN(PRStatus)
339PL_PostEvent(PLEventQueue* self, PLEvent* event);
340
341/*
342** Like PL_PostEvent, this routine posts an event to the event handling
343** thread, but does so synchronously, waiting for the result. The result
344** which is the value of the handler routine is returned.
345**
346** Any events delivered by this routine will be not be destroyed by
347** PL_HandleEvent, but instead will be destroyed just before the result is
348** returned (by the current thread).
349*/
350PR_EXTERN(void*)
351PL_PostSynchronousEvent(PLEventQueue* self, PLEvent* event);
352
353/*
354** Gets an event from an event queue. Returns NULL if no event is
355** available.
356*/
357PR_EXTERN(PLEvent*)
358PL_GetEvent(PLEventQueue* self);
359
360/*
361** Returns true if there is an event available for PL_GetEvent.
362*/
363PR_EXTERN(PRBool)
364PL_EventAvailable(PLEventQueue* self);
365
366/*
367** This is the type of the function that must be passed to PL_MapEvents
368** (see description below).
369*/
370typedef void
371(PR_CALLBACK *PLEventFunProc)(PLEvent* event, void* data, PLEventQueue* queue);
372
373/*
374** Applies a function to every event in the event queue. This can be used
375** to selectively handle, filter, or remove events. The data pointer is
376** passed to each invocation of the function fun.
377*/
378PR_EXTERN(void)
379PL_MapEvents(PLEventQueue* self, PLEventFunProc fun, void* data);
380
381/*
382** This routine walks an event queue and destroys any event whose owner is
383** the owner specified. The == operation is used to compare owners.
384*/
385PR_EXTERN(void)
386PL_RevokeEvents(PLEventQueue* self, void* owner);
387
388/*
389** This routine processes all pending events in the event queue. It can be
390** called from the thread's main event-processing loop whenever the event
391** queue's selectFD is ready (returned by PL_GetEventQueueSelectFD).
392*/
393PR_EXTERN(void)
394PL_ProcessPendingEvents(PLEventQueue* self);
395
396/*******************************************************************************
397 * Pure Event Queues
398 *
399 * For when you're only processing PLEvents and there is no native
400 * select, thread messages, or AppleEvents.
401 ******************************************************************************/
402
403/*
404** Blocks until an event can be returned from the event queue. This routine
405** may return NULL if the current thread is interrupted.
406*/
407PR_EXTERN(PLEvent*)
408PL_WaitForEvent(PLEventQueue* self);
409
410/*
411** One stop shopping if all you're going to do is process PLEvents. Just
412** call this and it loops forever processing events as they arrive. It will
413** terminate when your thread is interrupted or dies.
414*/
415PR_EXTERN(void)
416PL_EventLoop(PLEventQueue* self);
417
418/*******************************************************************************
419 * Native Event Queues
420 *
421 * For when you need to call select, or WaitNextEvent, and yet also want
422 * to handle PLEvents.
423 ******************************************************************************/
424
425/*
426** This routine allows you to grab the file descriptor associated with an
427** event queue and use it in the readFD set of select. Useful for platforms
428** that support select, and must wait on other things besides just PLEvents.
429*/
430PR_EXTERN(PRInt32)
431PL_GetEventQueueSelectFD(PLEventQueue* self);
432
433/*
434** This routine will allow you to check to see if the given eventQueue in
435** on the current thread. It will return PR_TRUE if so, else it will return
436** PR_FALSE
437*/
438PR_EXTERN(PRBool)
439 PL_IsQueueOnCurrentThread( PLEventQueue *queue );
440
441/*
442** Returns whether the queue is native (true) or monitored (false)
443*/
444PR_EXTERN(PRBool)
445PL_IsQueueNative(PLEventQueue *queue);
446
447#endif /* VBOX */
448
449/*******************************************************************************
450 * Event Operations
451 ******************************************************************************/
452
453/*
454** The type of an event handler function. This function is passed as an
455** initialization argument to PL_InitEvent, and called by
456** PL_HandleEvent. If the event is called synchronously, a void* result
457** may be returned (otherwise any result will be ignored).
458*/
459typedef void*
460(PR_CALLBACK *PLHandleEventProc)(PLEvent* self);
461
462/*
463** The type of an event destructor function. This function is passed as
464** an initialization argument to PL_InitEvent, and called by
465** PL_DestroyEvent.
466*/
467typedef void
468(PR_CALLBACK *PLDestroyEventProc)(PLEvent* self);
469
470#ifdef VBOX
471
472/*
473** Initializes an event. Usually events are embedded in a larger event
474** structure which holds event-specific data, so this is an initializer
475** for that embedded part of the structure.
476*/
477PR_EXTERN(void)
478PL_InitEvent(PLEvent* self, void* owner,
479 PLHandleEventProc handler,
480 PLDestroyEventProc destructor);
481
482/*
483** Returns the owner of an event.
484*/
485PR_EXTERN(void*)
486PL_GetEventOwner(PLEvent* self);
487
488/*
489** Handles an event, calling the event's handler routine.
490*/
491PR_EXTERN(void)
492PL_HandleEvent(PLEvent* self);
493
494/*
495** Destroys an event, calling the event's destructor.
496*/
497PR_EXTERN(void)
498PL_DestroyEvent(PLEvent* self);
499
500/*
501** Removes an event from an event queue.
502*/
503PR_EXTERN(void)
504PL_DequeueEvent(PLEvent* self, PLEventQueue* queue);
505
506
507/*
508 * Give hint to native PL_Event notification mechanism. If the native
509 * platform needs to tradeoff performance vs. native event starvation
510 * this hint tells the native dispatch code which to favor.
511 * The default is to prevent event starvation.
512 *
513 * Calls to this function may be nested. When the number of calls that
514 * pass PR_TRUE is subtracted from the number of calls that pass PR_FALSE
515 * is greater than 0, performance is given precedence over preventing
516 * event starvation.
517 *
518 * The starvationDelay arg is only used when
519 * favorPerformanceOverEventStarvation is PR_FALSE. It is the
520 * amount of time in milliseconds to wait before the PR_FALSE actually
521 * takes effect.
522 */
523PR_EXTERN(void)
524PL_FavorPerformanceHint(PRBool favorPerformanceOverEventStarvation, PRUint32 starvationDelay);
525
526
527/*******************************************************************************
528 * Private Stuff
529 ******************************************************************************/
530
531struct PLEvent {
532 RTLISTNODE link;
533 PLHandleEventProc handler;
534 PLDestroyEventProc destructor;
535 void* owner;
536 void* synchronousResult;
537 RTCRITSECT lock;
538 RTSEMEVENT condVar;
539 PRBool handled;
540#ifdef XP_UNIX
541 unsigned long id;
542#endif /* XP_UNIX */
543 /* other fields follow... */
544};
545
546/******************************************************************************/
547
548#ifdef XP_UNIX
549/* -----------------------------------------------------------------------
550** FUNCTION: PL_ProcessEventsBeforeID()
551**
552** DESCRIPTION:
553**
554** PL_ProcessEventsBeforeID() will process events in a native event
555** queue that have an id that is older than the ID passed in.
556**
557** INPUTS:
558** PLEventQueue *aSelf
559** unsigned long aID
560**
561** RETURNS:
562** PRInt32 number of requests processed, -1 on error.
563**
564** RESTRICTIONS: Unix only (well, X based unix only)
565*/
566PR_EXTERN(PRInt32)
567PL_ProcessEventsBeforeID(PLEventQueue *aSelf, unsigned long aID);
568
569/* This prototype is a function that can be called when an event is
570 posted to stick an ID on it. */
571
572typedef unsigned long
573(PR_CALLBACK *PLGetEventIDFunc)(void *aClosure);
574
575
576/* -----------------------------------------------------------------------
577** FUNCTION: PL_RegisterEventIDFunc()
578**
579** DESCRIPTION:
580**
581** This function registers a function for getting the ID on unix for
582** this event queue.
583**
584** INPUTS:
585** PLEventQueue *aSelf
586** PLGetEventIDFunc func
587** void *aClosure
588**
589** RETURNS:
590** void
591**
592** RESTRICTIONS: Unix only (well, X based unix only) */
593PR_EXTERN(void)
594PL_RegisterEventIDFunc(PLEventQueue *aSelf, PLGetEventIDFunc aFunc,
595 void *aClosure);
596
597/* -----------------------------------------------------------------------
598** FUNCTION: PL_RegisterEventIDFunc()
599**
600** DESCRIPTION:
601**
602** This function unregisters a function for getting the ID on unix for
603** this event queue.
604**
605** INPUTS:
606** PLEventQueue *aSelf
607**
608** RETURNS:
609** void
610**
611** RESTRICTIONS: Unix only (well, X based unix only) */
612PR_EXTERN(void)
613PL_UnregisterEventIDFunc(PLEventQueue *aSelf);
614
615#endif /* XP_UNIX */
616
617#endif /* VBOX */
618
619/* ----------------------------------------------------------------------- */
620
621PR_END_EXTERN_C
622
623#endif /* plevent_h___ */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette