1/*
2 * Copyright (c) 2003-2004, Marcus Overhagen <marcus@overhagen.de>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 *  * Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright notice,
11 *    this list of conditions and the following disclaimer in the documentation
12 *    and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include "MediaDebug.h"
26#include "SystemTimeSource.h"
27
28
29SystemTimeSource::SystemTimeSource()
30 :	BMediaNode("System clock"),
31 	BTimeSource(),
32 	fControlThread(-1)
33{
34	TRACE("SystemTimeSource::SystemTimeSource\n");
35}
36
37SystemTimeSource::~SystemTimeSource()
38{
39	TRACE("SystemTimeSource::~SystemTimeSource enter\n");
40
41	close_port(ControlPort());
42	if (fControlThread != -1) {
43		status_t err;
44		wait_for_thread(fControlThread, &err);
45	}
46	TRACE("SystemTimeSource::~SystemTimeSource exit\n");
47}
48
49BMediaAddOn*
50SystemTimeSource::AddOn(int32 * internal_id) const
51{
52	return NULL;
53}
54
55status_t
56SystemTimeSource::TimeSourceOp(const time_source_op_info & op, void * _reserved)
57{
58	TRACE("######## SystemTimeSource::TimeSourceOp\n");
59	bigtime_t real = RealTime();
60	PublishTime(real, real, 1.0);
61	return B_OK;
62}
63
64
65/* virtual */ void
66SystemTimeSource::NodeRegistered()
67{
68	ASSERT(fControlThread == -1);
69	fControlThread = spawn_thread(_ControlThreadStart, "System clock control", 12, this);
70	resume_thread(fControlThread);
71}
72
73int32
74SystemTimeSource::_ControlThreadStart(void *arg)
75{
76	reinterpret_cast<SystemTimeSource *>(arg)->ControlThread();
77	return 0;
78}
79
80void
81SystemTimeSource::ControlThread()
82{
83	TRACE("SystemTimeSource::ControlThread() enter\n");
84	status_t err;
85	do {
86		err = WaitForMessage(B_INFINITE_TIMEOUT);
87	} while (err == B_OK || err == B_ERROR);
88	TRACE("SystemTimeSource::ControlThread() exit\n");
89}
90