mirror of
https://github.com/ppy/osu.git
synced 2025-01-27 11:12:59 +08:00
Allow realm subscriptions to be initiated from a non-update thread
This commit is contained in:
parent
2938f44e6c
commit
b04ca111c6
@ -1,6 +1,8 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
#nullable enable
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
@ -17,6 +19,7 @@ using osu.Framework.Input.Bindings;
|
|||||||
using osu.Framework.Logging;
|
using osu.Framework.Logging;
|
||||||
using osu.Framework.Platform;
|
using osu.Framework.Platform;
|
||||||
using osu.Framework.Statistics;
|
using osu.Framework.Statistics;
|
||||||
|
using osu.Framework.Threading;
|
||||||
using osu.Game.Beatmaps;
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Configuration;
|
using osu.Game.Configuration;
|
||||||
using osu.Game.Input.Bindings;
|
using osu.Game.Input.Bindings;
|
||||||
@ -28,8 +31,6 @@ using osu.Game.Stores;
|
|||||||
using Realms;
|
using Realms;
|
||||||
using Realms.Exceptions;
|
using Realms.Exceptions;
|
||||||
|
|
||||||
#nullable enable
|
|
||||||
|
|
||||||
namespace osu.Game.Database
|
namespace osu.Game.Database
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -46,6 +47,8 @@ namespace osu.Game.Database
|
|||||||
|
|
||||||
private readonly IDatabaseContextFactory? efContextFactory;
|
private readonly IDatabaseContextFactory? efContextFactory;
|
||||||
|
|
||||||
|
private readonly SynchronizationContext? updateThreadSyncContext;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Version history:
|
/// Version history:
|
||||||
/// 6 ~2021-10-18 First tracked version.
|
/// 6 ~2021-10-18 First tracked version.
|
||||||
@ -143,12 +146,15 @@ namespace osu.Game.Database
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="storage">The game storage which will be used to create the realm backing file.</param>
|
/// <param name="storage">The game storage which will be used to create the realm backing file.</param>
|
||||||
/// <param name="filename">The filename to use for the realm backing file. A ".realm" extension will be added automatically if not specified.</param>
|
/// <param name="filename">The filename to use for the realm backing file. A ".realm" extension will be added automatically if not specified.</param>
|
||||||
|
/// <param name="updateThread">The game update thread, used to post realm operations into a thread-safe context.</param>
|
||||||
/// <param name="efContextFactory">An EF factory used only for migration purposes.</param>
|
/// <param name="efContextFactory">An EF factory used only for migration purposes.</param>
|
||||||
public RealmAccess(Storage storage, string filename, IDatabaseContextFactory? efContextFactory = null)
|
public RealmAccess(Storage storage, string filename, GameThread? updateThread = null, IDatabaseContextFactory? efContextFactory = null)
|
||||||
{
|
{
|
||||||
this.storage = storage;
|
this.storage = storage;
|
||||||
this.efContextFactory = efContextFactory;
|
this.efContextFactory = efContextFactory;
|
||||||
|
|
||||||
|
updateThreadSyncContext = updateThread?.SynchronizationContext ?? SynchronizationContext.Current;
|
||||||
|
|
||||||
Filename = filename;
|
Filename = filename;
|
||||||
|
|
||||||
if (!Filename.EndsWith(realm_extension, StringComparison.Ordinal))
|
if (!Filename.EndsWith(realm_extension, StringComparison.Ordinal))
|
||||||
@ -379,9 +385,6 @@ namespace osu.Game.Database
|
|||||||
public IDisposable RegisterForNotifications<T>(Func<Realm, IQueryable<T>> query, NotificationCallbackDelegate<T> callback)
|
public IDisposable RegisterForNotifications<T>(Func<Realm, IQueryable<T>> query, NotificationCallbackDelegate<T> callback)
|
||||||
where T : RealmObjectBase
|
where T : RealmObjectBase
|
||||||
{
|
{
|
||||||
if (!ThreadSafety.IsUpdateThread)
|
|
||||||
throw new InvalidOperationException(@$"{nameof(RegisterForNotifications)} must be called from the update thread.");
|
|
||||||
|
|
||||||
lock (realmLock)
|
lock (realmLock)
|
||||||
{
|
{
|
||||||
Func<Realm, IDisposable?> action = realm => query(realm).QueryAsyncWithNotifications(callback);
|
Func<Realm, IDisposable?> action = realm => query(realm).QueryAsyncWithNotifications(callback);
|
||||||
@ -459,23 +462,24 @@ namespace osu.Game.Database
|
|||||||
/// <returns>An <see cref="IDisposable"/> which should be disposed to unsubscribe any inner subscription.</returns>
|
/// <returns>An <see cref="IDisposable"/> which should be disposed to unsubscribe any inner subscription.</returns>
|
||||||
public IDisposable RegisterCustomSubscription(Func<Realm, IDisposable?> action)
|
public IDisposable RegisterCustomSubscription(Func<Realm, IDisposable?> action)
|
||||||
{
|
{
|
||||||
if (!ThreadSafety.IsUpdateThread)
|
if (updateThreadSyncContext == null)
|
||||||
throw new InvalidOperationException(@$"{nameof(RegisterForNotifications)} must be called from the update thread.");
|
throw new InvalidOperationException("Attempted to register a realm subscription before update thread registration.");
|
||||||
|
|
||||||
var syncContext = SynchronizationContext.Current;
|
|
||||||
|
|
||||||
total_subscriptions.Value++;
|
total_subscriptions.Value++;
|
||||||
|
|
||||||
registerSubscription(action);
|
if (ThreadSafety.IsUpdateThread)
|
||||||
|
updateThreadSyncContext.Send(_ => registerSubscription(action), null);
|
||||||
|
else
|
||||||
|
updateThreadSyncContext.Post(_ => registerSubscription(action), null);
|
||||||
|
|
||||||
// This token is returned to the consumer.
|
// This token is returned to the consumer.
|
||||||
// When disposed, it will cause the registration to be permanently ceased (unsubscribed with realm and unregistered by this class).
|
// When disposed, it will cause the registration to be permanently ceased (unsubscribed with realm and unregistered by this class).
|
||||||
return new InvokeOnDisposal(() =>
|
return new InvokeOnDisposal(() =>
|
||||||
{
|
{
|
||||||
if (ThreadSafety.IsUpdateThread)
|
if (ThreadSafety.IsUpdateThread)
|
||||||
syncContext.Send(_ => unsubscribe(), null);
|
updateThreadSyncContext.Send(_ => unsubscribe(), null);
|
||||||
else
|
else
|
||||||
syncContext.Post(_ => unsubscribe(), null);
|
updateThreadSyncContext.Post(_ => unsubscribe(), null);
|
||||||
|
|
||||||
void unsubscribe()
|
void unsubscribe()
|
||||||
{
|
{
|
||||||
|
@ -200,7 +200,7 @@ namespace osu.Game
|
|||||||
if (Storage.Exists(DatabaseContextFactory.DATABASE_NAME))
|
if (Storage.Exists(DatabaseContextFactory.DATABASE_NAME))
|
||||||
dependencies.Cache(EFContextFactory = new DatabaseContextFactory(Storage));
|
dependencies.Cache(EFContextFactory = new DatabaseContextFactory(Storage));
|
||||||
|
|
||||||
dependencies.Cache(realm = new RealmAccess(Storage, "client", EFContextFactory));
|
dependencies.Cache(realm = new RealmAccess(Storage, "client", Host.UpdateThread, EFContextFactory));
|
||||||
|
|
||||||
dependencies.CacheAs<RulesetStore>(RulesetStore = new RealmRulesetStore(realm, Storage));
|
dependencies.CacheAs<RulesetStore>(RulesetStore = new RealmRulesetStore(realm, Storage));
|
||||||
dependencies.CacheAs<IRulesetStore>(RulesetStore);
|
dependencies.CacheAs<IRulesetStore>(RulesetStore);
|
||||||
|
Loading…
Reference in New Issue
Block a user