summaryrefslogtreecommitdiffstats
path: root/Liaison/WriterThreadPool.m
blob: 6aefc4443ea21096ba99432b1e04d44d05dc9a38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
//  WriterThreadPool.m
//  Liaison
//
//  Created by Brian Cully on Wed Feb 26 2003.
//  Copyright (c) 2003 Brian Cully. All rights reserved.
//

#import "WriterThreadPool.h"

#import "WriterThread.h"

#import <signal.h>

@implementation WriterThreadPool
static WriterThreadPool *sharedPool = nil;

+ (WriterThreadPool *)sharedPool
{
    if (sharedPool == nil)
        sharedPool = [[WriterThreadPool alloc] init];
    return sharedPool;
}

void ign_handler()
{
    return;
}

- (id)init
{
    NSNotificationCenter *defaultCenter;
    struct sigaction ign_action;
    
    self = [super init];

    ign_action.sa_handler = ign_handler;
    sigemptyset(&ign_action.sa_mask);
    ign_action.sa_flags = SA_RESTART;
    sigaction(SIGPIPE, &ign_action, NULL);
    
    theWriterThreads = [[NSMutableDictionary alloc] init];

    defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver: self
                      selector: @selector(writerThreadDied:)
                          name: WriterThreadDied
                        object: nil];

    return self;
}

- (void)dealloc
{
    [theWriterThreads release];
    [super dealloc];
}

- (void)writeData: (NSData *)someData to: (NSFileHandle *)aFileHandle
{
    NSNumber *fd;
    WriterThread *writer;

    fd = [NSNumber numberWithInt: [aFileHandle fileDescriptor]];
    writer = [theWriterThreads objectForKey: fd];
    if (writer == nil) {
        writer = [[WriterThread alloc] initWithFileHandle: aFileHandle];
        [theWriterThreads setObject: writer forKey: fd];
        [writer release];
    }
    [writer writeData: someData];
}

- (void)killThreadFor: (NSFileHandle *)aFileHandle
{
    NSNumber *fd;
    WriterThread *writer;

    fd = [NSNumber numberWithInt: [aFileHandle fileDescriptor]];
    writer = [theWriterThreads objectForKey: fd];
    [writer die];
}

- (void)writerThreadDied: (NSNotification *)aNotification
{
    NSNumber *fd;
    WriterThread *writer;
    
    writer = [aNotification object];
        
    fd = [[aNotification userInfo] objectForKey: @"FileDescriptorKey"];
    [theWriterThreads removeObjectForKey: fd];
}
@synthesize theWriterThreads;
@end