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
|
// Copyright (C) 2005 David Sugar, Tycho Softworks.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
//
// This exception applies only to the code released under the name GNU
// ccScript. If you copy code from other releases into a copy of GNU
// ccScript, as the General Public License permits, the exception does
// not apply to the code that you add in this way. To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for GNU ccScript, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.
//
#include "script3.h"
#include <cc++/slog.h>
#include <cstdio>
namespace ccscript3Extension {
using namespace std;
using namespace ost;
class LockerChecks : public ScriptChecks
{
public:
const char *chkAquire(Line *line, ScriptImage *img);
const char *chkRelease(Line *line, ScriptImage *img);
};
class LockerMethods : public ScriptMethods
{
public:
bool scrAquire(void);
bool scrRelease(void);
};
class LockerBinder : public ScriptBinder, public Assoc, public SharedMemPager
{
private:
unsigned count;
void detach(ScriptInterp *interp);
void *getMemory(size_t size);
public:
void release(const char *id, void *v);
bool acquire(const char *id, void *v);
LockerBinder();
static LockerBinder locker;
};
static Script::Define runtime[] = {
{"acquire", false, (Script::Method)&LockerMethods::scrAquire,
(Script::Check)&LockerChecks::chkAquire},
{"release", false, (Script::Method)&LockerMethods::scrRelease,
(Script::Check)&LockerChecks::chkRelease},
{NULL, false, NULL, NULL}};
LockerBinder LockerBinder::locker;
LockerBinder::LockerBinder() :
ScriptBinder("locker"), Assoc(), SharedMemPager(1024)
{
count = 0;
bind(runtime);
slog.debug("locking service loaded");
}
void *LockerBinder::getMemory(size_t size)
{
return alloc(size);
}
bool LockerBinder::acquire(const char *id, void *v)
{
bool rtn = false;
void *dp;
enterMutex();
dp = getPointer(id);
if(!dp) {
setPointer(id, v);
++count;
rtn = true;
}
else if(dp == v)
rtn = true;
leaveMutex();
return rtn;
}
void LockerBinder::release(const char *id, void *v)
{
void *dp;
enterMutex();
dp = getPointer(id);
if(dp && dp == v) {
setPointer(id, NULL);
--count;
}
if(!count) {
slog.debug("locker purging...");
Assoc::clear();
SharedMemPager::purge();
}
leaveMutex();
}
void LockerBinder::detach(ScriptInterp *interp)
{
Symbol *list[65];
unsigned count = interp->gathertype(list, 64, "acquired", symCONST);
unsigned idx = 0;
Symbol *sym;
char name[65];
while(idx < count) {
sym = list[idx++];
if(!sym->data[0])
continue;
snprintf(name, sizeof(name), "%s.%s",
sym->id + 8, sym->data);
LockerBinder::locker.release(name, interp);
sym->data[0] = 0;
}
}
bool LockerMethods::scrAquire(void)
{
const char *tag = getMember();
char name[65];
char var[65];
Symbol *sym;
const char *id = getOption(NULL);
Name *scr = getName();
char *ep;
if(!tag) {
setString(var, sizeof(var), scr->name);
ep = strrchr(var, ':');
if(ep)
*ep = 0;
tag = var;
}
if(!id || !*id) {
error("invalid-lock");
return true;
}
snprintf(name, sizeof(name), "acquired.%s", tag);
sym = mapSymbol(name, 32);
if(sym->type == symINITIAL)
sym->type = symCONST;
if(!sym || sym->type != symCONST) {
error("lock-failed");
return true;
}
if(sym->data[0] && !stricmp(sym->data, id))
goto locked;
if(sym->data[0]) {
snprintf(name, sizeof(name), "%s.%s", tag, sym->data);
LockerBinder::locker.release(name, this);
sym->data[0] = 0;
}
snprintf(name, sizeof(name), "%s.%s", tag, id);
if(!LockerBinder::locker.acquire(name, this)) {
error("lock-used");
return false;
}
locked:
setString(sym->data, sym->size + 1, id);
if(frame[stack].index < frame[stack].line->argc)
intGoto();
advance();
return true;
}
bool LockerMethods::scrRelease(void)
{
const char *tag = getMember();
char name[65];
char var[65];
Symbol *sym;
char *ep;
Name *scr = getName();
if(!tag) {
setString(var, sizeof(var), scr->name);
ep = strchr(var, ':');
if(ep)
*ep = 0;
tag = var;
}
snprintf(name, sizeof(name), "acquired.%s", tag);
sym = mapSymbol(name, 0);
if(!sym || sym->type != symCONST || !sym->data[0]) {
error("no-lock");
return true;
}
snprintf(name, sizeof(name), "%s.%s", tag, sym->data);
sym->data[0] = 0;
LockerBinder::locker.release(name, this);
advance();
return true;
}
const char *LockerChecks::chkRelease(Line *line, ScriptImage *img)
{
if(line->argc)
return "release has no options or arguments";
return NULL;
}
const char *LockerChecks::chkAquire(Line *line, ScriptImage *img)
{
unsigned idx = 0;
const char *cp;
if(hasKeywords(line))
return "acquire has no keywords";
if(!getOption(line, &idx))
return "acquire needs id argument";
cp = getOption(line, &idx);
if(!cp)
return NULL;
if(getOption(line, &idx))
return "only one label for acquire";
return NULL;
}
};
|