|
From: <cli...@li...> - 2007-09-24 17:49:14
|
Revision: 72
http://cligg.svn.sourceforge.net/cligg/?rev=72&view=rev
Author: sithhell
Date: 2007-09-24 10:49:11 -0700 (Mon, 24 Sep 2007)
Log Message:
-----------
some new stuff ;)
Modified Paths:
--------------
src/bin/cligg_eventhandler.c
src/bin/cligg_eventhandler.h
src/bin/cligg_ipc.c
src/bin/cligg_mainloop.c
src/bin/cligg_modulehandler.c
src/bin/cligg_modulehandler.h
src/lib/cligg_module.c
Added Paths:
-----------
src/proto/
src/proto/README
src/proto/cligg.h
src/proto/cligg_list.c
src/proto/cligg_list.h
src/proto/cligg_rw_lock.c
src/proto/cligg_rw_lock.h
src/proto/cligg_sem.c
src/proto/cligg_sem.h
Modified: src/bin/cligg_eventhandler.c
===================================================================
--- src/bin/cligg_eventhandler.c 2007-06-25 19:23:21 UTC (rev 71)
+++ src/bin/cligg_eventhandler.c 2007-09-24 17:49:11 UTC (rev 72)
@@ -1,11 +1,14 @@
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include <pthread.h>
#include <cligglib.h>
#include <cligg_hashmap.h>
#include <cligg_list.h>
#include <cligg_eventhandler.h>
+static void *_cligg_call_event(void *);
+
/* Event map
* all registered events which can be used by all modules can be found here
* TODO: find a good hash function
@@ -131,8 +134,52 @@
return ret;
}
+int
+cligg_call_event(char *name,
+ void *data)
+{
+ /* remember this function waits untill the event callback returns.
+ * therefore we create another thread in which the callback gets processed */
+ char **event_data;
+ pthread_t tid;
+ int *ret;
+ int error;
+
+ event_data = malloc(2 * sizeof(char *));
+
+ printf("cligg_call_event: %x\n", data);
+
+ event_data[0] = name;
+ event_data[1] = data;
+
+
+ if((error = pthread_create(&tid, NULL, _cligg_call_event, event_data)) != 0) {
+ fprintf(stderr, "Creation of event handling thread failed: %s\n", strerror(error));
+ return error;
+ }
+
+ if((error = pthread_join(tid, (void **)(&ret))) != 0) {
+ fprintf(stderr, "Joining of event handling thread failed: %s\n", strerror(error));
+ return error;
+ }
+ return (int )ret;
+}
+
+static void *
+_cligg_call_event(void *_data)
+{
+ cligg_event_cb cb;
+ char **data = _data;
+
+ cb = cligg_lookup_event(data[0]);
+
+ if(cb == NULL) return (void *)-1;
+
+ return (void *)cb(data[1]);
+}
+
cligg_event_data *
-cligg_waiton_event(void)
+cligg_next_event(void)
{
cligg_event_data *new;
if(pthread_mutex_lock(&event_lock) != 0) {
Modified: src/bin/cligg_eventhandler.h
===================================================================
--- src/bin/cligg_eventhandler.h 2007-06-25 19:23:21 UTC (rev 71)
+++ src/bin/cligg_eventhandler.h 2007-09-24 17:49:11 UTC (rev 72)
@@ -30,12 +30,18 @@
int
cligg_unregister_event(char *);
+/* this function won't wait till the callback returns */
int
cligg_trigger_event(char *,
void *);
+/* this function waits till the callback returns */
+int
+cligg_call_event(char *,
+ void *);
+
cligg_event_data *
-cligg_waiton_event(void);
+cligg_next_event(void);
cligg_event_cb
cligg_lookup_event(char *name);
Modified: src/bin/cligg_ipc.c
===================================================================
--- src/bin/cligg_ipc.c 2007-06-25 19:23:21 UTC (rev 71)
+++ src/bin/cligg_ipc.c 2007-09-24 17:49:11 UTC (rev 72)
@@ -79,12 +79,13 @@
int clifd;
socklen_t len;
struct sockaddr_un un;
- char buf[2*1024];
- char *message[2];
+ char *buf;
+ char **message;
char **test;
int send = FALSE;
int size;
int i = 0;
+ int ret;
while(1) {
len = sizeof(un);
@@ -92,15 +93,36 @@
perror("accept");
continue;
}
+ buf = malloc((2*1024) * sizeof(char));
len = read(clifd, buf, 2*1024);
if(len == 0) continue;
- message[0] = (char *)buf;
- message[1] = (char *)buf+1024;
+ message = malloc(2 * sizeof(char *));
+ message[0] = malloc((strlen(buf)+1) * sizeof(char));
+ strcpy(message[0], (char *)buf);
+ message[1] = malloc((strlen(buf+1024)+1) * sizeof(char));
+ strcpy(message[1], (char *)buf+1024);
if(strcmp(message[1], "") == 0) message[1] = NULL;
-
+ ret = cligg_call_event(message[0], message[1]);
+
if(strcmp(message[0], "list_modules") == 0) {
+ write(clifd, &ret, sizeof(int));
+ test = (char **)message[1];
+ i = 0;
+ printf("%x\n", test);
+ while(test[i] != NULL) {
+ printf("i: %d\n", i);
+ write(clifd, test[i], PATH_MAX);
+ printf("%s\n", test[i]);
+ free(test[i]);
+ i++;
+ }
+ free(test);
+ }
+ printf("The Callback returned: %d\n", ret);
+
+ /*if(strcmp(message[0], "list_modules") == 0) {
test = cligg_list_modules(&size);
write(clifd, &size, sizeof(int));
i = 0;
@@ -117,7 +139,7 @@
send = TRUE;
}
else
- cligg_trigger_event(message[0], message[1]);
+ cligg_trigger_event(message[0], message[1]);*/
if(!send) {
close(clifd);
Modified: src/bin/cligg_mainloop.c
===================================================================
--- src/bin/cligg_mainloop.c 2007-06-25 19:23:21 UTC (rev 71)
+++ src/bin/cligg_mainloop.c 2007-09-24 17:49:11 UTC (rev 72)
@@ -49,7 +49,7 @@
cligg_event_cb cb = NULL;
while(TRUE) {
- new = cligg_waiton_event();
+ new = cligg_next_event();
if(new == NULL)
return (void *)EXIT_FAILURE;
Modified: src/bin/cligg_modulehandler.c
===================================================================
--- src/bin/cligg_modulehandler.c 2007-06-25 19:23:21 UTC (rev 71)
+++ src/bin/cligg_modulehandler.c 2007-09-24 17:49:11 UTC (rev 72)
@@ -13,6 +13,7 @@
static int cmp(void *a, void *b);
static int module_reg_event_cb(void *data);
static int module_unreg_event_cb(void *data);
+static int module_list_all(void *data);
static pthread_mutex_t module_lock = PTHREAD_MUTEX_INITIALIZER;
static cligg_list *module_list;
@@ -32,6 +33,10 @@
free(module_list);
return FALSE;
}
+ if(!cligg_register_event("list_modules", module_list_all)) {
+ free(module_list);
+ return FALSE;
+ }
return TRUE;
}
@@ -160,26 +165,27 @@
return ret;
}
-char **cligg_list_modules(int *size)
+static int cligg_list_modules(void *data)
{
- char **list = NULL;
+ char **list;
char *path;
int i = 0;
int len;
cligg_list_element *tmp;
- if(size == NULL) return NULL;
-
if(pthread_mutex_lock(&module_lock) != 0) {
fprintf(stderr, "Couldn't obtain module lock!\n");
- cligg_module_del(tmp);
dlerror();
return FALSE;
}
+ /*if(list != NULL) return 0;*/
+
list = (char **)malloc((module_list->size+1) * sizeof(char *));
if(list == NULL) return 0;
+ printf("%x\n", list);
+
tmp = module_list->first;
while(tmp != NULL) {
path = ((cligg_module *)tmp->data)->path;
@@ -192,13 +198,12 @@
}
strcpy(list[i], path);
+ list[i][len] = 0;
i++;
tmp = tmp->next;
}
if(list != NULL) list[i] = NULL;
- *size = module_list->size;
-
if(pthread_mutex_unlock(&module_lock) != 0) {
fprintf(stderr, "Couldn't unlock module lock!\n");
cligg_module_del(tmp);
@@ -206,7 +211,11 @@
return FALSE;
}
- return list;
+ data = (void *)list;
+
+ printf("%x\n", data);
+
+ return module_list->size+1;
}
static int cmp(void *a, void *b)
@@ -227,3 +236,11 @@
cligg_module_del(tmp);
return ret;
}
+
+static int module_list_all(void *data)
+{
+ printf("before callback: %x\n", (((char **)data)));
+ int ret = cligg_list_modules((char **)data);
+ printf("after callback: %x\n", (((char **)data)));
+ return ret;
+}
Modified: src/bin/cligg_modulehandler.h
===================================================================
--- src/bin/cligg_modulehandler.h 2007-06-25 19:23:21 UTC (rev 71)
+++ src/bin/cligg_modulehandler.h 2007-09-24 17:49:11 UTC (rev 72)
@@ -8,6 +8,6 @@
int cligg_register_module(char *);
int cligg_unregister_module(cligg_module *);
-char **cligg_list_modules(int *);
+/*int cligg_list_modules(char **);*/
#endif
Modified: src/lib/cligg_module.c
===================================================================
--- src/lib/cligg_module.c 2007-06-25 19:23:21 UTC (rev 71)
+++ src/lib/cligg_module.c 2007-09-24 17:49:11 UTC (rev 72)
@@ -26,5 +26,6 @@
cligg_module *old = (cligg_module *)module;
old->handle = NULL;
free(old->path);
+ old->path = NULL;
free(old);
}
Added: src/proto/README
===================================================================
--- src/proto/README (rev 0)
+++ src/proto/README 2007-09-24 17:49:11 UTC (rev 72)
@@ -0,0 +1,5 @@
+This here is for testing purpose ...
+The old structure of things doesn't feel right anymore,
+i try to integrate teh whole multithreaded environment better
+and put all the mutex INSIDE the datatypes,
+the old stuff remains there until all of the stuff is ready ;)
Added: src/proto/cligg.h
===================================================================
--- src/proto/cligg.h (rev 0)
+++ src/proto/cligg.h 2007-09-24 17:49:11 UTC (rev 72)
@@ -0,0 +1,22 @@
+#ifndef CLIGG_H
+#define CLIGG_H
+
+#ifdef TRUE
+# undef TRUE
+# define TRUE 1
+#endif
+
+#ifdef FALSE
+# undef FALSE
+# define FALSE 0
+#endif
+
+#define CLIGG_NOWAIT 0
+#define CLIGG_WAIT 1
+
+
+/* Typedef for compare function */
+typedef (*cligg_cmp) (void *, void *);
+typedef (*cligg_free) (void *);
+
+#endif
Added: src/proto/cligg_list.c
===================================================================
--- src/proto/cligg_list.c (rev 0)
+++ src/proto/cligg_list.c 2007-09-24 17:49:11 UTC (rev 72)
@@ -0,0 +1,185 @@
+#include <cligg.h>
+#include <cligg_list.h>
+
+static cligg_list_element *_element_init(void);
+static void _element_destroy(cligg_list_element *element, cligg_free free_func);
+static cligg_list_element *_next(cligg_list *list);
+static cligg_list_element *_goto(cligg_list *list, int index);
+static cligg_list_element *_goto_after(cligg_list *list, void *data, cligg_cmp cmp);
+
+struct cligg_list {
+ cligg_list_element *first;
+ cligg_list_element *last;
+ cligg_list_element *current;
+ int current_pos;
+ cligg_sem *sem;
+ cligg_rw_lock *lock;
+ cligg_free free_func;
+ int flags;
+}
+
+struct cligg_list_element {
+ cligg_list_element *next;
+ void *data;
+}
+
+cligg_list *
+cligg_list_init(cligg_free free_func, int flags)
+{
+ cligg_list *new;
+ int err;
+
+ if((new = (cligg_list *)malloc(sizeof(cligg_list))) == NULL) {
+ err = errno;
+ fprintf(stderr, "cligg_list_init: %s\n", strerror(err));
+ return NULL;
+ }
+
+ new->first = NULL;
+ new->last = NULL;
+ new->current = NULL;
+
+ if((new->sem = cligg_sem_init(0)) == NULL) {
+ free(new);
+ return NULL;
+ }
+
+ if((new->lock = cligg_rw_lock_init()) == NULL) {
+ cligg_sem_destroy(new->sem);
+ free(new);
+ return NULL;
+ }
+
+ new->free_func = free_func;
+ new->flags = flags;
+
+ return new;
+}
+
+int
+cligg_list_empty(cligg_list *list)
+{
+ int res;
+
+ cligg_rw_read_lock(list->lock);
+ res = list->sem->n;
+ cligg_rw_read_unlock(list->lock);
+
+ if(res != 0) return FALSE;
+
+ return TRUE;
+}
+
+int
+cligg_list_size(cligg_list *list)
+{
+ int res;
+
+ cligg_rw_read_lock(list->lock);
+ res = list->sem->n;
+ cligg_rw_read_unlock(list->lock);
+
+ return res;
+}
+
+int
+cligg_list_append(cligg_list *list, void *data)
+{
+ cligg_list_element *new;
+
+ if(list == NULL) return FALSE;
+
+ if((new = cligg_list_element_init(data)) == NULL)
+ return FALSE;
+
+ cligg_rw_write_lock(list->lock);
+
+ new->next = NULL;
+
+ if(list->first == NULL)
+ list->first = new;
+
+ if(list->last != NULL)
+ list->last->next = new;
+
+ list->last = new;
+
+ cligg_sem_V(list->sem);
+
+ cligg_rw_write_unlock(list->lock);
+
+ return TRUE;
+}
+
+int
+cligg_list_prepend(cligg_list *list, void *data)
+{
+ cligg_list_element_new *new;
+
+ if(list == NULL) return FALSE;
+
+ if((new = cligg_list_element_init(data)) == NULL)
+ return FALSE;
+
+ cligg_rw_write_lock(list->lock);
+
+ new->next = list->first;
+
+ if(list->last == NULL)
+ list->last = new;
+
+ list->first = new;
+
+ cligg_sem_V(list->sem);
+
+ cligg_rw_write_unlock(list->lock);
+
+ return TRUE;
+}
+
+int
+cligg_list_insert_pos(cligg_list *list,
+ void *data,
+ int pos)
+{
+ cligg_list_element *new;
+ cligg_list_element *tmp1;
+ cligg_list_element *tmp2;
+
+ if(list == NULL) return FALSE;
+
+ if(cligg_list_empty(list)) return cligg_list_append(data);
+
+ if((new == _element_init(data)) == NULL)
+ return ;
+
+ cligg_rw_write_lock(list->lock);
+
+ tmp1 = _goto(list, pos);
+ tmp2 = _goto(list, pos+1);
+
+ tmp1->next = new;
+ new->next = tmp2;
+
+ cligg_sem_V(list->sem);
+
+ cligg_rw_write_unlock(list->lock);
+
+ return TRUE;
+}
+
+int
+cligg_list_insert(cligg_list *list,
+ void *data,
+ cligg_cmp cmp)
+{
+ int pos;
+
+ pos = _find(list, data, cmp);
+
+ if(pos == 0) return cligg_list_append(list, data);
+
+ return cligg_list_insert_pos(list, data, pos);
+}
+
+
Added: src/proto/cligg_list.h
===================================================================
--- src/proto/cligg_list.h (rev 0)
+++ src/proto/cligg_list.h 2007-09-24 17:49:11 UTC (rev 72)
@@ -0,0 +1,35 @@
+#ifndef CLIGG_LIST
+#define CLIGG_LIST
+
+typedef struct cligg_list cligg_list;
+typedef struct cligg_list_element cligg_list_element;
+
+cligg_list *cligg_list_init(cligg_free free_func, int flags);
+
+int cligg_list_empty(cligg_list, *list);
+int cligg_list_size(cligg_list *list);
+
+int cligg_list_append(cligg_list *list, void *data);
+int cligg_list_prepend(cligg_list *list, void *data);
+int cligg_list_insert_pos(cligg_list *list, void *data, int pos);
+/* Insert sorted, with a compare function given ... */
+int cligg_list_insert(cligg_list *list, void *data, cligg_cmp cmp);
+
+int cligg_list_list_append(cligg_list *list1, cligg_list *list2);
+int cligg_list_list_prepend(cligg_list *list1, cligg_list *list2);
+
+void *cligg_list_del_first(cligg_list *list);
+void *cligg_list_del_last(cligg_list *list);
+void *cligg_list_del_pos(cligg_list *list, int pos);
+
+int cligg_list_find(cligg_list *list, void *data, cligg_cmp cmp);
+
+void *cligg_list_get_first(cligg_list *list);
+void *cligg_list_get_last(cligg_list *list);
+void *cligg_list_get_pos(cligg_list *list, int pos);
+
+cligg_list *cligg_list_copy(cligg_list *list);
+
+void cligg_list_destroy(cligg_list *list);
+
+#endif
Added: src/proto/cligg_rw_lock.c
===================================================================
--- src/proto/cligg_rw_lock.c (rev 0)
+++ src/proto/cligg_rw_lock.c 2007-09-24 17:49:11 UTC (rev 72)
@@ -0,0 +1,108 @@
+#include <pthreads.h>
+#include <cligg_rw_lock.h>
+
+cligg_rw_lock *
+cligg_rw_lock_init(void)
+{
+ cligg_rw_lock *new;
+ int err;
+
+ new = (cligg_rw_lock *)malloc(sizeof(cligg_rw_lock));
+ if(new == NULL) {
+ fprintf(stderr, "cligg_rw_lock_init: %s\n", strerror(errno));
+ return NULL;
+ }
+
+ if((err = pthread_cond_init(&read_cond, NULL)) < 0) {
+ fprintf(stderr, "cligg_rw_lock_init: %s\n", strerror(errno));
+ free(new);
+ return NULL;
+ }
+
+ if((err = pthread_cond_init(&write_cond, NULL)) < 0) {
+ fprintf(stderr, "cligg_rw_lock_init: %s\n", strerror(errno));
+ pthread_cond_destroy(&read_cond);
+ free(new);
+ return NULL;
+ }
+
+
+ if((err = pthread_mutex_init(&rw_lock, NULL)) < 0) {
+ fprintf(stderr, "cligg_rw_lock_init: %s\n", strerror(errno));
+ pthread_cond_destroy(&read_cond);
+ pthread_cond_destroy(&write_cond);
+ free(new);
+ return NULL;
+ }
+
+ new->readers = 0;
+ new->writer = 0;
+ new->pending_writers = 0;
+
+ return new;
+}
+
+void
+cligg_rw_write_lock(cligg_rw_lock *lock)
+{
+ pthread_mutex_lock(&lock->rw_lock);
+
+ lock->pending_writers++;
+
+ while(lock->readers > 0 && lock->writer != 0)
+ pthread_cond_wait(&lock->write_cond, &lock->rw_lock);
+
+ lock->writer = 1;
+ lock->pending_writers--;
+
+ pthread_mutex_unlock(&lock->rw_lock);
+}
+
+void
+cligg_rw_write_unlock(cligg_rw_lock *lock)
+{
+ pthread_mutex_lock(&lock->rw_lock);
+
+ lock->writer = 0;
+
+ pthread_cond_broadcast(&lock->write_cond);
+ pthread_cond_broadcast(&lock->read_cond);
+
+ pthread_mutex_unlock(&lock->rw_lock);
+}
+
+void
+cligg_rw_read_lock(cligg_rw_lock *lock)
+{
+ pthread_mutex_lock(&lock->rw_lock);
+
+ while(lock->writer != 0)
+ pthread_cond_wait(&lock->read_cond, &lock->rw_lock);
+
+ lock->readers++;
+
+ pthread_mutex_unlock(&lock->rw_lock);
+}
+
+void
+cligg_rw_read_unlock(cligg_rw_lock *lock)
+{
+ pthread_mutex_lock(&lock->rw_lock);
+
+ lock->readers--;
+
+ pthread_cond_broadcast(&lock->read_cond);
+ pthread_cond_broadcast(&lock->write_cond);
+
+ pthread_mutex_unlock(&lock->rw_lock);
+}
+
+void
+cligg_rw_lock_destroy(cligg_rw_lock *lock)
+{
+ pthread_mutex_destroy(&lock->rw_lock);
+ pthread_cond_destroy(&lock->read_cond);
+ pthread_cond_destroy(&lock->write_cond);
+ free(lock);
+ lock = NULL;
+}
Added: src/proto/cligg_rw_lock.h
===================================================================
--- src/proto/cligg_rw_lock.h (rev 0)
+++ src/proto/cligg_rw_lock.h 2007-09-24 17:49:11 UTC (rev 72)
@@ -0,0 +1,24 @@
+#ifndef GLIGG_RW_LOCK
+#define CLIGG_RW_LOCK
+
+#include <pthreads.h>
+
+typedef struct cligg_rw_lock cligg_rw_lock {
+ int readers;
+ char writer;
+ pthread_cond_t read_cond;
+ pthread_cond_t write_cond;
+ int pending_writers;
+ pthread_mutex_t rw_lock;
+}
+
+cligg_rw_lock *cligg_rw_lock_init(void);
+
+void cligg_rw_write_lock(cligg_rw_lock *lock);
+void cligg_rw_write_unlock(cligg_rw_lock *lock);
+void cligg_rw_read_lock(cligg_rw_lock *lock);
+void cligg_rw_read_unlock(cligg_rw_lock *lock);
+
+void cligg_rw_lock_destroy(cligg_rw_lock *lock);
+
+#endif
Added: src/proto/cligg_sem.c
===================================================================
--- src/proto/cligg_sem.c (rev 0)
+++ src/proto/cligg_sem.c 2007-09-24 17:49:11 UTC (rev 72)
@@ -0,0 +1,68 @@
+#include <cligg_sem.h>
+#include <pthread.h>
+
+cligg_sem *
+cligg_sem_init(int n)
+{
+ cligg_sem *new;
+ int err;
+
+ if((new = (cligg_sem *)malloc(sizeof(cligg_sem))) == NULL) {
+ err = errno;
+ fprintf(stderr, "cligg_sem_init: %s\n", strerror(err));
+ return NULL;
+ }
+
+ new->n = n;
+
+ if((err = pthread_mutex_init(&new->lock, NULL)) < 0) {
+ free(new);
+ fprintf(stderr, "cligg_sem_init: %s\n", strerror(err));
+ return NULL;
+ }
+
+ if((err = pthread_cond_init(&new->cond, NULL)) < 0) {
+ fprintf(stderr, "cligg_sem_init: %s\n", strerror(err));
+ pthread_mutex_destroy(new->lock);
+ free(new);
+ return NULL;
+ }
+
+ return new;
+}
+
+/* This Function may not fail!
+ */
+void
+cligg_sem_P(cligg_sem *sem, int n)
+{
+ pthread_mutex_lock(&sem->lock);
+
+ while(sem->n < n)
+ pthread_cond_wait(&sem->cond, &sem->lock);
+
+ new->n -= n;
+
+ pthread_mutex_unlock(&sem->lock);
+}
+
+void
+cligg_sem_V(cligg_sem *sem, int n)
+{
+ pthread_mutex_lock(&sem->lock);
+
+ sem->n += n;
+
+ pthread_mutex_unlock(&sem->lock);
+
+ pthread_cond_broadcast(&sem->cond);
+}
+
+void
+cligg_sem_destroy(cligg_sem *sem)
+{
+ pthread_mutex_destroy(&sem->lock);
+ pthread_cond_destroy(&sem->cond);
+ free(new);
+ new = NULL;
+}
Added: src/proto/cligg_sem.h
===================================================================
--- src/proto/cligg_sem.h (rev 0)
+++ src/proto/cligg_sem.h 2007-09-24 17:49:11 UTC (rev 72)
@@ -0,0 +1,23 @@
+/* This Semaphor will be used to synchronize all our data */
+
+#ifndef CLIGG_SEM_H
+#define CLIGG_SEM_H
+
+#include <pthread.h>
+
+typedef struct cligg_sem cligg_sem;
+ pthread_mutex_t *lock;
+ pthread_cond_t *cond;
+ int n;
+};
+
+cligg_sem *cligg_sem_init(int n);
+
+
+void cligg_sem_P(cligg_sem *sem, int n);
+void cligg_sem_V(cligg_sem *sem, int n);
+
+void cligg_sem_destroy(cligg_sem *sem);
+
+
+#endif
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|