xref: /haiku/src/servers/registrar/MessagingService.h (revision d0e2e53903c40d4515fcf5f007906b59e84b176d)
1 /*
2  * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef MESSAGING_SERVICE_H
7 #define MESSAGING_SERVICE_H
8 
9 #include <Locker.h>
10 #include <MessagingServiceDefs.h>
11 
12 // MessagingCommandHandler
13 class MessagingCommandHandler {
14 public:
15 	MessagingCommandHandler();
16 	virtual ~MessagingCommandHandler();
17 
18 	virtual void HandleMessagingCommand(uint32 command, const void *data,
19 		int32 dataSize) = 0;
20 };
21 
22 // MessagingArea
23 class MessagingArea {
24 public:
25 	~MessagingArea();
26 
27 	static status_t Create(area_id kernelAreaID, sem_id lockSem,
28 		sem_id counterSem, MessagingArea *&area);
29 
30 	bool Lock();
31 	void Unlock();
32 
33 	area_id ID() const;
34 	int32 Size() const;
35 
36 	int32 CountCommands() const;
37 	const messaging_command *PopCommand();
38 
39 	void Discard();
40 
41 	area_id NextKernelAreaID() const;
42 	void SetNextArea(MessagingArea *area);
43 	MessagingArea *NextArea() const;
44 
45 private:
46 	MessagingArea();
47 
48 	messaging_area_header	*fHeader;
49 	area_id					fID;
50 	int32					fSize;
51 	sem_id					fLockSem;
52 	sem_id					fCounterSem;	// TODO: Remove, if not needed.
53 	MessagingArea			*fNextArea;
54 };
55 
56 // MessagingService
57 class MessagingService {
58 private:
59 	MessagingService();
60 	~MessagingService();
61 
62 	status_t Init();
63 
64 public:
65 	static status_t CreateDefault();
66 	static void DeleteDefault();
67 	static MessagingService *Default();
68 
69 	void SetCommandHandler(uint32 command, MessagingCommandHandler *handler);
70 
71 private:
72 	MessagingCommandHandler *_GetCommandHandler(uint32 command) const;
73 
74 	static int32 _CommandProcessorEntry(void *data);
75 	int32 _CommandProcessor();
76 
77 	class DefaultSendCommandHandler;
78 	struct CommandHandlerMap;
79 
80 	static MessagingService	*sService;
81 
82 	mutable BLocker			fLock;
83 	sem_id					fLockSem;
84 	sem_id					fCounterSem;
85 	MessagingArea			*fFirstArea;
86 	CommandHandlerMap		*fCommandHandlers;
87 	thread_id				fCommandProcessor;
88 	volatile bool			fTerminating;
89 };
90 
91 #endif	// MESSAGING_SERVICE_H
92