Skip to content

Commit 5c893ef

Browse files
authored
Merge pull request ARMmbed#4 from geky/refactor
Refactor codebase based on feedback from initial users
2 parents 6830476 + 28412ed commit 5c893ef

13 files changed

+695
-108
lines changed

Diff for: .travis.yml

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
script:
2-
- git config user.email "test"
3-
- git config user.name "test"
4-
- git fetch origin posix:posix
5-
- git merge posix --no-commit
62
- make test

Diff for: Makefile

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
TARGET = libevents.a
22

3-
43
CC = gcc
54
AR = ar
65
SIZE = size
76

8-
SRC += $(wildcard *.c sys/*.c)
7+
SRC += $(wildcard *.c)
98
OBJ := $(SRC:.c=.o)
109
DEP := $(SRC:.c=.d)
1110
ASM := $(SRC:.c=.s)
1211

12+
TESTS = tests/tests
13+
TSRC += $(wildcard tests/*.c)
14+
TOBJ := $(TSRC:.c=.o)
15+
TDEP := $(TSRC:.c=.d)
16+
1317
ifdef DEBUG
1418
CFLAGS += -O0 -g3 -DMU_DEBUG
1519
CFLAGS += -fkeep-inline-functions
@@ -19,16 +23,20 @@ endif
1923
ifdef WORD
2024
CFLAGS += -m$(WORD)
2125
endif
26+
CFLAGS += -I.
2227
CFLAGS += -std=c99
2328
CFLAGS += -Wall -Winline
24-
CFLAGS += -D_POSIX_C_SOURCE=200112L
29+
CFLAGS += -D_XOPEN_SOURCE=500
2530

2631
LFLAGS += -lpthread
27-
LFLAGS += -lrt
2832

2933

3034
all: $(TARGET)
3135

36+
test: $(TOBJ) $(OBJ)
37+
$(CC) $(CFLAGS) $^ $(LFLAGS) -o $(TESTS)
38+
$(TESTS)
39+
3240
asm: $(ASM)
3341

3442
size: $(OBJ)
@@ -47,6 +55,8 @@ size: $(OBJ)
4755

4856
clean:
4957
rm -f $(TARGET)
58+
rm -f $(TESTS)
59+
rm -f $(TOBJ) $(TDEP)
5060
rm -f $(OBJ)
5161
rm -f $(DEP)
5262
rm -f $(ASM)

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void print(void *s) {
1414

1515
int main() {
1616
// creates a queue with 32 events with default size
17-
struct equeue queue;
17+
equeue_t queue;
1818
equeue_create(&queue, 32, 0);
1919

2020
// events are simple callbacks

Diff for: events.c

+19-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <string.h>
55

66

7-
int equeue_create(struct equeue *q, unsigned size) {
7+
int equeue_create(equeue_t *q, unsigned size) {
88
void *buffer = malloc(size);
99
if (!buffer) {
1010
return -1;
@@ -15,7 +15,7 @@ int equeue_create(struct equeue *q, unsigned size) {
1515
return err;
1616
}
1717

18-
int equeue_create_inplace(struct equeue *q, unsigned size, void *buffer) {
18+
int equeue_create_inplace(equeue_t *q, unsigned size, void *buffer) {
1919
q->slab.size = size;
2020
q->slab.data = buffer;
2121
q->chunks = 0;
@@ -47,7 +47,7 @@ int equeue_create_inplace(struct equeue *q, unsigned size, void *buffer) {
4747
return 0;
4848
}
4949

50-
void equeue_destroy(struct equeue *q) {
50+
void equeue_destroy(equeue_t *q) {
5151
while (q->queue) {
5252
struct event *e = q->queue;
5353
q->queue = e->next;
@@ -61,7 +61,7 @@ void equeue_destroy(struct equeue *q) {
6161
}
6262

6363
// equeue allocation functions
64-
static void *equeue_alloc(struct equeue *q, unsigned size) {
64+
static void *equeue_alloc(equeue_t *q, unsigned size) {
6565
size = size + sizeof(unsigned);
6666
size = (size + sizeof(unsigned)-1) & ~(sizeof(unsigned)-1);
6767
if (size < sizeof(struct equeue_chunk)) {
@@ -97,7 +97,7 @@ static void *equeue_alloc(struct equeue *q, unsigned size) {
9797
return 0;
9898
}
9999

100-
static void equeue_dealloc(struct equeue *q, void *e) {
100+
static void equeue_dealloc(equeue_t *q, void *e) {
101101
struct equeue_chunk *c = (struct equeue_chunk *)((unsigned *)e - 1);
102102

103103
events_mutex_lock(&q->freelock);
@@ -120,15 +120,15 @@ static void equeue_dealloc(struct equeue *q, void *e) {
120120
}
121121

122122
// event allocation functions
123-
static inline int event_next_id(struct equeue *q) {
123+
static inline int event_next_id(equeue_t *q) {
124124
int id = q->next_id++;
125125
if (q->next_id < 0) {
126126
q->next_id = 42;
127127
}
128128
return id;
129129
}
130130

131-
void *event_alloc(struct equeue *q, unsigned size) {
131+
void *event_alloc(equeue_t *q, unsigned size) {
132132
struct event *e = equeue_alloc(q, sizeof(struct event) + size);
133133
if (!e) {
134134
return 0;
@@ -142,7 +142,7 @@ void *event_alloc(struct equeue *q, unsigned size) {
142142
return e + 1;
143143
}
144144

145-
void event_dealloc(struct equeue *q, void *p) {
145+
void event_dealloc(equeue_t *q, void *p) {
146146
struct event *e = (struct event*)p - 1;
147147

148148
if (e->dtor) {
@@ -157,7 +157,7 @@ static inline int equeue_tickdiff(unsigned a, unsigned b) {
157157
return (int)(a - b);
158158
}
159159

160-
static int equeue_enqueue(struct equeue *q, struct event *e, int ms) {
160+
static int equeue_enqueue(equeue_t *q, struct event *e, int ms) {
161161
e->target = events_tick() + (unsigned)ms;
162162

163163
struct event **p = &q->queue;
@@ -171,7 +171,7 @@ static int equeue_enqueue(struct equeue *q, struct event *e, int ms) {
171171
return e->id;
172172
}
173173

174-
static struct event *equeue_dequeue(struct equeue *q, int id) {
174+
static struct event *equeue_dequeue(equeue_t *q, int id) {
175175
for (struct event **p = &q->queue; *p; p = &(*p)->next) {
176176
if ((*p)->id == id) {
177177
struct event *e = *p;
@@ -183,15 +183,15 @@ static struct event *equeue_dequeue(struct equeue *q, int id) {
183183
return 0;
184184
}
185185

186-
static int equeue_post(struct equeue *q, struct event *e, int ms) {
186+
static int equeue_post(equeue_t *q, struct event *e, int ms) {
187187
events_mutex_lock(&q->queuelock);
188188
int id = equeue_enqueue(q, e, ms);
189189
events_mutex_unlock(&q->queuelock);
190190
events_sema_release(&q->eventsema);
191191
return id;
192192
}
193193

194-
static void equeue_cancel(struct equeue *q, int id) {
194+
static void equeue_cancel(equeue_t *q, int id) {
195195
events_mutex_lock(&q->queuelock);
196196
struct event *e = equeue_dequeue(q, id);
197197
events_mutex_unlock(&q->queuelock);
@@ -201,11 +201,11 @@ static void equeue_cancel(struct equeue *q, int id) {
201201
}
202202
}
203203

204-
void equeue_break(struct equeue *q) {
204+
void equeue_break(equeue_t *q) {
205205
equeue_post(q, &q->break_, 0);
206206
}
207207

208-
void equeue_dispatch(struct equeue *q, int ms) {
208+
void equeue_dispatch(equeue_t *q, int ms) {
209209
if (ms >= 0) {
210210
equeue_post(q, &q->break_, ms);
211211
}
@@ -271,14 +271,14 @@ void event_dtor(void *p, void (*dtor)(void *)) {
271271
}
272272

273273
// event operations
274-
int event_post(struct equeue *q, void (*cb)(void*), void *p) {
274+
int event_post(equeue_t *q, void (*cb)(void*), void *p) {
275275
struct event *e = (struct event*)p - 1;
276276
e->cb = cb;
277277
int id = equeue_post(q, e, e->target);
278278
return id;
279279
}
280280

281-
void event_cancel(struct equeue *q, int id) {
281+
void event_cancel(equeue_t *q, int id) {
282282
return equeue_cancel(q, id);
283283
}
284284

@@ -293,7 +293,7 @@ static void ecallback_dispatch(void *p) {
293293
e->cb(e->data);
294294
}
295295

296-
int event_call(struct equeue *q, void (*cb)(void*), void *data) {
296+
int event_call(equeue_t *q, void (*cb)(void*), void *data) {
297297
struct ecallback *e = event_alloc(q, sizeof(struct ecallback));
298298
if (!e) {
299299
return 0;
@@ -304,7 +304,7 @@ int event_call(struct equeue *q, void (*cb)(void*), void *data) {
304304
return event_post(q, ecallback_dispatch, e);
305305
}
306306

307-
int event_call_in(struct equeue *q, void (*cb)(void*), void *data, int ms) {
307+
int event_call_in(equeue_t *q, int ms, void (*cb)(void*), void *data) {
308308
struct ecallback *e = event_alloc(q, sizeof(struct ecallback));
309309
if (!e) {
310310
return 0;
@@ -316,7 +316,7 @@ int event_call_in(struct equeue *q, void (*cb)(void*), void *data, int ms) {
316316
return event_post(q, ecallback_dispatch, e);
317317
}
318318

319-
int event_call_every(struct equeue *q, void (*cb)(void*), void *data, int ms) {
319+
int event_call_every(equeue_t *q, int ms, void (*cb)(void*), void *data) {
320320
struct ecallback *e = event_alloc(q, sizeof(struct ecallback));
321321
if (!e) {
322322
return 0;

Diff for: events.h

+19-19
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ extern "C" {
99
#endif
1010

1111
// System specific files
12-
#include "sys/events_tick.h"
13-
#include "sys/events_mutex.h"
14-
#include "sys/events_sema.h"
12+
#include "events_tick.h"
13+
#include "events_mutex.h"
14+
#include "events_sema.h"
1515

1616

1717
// Event/queue structures
@@ -26,7 +26,7 @@ struct event {
2626
// data follows
2727
};
2828

29-
struct equeue {
29+
typedef struct equeue {
3030
struct event *queue;
3131
int next_id;
3232

@@ -46,26 +46,26 @@ struct equeue {
4646
events_sema_t eventsema;
4747
events_mutex_t queuelock;
4848
events_mutex_t freelock;
49-
};
49+
} equeue_t;
5050

5151
// Queue operations
5252
//
5353
// Creation results in negative value on failure.
54-
int equeue_create(struct equeue*, unsigned size);
55-
int equeue_create_inplace(struct equeue*, unsigned size, void *buffer);
56-
void equeue_destroy(struct equeue*);
54+
int equeue_create(equeue_t *queue, unsigned size);
55+
int equeue_create_inplace(equeue_t *queue, unsigned size, void *buffer);
56+
void equeue_destroy(equeue_t *queue);
5757

5858
// Dispatch events
5959
//
6060
// Executes any callbacks enqueued for the specified time in milliseconds,
6161
// or forever if ms is negative
62-
void equeue_dispatch(struct equeue*, int ms);
62+
void equeue_dispatch(equeue_t *queue, int ms);
6363

6464
// Break a running event loop
6565
//
66-
// Shuts down an unbounded event loop. Already pending events may finish executing,
67-
// but the queue will not continue looping indefinitely.
68-
void equeue_break(struct equeue*);
66+
// Shuts down an unbounded event loop. Already pending events may finish
67+
// executing, but the queue will not continue looping indefinitely.
68+
void equeue_break(equeue_t *queue);
6969

7070
// Simple event calls
7171
//
@@ -78,9 +78,9 @@ void equeue_break(struct equeue*);
7878
//
7979
// These calls will result in 0 if no memory is available, otherwise they
8080
// will result in a unique identifier that can be passed to event_cancel.
81-
int event_call(struct equeue*, void (*cb)(void*), void *data);
82-
int event_call_in(struct equeue*, void (*cb)(void*), void *data, int ms);
83-
int event_call_every(struct equeue*, void (*cb)(void*), void *data, int ms);
81+
int event_call(equeue_t *queue, void (*cb)(void *), void *data);
82+
int event_call_in(equeue_t *queue, int ms, void (*cb)(void *), void *data);
83+
int event_call_every(equeue_t *queue, int ms, void (*cb)(void *), void *data);
8484

8585
// Events with queue handled blocks of memory
8686
//
@@ -90,8 +90,8 @@ int event_call_every(struct equeue*, void (*cb)(void*), void *data, int ms);
9090
//
9191
// event_alloc will result in null if no memory is available
9292
// or the requested size is less than the size passed to equeue_create.
93-
void *event_alloc(struct equeue*, unsigned size);
94-
void event_dealloc(struct equeue*, void*);
93+
void *event_alloc(equeue_t *queue, unsigned size);
94+
void event_dealloc(equeue_t *queue, void *event);
9595

9696
// Configure an allocated event
9797
//
@@ -110,15 +110,15 @@ void event_dtor(void *event, void (*dtor)(void *));
110110
//
111111
// This call results in an unique identifier that can be passed to
112112
// event_cancel.
113-
int event_post(struct equeue*, void (*cb)(void*), void *event);
113+
int event_post(equeue_t *queue, void (*cb)(void *), void *event);
114114

115115
// Cancel events that are in flight
116116
//
117117
// Every event_call function returns a non-negative identifier on success
118118
// that can be used to cancel an in-flight event. If the event has already
119119
// been dispatched or does not exist, no error occurs. Note, this can not
120120
// stop a currently executing event
121-
void event_cancel(struct equeue*, int event);
121+
void event_cancel(equeue_t *queue, int event);
122122

123123

124124
#ifdef __cplusplus

0 commit comments

Comments
 (0)