File: domlock.c

package info (click to toggle)
tdom 0.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,260 kB
  • sloc: ansic: 56,762; xml: 20,797; tcl: 3,618; sh: 658; makefile: 83; cpp: 30
file content (177 lines) | stat: -rw-r--r-- 4,668 bytes parent folder | download
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
/*----------------------------------------------------------------------------
|   Copyright (C) 1999  Jochen C. Loewer (loewerj@hotmail.com)
+-----------------------------------------------------------------------------
|
|   The contents of this file are subject to the Mozilla Public License
|   Version 2.0 (the "License"); you may not use this file except in
|   compliance with the License. You may obtain a copy of the License at
|   http://www.mozilla.org/MPL/
|
|   Software distributed under the License is distributed on an "AS IS"
|   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|   License for the specific language governing rights and limitations
|   under the License.
|
|   The Original Code is tDOM.
|
|   The Initial Developer of the Original Code is Jochen Loewer
|   Portions created by Jochen Loewer are Copyright (C) 1998, 1999
|   Jochen Loewer. All Rights Reserved.
|
|   This implements many-readers/single-writer locking of DOM documents.
|
|   Contributor(s):
|       May02  Zoran Vasiljevic  Added this file.
|
\---------------------------------------------------------------------------*/

#ifdef TCL_THREADS

#include <dom.h>


/*----------------------------------------------------------------------------
|   Global list of document lock structures. These should be finalized
|   only on application exit.
|
\---------------------------------------------------------------------------*/
static Tcl_Mutex lockMutex = 0;
static domlock *domLocks = NULL;


/*----------------------------------------------------------------------------
|   Lock the document according to passed flag
|
\---------------------------------------------------------------------------*/
void
domLocksLock(domlock *dl, int how)
{

    Tcl_MutexLock(&dl->mutex);

    switch (how) {
    case LOCK_READ:
        while (dl->lrcnt < 0 || dl->numwr > 0) {
            dl->numrd++;
            Tcl_ConditionWait(&dl->rcond, &dl->mutex, NULL);
            dl->numrd--;
        }
        dl->lrcnt++;
        break;

    case LOCK_WRITE:
        while (dl->lrcnt != 0) {
            dl->numwr++;
            Tcl_ConditionWait(&dl->wcond, &dl->mutex, NULL);
            dl->numwr--;
        }
        dl->lrcnt = -1; /* This designates the sole writer */
        break;
    }

    Tcl_MutexUnlock(&dl->mutex);
}


/*----------------------------------------------------------------------------
|   Unlock the previously locked document.
|
\---------------------------------------------------------------------------*/
void
domLocksUnlock(domlock *dl)
{
    Tcl_MutexLock(&dl->mutex);

    if (--dl->lrcnt < 0) {
        dl->lrcnt = 0;
    }
    if (dl->numwr) {
        Tcl_ConditionNotify(&dl->wcond);
    } else if (dl->numrd) {
        Tcl_ConditionNotify(&dl->rcond);
    }

    Tcl_MutexUnlock (&dl->mutex);
}


/*----------------------------------------------------------------------------
|   Associate a lock with the document..
|
\---------------------------------------------------------------------------*/
void
domLocksAttach(domDocument *doc)
{
    domlock *dl;

    Tcl_MutexLock(&lockMutex);
    
    dl = domLocks;
    if (dl == NULL) {
        dl = (domlock*)MALLOC(sizeof(domlock));
        memset(dl, 0, sizeof(domlock));
    } else {
        domLocks = dl->next;
    }

    dl->doc = doc;
    doc->lock = dl;

    Tcl_MutexUnlock(&lockMutex);
}


/*----------------------------------------------------------------------------
|   Divorce DOM document from its lock. The lock structure is not
|   disposed and may be used for locking other documents.
|
\---------------------------------------------------------------------------*/
void
domLocksDetach(domDocument *doc)
{
    domlock *dl = doc->lock;

    Tcl_MutexLock(&lockMutex);

    if (dl->doc != doc) {
        domPanic("document lock mismatch");
    }

    dl->next = domLocks;
    domLocks = dl;

    dl->doc = NULL;
    doc->lock = NULL;

    Tcl_MutexUnlock(&lockMutex);
}


/*----------------------------------------------------------------------------
|   Reclaim storage used for lock structures. This should be done only
|   on application exit.
|
\---------------------------------------------------------------------------*/
void
domLocksFinalize(ClientData UNUSED(dummy))
{
    domlock *tmp, *dl;

    Tcl_MutexLock(&lockMutex);

    dl = domLocks;

    while (dl != NULL) {
        Tcl_MutexFinalize(&dl->mutex);
        Tcl_ConditionFinalize(&dl->rcond);
        Tcl_ConditionFinalize(&dl->wcond);
        tmp = dl;
        dl  = dl->next;
        FREE((char*)tmp);
    }
    domLocks = NULL;

    Tcl_MutexUnlock(&lockMutex);
}

#endif /* TCL_THREADS */