Skip to content

Commit aa5570c

Browse files
committed
improve locking and print lock usage
1 parent cfb9005 commit aa5570c

File tree

1 file changed

+82
-61
lines changed

1 file changed

+82
-61
lines changed

Signal-Windows/ViewModels/MainPageViewModel.cs

Lines changed: 82 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ public void AddThread(SignalConversation contact)
7171

7272
internal async void BackButton_Click(object sender, BackRequestedEventArgs e)
7373
{
74+
Debug.WriteLine("MPVMBack lock wait");
7475
using (await ActionInProgress.LockAsync())
7576
{
77+
Debug.WriteLine("MPVMBack lock grabbed");
7678
SelectedThread = null;
7779
View.Thread.DisposeCurrentThread();
7880
ThreadVisibility = Visibility.Collapsed;
@@ -81,6 +83,7 @@ internal async void BackButton_Click(object sender, BackRequestedEventArgs e)
8183
Utils.DisableBackButton(BackButton_Click);
8284
e.Handled = true;
8385
}
86+
Debug.WriteLine("MPVMBack lock released");
8487
}
8588

8689
public void UIUpdateThread(SignalConversation thread)
@@ -104,92 +107,98 @@ public MainPageViewModel()
104107
public async Task Init()
105108
{
106109
CancelSource = new CancellationTokenSource();
107-
var l = ActionInProgress.Lock(CancelSource.Token);
108-
try
110+
Debug.WriteLine("Init lock wait");
111+
using (await ActionInProgress.LockAsync(CancelSource.Token))
109112
{
110-
await Task.Run(async () =>
113+
Debug.WriteLine("Init lock grabbed");
114+
try
111115
{
112-
SignalDBContext.FailAllPendingMessages();
113-
List<SignalContact> contacts = SignalDBContext.GetAllContactsLocked();
114-
List<SignalGroup> groups = SignalDBContext.GetAllGroupsLocked();
115-
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
116+
await Task.Run(async () =>
116117
{
117-
try
118+
SignalDBContext.FailAllPendingMessages();
119+
List<SignalContact> contacts = SignalDBContext.GetAllContactsLocked();
120+
List<SignalGroup> groups = SignalDBContext.GetAllGroupsLocked();
121+
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
118122
{
119-
int amountContacts = contacts.Count;
120-
int amountGroups = groups.Count;
121-
int contactsIdx = 0;
122-
int groupsIdx = 0;
123-
while (contactsIdx < amountContacts || groupsIdx < amountGroups)
123+
try
124124
{
125-
if (contactsIdx < amountContacts)
125+
int amountContacts = contacts.Count;
126+
int amountGroups = groups.Count;
127+
int contactsIdx = 0;
128+
int groupsIdx = 0;
129+
while (contactsIdx < amountContacts || groupsIdx < amountGroups)
126130
{
127-
SignalConversation contact = contacts[contactsIdx];
128-
if (groupsIdx < amountGroups)
131+
if (contactsIdx < amountContacts)
129132
{
130-
SignalConversation group = groups[groupsIdx];
131-
if (contact.LastActiveTimestamp > group.LastActiveTimestamp)
133+
SignalConversation contact = contacts[contactsIdx];
134+
if (groupsIdx < amountGroups)
132135
{
133-
contactsIdx++;
134-
AddThread(contact);
136+
SignalConversation group = groups[groupsIdx];
137+
if (contact.LastActiveTimestamp > group.LastActiveTimestamp)
138+
{
139+
contactsIdx++;
140+
AddThread(contact);
141+
}
142+
else
143+
{
144+
groupsIdx++;
145+
AddThread(group);
146+
}
135147
}
136148
else
137149
{
138-
groupsIdx++;
139-
AddThread(group);
150+
contactsIdx++;
151+
AddThread(contact);
140152
}
141153
}
142-
else
154+
else if (groupsIdx < amountGroups)
143155
{
144-
contactsIdx++;
145-
AddThread(contact);
156+
SignalConversation group = groups[groupsIdx];
157+
groupsIdx++;
158+
AddThread(group);
146159
}
147160
}
148-
else if (groupsIdx < amountGroups)
149-
{
150-
SignalConversation group = groups[groupsIdx];
151-
groupsIdx++;
152-
AddThread(group);
153-
}
154161
}
155-
}
156-
catch (Exception e)
157-
{
158-
Debug.WriteLine(e.Message);
159-
Debug.WriteLine(e.StackTrace);
160-
}
162+
catch (Exception e)
163+
{
164+
Debug.WriteLine(e.Message);
165+
Debug.WriteLine(e.StackTrace);
166+
}
167+
});
168+
MessageReceiver = new SignalServiceMessageReceiver(CancelSource.Token, App.ServiceUrls, new StaticCredentialsProvider(App.Store.Username, App.Store.Password, App.Store.SignalingKey, (int)App.Store.DeviceId), App.USER_AGENT);
169+
Pipe = MessageReceiver.createMessagePipe();
170+
MessageSender = new SignalServiceMessageSender(CancelSource.Token, App.ServiceUrls, App.Store.Username, App.Store.Password, (int)App.Store.DeviceId, new Store(), Pipe, null, App.USER_AGENT);
171+
IncomingMessagesTask = Task.Factory.StartNew(HandleIncomingMessages, TaskCreationOptions.LongRunning);
172+
OutgoingMessagesTask = Task.Factory.StartNew(HandleOutgoingMessages, TaskCreationOptions.LongRunning);
161173
});
162-
MessageReceiver = new SignalServiceMessageReceiver(CancelSource.Token, App.ServiceUrls, new StaticCredentialsProvider(App.Store.Username, App.Store.Password, App.Store.SignalingKey, (int)App.Store.DeviceId), App.USER_AGENT);
163-
Pipe = MessageReceiver.createMessagePipe();
164-
MessageSender = new SignalServiceMessageSender(CancelSource.Token, App.ServiceUrls, App.Store.Username, App.Store.Password, (int)App.Store.DeviceId, new Store(), Pipe, null, App.USER_AGENT);
165-
IncomingMessagesTask = Task.Factory.StartNew(HandleIncomingMessages, TaskCreationOptions.LongRunning);
166-
OutgoingMessagesTask = Task.Factory.StartNew(HandleOutgoingMessages, TaskCreationOptions.LongRunning);
167-
});
168-
}
169-
catch(AuthorizationFailedException)
170-
{
171-
Debug.WriteLine("OWS server rejected our credentials - redirecting to StartPage");
172-
View.Frame.Navigate(typeof(StartPage));
173-
}
174-
catch (Exception e)
175-
{
176-
Debug.WriteLine(e.Message);
177-
Debug.WriteLine(e.StackTrace);
178-
}
179-
finally
180-
{
181-
l.Dispose();
174+
}
175+
catch (AuthorizationFailedException)
176+
{
177+
Debug.WriteLine("OWS server rejected our credentials - redirecting to StartPage");
178+
View.Frame.Navigate(typeof(StartPage));
179+
}
180+
catch (Exception e)
181+
{
182+
Debug.WriteLine(e.Message);
183+
Debug.WriteLine(e.StackTrace);
184+
}
182185
}
186+
Debug.WriteLine("Init lock released");
183187
}
184188

