pEpObjCAdapter/PEPSessionProvider.m
author Dirk Zimmermann <dirk@pep-project.org>
Wed, 11 Jul 2018 18:16:18 +0200
changeset 642 19ca3f566cfa
parent 640 4a7ead33bbd9
child 647 d8830e5d240e
permissions -rw-r--r--
IOS-1154 finer-grained lock/unlock providing main session
andreas@262
     1
//
andreas@262
     2
//  PEPSessionProvider.m
andreas@262
     3
//  pEpObjCAdapter
andreas@262
     4
//
andreas@262
     5
//  Created by Andreas Buff on 09.10.17.
andreas@262
     6
//  Copyright © 2017 p≡p. All rights reserved.
andreas@262
     7
//
andreas@262
     8
andreas@262
     9
#import "PEPSessionProvider.h"
andreas@262
    10
andreas@416
    11
#import "PEPObjCAdapter+Internal.h"
andreas@269
    12
#import "PEPInternalSession.h"
andreas@262
    13
#import "PEPCopyableThread.h"
andreas@262
    14
andreas@262
    15
@implementation PEPSessionProvider
andreas@262
    16
andreas@262
    17
static NSLock *s_sessionForThreadLock = nil;
andreas@269
    18
static NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *s_sessionForThreadDict;
andreas@262
    19
andreas@290
    20
/** We have to conform to the Engines rule: "The first session has to be created on the main thread and kept
andreas@290
    21
 alive until all sessiopns created afterwards have been teared down."
andreas@290
    22
 Here we hold it.
andreas@290
    23
 */
dirk@638
    24
static PEPInternalSession *s_sessionForMainThread = nil;
andreas@290
    25
andreas@262
    26
#pragma mark - Public API
andreas@262
    27
andreas@269
    28
+ (PEPInternalSession * _Nonnull)session
andreas@262
    29
{
andreas@290
    30
    // Assure a session for the main thread exists and is kept alive before anyother session is created.
andreas@290
    31
    [self assureSessionForMainThreadExists];
andreas@290
    32
andreas@290
    33
    if ([NSThread isMainThread]) {
dirk@638
    34
        return s_sessionForMainThread;
andreas@290
    35
    }
andreas@290
    36
dirk@642
    37
    [[self sessionForThreadLock] lock];
andreas@288
    38
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@262
    39
    PEPCopyableThread *currentThread = [[PEPCopyableThread alloc] initWithThread:[NSThread currentThread]];
dirk@550
    40
    PEPInternalSession * __strong newOrExistingSession = dict[currentThread];
dirk@550
    41
    if (!newOrExistingSession) {
dirk@550
    42
        newOrExistingSession = [PEPInternalSession new];
dirk@550
    43
        dict[currentThread] = newOrExistingSession;
andreas@262
    44
    }
dirk@625
    45
dirk@625
    46
    // configuration
dirk@601
    47
    [self setConfigUnEncryptedSubjectOnSession:newOrExistingSession];
dirk@625
    48
    [self setPassiveModeOnSession:newOrExistingSession];
dirk@625
    49
andreas@272
    50
    [self nullifySessionsOfFinishedThreads];
andreas@273
    51
andreas@262
    52
    [[self sessionForThreadLock] unlock];
andreas@273
    53
dirk@550
    54
    return newOrExistingSession;
andreas@262
    55
}
andreas@262
    56
andreas@262
    57
+ (void)cleanup
andreas@262
    58
{
andreas@262
    59
    [[self sessionForThreadLock] lock];
andreas@273
    60
andreas@273
    61
    [self cleanupInternal];
andreas@273
    62
andreas@262
    63
    [[self sessionForThreadLock] unlock];
andreas@262
    64
}
andreas@262
    65
andreas@262
    66
#pragma mark - Life Cycle
andreas@262
    67
andreas@262
    68
+ (void)initialize
andreas@262
    69
{
andreas@262
    70
    s_sessionForThreadLock = [NSLock new];
andreas@262
    71
    s_sessionForThreadDict = [NSMutableDictionary new];
andreas@262
    72
}
andreas@262
    73
andreas@262
    74
#pragma mark - Lock
andreas@262
    75
andreas@262
    76
+ (NSLock *)sessionForThreadLock
andreas@262
    77
{
andreas@262
    78
    return s_sessionForThreadLock;
andreas@262
    79
}
andreas@262
    80
andreas@262
    81
+ (NSMutableDictionary *)sessionForThreadDict
andreas@262
    82
{
andreas@262
    83
    return s_sessionForThreadDict;
andreas@262
    84
}
andreas@262
    85
