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