Thread.cxx 1.56 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "Thread.hxx"
#include "thread/Name.hxx"
22
#include "thread/Slack.hxx"
23 24
#include "thread/Util.hxx"
#include "Log.hxx"
25 26 27 28

void
EventThread::Start()
{
29
	assert(!event_loop.IsAlive());
30 31
	assert(!thread.IsDefined());

32 33
	event_loop.SetAlive(true);

34
	thread.Start();
35 36 37
}

void
38
EventThread::Stop() noexcept
39 40
{
	if (thread.IsDefined()) {
41 42 43
		assert(event_loop.IsAlive());
		event_loop.SetAlive(false);

44 45 46 47 48 49
		event_loop.Break();
		thread.Join();
	}
}

void
50
EventThread::Run() noexcept
51
{
52 53 54
	SetThreadName(realtime ? "rtio" : "io");

	if (realtime) {
55
		SetThreadTimerSlack(std::chrono::microseconds(10));
56

57 58 59
		try {
			SetThreadRealtime();
		} catch (...) {
60 61
			Log(LogLevel::INFO, std::current_exception(),
			    "RTIOThread could not get realtime scheduling, continuing anyway");
62 63
		}
	}
64 65

	event_loop.Run();
66
}