File: listener.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (232 lines) | stat: -rw-r--r-- 6,488 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
/*
 * Copyright (c) 1997-2000 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

/*
 * This implements an threaded adaptor on an oskit_listener. Standard
 * listeners run the callback as a direct upcall from whatever environment
 * the listener was notified in. The threaded listener arranges to run the
 * callback in a thread context. 
 *
 * Creating a listener requires creating a thread that when run will invoke
 * callback(arg). However, since listeners can be fired more than once, the
 * thread will actually loop, suspending itself after each notification,
 * until the next notification or until the listener is released and the
 * thread is canceled.
 *
 * This implementation is rather doggy. If it becomes useful, we can get
 * serious about making it better.
 */

#include <oskit/com.h>
#include <oskit/com/listener.h>
#include <oskit/c/stdio.h>
#include <oskit/c/stdlib.h>
#include <oskit/c/string.h>
#include <oskit/c/malloc.h>
#include "pthread_internal.h"
#include "pthread_mutex.h"

typedef struct pthread_listener_impl {
	oskit_listener_t	iol;		/* COM interface */
        unsigned                count;		/* reference count */
	pthread_t		tid;		/* The thread */
	oskit_listener_t       *listener;	/* Actual listener */
	oskit_iunknown_t       *notify;		/* Time to notify */
	pthread_mutex_t		mutex;
	pthread_cond_t		cond;
} pthread_listener_impl_t;

static OSKIT_COMDECL
listener_query(oskit_listener_t *io,
	       const oskit_iid_t *iid, void **out_ihandle)
{
        pthread_listener_impl_t *li = (pthread_listener_impl_t *)io;

        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_listener_iid, sizeof(*iid)) == 0) {
                *out_ihandle = &li->iol;
                ++li->count;
                return 0;
        }

        *out_ihandle = NULL;
        return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U
listener_addref(oskit_listener_t *io)
{
        pthread_listener_impl_t *li = (pthread_listener_impl_t *)io;

        if (li->count == 0)
		return OSKIT_E_INVALIDARG;

        return ++li->count;
}

static OSKIT_COMDECL_U
listener_release(oskit_listener_t *io)
{
        pthread_listener_impl_t *li = (pthread_listener_impl_t *)io;
        unsigned newcount;

        if (li == NULL || li->count == 0)
		return OSKIT_E_INVALIDARG;

        newcount = --li->count;
        if (newcount == 0) {
		/*
		 * Cancel the thread, but do not free the listener yet, since
		 * we could end up deleting it right out from under the thread
		 * while it is using it. Let the cancel cleanup handler take
		 * care of it.
		 */
		pthread_cancel(li->tid);
        }

        return newcount;
}

/* ARGSUSED */
static OSKIT_COMDECL
listener_notify(oskit_listener_t *io, oskit_iunknown_t *obj)
{
        pthread_listener_impl_t *li = (pthread_listener_impl_t *)io;

	/*
	 * Spin lock the mutex and set the notify object. Signal the
	 * the thread to invoke the listener. This could happen in
	 * an interrupt context, hence the spinning mutex lock. The
	 * thread will not keep this lock very long, so its okay.
	 * It will also disable interrupts to prevent interrupt deadlock.
	 */
	fast_mutex_spinlock(&li->mutex);
	li->notify = obj;
	fast_mutex_unlock(&li->mutex);
	pthread_cond_signal(&li->cond);

	return 0;
}

static struct oskit_listener_ops oskit_pthread_listener_ops = {
	listener_query,
	listener_addref,
	listener_release,
	listener_notify
};

static void
listener_canceled(void *arg)
{
	pthread_listener_impl_t	*li = (pthread_listener_impl_t *) arg;

	DPRINTF("%p\n", arg);
	
	oskit_listener_release(li->listener);
	sfree(li, sizeof(*li));
}

static void *
listener_run(void *arg)
{
	pthread_listener_impl_t	*li = (pthread_listener_impl_t *) arg;
	oskit_iunknown_t	*obj;

	pthread_cleanup_push(listener_canceled, li);

	/*
	 * First off, just signal the creator that we are ready.
	 */
	pthread_mutex_lock(&li->mutex);
	pthread_mutex_unlock(&li->mutex);
	pthread_cond_signal(&li->cond);

	/*
	 * Now loop. The safe version of condwait allows interrupts to
	 * be disabled on entry, but does not check for cancelation.
	 */
	while (1) {
		assert_interrupts_enabled();
		disable_interrupts();
		
		pthread_mutex_lock(&li->mutex);
		while (!li->notify) {
			pthread_cond_wait_safe(&li->cond, &li->mutex);
			pthread_testcancel();
		}
		obj = li->notify;
		li->notify = NULL;
		pthread_mutex_unlock(&li->mutex);
		
		enable_interrupts();

		oskit_listener_notify(li->listener, obj);
		pthread_testcancel();
	}

	pthread_cleanup_pop(1);
}

oskit_listener_t *
oskit_create_threaded_listener(oskit_listener_callback_t handler, void *arg)
{
	pthread_listener_impl_t	*li;
	oskit_error_t		rc;
	pthread_attr_t		attr;
	
	if ((li = (pthread_listener_impl_t *) smalloc(sizeof(*li))) == NULL)
		return 0;

	li->iol.ops = &oskit_pthread_listener_ops;
	li->count = 1;

	/*
	 * Create the underlying listener.
	 */
	li->listener = oskit_create_listener(handler, arg);

	pthread_mutex_init(&li->mutex, NULL);
	pthread_cond_init(&li->cond, NULL);
	pthread_mutex_lock(&li->mutex);

	/*
	 * Create the thread.
	 * XXX we create it to run at the highest priority so somewhat
	 * simulate delivery at interrupt time; i.e., it should get stuck
	 * behind "regular" threads.
	 */
	rc = pthread_attr_init(&attr);
	if (rc == 0)
		rc = oskit_pthread_attr_setprio(&attr, PRIORITY_MAX);
	if (rc == 0)
		rc = pthread_create(&li->tid, &attr, listener_run, (void *) li);
	if (rc != 0) {
		oskit_listener_release(li->listener);
		sfree(li, sizeof(li));
		return 0;
	}
	pthread_detach(li->tid);

	/*
	 * Wait until the thread runs and waits.
	 */
	pthread_cond_wait(&li->cond, &li->mutex);
	pthread_mutex_unlock(&li->mutex);

	return &li->iol;
}