1/*
2  File: AppleSCSIEmulatorEventSource.cpp
3
4  Contains:
5
6  Version: 1.0.0
7
8  Copyright: Copyright (c) 2007 by Apple Inc., All Rights Reserved.
9
10Disclaimer:IMPORTANT:  This Apple software is supplied to you by Apple Inc.
11("Apple") in consideration of your agreement to the following terms, and your use,
12installation, modification or redistribution of this Apple software constitutes acceptance
13of these terms.  If you do not agree with these terms, please do not use, install, modify or
14redistribute this Apple software.
15
16In consideration of your agreement to abide by the following terms, and subject
17to these terms, Apple grants you a personal, non-exclusive license, under Apple's
18copyrights in this original Apple software (the "Apple Software"), to use, reproduce,
19modify and redistribute the Apple Software, with or without modifications, in source and/or
20binary forms; provided that if you redistribute the Apple Software in its entirety
21and without modifications, you must retain this notice and the following text
22and disclaimers in all such redistributions of the Apple Software.  Neither the
23name, trademarks, service marks or logos of Apple Inc. may be used to
24endorse or promote products derived from the Apple Software without specific prior
25written permission from Apple.  Except as expressly stated in this notice, no
26other rights or licenses, express or implied, are granted by Apple herein,
27including but not limited to any patent rights that may be infringed by your derivative
28works or by other works in which the Apple Software may be incorporated.
29
30The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO WARRANTIES,
31EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
32MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE
33OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL APPLE
34BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
37REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
38AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT
39LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40*/
41
42
43//-----------------------------------------------------------------------------
44//	Includes
45//-----------------------------------------------------------------------------
46
47#include "DebugSupport.h"
48#include "AppleSCSIEmulatorEventSource.h"
49#include "AppleSCSIEmulatorAdapter.h"
50
51
52// Define superclass
53#define super IOEventSource
54OSDefineMetaClassAndStructors ( AppleSCSIEmulatorEventSource, IOEventSource )
55
56
57//-----------------------------------------------------------------------------
58//	Create
59//-----------------------------------------------------------------------------
60
61AppleSCSIEmulatorEventSource *
62AppleSCSIEmulatorEventSource::Create ( OSObject * owner, Action action )
63{
64
65	AppleSCSIEmulatorEventSource *	es		= NULL;
66	bool							result	= false;
67
68	es = OSTypeAlloc ( AppleSCSIEmulatorEventSource );
69	require_nonzero ( es, ErrorExit );
70
71	result = es->Init ( owner, action );
72	require ( result, ReleaseEventSource );
73
74	return es;
75
76
77ReleaseEventSource:
78
79
80	es->release ( );
81
82
83ErrorExit:
84
85
86	return NULL;
87
88}
89
90
91//-----------------------------------------------------------------------------
92//	Init
93//-----------------------------------------------------------------------------
94
95bool
96AppleSCSIEmulatorEventSource::Init ( OSObject * owner, Action action )
97{
98
99	bool	result = false;
100
101	// Initialize the queue head.
102	queue_init ( &fResponderQueue );
103
104	fLock = IOSimpleLockAlloc ( );
105	if ( fLock != NULL )
106	{
107
108		// Call the superclass.
109		result = super::init ( owner, ( IOEventSource::Action ) action );
110
111	}
112
113	return result;
114
115}
116
117
118//-----------------------------------------------------------------------------
119//	AddItemToQueue
120//-----------------------------------------------------------------------------
121
122void
123AppleSCSIEmulatorEventSource::AddItemToQueue ( SCSIEmulatorRequestBlock * srb )
124{
125
126	// Take the lock to protect the queue.
127	IOSimpleLockLock ( fLock );
128
129	// Add the item to the queue.
130	queue_enter ( &fResponderQueue, srb, SCSIEmulatorRequestBlock *, fQueueChain );
131
132	// Drop the lock.
133	IOSimpleLockUnlock ( fLock );
134
135	// Wakeup the thread since there's work to do...
136	signalWorkAvailable ( );
137
138}
139
140
141//-----------------------------------------------------------------------------
142//	RemoveItemFromQueue
143//-----------------------------------------------------------------------------
144
145SCSIEmulatorRequestBlock *
146AppleSCSIEmulatorEventSource::RemoveItemFromQueue ( void )
147{
148
149	SCSIEmulatorRequestBlock *	srb = NULL;
150
151	// Take the lock to protect the queue.
152	IOSimpleLockLock ( fLock );
153
154	if ( !queue_empty ( &fResponderQueue ) )
155	{
156
157		// Remove the item to the queue.
158		queue_remove_first ( &fResponderQueue, srb, SCSIEmulatorRequestBlock *, fQueueChain );
159
160	}
161
162	// Drop the lock.
163	IOSimpleLockUnlock ( fLock );
164
165	return srb;
166
167}
168
169
170//-----------------------------------------------------------------------------
171//	checkForWork
172//-----------------------------------------------------------------------------
173
174bool
175AppleSCSIEmulatorEventSource::checkForWork ( void )
176{
177
178	SCSIEmulatorRequestBlock *	srb = NULL;
179
180	srb = RemoveItemFromQueue ( );
181	while ( srb != NULL )
182	{
183
184		// Complete it if it's non-NULL.
185		( *action ) ( owner, srb->fParallelRequest );
186
187		srb = RemoveItemFromQueue ( );
188
189	}
190
191	return false;
192
193}
194