xref: /haiku/src/system/kernel/messaging/MessagingService.h (revision adb0d19d561947362090081e81d90dde59142026)
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 // kernel-side implementation-private definitions for the messaging service
7 
8 #ifndef MESSAGING_SERVICE_H
9 #define MESSAGING_SERVICE_H
10 
11 #include <MessagingServiceDefs.h>
12 
13 #include "Locker.h"
14 
15 namespace BPrivate {
16 
17 // MessagingArea
18 class MessagingArea {
19 public:
20 	~MessagingArea();
21 
22 	static MessagingArea *Create(sem_id lockSem, sem_id counterSem);
23 
24 	static bool CheckCommandSize(int32 dataSize);
25 
26 	void InitHeader();
27 
28 	bool Lock();
29 	void Unlock();
30 
31 	area_id ID() const;
32 	int32 Size() const;
33 	bool IsEmpty() const;
34 
35 	void *AllocateCommand(uint32 commandWhat, int32 dataSize, bool &wasEmpty);
36 	void CommitCommand();
37 
38 	void SetNextArea(MessagingArea *area);
39 	MessagingArea *NextArea() const;
40 
41 private:
42 	MessagingArea();
43 
44 	messaging_command *_CheckCommand(int32 offset, int32 &size);
45 
46 	messaging_area_header	*fHeader;
47 	area_id					fID;
48 	int32					fSize;
49 	sem_id					fLockSem;
50 	sem_id					fCounterSem;
51 	MessagingArea			*fNextArea;
52 };
53 
54 // MessagingService
55 class MessagingService {
56 public:
57 	MessagingService();
58 	~MessagingService();
59 
60 	status_t InitCheck() const;
61 
62 	bool Lock();
63 	void Unlock();
64 
65 	status_t RegisterService(sem_id lockingSem, sem_id counterSem,
66 		area_id &areaID);
67 	status_t UnregisterService();
68 
69 	status_t SendMessage(const void *message, int32 messageSize,
70 		const messaging_target *targets, int32 targetCount);
71 
72 private:
73 	status_t _AllocateCommand(int32 commandWhat, int32 size,
74 		MessagingArea *&area, void *&data, bool &wasEmpty);
75 
76 	BLocker			fLock;
77 	team_id			fServerTeam;
78 	sem_id			fLockSem;
79 	sem_id			fCounterSem;
80 	MessagingArea	*fFirstArea;
81 	MessagingArea	*fLastArea;
82 };
83 
84 }	// namespace BPrivate
85 
86 using BPrivate::MessagingArea;
87 using BPrivate::MessagingService;
88 
89 #endif	// MESSAGING_SERVICE_H
90