summaryrefslogtreecommitdiff
path: root/pr/src/io/prpolevt.c
blob: e39e539fe67c18720d835205c73eb32a35049815 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "NPL"); you may not use this file except in
 * compliance with the NPL.  You may obtain a copy of the NPL at
 * http://www.mozilla.org/NPL/
 * 
 * Software distributed under the NPL is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
 * for the specific language governing rights and limitations under the
 * NPL.
 * 
 * The Initial Developer of this code under the NPL is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
 * Reserved.
 */

/*
 *********************************************************************
 *
 * Pollable events
 *
 * Pollable events are implemented using layered I/O.  The only
 * I/O methods that are implemented for pollable events are poll
 * and close.  No other methods can be invoked on a pollable
 * event.
 *
 * A pipe or socket pair is created and the pollable event layer
 * is pushed onto the read end.  A pointer to the write end is
 * saved in the PRFilePrivate structure of the pollable event.
 *
 *********************************************************************
 */

#include "prinit.h"
#include "prio.h"
#include "prmem.h"
#include "prerror.h"
#include "prlog.h"

#ifdef VMS

/*
 * On OpenVMS we use an event flag instead of a pipe or a socket since
 * event flags are much more efficient on OpenVMS.
 */
#include "pprio.h"
#include <lib$routines.h>
#include <starlet.h>
#include <stsdef.h>

PR_IMPLEMENT(PRFileDesc *) PR_NewPollableEvent(void)
{
    unsigned int status;
    int flag = -1;
    PRFileDesc *event;

    /*
    ** Allocate an event flag and clear it.
    */
    status = lib$get_ef(&flag);
    if ((!$VMS_STATUS_SUCCESS(status)) || (flag == -1)) {
        PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, status);
        return NULL;
    }
    sys$clref(flag);

    /*
    ** Give NSPR the event flag's negative value. We do this because our
    ** select interprets a negative fd as an event flag rather than a
    ** regular file fd.
    */
    event = PR_CreateSocketPollFd(-flag);
    if (NULL == event) {
        lib$free_ef(&flag);
        return NULL;
    }

    return event;
}

PR_IMPLEMENT(PRStatus) PR_DestroyPollableEvent(PRFileDesc *event)
{
    int flag = -PR_FileDesc2NativeHandle(event);
    PR_DestroySocketPollFd(event);
    lib$free_ef(&flag);
    return PR_SUCCESS;
}

PR_IMPLEMENT(PRStatus) PR_SetPollableEvent(PRFileDesc *event)
{
    /*
    ** Just set the event flag.
    */
    unsigned int status;
    status = sys$setef(-PR_FileDesc2NativeHandle(event));
    if (!$VMS_STATUS_SUCCESS(status)) {
        PR_SetError(PR_INVALID_ARGUMENT_ERROR, status);
        return PR_FAILURE;
    }
    return PR_SUCCESS;
}

PR_IMPLEMENT(PRStatus) PR_WaitForPollableEvent(PRFileDesc *event)
{
    /*
    ** Just clear the event flag.
    */
    unsigned int status;
    status = sys$clref(-PR_FileDesc2NativeHandle(event));
    if (!$VMS_STATUS_SUCCESS(status)) {
        PR_SetError(PR_INVALID_ARGUMENT_ERROR, status);
        return PR_FAILURE;
    }
    return PR_SUCCESS;
}

#elif defined (XP_MAC)

#include "primpl.h"

/*
 * On Mac, local sockets cannot be used, because the networking stack
 * closes them when the machine goes to sleep. Instead, we'll use a simple
 * flag.
 */


/*
 * PRFilePrivate structure for the NSPR pollable events layer
 */
typedef struct PRPollableDesc {
    PRBool      gotEvent;
    PRThread    *pollingThread;
} PRPollableDesc;

static PRStatus PR_CALLBACK _pr_MacPolEvtClose(PRFileDesc *fd);

static PRInt16 PR_CALLBACK _pr_MacPolEvtPoll(
    PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags);

