libcamera/test/object.cpp
Laurent Pinchart 93e72b695e libcamera: Move internal headers to include/libcamera/internal/
The libcamera internal headers are located in src/libcamera/include/.
The directory is added to the compiler headers search path with a meson
include_directories() directive, and internal headers are included with
(e.g. for the internal semaphore.h header)

  #include "semaphore.h"

All was well, until libcxx decided to implement the C++20
synchronization library. The __threading_support header gained a

  #include <semaphore.h>

to include the pthread's semaphore support. As include_directories()
adds src/libcamera/include/ to the compiler search path with -I, the
internal semaphore.h is included instead of the pthread version.
Needless to say, the compiler isn't happy.

Three options have been considered to fix this issue:

- Use -iquote instead of -I. The -iquote option instructs gcc to only
  consider the header search path for headers included with the ""
  version. Meson unfortunately doesn't support this option.

- Rename the internal semaphore.h header. This was deemed to be the
  beginning of a long whack-a-mole game, where namespace clashes with
  system libraries would appear over time (possibly dependent on
  particular system configurations) and would need to be constantly
  fixed.

- Move the internal headers to another directory to create a unique
  namespace through path components. This causes lots of churn in all
  the existing source files through the all project.

The first option would be best, but isn't available to us due to missing
support in meson. Even if -iquote support was added, we would need to
fix the problem before a new version of meson containing the required
support would be released.

The third option is thus the only practical solution available. Bite the
bullet, and do it, moving headers to include/libcamera/internal/.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Jacopo Mondi <jacopo@jmondi.org>
2020-05-16 03:38:11 +03:00

158 lines
3.4 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* object.cpp - Object tests
*/
#include <iostream>
#include <libcamera/object.h>
#include "libcamera/internal/message.h"
#include "libcamera/internal/thread.h"
#include "test.h"
using namespace std;
using namespace libcamera;
class InstrumentedObject : public Object
{
public:
enum Status {
NoMessage,
MessageReceived,
};
InstrumentedObject(Object *parent = nullptr)
: Object(parent), status_(NoMessage)
{
}
Status status() const { return status_; }
void reset() { status_ = NoMessage; }
protected:
void message(Message *msg) override
{
if (msg->type() == Message::ThreadMoveMessage)
status_ = MessageReceived;
Object::message(msg);
}
private:
Status status_;
};
class ObjectTest : public Test
{
protected:
int init()
{
/*
* Create a hierarchy of objects:
* A -> B -> C
* \->D
* E
*/
a_ = new InstrumentedObject();
b_ = new InstrumentedObject(a_);
c_ = new InstrumentedObject(b_);
d_ = new InstrumentedObject(a_);
e_ = new InstrumentedObject();
f_ = nullptr;
return TestPass;
}
int run()
{
/* Verify the parent-child relationships. */
if (a_->parent() != nullptr || b_->parent() != a_ ||
c_->parent() != b_ || d_->parent() != a_ ||
e_->parent() != nullptr) {
cout << "Incorrect parent-child relationships" << endl;
return TestFail;
}
/*
* Verify that moving an object with no parent to a different
* thread succeeds.
*/
e_->moveToThread(&thread_);
if (e_->thread() != &thread_ || e_->thread() == Thread::current()) {
cout << "Failed to move object to thread" << endl;
return TestFail;
}
/*
* Verify that moving an object with a parent to a different
* thread fails. This results in an undefined behaviour, the
* test thus depends on the internal implementation returning
* without performing any change.
*/
b_->moveToThread(&thread_);
if (b_->thread() != Thread::current()) {
cout << "Moving object with parent to thread shouldn't succeed" << endl;
return TestFail;
}
/*
* Verify that moving an object with children to a different
* thread moves all the children.
*/
a_->moveToThread(&thread_);
if (a_->thread() != &thread_ || b_->thread() != &thread_ ||
c_->thread() != &thread_ || d_->thread() != &thread_) {
cout << "Failed to move children to thread" << endl;
return TestFail;
}
/* Verify that objects are bound to the thread of their parent. */
f_ = new InstrumentedObject(d_);
if (f_->thread() != &thread_) {
cout << "Failed to bind child to parent thread" << endl;
return TestFail;
}
/* Verify that objects receive a ThreadMoveMessage when moved. */
if (a_->status() != InstrumentedObject::MessageReceived ||
b_->status() != InstrumentedObject::MessageReceived ||
c_->status() != InstrumentedObject::MessageReceived ||
d_->status() != InstrumentedObject::MessageReceived ||
e_->status() != InstrumentedObject::MessageReceived) {
cout << "Moving object didn't deliver ThreadMoveMessage" << endl;
return TestFail;
}
return TestPass;
}
void cleanup()
{
delete a_;
delete b_;
delete c_;
delete d_;
delete e_;
delete f_;
}
private:
InstrumentedObject *a_;
InstrumentedObject *b_;
InstrumentedObject *c_;
InstrumentedObject *d_;
InstrumentedObject *e_;
InstrumentedObject *f_;
Thread thread_;
};
TEST_REGISTER(ObjectTest)