forked from Yellow-Camper/libevhtp
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mark Ellzey
committed
Jul 8, 2011
1 parent
354e71d
commit 1e0ec98
Showing
91 changed files
with
53,450 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
SRC = evthr.c | ||
OUT = libevthr.a | ||
OBJ = $(SRC:.c=.o) | ||
INCLUDES = -I. | ||
CFLAGS += -Wall -ggdb | ||
LDFLAGS += -ggdb | ||
CC = gcc | ||
|
||
.SUFFIXES: .c | ||
|
||
default: $(OUT) | ||
|
||
.c.o: | ||
$(CC) $(INCLUDES) $(CFLAGS) -c $< -o $@ | ||
|
||
$(OUT): $(OBJ) | ||
ar rcs $(OUT) $(OBJ) | ||
|
||
test: $(OUT) test.c | ||
$(CC) $(INCLUDES) $(CFLAGS) test.c -o test $(OUT) -levent -levent_pthreads -lpthread | ||
|
||
clean: | ||
rm -f $(OBJ) $(OUT) test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
Libevthr is an API which manages threads and thread-pools in an event based | ||
manner. This API requires libevent with threading support. | ||
|
||
Libevthr works a bit differently than most thread management systems. Instead of | ||
conditional signalling and some type of pre-thread queue, Libevthr uses a | ||
deferral type mechanism. That is, a thread is always running, abstracted to a | ||
point where you "defer" your function *into* a thread. | ||
|
||
For example you can start up a single thread with a backlog of 10 (a backlog | ||
being the max number of outstanding callbacks to run within the thread), and | ||
execute a function you would like to run inside the thread one or many times. | ||
The act of deferrals is non-blocking. | ||
|
||
Example Code for evthrs: | ||
|
||
evthr_t * thr = evthr_new(10, NULL); | ||
|
||
if (evthr_start(thr) < 0) { | ||
exit(1); | ||
} | ||
|
||
evthr_defer(thr, my_cb_1, NULL); | ||
evthr_defer(thr, my_cb_2, NULL); | ||
evthr_defer(thr, my_cb_3, NULL); | ||
|
||
sleep(n_seconds); | ||
|
||
evthr_stop(thr); | ||
|
||
Libevthr also has the ability to create pools using the same methods that a | ||
single evthr has. For example, if you would like to create 10 threads, each | ||
with a backlog of 5: | ||
|
||
evthr_pool_t * thr_pool = evthr_pool_new(10, 5, NULL); | ||
|
||
if (evthr_pool_start(thr_pool) < 0) { | ||
exit(1); | ||
} | ||
|
||
evthr_pool_defer(thr_pool, my_cb_1, NULL); | ||
evthr_pool_defer(thr_pool, my_cb_2, NULL); | ||
evthr_pool_defer(thr_pool, my_cb_3, NULL); | ||
|
||
Your callback functions which you defer must be of type "evthr_cb", or | ||
"void cb_name(void * arg, void * shared)". In this case, the "arg" variable is | ||
the data you passed as the third argument to either evthr_pool_defer, or | ||
evthr_defer. The "shared" variable is the data that was either the second | ||
variable in evthr_new(), or the third variable in evthr_pool_new(). | ||
|
||
The gist of this is to allow a global dataset, along with deferred specific | ||
data. | ||
|
||
See test.c for a quick example. |
Oops, something went wrong.