Skip to content

Commit b027d33

Browse files
author
Stephen Mathieson
committed
Initial commit
0 parents  commit b027d33

11 files changed

+597
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
deps/
2+
example
3+
test/end
4+
test/new
5+
test/push
6+
test/wait
7+
*.o

Makefile

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
CC ?= gcc
3+
CFLAGS = -std=c99 -Wall -Wextra -Isrc
4+
LDFLAGS = -pthread
5+
SRC = $(wildcard src/*.c)
6+
SRC += $(wildcard deps/*/*.c)
7+
OBJS = $(SRC:.c=.o)
8+
TEST_SOURCES = $(wildcard test/*.c)
9+
TEST_EXECUTABLES = $(TEST_SOURCES:.c=)
10+
11+
test: $(TEST_EXECUTABLES)
12+
13+
$(TEST_EXECUTABLES): CFLAGS += -Wunused-parameter
14+
$(TEST_EXECUTABLES): CFLAGS += -Ideps/describe -Ideps
15+
$(TEST_EXECUTABLES): $(OBJS)
16+
@$(CC) $@.c $^ -o $@ $(CFLAGS) $(LDFLAGS)
17+
@$@
18+
19+
example: CFLAGS += -Wint-to-void-pointer-cast
20+
example: example.o $(OBJS)
21+
$(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS)
22+
23+
%.o: %.c
24+
$(CC) $< -c -o $@ $(CFLAGS)
25+
26+
clean:
27+
rm -f $(OBJS)
28+
rm -f example example.o
29+
rm -f $(TEST_OBJECTS) $(TEST_EXECUTABLES)
30+
31+
.PHONY: test clean

Readme.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
# batch.c
3+
4+
A tiny async batch thingy using POSIX threads. Asynchronously execute callbacks in parallel with built-in concurrency control.
5+
6+
## API
7+
8+
```c
9+
```
10+
11+
## License
12+
13+
MIT

example.c

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
#include <assert.h>
3+
#include <stdio.h>
4+
#include <pthread.h> // pthread_self
5+
#include <unistd.h> // sleep
6+
#include "batch.h"
7+
8+
int count = 0;
9+
10+
int
11+
is_even(int n) {
12+
return 0 == n % 2 ? 1 : 0;
13+
}
14+
15+
void *
16+
function_even(void *arg) {
17+
int n = (int) arg;
18+
assert(is_even(n));
19+
printf("thread 0x%x: EVEN <--> %d\n", (int) pthread_self(), n);
20+
count++;
21+
sleep(1);
22+
return NULL;
23+
}
24+
25+
void *
26+
function_odd(void *arg) {
27+
int n = (int) arg;
28+
assert(0 == is_even(n));
29+
printf("thread 0x%x: ODD <--> %d\n", (int) pthread_self(), n);
30+
count++;
31+
sleep(1);
32+
return NULL;
33+
}
34+
35+
int
36+
main() {
37+
void *batch = batch_new(4);
38+
assert(batch);
39+
40+
for (int i = 0; i < 20; ++i) {
41+
void *(*function)(void *) = is_even(i)
42+
? function_even
43+
: function_odd;
44+
batch_push(batch, function, (void *) i);
45+
}
46+
47+
batch_wait(batch);
48+
batch_end(batch);
49+
50+
assert(20 == count);
51+
52+
return 0;
53+
}

package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "batch",
3+
"version": "0.0.0",
4+
"repo": "stephenmathieson/batch.c",
5+
"description": "A tiny async batch thingy using POSIX threads",
6+
"keywords": ["pthread", "async", "concurrency"],
7+
"license": "MIT",
8+
"src": ["src/batch.h", "src/batch.c"],
9+
"development": {
10+
"stephenmathieson/describe.h": "*"
11+
}
12+
}

0 commit comments

Comments
 (0)