185189
public async Task Shutdown()
186190
{
187191
Running = false;
188192
App.MainPageActive = false;
189-
var l = await ActionInProgress.LockAsync();
190-
CancelSource.Cancel();
191-
await IncomingMessagesTask;
192-
await OutgoingMessagesTask;
193+
Debug.WriteLine("Shutdown lock await");
194+
using (await ActionInProgress.LockAsync())
195+
{
196+
Debug.WriteLine("Shutdown lock grabbed");
197+
CancelSource.Cancel();
198+
await IncomingMessagesTask;
199+
await OutgoingMessagesTask;
200+
}
201+
Debug.WriteLine("Shutdown lock released");
193202
}
194203

195204
internal async void ContactsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
@@ -198,8 +207,10 @@ internal async void ContactsList_SelectionChanged(object sender, SelectionChange
198207
{
199208
try
200209
{
210+
Debug.WriteLine("SelectionChanged lock await");
201211
using (await ActionInProgress.LockAsync())
202212
{
213+
Debug.WriteLine("SelectionChanged lock grabbed");
203214
WelcomeVisibility = Visibility.Collapsed;
204215
ThreadVisibility = Visibility.Visible;
205216
SelectedThread = (SignalConversation)e.AddedItems[0];
@@ -212,6 +223,7 @@ internal async void ContactsList_SelectionChanged(object sender, SelectionChange
212223
UIResetRead(conversation);
213224
View.Thread.ScrollToBottom();
214225
}
226+
Debug.WriteLine("SelectionChanged lock released");
215227
}
216228
catch (Exception ex)
217229
{
@@ -244,8 +256,10 @@ internal async Task TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
244256
Read = true,
245257
Type = SignalMessageType.Normal
246258
};
259+
Debug.WriteLine("keydown lock await");
247260
using (await ActionInProgress.LockAsync())
248261
{
262+
Debug.WriteLine("keydown lock grabbed");
249263
View.Thread.Append(message);
250264
View.Thread.ScrollToBottom();
251265
SelectedThread.LastMessage = message;
@@ -267,6 +281,7 @@ await Task.Run(() =>
267281
Debug.WriteLine("ms until out queue: " + (after - now));
268282
View.Thread.RemoveUnreadMarker();
269283
}
284+
Debug.WriteLine("keydown lock released");
270285
}
271286
}
272287
}
@@ -300,8 +315,10 @@ public void MoveThreadToTop(SignalConversation thread)
300315

301316
public async Task UIHandleIncomingMessage(SignalMessage message)
302317
{
318+
Debug.WriteLine("incoming lock await");
303319
using (await ActionInProgress.LockAsync())
304320
{
321+
Debug.WriteLine("incoming lock grabbed");
305322
var thread = ThreadsDictionary[message.ThreadId];
306323
uint unreadCount = thread.UnreadCount;
307324
if (SelectedThread == thread)
@@ -351,6 +368,7 @@ await Task.Run(() =>
351368
thread.View.UpdateConversationDisplay(thread);
352369
MoveThreadToTop(thread);
353370
}
371+
Debug.WriteLine("incoming lock released");
354372
}
355373

356374
public void UIResetRead(SignalConversation conversation)
@@ -371,8 +389,10 @@ public void UIUpdateMessageBox(SignalMessage updatedMessage)
371389

372390
public async Task UIHandleIdentityKeyChange(string number)
373391
{
392+
Debug.WriteLine("IKChange lock await");
374393
using (await ActionInProgress.LockAsync())
375394
{
395+
Debug.WriteLine("IKChange lock grabbed");
376396
var messages = SignalDBContext.InsertIdentityChangedMessages(number);
377397
foreach (var message in messages)
378398
{
@@ -387,6 +407,7 @@ public async Task UIHandleIdentityKeyChange(string number)
387407
thread.View.UpdateConversationDisplay(thread);
388408
}
389409
}
410+
Debug.WriteLine("IKChange lock released");
390411
}
391412

392413
#endregion UIThread

0 commit comments

Comments
 (0)