summaryrefslogtreecommitdiffstats
path: root/private/os2/server/srvsem.c
blob: e58f0354e54719e9666d60312ff80c3422d7bfb2 (plain) (blame)
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
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    srvsem.c

Abstract:

    Semaphore API

Author:

    Steve Wood (stevewo) 11-Oct-1989

Revision History:

--*/

#define INCL_OS2V20_SEMAPHORES
#define INCL_OS2V20_ERRORS
#include "os2srv.h"

//
// These are dummy variable & dummy routine for the use of two macros
// AcquireHandleTableLock and ReleaseHandleTableLock. In the server,
// these macros should always work, so we set them both to NULL.
//
BOOLEAN  Od2SigHandlingInProgress = (BOOLEAN)NULL;
ULONG Od2ThreadId()
{
    return((ULONG)NULL);
}

NTSTATUS
Os2InitializeSemaphores( VOID )
{
    Os2SharedSemaphoreTable =
        Or2CreateHandleTable( Os2Heap, sizeof( OS2_SEMAPHORE ), 32 );

    if (Os2SharedSemaphoreTable == NULL) {
        return( STATUS_NO_MEMORY );
        }
    else {
        return( STATUS_SUCCESS );
        }
}

BOOLEAN
Os2SemaphoreCreateProcedure(
    IN POS2_SEMAPHORE Semaphore,
    IN POS2_SEMAPHORE NewSemaphore
    )
{
    if (Semaphore->Name.Length != 0) {
        if (RtlEqualString( &Semaphore->Name, &NewSemaphore->Name, FALSE )) {
            return( TRUE );
            }
        }

    return( FALSE );
}

BOOLEAN
Os2SemaphoreOpenProcedure(
    IN POS2_SEMAPHORE Semaphore,
    IN POS2_SEMAPHORE OpenSemaphore
    )
{
    if (Semaphore->Name.Length != 0) {
        if (RtlEqualString( &Semaphore->Name, &OpenSemaphore->Name, FALSE )) {
            return( TRUE );
            }
        }
    return( FALSE );
}


APIRET
Os2ProcessSemaphoreName(
    IN PSTRING ObjectName,
    IN POS2_SEMAPHORE Semaphore OPTIONAL,
    OUT PULONG ExistingHandleIndex OPTIONAL
    )
{
    OS2_SEMAPHORE ExistingSemaphore;
    PSZ src, dst;
    USHORT n;
    APIRET rc;

    AcquireHandleTableLock( Os2SharedSemaphoreTable );

    if (ARGUMENT_PRESENT( ExistingHandleIndex )) {
        Semaphore = &ExistingSemaphore;
        }

    rc = NO_ERROR;
    if ((n = ObjectName->Length) == 0) {
        Semaphore->Name.Length = 0;
        Semaphore->Name.MaximumLength = 0;
        Semaphore->Name.Buffer = NULL;
        }
    else {
        dst = RtlAllocateHeap( Os2Heap, 0, n );
        if (dst == NULL) {
            rc = ERROR_NOT_ENOUGH_MEMORY;
            }
        else {
            src = ObjectName->Buffer;
            Semaphore->Name.Length = n;
            Semaphore->Name.MaximumLength = n;
            Semaphore->Name.Buffer = dst;
            while (n--) {
                *dst++ = *src++;
                }

            if (Or2EnumHandleTable(
                    Os2SharedSemaphoreTable,
                    (OR2_ENUMERATE_HANDLE_ROUTINE)
                        (ARGUMENT_PRESENT( ExistingHandleIndex ) ?
                            Os2SemaphoreOpenProcedure :
                            Os2SemaphoreCreateProcedure),
                    (PVOID)Semaphore,
                    ExistingHandleIndex
                    )
               ) {
                if (!ARGUMENT_PRESENT( ExistingHandleIndex )) {
                    rc = ERROR_DUPLICATE_NAME;
                    }
                }
            else {
                if (ARGUMENT_PRESENT( ExistingHandleIndex )) {
                    rc = ERROR_SEM_NOT_FOUND;
                    }
                }

            if (ARGUMENT_PRESENT( ExistingHandleIndex ) || rc != NO_ERROR) {
                RtlFreeHeap( Os2Heap, 0,
                             Semaphore->Name.Buffer
                           );
                }
            }
        }

    return( rc );
}


PVOID
Os2DestroySemaphore(
    IN POS2_SEMAPHORE Semaphore,
    IN ULONG HandleIndex
    )
{
    PVOID Value;

    Value = Semaphore->u.Value;

    if (Semaphore->Name.Length != 0) {
        RtlFreeHeap( Os2Heap, 0,
                     Semaphore->Name.Buffer
                   );
        }

    Or2DestroyHandle( Os2SharedSemaphoreTable,
                               HandleIndex
                             );

    return( Value );
}


POS2_SEMAPHORE
Os2ReferenceSemaphore(
    IN POS2_SEMAPHORE Semaphore
    )
{
    Semaphore->PointerCount++;

    return( Semaphore );
}


VOID
Os2DereferenceSemaphore(
    IN POS2_SEMAPHORE Semaphore
    )
{
    Semaphore->PointerCount--;

    return;
}


VOID
Os2ThreadWaitingOnSemaphore(
    IN POS2_THREAD t,
    IN POS2_SEMAPHORE Semaphore,
    IN BOOLEAN AboutToWait
    )
{
    if (AboutToWait) {
        t->WaitingForSemaphore = Os2ReferenceSemaphore( Semaphore );
        ReleaseHandleTableLock( Os2SharedSemaphoreTable );
        }
    else {
        AcquireHandleTableLock( Os2SharedSemaphoreTable );
        t->WaitingForSemaphore = NULL;
        Os2DereferenceSemaphore( Semaphore );
        ReleaseHandleTableLock( Os2SharedSemaphoreTable );
        }
}


#if DBG

VOID
Os2SemaphoreDumpProcedure(
    IN POS2_SEMAPHORE Semaphore,
    IN ULONG HandleIndex,
    IN PVOID DumpParameter
    )
{
    UNREFERENCED_PARAMETER(DumpParameter);
    DbgPrint( " %3ld   %2ld  %4ld %4ld %8lx    %.*s\n",
              HandleIndex,
              (ULONG)Semaphore->Type,
              (ULONG)Semaphore->OpenCount,
              (ULONG)Semaphore->PointerCount,
              (ULONG)Semaphore->u.Value,
              Semaphore->Name.Length,
              Semaphore->Name.Buffer
            );
    return;
}

VOID
Os2DumpSemaphoreTable(
    IN PCHAR Title
    )
{
    ULONG n;

    if (Os2SharedSemaphoreTable->CountEntries >
        Os2SharedSemaphoreTable->CountFreeEntries
       ) {
        DbgPrint( "\nDump Of OS/2 Server Shared Semaphore Table: %s\n", Title );
        DbgPrint( "Index Type Ocnt Pcnt   Value   Name\n" );
        n = Or2DumpHandleTable(
                Os2SharedSemaphoreTable,
                (OR2_DUMP_HANDLE_ROUTINE)Os2SemaphoreDumpProcedure,
                NULL
                );

        DbgPrint( "Total number of valid shared semaphores: %ld\n", n );
        }
}

#endif // DBG