static PRIOMethods _pr_mac_polevt_methods = {
    PR_DESC_LAYERED,
    _pr_MacPolEvtClose,
    (PRReadFN)_PR_InvalidInt,
    (PRWriteFN)_PR_InvalidInt,
    (PRAvailableFN)_PR_InvalidInt,
    (PRAvailable64FN)_PR_InvalidInt64,
    (PRFsyncFN)_PR_InvalidStatus,
    (PRSeekFN)_PR_InvalidInt,
    (PRSeek64FN)_PR_InvalidInt64,
    (PRFileInfoFN)_PR_InvalidStatus,
    (PRFileInfo64FN)_PR_InvalidStatus,
    (PRWritevFN)_PR_InvalidInt,        
    (PRConnectFN)_PR_InvalidStatus,        
    (PRAcceptFN)_PR_InvalidDesc,        
    (PRBindFN)_PR_InvalidStatus,        
    (PRListenFN)_PR_InvalidStatus,        
    (PRShutdownFN)_PR_InvalidStatus,    
    (PRRecvFN)_PR_InvalidInt,        
    (PRSendFN)_PR_InvalidInt,        
    (PRRecvfromFN)_PR_InvalidInt,    
    (PRSendtoFN)_PR_InvalidInt,        
    _pr_MacPolEvtPoll,
    (PRAcceptreadFN)_PR_InvalidInt,   
    (PRTransmitfileFN)_PR_InvalidInt, 
    (PRGetsocknameFN)_PR_InvalidStatus,    
    (PRGetpeernameFN)_PR_InvalidStatus,    
    (PRReservedFN)_PR_InvalidInt,    
    (PRReservedFN)_PR_InvalidInt,    
    (PRGetsocketoptionFN)_PR_InvalidStatus,
    (PRSetsocketoptionFN)_PR_InvalidStatus,
    (PRSendfileFN)_PR_InvalidInt, 
    (PRReservedFN)_PR_InvalidInt, 
    (PRReservedFN)_PR_InvalidInt, 
    (PRReservedFN)_PR_InvalidInt, 
    (PRReservedFN)_PR_InvalidInt, 
    (PRReservedFN)_PR_InvalidInt
};

static PRDescIdentity _pr_mac_polevt_id;
static PRCallOnceType _pr_mac_polevt_once_control;
static PRStatus PR_CALLBACK _pr_MacPolEvtInit(void);

static PRInt16 PR_CALLBACK _pr_MacPolEvtPoll(
    PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags)
{
    PRPollableDesc   *pollDesc = (PRPollableDesc *)fd->secret;
    PR_ASSERT(pollDesc);

    // set the current thread so that we can wake up the poll thread
    pollDesc->pollingThread = PR_GetCurrentThread();

    if ((in_flags & PR_POLL_READ) && pollDesc->gotEvent)
        *out_flags = PR_POLL_READ;
    else
        *out_flags = 0;
    return pollDesc->gotEvent ? 1 : 0;
}

static PRStatus PR_CALLBACK _pr_MacPolEvtInit(void)
{
    _pr_mac_polevt_id = PR_GetUniqueIdentity("NSPR pollable events");
    if (PR_INVALID_IO_LAYER == _pr_mac_polevt_id) {
        return PR_FAILURE;
    }
    return PR_SUCCESS;
}

static PRStatus PR_CALLBACK _pr_MacPolEvtClose(PRFileDesc *fd)
{
    PRPollableDesc   *pollDesc = (PRPollableDesc *)fd->secret;
    PR_ASSERT(NULL == fd->higher && NULL == fd->lower);
    PR_ASSERT(pollDesc);
    PR_DELETE(pollDesc);
    fd->dtor(fd);
    return PR_SUCCESS;
}

PR_IMPLEMENT(PRFileDesc *) PR_NewPollableEvent(void)
{
    PRFileDesc      *event;
    PRPollableDesc   *pollDesc;
    
    if (PR_CallOnce(&_pr_mac_polevt_once_control, _pr_MacPolEvtInit) == PR_FAILURE) {
        return NULL;
    }

    event = PR_CreateIOLayerStub(_pr_mac_polevt_id, &_pr_mac_polevt_methods);
    if (NULL == event) {
        return NULL;
    } 

    /*
    ** Allocate an event flag and clear it.
    */
    pollDesc = PR_NEW(PRPollableDesc);
    if (!pollDesc) {
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        goto errorExit;
    }

    pollDesc->gotEvent = PR_FALSE;
    pollDesc->pollingThread = NULL;
    
    event->secret = (PRFilePrivate*)pollDesc;
    return event;

errorExit:

    if (event) {
        PR_DELETE(event->secret);
        event->dtor(event);
    }
    return NULL;
}

PR_IMPLEMENT(PRStatus) PR_DestroyPollableEvent(PRFileDesc *event)
{
    return PR_Close(event);
}

/* from macsockotpt.c. I wish there was a cleaner way */
extern void WakeUpNotifiedThread(PRThread *thread, OTResult result);

