]> git.pond.sub.org Git - empserver/blob - src/lib/lwp/rwlock.c
ea2e13a959e8f519dbac96eb78c84078360ab0bb
[empserver] / src / lib / lwp / rwlock.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1994-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *  Copyright (C) 1991-3 Stephen Crane
6  *
7  *  Empire is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  rwlock.c: Read-write locks
29  *
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2007
32  *     Markus Armbruster, 2007-2009
33  */
34
35 #include <config.h>
36
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "lwp.h"
41 #include "lwpint.h"
42
43 struct lwp_rwlock {
44     /*
45      * Lock counter
46      * 0: unlocked
47      * -1: locked for writing
48      * >0: locked for reading that many times
49      */
50     int count;
51     struct lwpQueue rq;         /* read lock sleepers */
52     struct lwpQueue wq;         /* write lock sleepers */
53     char *name;
54 };
55
56 struct lwp_rwlock *
57 lwp_rwlock_create(char *name)
58 {
59     struct lwp_rwlock *rwlock;
60
61     rwlock = malloc(sizeof(*rwlock));
62     if (!rwlock)
63         return NULL;
64
65     memset(rwlock, 0, sizeof(*rwlock));
66     rwlock->name = strdup(name);
67     return rwlock;
68 }
69
70 void
71 lwp_rwlock_destroy(struct lwp_rwlock *rwlock)
72 {
73     if (CANT_HAPPEN(rwlock->count))
74         return;
75     free(rwlock->name);
76     free(rwlock);
77 }
78
79 void
80 lwp_rwlock_wrlock(struct lwp_rwlock *rwlock)
81 {
82     if (rwlock->count) {
83         lwpAddTail(&rwlock->wq, LwpCurrent);
84         lwpStatus(LwpCurrent, "blocked to acquire rwlock %s for writing",
85                   rwlock->name);
86         lwpReschedule();
87     }
88     CANT_HAPPEN(rwlock->count != 0);
89     rwlock->count = -1;
90     lwpStatus(LwpCurrent, "acquired rwlock %s for writing", rwlock->name);
91 }
92
93 void
94 lwp_rwlock_rdlock(struct lwp_rwlock *rwlock)
95 {
96     if (rwlock->count < 0 || rwlock->wq.head) {
97         lwpStatus(LwpCurrent, "blocked to acquire rwlock %s for reading",
98                   rwlock->name);
99         lwpAddTail(&rwlock->rq, LwpCurrent);
100         lwpReschedule();
101     }
102     CANT_HAPPEN(rwlock->count < 0);
103     rwlock->count++;
104     lwpStatus(LwpCurrent, "acquired rwlock %s for reading", rwlock->name);
105 }
106
107 void
108 lwp_rwlock_unlock(struct lwp_rwlock *rwlock)
109 {
110     struct lwpProc *p;
111     int maxpri;
112
113     lwpStatus(LwpCurrent, "unlocking rwlock %s", rwlock->name);
114     if (CANT_HAPPEN(rwlock->count == 0))
115         return;
116     if (rwlock->count < 0)
117         rwlock->count = 0;
118     else
119         rwlock->count--;
120
121     if (rwlock->count == 0 && rwlock->wq.head) {
122         p = lwpGetFirst(&rwlock->wq);
123         lwpStatus(p, "wake up next writer of rwlock %s", rwlock->name);
124         maxpri = p->pri;
125         lwpReady(p);
126     } else if (rwlock->count >= 0 && rwlock->rq.head && !rwlock->wq.head) {
127         maxpri = 0;
128         while ((p = lwpGetFirst(&rwlock->rq))) {
129             lwpStatus(p, "wake up next reader of rwlock %s", rwlock->name);
130             maxpri = MAX(maxpri, p->pri);
131             lwpReady(p);
132         }
133     } else
134         return;
135
136     if (LwpCurrent->pri < maxpri) {
137         lwpStatus(LwpCurrent, "yielding to thread with higher priority");
138         lwpYield();
139     }
140 }