xref: /haiku/src/apps/cortex/NodeManager/NodeSyncThread.h (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // NodeSyncThread.h [rewrite 14oct99]
33 // * PURPOSE
34 //   Provide continuous synchronization notices on
35 //   a particular BMediaNode.  Notification is sent
36 //   to a provided BMessenger.
37 //
38 //   As long as a NodeSyncThread object exists its thread
39 //   is running.  The thread blocks indefinitely, waiting
40 //   for a message to show up on an internal port.
41 //
42 //   Sync-notice requests (via the +++++ sync() operation)
43 //   trigger a message sent to that port.  The thread wakes
44 //   up, then calls BMediaRoster::SyncToNode() to wait
45 //   until a particular performace time arrives for that node.
46 //
47 //   If SyncToNode() times out, an M_TIMED_OUT message is sent;
48 //   otherwise, an M_SYNC_COMPLETE message is sent.
49 //
50 // * HISTORY
51 //   e.moon		14oct99		Rewrite begun.
52 
53 #ifndef __NodeSyncThread_H__
54 #define __NodeSyncThread_H__
55 
56 #include <MediaNode.h>
57 #include <Messenger.h>
58 #include <OS.h>
59 
60 #include "cortex_defs.h"
61 __BEGIN_CORTEX_NAMESPACE
62 
63 class NodeSyncThread {
64 public:													// *** messages
65 	enum message_t {
66 		// 'nodeID' (int32)         media_node_id value
67 		// 'perfTime' (int64)				the performance time
68 		// 'position' (int64)       corresponding (caller-provided) position
69 		M_SYNC_COMPLETE							=NodeSyncThread_message_base,
70 
71 		// 'nodeID' (int32)         media_node_id value
72 		// 'error' (int32)					status_t value from SyncToNode()
73 		// 'perfTime' (int64)				the performance time
74 		// 'position' (int64)       corresponding (caller-provided) position
75 		M_SYNC_FAILED
76 	};
77 
78 public:													// *** dtor/ctors
79 	virtual ~NodeSyncThread();
80 
81 	NodeSyncThread(
82 		const media_node&						node,
83 		BMessenger*									messenger);
84 
85 public:													// *** operations
86 	// trigger a sync operation: when 'perfTime' arrives
87 	// for the node, a M_SYNC_COMPLETE message with the given
88 	// position value will be sent,  unless the sync operation
89 	// times out, in which case M_TIMED_OUT will be sent.
90 	status_t sync(
91 		bigtime_t										perfTime,
92 		bigtime_t										position,
93 		bigtime_t										timeout);
94 
95 private:
96 
97 	// the node to watch
98 	media_node										m_node;
99 
100 	// the messenger to inform
101 	BMessenger*										m_messenger;
102 
103 	// if the thread is running, it has exclusive write access
104 	// to this flag.
105 	volatile bool									m_syncInProgress;
106 
107 	// thread guts
108 	thread_id											m_thread;
109 	port_id												m_port;
110 	char*													m_portBuffer;
111 	ssize_t												m_portBufferSize;
112 
113 	enum _op_codes {
114 		M_TRIGGER
115 	};
116 
117 	struct _sync_op {
118 		bigtime_t 	targetTime;
119 		bigtime_t		position;
120 		bigtime_t		timeout;
121 	};
122 
123 	static status_t _Sync(
124 		void*												cookie);
125 	void _sync();
126 };
127 
128 __END_CORTEX_NAMESPACE
129 #endif /*__NodeSyncThread_H__*/
130