PR_IMPLEMENT(PRStatus) PR_SetPollableEvent(PRFileDesc *event)
{
    PRPollableDesc   *pollDesc = (PRPollableDesc *)event->secret;
    PR_ASSERT(pollDesc);
    PR_ASSERT(pollDesc->pollingThread->state != _PR_DEAD_STATE);
    
    if (pollDesc->pollingThread->state == _PR_DEAD_STATE)
      return PR_FAILURE;

    pollDesc->gotEvent = PR_TRUE;
    
    if (pollDesc->pollingThread)
        WakeUpNotifiedThread(pollDesc->pollingThread, noErr);
        
    return PR_SUCCESS;
}

PR_IMPLEMENT(PRStatus) PR_WaitForPollableEvent(PRFileDesc *event)
{
    PRPollableDesc   *pollDesc = (PRPollableDesc *)event->secret;
    PRStatus status;    
    PR_ASSERT(pollDesc);
    
    /*
      FIXME: Danger Will Robinson!
      
      The current implementation of PR_WaitForPollableEvent is somewhat
      bogus; it makes the assumption that, in Mozilla, this will only
      ever be called when PR_Poll has returned, telling us that an
      event has been set.
    */
    
    PR_ASSERT(pollDesc->gotEvent);
    
    status = (pollDesc->gotEvent) ? PR_SUCCESS : PR_FAILURE;
    pollDesc->gotEvent = PR_FALSE;
    return status;    
}

#else /* VMS */

/*
 * These internal functions are declared in primpl.h,
 * but we can't include primpl.h because the definition
 * of struct PRFilePrivate in this file (for the pollable
 * event layer) will conflict with the definition of
 * struct PRFilePrivate in primpl.h (for the NSPR layer).
 */
extern PRIntn _PR_InvalidInt(void);
extern PRInt64 _PR_InvalidInt64(void);
extern PRStatus _PR_InvalidStatus(void);
extern PRFileDesc *_PR_InvalidDesc(void);

/*
 * PRFilePrivate structure for the NSPR pollable events layer
 */
struct PRFilePrivate {
    PRFileDesc *writeEnd;  /* the write end of the pipe/socketpair */
};

static PRStatus PR_CALLBACK _pr_PolEvtClose(PRFileDesc *fd);

static PRInt16 PR_CALLBACK _pr_PolEvtPoll(
    PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags);

static PRIOMethods _pr_polevt_methods = {
    PR_DESC_LAYERED,
    _pr_PolEvtClose,
    (PRReadFN)_PR_InvalidInt,
    (PRWriteFN)_PR_InvalidInt,
    (PRAvailableFN)_PR_InvalidInt,
    (PRAvailable64FN)_PR_InvalidInt64,
    (PRFsyncFN)_PR_InvalidStatus,
    (PRSeekFN)_PR_InvalidInt,
    (PRSeek64FN)_PR_InvalidInt64,
    (PRFileInfoFN)_PR_InvalidStatus,
    (PRFileInfo64FN)_PR_InvalidStatus,
    (PRWritevFN)_PR_InvalidInt,        
    (PRConnectFN)_PR_InvalidStatus,        
    (PRAcceptFN)_PR_InvalidDesc,        
    (PRBindFN)_PR_InvalidStatus,        
    (PRListenFN)_PR_InvalidStatus,        
    (PRShutdownFN)_PR_InvalidStatus,    
    (PRRecvFN)_PR_InvalidInt,        
    (PRSendFN)_PR_InvalidInt,        
    (PRRecvfromFN)_PR_InvalidInt,    
    (PRSendtoFN)_PR_InvalidInt,        
    _pr_PolEvtPoll,
    (PRAcceptreadFN)_PR_InvalidInt,   
    (PRTransmitfileFN)_PR_InvalidInt, 
    (PRGetsocknameFN)_PR_InvalidStatus,    
    (PRGetpeernameFN)_PR_InvalidStatus,    
    (PRReservedFN)_PR_InvalidInt,    
    (PRReservedFN)_PR_InvalidInt,    
    (PRGetsocketoptionFN)_PR_InvalidStatus,
    (PRSetsocketoptionFN)_PR_InvalidStatus,
    (PRSendfileFN)_PR_InvalidInt, 
    (PRReservedFN)_PR_InvalidInt, 
    (PRReservedFN)_PR_InvalidInt, 
    (PRReservedFN)_PR_InvalidInt, 
    (PRReservedFN)_PR_InvalidInt, 
    (PRReservedFN)_PR_InvalidInt
};

