xref: /haiku/src/add-ons/kernel/file_systems/nfs4/WorkQueue.h (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2012 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Paweł Dziepak, pdziepak@quarnos.org
7  */
8 #ifndef WORKQUEUE_H
9 #define WORKQUEUE_H
10 
11 
12 #include <io_requests.h>
13 #include <lock.h>
14 #include <SupportDefs.h>
15 #include <util/DoublyLinkedList.h>
16 
17 #include "Delegation.h"
18 #include "Inode.h"
19 
20 
21 enum JobType {
22 	DelegationRecall,
23 	IORequest
24 };
25 
26 struct DelegationRecallArgs {
27 	Delegation*		fDelegation;
28 	bool			fTruncate;
29 };
30 
31 struct IORequestArgs {
32 	io_request*		fRequest;
33 	Inode*			fInode;
34 };
35 
36 struct WorkQueueEntry : public DoublyLinkedListLinkImpl<WorkQueueEntry> {
37 	JobType			fType;
38 	void*			fArguments;
39 };
40 
41 class WorkQueue {
42 public:
43 						WorkQueue();
44 						~WorkQueue();
45 
46 	inline	status_t	InitStatus();
47 
48 			status_t	EnqueueJob(JobType type, void* args);
49 
50 protected:
51 	static	status_t	LaunchWorkingThread(void* object);
52 			status_t	WorkingThread();
53 
54 			void		DequeueJob();
55 
56 			void		JobRecall(DelegationRecallArgs* args);
57 			void		JobIO(IORequestArgs* args);
58 
59 private:
60 			status_t	fInitError;
61 
62 			sem_id		fQueueSemaphore;
63 			mutex		fQueueLock;
64 			DoublyLinkedList<WorkQueueEntry>	fQueue;
65 
66 			sem_id		fThreadCancel;
67 			thread_id	fThread;
68 };
69 
70 
71 inline status_t
72 WorkQueue::InitStatus()
73 {
74 	return fInitError;
75 }
76 
77 
78 extern WorkQueue*		gWorkQueue;
79 
80 
81 #endif	// WORKQUEUE_H
82 
83