summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpixel <pixel>2007-05-27 13:04:17 +0000
committerpixel <pixel>2007-05-27 13:04:17 +0000
commit8e017078576f9dc26a35733a3af3d1d92bc98726 (patch)
treea6ce513a267d877eafb406c5ca5e434d3d6c3420
parent981e2c7c70b45ff6336c98eaac2af5a6a32bc5c4 (diff)
Adding better threading support for LuaTask, and fixing LuaTask a bit.
-rw-r--r--include/LuaTask.h8
-rw-r--r--include/hashtab.h4
-rw-r--r--lib/LuaTask.cc48
3 files changed, 43 insertions, 17 deletions
diff --git a/include/LuaTask.h b/include/LuaTask.h
index fcc1363..eded9ac 100644
--- a/include/LuaTask.h
+++ b/include/LuaTask.h
@@ -1,6 +1,8 @@
#ifndef __LUATASK_H__
#define __LUATASK_H__
+#include <hashtab.h>
+
#include <Task.h>
#include <Buffer.h>
#include <BLua.h>
@@ -9,12 +11,14 @@
class LuaTask : public Task {
public:
LuaTask(Lua *, const String &);
- LuaTask(Lua *, int);
+ LuaTask(Lua *, int) throw (GeneralException);
virtual ~LuaTask();
virtual String GetName();
protected:
virtual int Do() throw (GeneralException);
private:
+ LuaTask * gettop();
+ void settop(LuaTask *);
Lua * L;
String cmd;
int nargs, stacktop, r;
@@ -24,7 +28,7 @@ class LuaTask : public Task {
String task;
- static LuaTask * top;
+ static htab * h;
};
#endif
diff --git a/include/hashtab.h b/include/hashtab.h
index c2e7c46..6d2d504 100644
--- a/include/hashtab.h
+++ b/include/hashtab.h
@@ -31,6 +31,8 @@ This implements a hash table.
#ifndef HASHTAB
#define HASHTAB
+#include <generic.h>
+
/* PRIVATE TYPES AND DEFINITIONS */
struct hitem
@@ -126,7 +128,7 @@ int hfind(htab *t, Uint8 *key, Uint32 keyl);
RETURNS:
FALSE if the operation fails (because that key is already there).
*/
-int hadd(htab *t, ub1 *key, ub4 keyl, void *stuff);
+int hadd(htab *t, Uint8 *key, Uint32 keyl, void *stuff);
/* hdel - delete the item at the current position
diff --git a/lib/LuaTask.cc b/lib/LuaTask.cc
index dfb12d7..4bc0208 100644
--- a/lib/LuaTask.cc
+++ b/lib/LuaTask.cc
@@ -8,31 +8,50 @@
#include <CopyJob.h>
#endif
-LuaTask * LuaTask::top = 0;
+htab * LuaTask::h = hcreate(1);
LuaTask::LuaTask(Lua * _L, const String & _cmd) : L(_L), cmd(_cmd), nargs(0), c(0), b(0) {
+ LuaTask * top = gettop();
+
if (top) {
WaitFor(top);
} else {
SetBurst();
}
- top = this;
+ settop(this);
stacktop = L->gettop() + 1;
}
-LuaTask::LuaTask(Lua * _L, int _nargs) : L(_L), cmd(""), nargs(_nargs), c(0), b(0) {
- if (top) {
- WaitFor(top);
- } else {
- SetBurst();
- }
- top = this;
+LuaTask::LuaTask(Lua * _L, int _nargs) throw (GeneralException) : L(_L), cmd(""), nargs(_nargs), c(0), b(0) {
+ if (gettop())
+ throw GeneralException("Can't run a stack-based LuaTask when there are other tasks waiting.");
+
+ SetBurst();
+ settop(this);
stacktop = L->gettop() - nargs;
}
LuaTask::~LuaTask() {
- if (top == this) {
- top = 0;
+ if (gettop() == this) {
+ settop(0);
+ }
+}
+
+LuaTask * LuaTask::gettop() {
+ if (!hfind(h, (Uint8 *) L, sizeof(L))) {
+ hadd(h, (Uint8 *) L, sizeof(L), 0);
+ }
+
+ return (LuaTask *) hstuff(h);
+}
+
+void LuaTask::settop(LuaTask * v) {
+ if (!hfind(h, (Uint8 *) L, sizeof(L)))
+ return;
+ if (v) {
+ hstuff(h) = v;
+ } else {
+ hdel(h);
}
}
@@ -41,7 +60,6 @@ String LuaTask::GetName() {
}
int LuaTask::Do() throw (GeneralException) {
- int nargs = 0;
switch (current) {
case 0:
current = 1;
@@ -51,6 +69,7 @@ int LuaTask::Do() throw (GeneralException) {
L->resume(nargs);
}
case 2:
+ nargs = 0;
#ifndef LUATASK_OMIT_HTTPCLIENT
if (task == "HttpClient") {
LuaBuffer o(b);
@@ -72,7 +91,8 @@ int LuaTask::Do() throw (GeneralException) {
delete c;
c = 0;
- L->resume(nargs);
+ if (current != 1)
+ L->resume(nargs);
case 1:
current = 2;
if (L->gettop() >= 1) {
@@ -96,7 +116,7 @@ int LuaTask::Do() throw (GeneralException) {
} else if (task == "Command") {
pid_t pid;
int i;
-
+
char * cmd;
char ** args, ** ptr;