9e60c39529caf64803e4884d4aec55f5934f0ff3
[ldk-java] / c_sharp / src / org / ldk / structs / LockableScore.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8 /**
9  * A scorer that is accessed under a lock.
10  * 
11  * Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
12  * having shared ownership of a scorer but without requiring internal locking in [`Score`]
13  * implementations. Internal locking would be detrimental to route finding performance and could
14  * result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
15  * 
16  * [`find_route`]: crate::routing::router::find_route
17  */
18 public class LockableScore : CommonBase {
19         internal readonly bindings.LDKLockableScore bindings_instance;
20         internal LockableScore(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
21         private LockableScore(bindings.LDKLockableScore arg) : base(bindings.LDKLockableScore_new(arg)) {
22                 this.ptrs_to.AddLast(arg);
23                 this.bindings_instance = arg;
24         }
25         ~LockableScore() {
26                 if (ptr != 0) { bindings.LockableScore_free(ptr); }
27         }
28
29         public interface LockableScoreInterface {
30                 /**
31                  * Returns the locked scorer.
32                  */
33                 Score do_lock();
34         }
35         private class LDKLockableScoreHolder { internal LockableScore held; }
36         private class LDKLockableScoreImpl : bindings.LDKLockableScore {
37                 internal LDKLockableScoreImpl(LockableScoreInterface arg, LDKLockableScoreHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
38                 private LockableScoreInterface arg;
39                 private LDKLockableScoreHolder impl_holder;
40                 public long do_lock() {
41                         Score ret = arg.do_lock();
42                                 GC.KeepAlive(arg);
43                         long result = ret == null ? 0 : ret.ptr;
44                         if (impl_holder.held != null) { impl_holder.held.ptrs_to.AddLast(ret); };
45                         return result;
46                 }
47         }
48         public static LockableScore new_impl(LockableScoreInterface arg) {
49                 LDKLockableScoreHolder impl_holder = new LDKLockableScoreHolder();
50                 impl_holder.held = new LockableScore(new LDKLockableScoreImpl(arg, impl_holder));
51                 return impl_holder.held;
52         }
53         /**
54          * Returns the locked scorer.
55          */
56         public Score do_lock() {
57                 long ret = bindings.LockableScore_lock(this.ptr);
58                 GC.KeepAlive(this);
59                 if (ret >= 0 && ret <= 4096) { return null; }
60                 Score ret_hu_conv = new Score(null, ret);
61                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
62                 return ret_hu_conv;
63         }
64
65 }
66 } } }