summaryrefslogtreecommitdiff
path: root/src/cam/event_loop.cpp
blob: e8ab861790ed05d27ea149748e7783f0d3be73f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * event_loop.cpp - cam - Event loop
 */

#include <libcamera/event_dispatcher.h>

#include "event_loop.h"

using namespace libcamera;

EventLoop::EventLoop(EventDispatcher *dispatcher)
	: dispatcher_(dispatcher)
{
}

EventLoop::~EventLoop()
{
}

int EventLoop::exec()
{
	exitCode_ = -1;
	exit_.store(false, std::memory_order_release);

	while (!exit_.load(std::memory_order_acquire))
		dispatcher_->processEvents();

	return exitCode_;
}

void EventLoop::exit(int code)
{
	exitCode_ = code;
	exit_.store(true, std::memory_order_release);
	dispatcher_->interrupt();
}
~TestObject() { /* Count the deletions from the correct thread. */ if (thread() == Thread::current()) (*deleteCount_)++; } unsigned int *deleteCount_; }; class NewThread : public Thread { public: NewThread(Object *obj) : object_(obj) { } protected: void run() { object_->deleteLater(); } private: Object *object_; }; class ObjectDeleteTest : public Test { protected: int run() { /* * Test that deferred deletion is executed from the object's * thread, not the caller's thread. */ unsigned int count = 0; TestObject *obj = new TestObject(&count); NewThread thread(obj); thread.start(); thread.wait(); Thread::current()->dispatchMessages(Message::Type::DeferredDelete); if (count != 1) { cout << "Failed to dispatch DeferredDelete (" << count << ")" << endl; return TestFail; } /* * Test that multiple calls to deleteLater() delete the object * once only. */ count = 0; obj = new TestObject(&count); obj->deleteLater(); obj->deleteLater(); Thread::current()->dispatchMessages(Message::Type::DeferredDelete); if (count != 1) { cout << "Multiple deleteLater() failed (" << count << ")" << endl; return TestFail; } return TestPass; } }; TEST_REGISTER(ObjectDeleteTest)