xref: /haiku/src/apps/cortex/NodeManager/NodeSyncThread.h (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
1 // NodeSyncThread.h [rewrite 14oct99]
2 // * PURPOSE
3 //   Provide continuous synchronization notices on
4 //   a particular BMediaNode.  Notification is sent
5 //   to a provided BMessenger.
6 //
7 //   As long as a NodeSyncThread object exists its thread
8 //   is running.  The thread blocks indefinitely, waiting
9 //   for a message to show up on an internal port.
10 //
11 //   Sync-notice requests (via the +++++ sync() operation)
12 //   trigger a message sent to that port.  The thread wakes
13 //   up, then calls BMediaRoster::SyncToNode() to wait
14 //   until a particular performace time arrives for that node.
15 //
16 //   If SyncToNode() times out, an M_TIMED_OUT message is sent;
17 //   otherwise, an M_SYNC_COMPLETE message is sent.
18 //
19 // * HISTORY
20 //   e.moon		14oct99		Rewrite begun.
21 
22 #ifndef __NodeSyncThread_H__
23 #define __NodeSyncThread_H__
24 
25 #include <MediaNode.h>
26 #include <Messenger.h>
27 #include <OS.h>
28 
29 #include "cortex_defs.h"
30 __BEGIN_CORTEX_NAMESPACE
31 
32 class NodeSyncThread {
33 public:													// *** messages
34 	enum message_t {
35 		// 'nodeID' (int32)         media_node_id value
36 		// 'perfTime' (int64)				the performance time
37 		// 'position' (int64)       corresponding (caller-provided) position
38 		M_SYNC_COMPLETE							=NodeSyncThread_message_base,
39 
40 		// 'nodeID' (int32)         media_node_id value
41 		// 'error' (int32)					status_t value from SyncToNode()
42 		// 'perfTime' (int64)				the performance time
43 		// 'position' (int64)       corresponding (caller-provided) position
44 		M_SYNC_FAILED
45 	};
46 
47 public:													// *** dtor/ctors
48 	virtual ~NodeSyncThread();
49 
50 	NodeSyncThread(
51 		const media_node&						node,
52 		BMessenger*									messenger);
53 
54 public:													// *** operations
55 	// trigger a sync operation: when 'perfTime' arrives
56 	// for the node, a M_SYNC_COMPLETE message with the given
57 	// position value will be sent,  unless the sync operation
58 	// times out, in which case M_TIMED_OUT will be sent.
59 	status_t sync(
60 		bigtime_t										perfTime,
61 		bigtime_t										position,
62 		bigtime_t										timeout);
63 
64 private:
65 
66 	// the node to watch
67 	media_node										m_node;
68 
69 	// the messenger to inform
70 	BMessenger*										m_messenger;
71 
72 	// if the thread is running, it has exclusive write access
73 	// to this flag.
74 	volatile bool									m_syncInProgress;
75 
76 	// thread guts
77 	thread_id											m_thread;
78 	port_id												m_port;
79 	char*													m_portBuffer;
80 	ssize_t												m_portBufferSize;
81 
82 	enum _op_codes {
83 		M_TRIGGER
84 	};
85 
86 	struct _sync_op {
87 		bigtime_t 	targetTime;
88 		bigtime_t		position;
89 		bigtime_t		timeout;
90 	};
91 
92 	static status_t _Sync(
93 		void*												cookie);
94 	void _sync();
95 };
96 
97 __END_CORTEX_NAMESPACE
98 #endif /*__NodeSyncThread_H__*/
99