static PRDescIdentity _pr_polevt_id;
static PRCallOnceType _pr_polevt_once_control;
static PRStatus PR_CALLBACK _pr_PolEvtInit(void);

static PRInt16 PR_CALLBACK _pr_PolEvtPoll(
    PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags)
{
    return (fd->lower->methods->poll)(fd->lower, in_flags, out_flags);
}

static PRStatus PR_CALLBACK _pr_PolEvtInit(void)
{
    _pr_polevt_id = PR_GetUniqueIdentity("NSPR pollable events");
    if (PR_INVALID_IO_LAYER == _pr_polevt_id) {
        return PR_FAILURE;
    }
    return PR_SUCCESS;
}

#if !defined(XP_UNIX)
#define USE_TCP_SOCKETPAIR
#endif

PR_IMPLEMENT(PRFileDesc *) PR_NewPollableEvent(void)
{
    PRFileDesc *event;
    PRFileDesc *fd[2]; /* fd[0] is the read end; fd[1] is the write end */
#ifdef USE_TCP_SOCKETPAIR
    PRSocketOptionData socket_opt;
    PRStatus rv;
#endif

    fd[0] = fd[1] = NULL;

    if (PR_CallOnce(&_pr_polevt_once_control, _pr_PolEvtInit) == PR_FAILURE) {
        return NULL;
    }

    event = PR_CreateIOLayerStub(_pr_polevt_id, &_pr_polevt_methods);
    if (NULL == event) {
        goto errorExit;
    } 
    event->secret = PR_NEW(PRFilePrivate);
    if (event->secret == NULL) {
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        goto errorExit;
    }

#ifndef USE_TCP_SOCKETPAIR
    if (PR_CreatePipe(&fd[0], &fd[1]) == PR_FAILURE) {
        fd[0] = fd[1] = NULL;
        goto errorExit;
    }
#else
    if (PR_NewTCPSocketPair(fd) == PR_FAILURE) {
        fd[0] = fd[1] = NULL;
        goto errorExit;
    }
	/*
	 * set the TCP_NODELAY option to reduce notification latency
	 */
    socket_opt.option = PR_SockOpt_NoDelay;
    socket_opt.value.no_delay = PR_TRUE;
    rv = PR_SetSocketOption(fd[1], &socket_opt);
    PR_ASSERT(PR_SUCCESS == rv);
#endif

    event->secret->writeEnd = fd[1];
    if (PR_PushIOLayer(fd[0], PR_TOP_IO_LAYER, event) == PR_FAILURE) {
        goto errorExit;
    }

    return fd[0];

errorExit:
    if (fd[0]) {
        PR_Close(fd[0]);
        PR_Close(fd[1]);
    }
    if (event) {
        PR_DELETE(event->secret);
        event->dtor(event);
    }
    return NULL;
}

static PRStatus PR_CALLBACK _pr_PolEvtClose(PRFileDesc *fd)
{
    PRFileDesc *event;

    event = PR_PopIOLayer(fd, PR_TOP_IO_LAYER);
    PR_ASSERT(NULL == event->higher && NULL == event->lower);
    PR_Close(fd);
    PR_Close(event->secret->writeEnd);
    PR_DELETE(event->secret);
    event->dtor(event);
    return PR_SUCCESS;
}

PR_IMPLEMENT(PRStatus) PR_DestroyPollableEvent(PRFileDesc *event)
{
    return PR_Close(event);
}

static const char magicChar = '\x38';

PR_IMPLEMENT(PRStatus) PR_SetPollableEvent(PRFileDesc *event)
{
    if (PR_Write(event->secret->writeEnd, &magicChar, 1) != 1) {
        return PR_FAILURE;
    }
    return PR_SUCCESS;
}

PR_IMPLEMENT(PRStatus) PR_WaitForPollableEvent(PRFileDesc *event)
{
    char buf[1024];
    PRInt32 nBytes;
#ifdef DEBUG
    PRIntn i;
#endif

    nBytes = PR_Read(event->lower, buf, sizeof(buf));
    if (nBytes == -1) {
        return PR_FAILURE;
    }

#ifdef DEBUG
    /*
     * Make sure people do not write to the pollable event fd
     * directly.
     */
    for (i = 0; i < nBytes; i++) {
        PR_ASSERT(buf[i] == magicChar);
    }
#endif

    return PR_SUCCESS;
}

#endif /* VMS */