dirk@625
    86
#pragma mark - configuration
andreas@416
    87
dirk@601
    88
+ (void)setConfigUnEncryptedSubjectOnSession:(PEPInternalSession *)session
andreas@416
    89
{
dirk@601
    90
    BOOL unEncryptedSubjectEnabled = [PEPObjCAdapter unEncryptedSubjectEnabled];
dirk@601
    91
    [session configUnEncryptedSubjectEnabled:unEncryptedSubjectEnabled];
andreas@416
    92
}
andreas@416
    93
dirk@625
    94
+ (void)setPassiveModeOnSession:(PEPInternalSession *)session
dirk@625
    95
{
dirk@625
    96
    BOOL passiveModeEnabled = [PEPObjCAdapter passiveModeEnabled];
dirk@625
    97
    [session configurePassiveModeEnabled:passiveModeEnabled];
dirk@625
    98
}
dirk@625
    99
dirk@625
   100
#pragma mark -
dirk@625
   101
andreas@290
   102
/**
andreas@290
   103
 Assures a session for the main thread is set.
andreas@290
   104
 */
andreas@290
   105
+ (void)assureSessionForMainThreadExists
andreas@290
   106
{
dirk@627
   107
    // shared code to set global configuration every time
dirk@627
   108
    void (^configurationBlock)(void) = ^{
dirk@642
   109
        [[self sessionForThreadLock] lock];
dirk@638
   110
        [self setConfigUnEncryptedSubjectOnSession:s_sessionForMainThread];
dirk@638
   111
        [self setPassiveModeOnSession:s_sessionForMainThread];
dirk@642
   112
        [[self sessionForThreadLock] unlock];
dirk@627
   113
    };
dirk@627
   114
dirk@638
   115
    if (s_sessionForMainThread) {
dirk@627
   116
        configurationBlock();
dirk@549
   117
        return;
dirk@549
   118
    }
dirk@549
   119
dirk@627
   120
    // shared code that is executed in any case, either on the main thread or in the background
dirk@548
   121
    void (^creationBlock)(void) = ^{
dirk@638
   122
        if (s_sessionForMainThread) {
dirk@548
   123
            return;
dirk@548
   124
        }
dirk@642
   125
        [[self sessionForThreadLock] lock];
dirk@638
   126
        s_sessionForMainThread = [PEPInternalSession new];
dirk@642
   127
        [[self sessionForThreadLock] unlock];
dirk@627
   128
        configurationBlock();
dirk@548
   129
    };
dirk@548
   130
andreas@290
   131
andreas@290
   132
    if ([NSThread isMainThread]) {
dirk@548
   133
        creationBlock();
andreas@290
   134
    } else {
dirk@548
   135
        dispatch_sync(dispatch_get_main_queue(), creationBlock);
andreas@290
   136
    }
andreas@290
   137
}
andreas@262
   138
andreas@273
   139
+ (void)cleanupInternal
andreas@273
   140
{
andreas@273
   141
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@273
   142
andreas@273
   143
    for (PEPCopyableThread *thread in dict.allKeys) {
andreas@275
   144
        [self nullifySessionForThread:thread];
andreas@273
   145
    }
dirk@638
   146
    s_sessionForMainThread = nil;
andreas@288
   147
    [dict removeAllObjects];
andreas@273
   148
}
andreas@273
   149
andreas@273
   150
/**
andreas@273
   151
 Tears down all sessions that belong to a thread that has finish executing forever.
andreas@273
   152
 */
andreas@272
   153
+ (void)nullifySessionsOfFinishedThreads
andreas@262
   154
{
andreas@269
   155
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@262
   156
    for (PEPCopyableThread *thread in dict.allKeys) {
andreas@262
   157
        if (thread.isFinished) {
andreas@273
   158
            // The session has been created to run on the one and only thread.
andreas@273
   159
            // As this thread did finish executing forever, we can be sure the session can not be
andreas@273
   160
            // accessed anymore, thus it is OK to nullify it on another thread as the one it has been
andreas@273
   161
            // created for.
andreas@262
   162
            [self nullifySessionForThread:thread];
andreas@262
   163
        }
andreas@262
   164
    }
andreas@262
   165
}
andreas@262
   166
andreas@262
   167
+ (void)nullifySessionForThread:(PEPCopyableThread *)thread
andreas@262
   168
{
andreas@269
   169
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@262
   170
    dict[thread] = nil;
andreas@262
   171
}
andreas@262
   172
andreas@262
   173
@end