/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * message.cpp - Messages test
 */

#include <chrono>
#include <iostream>
#include <thread>

#include "libcamera/internal/message.h"
#include "libcamera/internal/thread.h"

#include "test.h"

using namespace std;
using namespace libcamera;

class MessageReceiver : public Object
{
public:
	enum Status {
		NoMessage,
		InvalidThread,
		MessageReceived,
	};

	MessageReceiver()
		: status_(NoMessage)
	{
	}

	Status status() const { return status_; }
	void reset() { status_ = NoMessage; }

protected:
	void message(Message *msg)
	{
		if (msg->type() != Message::None) {
			Object::message(msg);
			return;
		}

		if (thread() != Thread::current())
			status_ = InvalidThread;
		else
			status_ = MessageReceived;
	}

private:
	Status status_;
};

class SlowMessageReceiver : public Object
{
protected:
	void message(Message *msg)
	{
		if (msg->type() != Message::None) {
			Object::message(msg);
			return;
		}

		/*
		 * Don't access any member of the object here (including the
		 * vtable) as the object will be deleted by the main thread
		 * while we're sleeping.
		 */
		this_thread::sleep_for(chrono::milliseconds(100));
	}
};

class MessageTest : public Test
{
protected:
	int run()
	{
		Message::Type msgType[2] = {
			Message::registerMessageType(),
			Message::registerMessageType(),
		};

		if (msgType[0] != Message::UserMessage ||
		    msgType[1] != Message::UserMessage + 1) {
			cout << "Failed to register message types" << endl;
			return TestFail;
		}

		MessageReceiver receiver;
		receiver.moveToThread(&thread_);

		thread_.start();

		receiver.postMessage(std::make_unique<Message>(Message::None));

		this_thread::sleep_for(chrono::milliseconds(100));

		switch (receiver.status()) {
		case MessageReceiver::NoMessage:
			cout << "No message received" << endl;
			return TestFail;
		case MessageReceiver::InvalidThread:
			cout << "Message received in incorrect thread" << endl;
			return TestFail;
		default:
			break;
		}

		/*
		 * Test for races between message delivery and object deletion.
		 * Failures result in assertion errors, there is no need for
		 * explicit checks.
		 */
		SlowMessageReceiver *slowReceiver = new SlowMessageReceiver();
		slowReceiver->moveToThread(&thread_);
		slowReceiver->postMessage(std::make_unique<Message>(Message::None));

		this_thread::sleep_for(chrono::milliseconds(10));

		delete slowReceiver;

		return TestPass;
	}

	void cleanup()
	{
		thread_.exit(0);
		thread_.wait();
	}

private:
	Thread thread_;
};

TEST_REGISTER(MessageTest)
e='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/libcamera/vivid.git/tree/?h=vivid-pre-a07968bed276&amp;id=0416488635fc406db1674d6c47e4e40462764c7d'>root</a>/<a href='/libcamera/vivid.git/tree/src?h=vivid-pre-a07968bed276&amp;id=0416488635fc406db1674d6c47e4e40462764c7d'>src</a>/<a href='/libcamera/vivid.git/tree/src/qcam?h=vivid-pre-a07968bed276&amp;id=0416488635fc406db1674d6c47e4e40462764c7d'>qcam</a>/<a href='/libcamera/vivid.git/tree/src/qcam/assets?h=vivid-pre-a07968bed276&amp;id=0416488635fc406db1674d6c47e4e40462764c7d'>assets</a>/<a href='/libcamera/vivid.git/tree/src/qcam/assets/feathericons?h=vivid-pre-a07968bed276&amp;id=0416488635fc406db1674d6c47e4e40462764c7d'>feathericons</a>/<a href='/libcamera/vivid.git/tree/src/qcam/assets/feathericons/toggle-left.svg?h=vivid-pre-a07968bed276&amp;id=0416488635fc406db1674d6c47e4e40462764c7d'>toggle-left.svg</a></div><div class='content'>blob: 240be290bc4b616f0e7a07b331e31592517bceb1 (<a href='/libcamera/vivid.git/plain/src/qcam/assets/feathericons/toggle-left.svg?h=vivid-pre-a07968bed276&amp;id=0416488635fc406db1674d6c47e4e40462764c7d'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
</pre></td>
<td class='lines'><pre><code><span class="hl kwa">&lt;svg</span> <span class="hl kwb">xmlns</span>=<span class="hl str">&quot;http://www.w3.org/2000/svg&quot;</span> <span class="hl kwb">width</span>=<span class="hl str">&quot;24&quot;</span> <span class="hl kwb">height</span>=<span class="hl str">&quot;24&quot;</span> <span class="hl kwb">viewBox</span>=<span class="hl str">&quot;0 0 24 24&quot;</span> <span class="hl kwb">fill</span>=<span class="hl str">&quot;none&quot;</span> <span class="hl kwb">stroke</span>=<span class="hl str">&quot;currentColor&quot;</span> <span class="hl kwb">stroke-width</span>=<span class="hl str">&quot;2&quot;</span> <span class="hl kwb">stroke-linecap</span>=<span class="hl str">&quot;round&quot;</span> <span class="hl kwb">stroke-linejoin</span>=<span class="hl str">&quot;round&quot;</span> <span class="hl kwb">class</span>=<span class="hl str">&quot;feather feather-toggle-left&quot;</span><span class="hl kwa">&gt;&lt;rect</span> <span class="hl kwb">x</span>=<span class="hl str">&quot;1&quot;</span> <span class="hl kwb">y</span>=<span class="hl str">&quot;5&quot;</span> <span class="hl kwb">width</span>=<span class="hl str">&quot;22&quot;</span> <span class="hl kwb">height</span>=<span class="hl str">&quot;14&quot;</span> <span class="hl kwb">rx</span>=<span class="hl str">&quot;7&quot;</span> <span class="hl kwb">ry</span>=<span class="hl str">&quot;7&quot;</span><span class="hl kwa">&gt;&lt;/rect&gt;&lt;circle</span> <span class="hl kwb">cx</span>=<span class="hl str">&quot;8&quot;</span> <span class="hl kwb">cy</span>=<span class="hl str">&quot;12&quot;</span> <span class="hl kwb">r</span>=<span class="hl str">&quot;3&quot;</span><span class="hl kwa">&gt;&lt;/circle&gt;&lt;/svg&gt;</span>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit v1.2.1</a> (<a href='https://git-scm.com/'>git 2.18.0</a>) at 2025-03-04 16:09:11 +0000</div>
</div> <!-- id=cgit -->
</body>
</html>