1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  *  You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21fcf3ce44SJohn Forte /*
22fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fcf3ce44SJohn Forte  * Use is subject to license terms.
24fcf3ce44SJohn Forte  */
25fcf3ce44SJohn Forte 
26fcf3ce44SJohn Forte 
27fcf3ce44SJohn Forte 
28fcf3ce44SJohn Forte #include "Listener.h"
29fcf3ce44SJohn Forte #include "Exceptions.h"
30fcf3ce44SJohn Forte #include "Lockable.h"
31fcf3ce44SJohn Forte 
32fcf3ce44SJohn Forte using namespace std;
33fcf3ce44SJohn Forte 
34fcf3ce44SJohn Forte /**
35fcf3ce44SJohn Forte  * Global lock for list of listeners
36fcf3ce44SJohn Forte  */
37fcf3ce44SJohn Forte pthread_mutex_t Listener::staticLock = PTHREAD_MUTEX_INITIALIZER;
38fcf3ce44SJohn Forte 
39fcf3ce44SJohn Forte /**
40fcf3ce44SJohn Forte  * Global list of listeners
41fcf3ce44SJohn Forte  */
42fcf3ce44SJohn Forte vector<Listener*> Listener::listeners;
43fcf3ce44SJohn Forte 
44fcf3ce44SJohn Forte /**
45fcf3ce44SJohn Forte  * Type for listener list iteration
46fcf3ce44SJohn Forte  */
47fcf3ce44SJohn Forte typedef vector<Listener *>::iterator ListenerIterator;
48fcf3ce44SJohn Forte 
49fcf3ce44SJohn Forte /**
50fcf3ce44SJohn Forte  * @memo	    Create a new generic listener
51fcf3ce44SJohn Forte  * @exception	    ... underlying exceptions will be thrown
52fcf3ce44SJohn Forte  * @param	    userData The opaque user data for event callback
53*f7c08772SToomas Soome  *
54fcf3ce44SJohn Forte  */
Listener(void * userData)55fcf3ce44SJohn Forte Listener::Listener(void *userData) {
56fcf3ce44SJohn Forte 	data = userData;
57fcf3ce44SJohn Forte 	Lockable::lock(&staticLock);
58fcf3ce44SJohn Forte 	try {
59fcf3ce44SJohn Forte 	    listeners.insert(listeners.begin(), this);
60fcf3ce44SJohn Forte 	} catch (...) {
61fcf3ce44SJohn Forte 	    Lockable::unlock(&staticLock);
62fcf3ce44SJohn Forte 	    throw;
63fcf3ce44SJohn Forte 	}
64fcf3ce44SJohn Forte 	Lockable::unlock(&staticLock);
65fcf3ce44SJohn Forte }
66fcf3ce44SJohn Forte 
67fcf3ce44SJohn Forte /**
68fcf3ce44SJohn Forte  * @memo	    Free up a generic listener, keeping global list in sync.
69fcf3ce44SJohn Forte  */
~Listener()70fcf3ce44SJohn Forte Listener::~Listener() {
71fcf3ce44SJohn Forte 	Lockable::lock(&staticLock);
72*f7c08772SToomas Soome 	for (ListenerIterator tmp = listeners.begin();
73*f7c08772SToomas Soome 	    tmp != listeners.end(); tmp++) {
74fcf3ce44SJohn Forte 		if (*tmp == this) {
75*f7c08772SToomas Soome 			listeners.erase(tmp);
76*f7c08772SToomas Soome 			break;
77fcf3ce44SJohn Forte 		}
78fcf3ce44SJohn Forte 	}
79fcf3ce44SJohn Forte 	Lockable::unlock(&staticLock);
80fcf3ce44SJohn Forte }
81fcf3ce44SJohn Forte 
82fcf3ce44SJohn Forte /**
83fcf3ce44SJohn Forte  * @memo	    Find a listener based on the callback handle
84fcf3ce44SJohn Forte  * @exception	    InvalidHandleException if the callback handle does not
85fcf3ce44SJohn Forte  *		    match any listeners
86fcf3ce44SJohn Forte  * @return	    The Listener who matches the callback handle
87fcf3ce44SJohn Forte  * @param	    cbh The callback handle
88fcf3ce44SJohn Forte  */
findListener(void * cbh)89fcf3ce44SJohn Forte Listener* Listener::findListener(void *cbh) {
90fcf3ce44SJohn Forte 	Lockable::lock(&staticLock);
91fcf3ce44SJohn Forte 	try {
92fcf3ce44SJohn Forte 	    for (ListenerIterator tmp = listeners.begin();
93fcf3ce44SJohn Forte 			tmp != listeners.end(); tmp++) {
94fcf3ce44SJohn Forte 		if (*tmp == cbh) {
95fcf3ce44SJohn Forte 		    Lockable::unlock(&staticLock);
96fcf3ce44SJohn Forte 		    return (*tmp);
97fcf3ce44SJohn Forte 		}
98fcf3ce44SJohn Forte 	    }
99fcf3ce44SJohn Forte 	} catch (...) {
100fcf3ce44SJohn Forte 	    Lockable::unlock(&staticLock);
101fcf3ce44SJohn Forte 	    throw;
102fcf3ce44SJohn Forte 	}
103fcf3ce44SJohn Forte 	Lockable::unlock(&staticLock);
104fcf3ce44SJohn Forte 	throw InvalidHandleException();
105fcf3ce44SJohn Forte }
106