xref: /haiku/src/system/kernel/device_manager/IOScheduler.h (revision b06a48ab8f30b45916a9c157b992827779182163)
1 /*
2  * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2004-2008, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef IO_SCHEDULER_H
7 #define IO_SCHEDULER_H
8 
9 #include <KernelExport.h>
10 
11 #include <condition_variable.h>
12 #include <lock.h>
13 #include <util/DoublyLinkedList.h>
14 
15 #include "dma_resources.h"
16 #include "io_requests.h"
17 
18 
19 class IOCallback {
20 public:
21 	virtual	status_t			DoIO(IOOperation* operation);
22 };
23 
24 typedef status_t (*io_callback)(void* data, io_operation* operation);
25 
26 
27 class IOScheduler {
28 public:
29 								IOScheduler(DMAResource* resource);
30 								~IOScheduler();
31 
32 			status_t			Init(const char* name);
33 
34 			void				SetCallback(IOCallback& callback);
35 			void				SetCallback(io_callback callback, void* data);
36 
37 			status_t			ScheduleRequest(IORequest* request);
38 
39 			void				AbortRequest(IORequest* request,
40 									status_t status = B_CANCELED);
41 			void				OperationCompleted(IOOperation* operation,
42 									status_t status, size_t transferredBytes);
43 									// called by the driver when the operation
44 									// has been completed successfully or failed
45 									// for some reason
46 
47 private:
48 			void				_Finisher();
49 			bool				_FinisherWorkPending();
50 			IOOperation*		_GetOperation();
51 			IORequest*			_GetNextUnscheduledRequest();
52 			status_t			_Scheduler();
53 	static	status_t			_SchedulerThread(void* self);
54 			status_t			_RequestNotifier();
55 	static	status_t			_RequestNotifierThread(void* self);
56 
57 	static	status_t			_IOCallbackWrapper(void* data,
58 									io_operation* operation);
59 
60 private:
61 			DMAResource*		fDMAResource;
62 			spinlock			fFinisherLock;
63 			mutex				fLock;
64 			thread_id			fSchedulerThread;
65 			thread_id			fRequestNotifierThread;
66 			io_callback			fIOCallback;
67 			void*				fIOCallbackData;
68 			IORequestList		fUnscheduledRequests;
69 			IORequestList		fFinishedRequests;
70 			ConditionVariable	fNewRequestCondition;
71 			ConditionVariable	fFinishedOperationCondition;
72 			ConditionVariable	fFinishedRequestCondition;
73 			IOOperationList		fUnusedOperations;
74 			IOOperationList		fCompletedOperations;
75 			bool				fWaiting;
76 };
77 
78 #endif	// IO_SCHEDULER_H
79