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
|
/* $Header$ */
/*
* Copyright © 1988-2004 Keith Packard and Bart Massey.
* All Rights Reserved. See the file COPYING in this directory
* for licensing information.
*/
#include "nickle.h"
extern Bool complete;
Value
do_Semaphore_wait (Value s)
{
ENTER ();
if (aborting)
RETURN (Void);
if (!running->thread.partial) {
--s->semaphore.count;
if (s->semaphore.count < 0)
{
running->thread.partial = 1;
ThreadSleep (running, s, PrioritySync);
RETURN (Void);
}
}
complete = True;
RETURN (Void);
}
Value
do_Semaphore_test (Value s)
{
ENTER ();
if (s->semaphore.count <= 0)
RETURN (FalseVal);
do_Semaphore_wait (s);
RETURN (TrueVal);
}
Value
do_Semaphore_count (Value s)
{
ENTER();
RETURN (NewInt(s->semaphore.count));
}
Value
do_Semaphore_signal (Value s)
{
ENTER ();
if (aborting)
RETURN (Void);
++s->semaphore.count;
if (s->semaphore.count <= 0)
ThreadsWakeup (s, WakeOne);
complete = True;
RETURN (Void);
}
static Bool
SemaphorePrint (Value f, Value av, char format, int base, int width, int prec, int fill)
{
FilePrintf (f, "semaphore %d (%d)", av->semaphore.id, av->semaphore.count);
return True;
}
ValueRep SemaphoreRep = {
{ 0, 0, "SemaphoreRep" }, /* base */
rep_semaphore, /* tag */
{ /* binary */
0,
0,
0,
0,
0,
0,
0,
ValueEqual,
0,
0,
},
{ /* unary */
0,
0,
0,
},
0,
0,
SemaphorePrint,
0,
};
Value
do_Semaphore_new (int n, Value *value)
{
ENTER ();
Value ret;
int count;
static int id;
switch (n) {
case 0:
count = 0;
break;
case 1:
count = IntPart (value[0], "Illegal initial semaphore count");
break;
default:
RaiseStandardException (exception_invalid_argument, 3,
NewStrString ("new: wrong number of arguments"),
NewInt (1), NewInt (n));
RETURN(Void);
}
ret = ALLOCATE (&SemaphoreRep.data, sizeof (Semaphore));
ret->semaphore.count = count;
ret->semaphore.id = ++id;
RETURN (ret);
}
|