Akonadi: fix racy code in (MimeType|Resource)::retrieveByNameOrCreate

Authored by dfaure on Feb 9 2019, 9:38 PM.

Description

Akonadi: fix racy code in (MimeType|Resource)::retrieveByNameOrCreate

Summary:
If two threads (TA and TB) would call retrieveByNameOrCreate() with a
*different* argument (say A and B), TA might acquire the lock to create A,
and meanwhile TB will fail the tryLock() and never create B.

Solution: lock around lookup+creation, don't use tryLock, simplify code by using QMutexLocker.

I found this by reading the code, but indirectly this seems to also
fix akonadiserver locking up in mysql queries during akonaditest
startup (sometimes), probably because it reduces concurrent querying a bit...

Test Plan:
ctest, the "Resource synchronization timed out for akonadi_knut_resource_0"
errors (that happened approx 30% of the time) are gone.

Reviewers: dvratil

Reviewed By: dvratil

Subscribers: mlaurent, vkrause, kde-pim

Tags: KDE PIM

Differential Revision: https://phabricator.kde.org/D18888