2002-08-27 04:47:49 +00:00
|
|
|
/*
|
|
|
|
#FILENAME#
|
|
|
|
|
|
|
|
#DESCRIPTION#
|
|
|
|
|
|
|
|
Copyright (C) 2002 #AUTHOR#
|
|
|
|
|
|
|
|
Author: #AUTHOR#
|
|
|
|
Date: #DATE#
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2003-01-14 20:18:29 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
static __attribute__ ((unused))
|
|
|
|
const char rcsid[] = "$Id$";
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
#include <stdlib.h>
|
2002-08-27 04:47:49 +00:00
|
|
|
#include <stdarg.h>
|
2003-01-03 04:30:38 +00:00
|
|
|
#include <string.h>
|
2002-08-27 04:47:49 +00:00
|
|
|
|
2003-02-14 22:42:11 +00:00
|
|
|
#include "QF/sys.h"
|
2002-08-27 04:47:49 +00:00
|
|
|
#include "QF/cbuf.h"
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "QF/gib.h"
|
2002-08-27 04:47:49 +00:00
|
|
|
#include "QF/dstring.h"
|
2003-01-03 04:30:38 +00:00
|
|
|
#include "QF/hash.h"
|
2002-08-27 04:47:49 +00:00
|
|
|
|
2003-04-14 01:17:55 +00:00
|
|
|
#include "gib_handle.h"
|
2003-04-13 22:07:58 +00:00
|
|
|
#include "gib_tree.h"
|
|
|
|
#include "gib_function.h"
|
|
|
|
#include "gib_thread.h"
|
|
|
|
|
2003-04-14 01:17:55 +00:00
|
|
|
gib_thread_t *gib_thread_first = 0;
|
|
|
|
gib_thread_t *gib_thread_last = 0;
|
2003-01-30 23:26:43 +00:00
|
|
|
|
2003-04-14 01:17:55 +00:00
|
|
|
unsigned short int gib_thread_class;
|
2003-01-30 23:26:43 +00:00
|
|
|
|
2003-04-14 01:17:55 +00:00
|
|
|
hashtab_t *gib_events;
|
2002-08-27 04:47:49 +00:00
|
|
|
|
|
|
|
void
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Thread_Add (gib_thread_t * thread)
|
2002-08-27 04:47:49 +00:00
|
|
|
{
|
2003-04-14 01:17:55 +00:00
|
|
|
if (!gib_thread_first)
|
|
|
|
gib_thread_first = thread;
|
|
|
|
|
|
|
|
thread->prev = gib_thread_last;
|
|
|
|
if (!gib_thread_last)
|
|
|
|
gib_thread_last = thread;
|
|
|
|
else {
|
|
|
|
gib_thread_last->next = thread;
|
|
|
|
gib_thread_last = thread;
|
|
|
|
}
|
2002-08-27 04:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Thread_Remove (gib_thread_t * thread)
|
2002-08-27 04:47:49 +00:00
|
|
|
{
|
2003-01-30 23:26:43 +00:00
|
|
|
if (thread->prev)
|
2002-08-27 04:47:49 +00:00
|
|
|
thread->prev->next = thread->next;
|
2003-01-30 23:26:43 +00:00
|
|
|
else
|
2003-04-14 01:17:55 +00:00
|
|
|
gib_thread_first = thread->next;
|
2003-01-30 23:26:43 +00:00
|
|
|
if (thread->next)
|
|
|
|
thread->next->prev = thread->prev;
|
|
|
|
else
|
2003-05-17 17:36:10 +00:00
|
|
|
gib_thread_last = thread->prev;
|
2002-08-27 04:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gib_thread_t *
|
|
|
|
GIB_Thread_New (void)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_thread_t *new = calloc (1, sizeof (gib_thread_t));
|
|
|
|
|
2003-04-13 22:07:58 +00:00
|
|
|
new->cbuf = Cbuf_New (GIB_Interpreter ());
|
2003-04-14 01:17:55 +00:00
|
|
|
new->id = GIB_Handle_New (new, gib_thread_class);
|
2002-08-27 04:47:49 +00:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2003-04-11 05:58:58 +00:00
|
|
|
void
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Thread_Delete (gib_thread_t * thread)
|
2002-08-27 04:47:49 +00:00
|
|
|
{
|
|
|
|
Cbuf_DeleteStack (thread->cbuf);
|
2003-04-14 01:17:55 +00:00
|
|
|
GIB_Handle_Free (thread->id, gib_thread_class);
|
2002-08-27 04:47:49 +00:00
|
|
|
free (thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GIB_Thread_Execute (void)
|
|
|
|
{
|
|
|
|
gib_thread_t *cur, *tmp;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-04-14 01:17:55 +00:00
|
|
|
if (!gib_thread_first)
|
2002-08-27 04:47:49 +00:00
|
|
|
return;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-04-14 01:17:55 +00:00
|
|
|
for (cur = gib_thread_first; cur; cur = tmp) {
|
2003-01-30 23:26:43 +00:00
|
|
|
tmp = cur->next;
|
2003-02-16 02:44:24 +00:00
|
|
|
if (GIB_DATA(cur->cbuf)->program)
|
|
|
|
Cbuf_Execute_Stack (cur->cbuf);
|
|
|
|
else {
|
2002-08-27 04:47:49 +00:00
|
|
|
GIB_Thread_Remove (cur);
|
|
|
|
GIB_Thread_Delete (cur);
|
2003-04-14 01:17:55 +00:00
|
|
|
}
|
2002-08-27 04:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-14 01:17:55 +00:00
|
|
|
void
|
|
|
|
GIB_Thread_Init (void)
|
|
|
|
{
|
|
|
|
gib_thread_class = GIB_Handle_Class_New ();
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static const char *
|
2003-01-03 04:30:38 +00:00
|
|
|
GIB_Event_Get_Key (void *ele, void *ptr)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
return ((gib_event_t *) ele)->name;
|
2003-01-03 04:30:38 +00:00
|
|
|
}
|
2003-01-06 18:28:13 +00:00
|
|
|
|
|
|
|
static void
|
2003-01-03 04:30:38 +00:00
|
|
|
GIB_Event_Free (void *ele, void *ptr)
|
|
|
|
{
|
2003-02-14 22:42:11 +00:00
|
|
|
gib_event_t *ev = (gib_event_t *) ele;
|
|
|
|
|
|
|
|
free ((void *) ev->name);
|
2003-01-03 04:30:38 +00:00
|
|
|
free (ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
gib_event_t *
|
|
|
|
GIB_Event_New (const char *name)
|
|
|
|
{
|
|
|
|
gib_event_t *new;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-03 04:30:38 +00:00
|
|
|
new = calloc (1, sizeof (gib_event_t));
|
|
|
|
new->name = strdup (name);
|
|
|
|
Hash_Add (gib_events, new);
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Event_Register (const char *name, gib_function_t * func)
|
2003-01-03 04:30:38 +00:00
|
|
|
{
|
|
|
|
gib_event_t *ev;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-03 04:30:38 +00:00
|
|
|
if (!(ev = Hash_Find (gib_events, name)))
|
|
|
|
return -1;
|
|
|
|
ev->func = func;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-27 04:47:49 +00:00
|
|
|
void
|
2003-02-14 22:42:11 +00:00
|
|
|
GIB_Event_Callback (gib_event_t * event, unsigned int argc, ...)
|
2002-08-27 04:47:49 +00:00
|
|
|
{
|
2003-01-03 04:30:38 +00:00
|
|
|
gib_function_t *f = event->func;
|
2002-08-27 04:47:49 +00:00
|
|
|
gib_thread_t *thread;
|
|
|
|
cbuf_args_t *args;
|
2003-02-14 22:42:11 +00:00
|
|
|
va_list ap;
|
2002-08-27 04:47:49 +00:00
|
|
|
unsigned int i;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-27 04:47:49 +00:00
|
|
|
if (!f)
|
|
|
|
return;
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-27 04:47:49 +00:00
|
|
|
thread = GIB_Thread_New ();
|
|
|
|
args = Cbuf_ArgsNew ();
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2002-08-27 04:47:49 +00:00
|
|
|
va_start (ap, argc);
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-01-28 21:16:21 +00:00
|
|
|
Cbuf_ArgsAdd (args, f->name);
|
2002-08-27 04:47:49 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
2003-02-14 22:42:11 +00:00
|
|
|
Cbuf_ArgsAdd (args, va_arg (ap, const char *));
|
|
|
|
|
2002-08-27 04:47:49 +00:00
|
|
|
va_end (ap);
|
2003-02-14 22:42:11 +00:00
|
|
|
|
2003-04-11 02:57:11 +00:00
|
|
|
GIB_Function_Execute_D (thread->cbuf, f, args->argv, args->argc);
|
2002-08-27 04:47:49 +00:00
|
|
|
GIB_Thread_Add (thread);
|
|
|
|
Cbuf_ArgsDelete (args);
|
2002-08-27 06:46:49 +00:00
|
|
|
}
|
2003-01-03 04:30:38 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
GIB_Event_Init (void)
|
|
|
|
{
|
|
|
|
gib_events = Hash_NewTable (1024, GIB_Event_Get_Key, GIB_Event_Free, 0);
|
|
|
|
}
|