Skip to content

Commit e4f7213

Browse files
committed
Parsing empty config strings, working on the RTSP library, and using
writev to serve HTTP with less context switching
1 parent 8e4d95f commit e4f7213

7 files changed

Lines changed: 146 additions & 140 deletions

File tree

src/hal/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define MAX_SECTIONS 16
1414
#define REG_SECTION "^([[:space:]]*\\[(\\w+)\\][[:space:]]|(\\w+):)"
15-
#define REG_PARAM "^[[:space:]]*%s[[:space:]]*[=:][[:space:]]*(.[^;#\n\r]*)"
15+
#define REG_PARAM "^[[:space:]]*%s[[:blank:]]*[=:][[:blank:]]*(.[^;#\n\r]*)"
1616

1717
struct IniConfig {
1818
char *str;

src/rtsp/bufpool.h

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static inline int bufpool_attach(bufpool_handle h, void *buf);
4545
static inline void bufpool_statistics(bufpool_handle h);
4646

4747
/******************************************************************************
48-
* INLINE FUNCTIONS
48+
* INLINE FUNCTIONS
4949
******************************************************************************/
5050
/* to use strict-aliase compiler optimization, we cannot use void **, so use macro : completely dependent of gcc-extension :p */
5151
#define bufpool_get_free(__h,__p_buf) ({ \
@@ -68,14 +68,14 @@ __unlock: \
6868
static inline int __bufpool_ref_manipulate(bufpool_handle h, void *buf, int val)
6969
{
7070
struct __bufpool_elem_t *p;
71-
71+
7272
DASSERT(h->elems,return FAILURE);
7373
DASSERT(h->num > 0,return FAILURE);
7474

7575
ASSERT(p = hash_lookup(h->buf_table,(hash_key_t) buf), return FAILURE);
7676

7777
DASSERT(CHECK_MAGIC(MAGIC_BUFPOOL_ELEM,&p), return FAILURE);
78-
78+
7979
p->ref_count += val;
8080

8181
TEST(p->ref_count >= 0, ({
@@ -112,7 +112,7 @@ static inline void bufpool_statistics(bufpool_handle h)
112112
pthread_mutex_lock(&h->mutex);
113113

114114
free_elems = list_length(&h->free_list);
115-
free_elems_ratio = ((float)free_elems/(float)h->num) * max;
115+
free_elems_ratio = ((float)free_elems/(float)h->num) * max;
116116

117117
for(i = 0; i < free_elems_ratio; i++) {
118118
tmp[j++] = '=';
@@ -135,9 +135,9 @@ static inline int bufpool_attach(bufpool_handle h, void *buf)
135135
DASSERT(h,return FAILURE);
136136

137137
pthread_mutex_lock(&h->mutex);
138-
138+
139139
ret = __bufpool_ref_manipulate(h,buf,1);
140-
140+
141141
pthread_mutex_unlock(&h->mutex);
142142

143143
return ret;
@@ -149,9 +149,9 @@ static inline int bufpool_detach(bufpool_handle h, void *buf)
149149
DASSERT(h,return FAILURE);
150150

151151
pthread_mutex_lock(&h->mutex);
152-
152+
153153
ret = __bufpool_ref_manipulate(h,buf,-1);
154-
154+
155155
pthread_mutex_unlock(&h->mutex);
156156

157157
return ret;
@@ -207,7 +207,7 @@ static inline bufpool_handle bufpool_create(int num, void * (*bufgetter_fxn)(int
207207
ASSERT(p == &nh->elems[i], goto error);
208208
#endif
209209
}
210-
210+
211211

212212
nh->num = num;
213213

@@ -220,26 +220,25 @@ static inline bufpool_handle bufpool_create(int num, void * (*bufgetter_fxn)(int
220220

221221
static void bufpool_delete(bufpool_handle h)
222222
{
223-
int i;
224-
if (h) {
225-
if (h->elems) {
226-
for(i = 0;i < h->num; i++) {
227-
if(h->elems[i].reset) {
228-
h->elems[i].reset(h->elems[i].buf);
229-
}
223+
if (!h) return;
224+
225+
if (h->elems) {
226+
for (int i = 0;i < h->num; i++) {
227+
if(h->elems[i].reset) {
228+
h->elems[i].reset(h->elems[i].buf);
230229
}
231-
FREE(h->elems);
232230
}
231+
FREE(h->elems);
232+
}
233233

234-
hash_destroy(h->buf_table);
234+
hash_destroy(h->buf_table);
235235

236-
pthread_mutex_destroy(&h->mutex);
237-
FREE(h);
238-
}
236+
pthread_mutex_destroy(&h->mutex);
237+
FREE(h);
239238
}
240239

241240

242241
#if defined (__cplusplus)
243242
}
244243
#endif
245-
#endif
244+
#endif

src/rtsp/hash.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,16 @@ static inline struct __hash_entry *__new_hash_entry(hash_key_t key, void *value)
6161

6262
static inline void hash_destroy(hash_handle h)
6363
{
64-
int i;
65-
if(h){
66-
if(h->pool){
67-
for(i = 0;i < h->size;i++) {
68-
list_destroy(&(h->pool[i]));
69-
}
70-
FREE(h->pool);
71-
}
64+
if(!h) return;
7265

73-
FREE(h);
66+
if(h->pool){
67+
for(int i = 0;i < h->size;i++) {
68+
list_destroy(&(h->pool[i]));
69+
}
70+
FREE(h->pool);
7471
}
72+
73+
FREE(h);
7574
}
7675

7776
static inline hash_handle hash_create(int size, size_t buf_size)
@@ -101,7 +100,7 @@ static inline int __lookup_key(struct list_t *e, void *v)
101100
return (p->key == key);
102101
}
103102

104-
static inline struct __hash_entry *__find_entry(hash_handle h, int pos, hash_key_t key)
103+
static inline struct __hash_entry *__find_entry(hash_handle h, int pos, hash_key_t key)
105104
{
106105
struct __hash_entry *p;
107106
struct list_t *e;
@@ -128,15 +127,15 @@ static inline void *hash_lookup(hash_handle h, hash_key_t key)
128127
struct __hash_entry *p;
129128
return (p = __find_entry(h,pos,key))? p->value : NULL;
130129
}
131-
130+
132131
static inline int hash_add(hash_handle h, hash_key_t key, void *val)
133132
{
134133
struct __hash_entry *p;
135134
int pos = __hash_buf(h->size,key,h->buf_size);
136135

137-
ASSERT(p = __new_hash_entry(key,val),
136+
ASSERT(p = __new_hash_entry(key,val),
138137
return FAILURE);
139-
138+
140139
list_add(&(h->pool[pos]), &p->list_entry);
141140

142141
h->num_items += 1;
@@ -168,4 +167,4 @@ static inline hash_key_t __hash_buf(int size, hash_key_t key, size_t buf_size)
168167
#ifdef __cplusplus
169168
}
170169
#endif
171-
#endif
170+
#endif

src/rtsp/rtsp.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,11 +913,15 @@ void rtsp_configure_auth(rtsp_handle h, const char *user, const char *pass)
913913
int rtsp_tick(rtsp_handle h)
914914
{
915915
ASSERT(h, return FAILURE);
916+
struct timespec ts;
916917
struct timeval tv;
917918

918-
ASSERT(gettimeofday(&tv, NULL) == 0, ({
919-
ERR("gettimeofday failed\n");
919+
ASSERT(clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == 0, ({
920+
ERR("clock_gettime failed\n");
920921
return FAILURE;}));
921922

923+
tv.tv_sec = ts.tv_sec;
924+
tv.tv_usec = ts.tv_nsec / 1000;
925+
922926
return __get_timestamp_offset(&h->stat, &tv);
923927
}

src/rtsp/thread.h

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,26 @@ static inline void rendezvous_meet(rendezvous_handle hRv)
9090
DASSERT(hRv,return);
9191

9292

93-
93+
9494
pthread_mutex_lock(&hRv->mutex);
95-
95+
9696
if (!hRv->force) {
97-
97+
9898
if (hRv->count > 0) {
9999
hRv->count--;
100100
}
101-
101+
102102
if (hRv->count > 0 ) {
103103
pthread_cond_wait(&hRv->cond, &hRv->mutex);
104-
}
104+
}
105105
else {
106106
pthread_cond_broadcast(&hRv->cond);
107107
hRv->count = hRv->orig;
108108
}
109-
109+
110110
}
111111
pthread_mutex_unlock(&hRv->mutex);
112-
112+
113113
}
114114

115115
/******************************************************************************
@@ -289,7 +289,7 @@ fifo_flush(fifo_handle hfifo)
289289
pthread_mutex_unlock(&hfifo->mutex);
290290

291291
/*
292-
* Make sure any fifo_get() calls are unblocked
292+
* Make sure any fifo_get() calls are unblocked
293293
*/
294294
if (write(hfifo->pipes[1], &ch, 1) != 1) {
295295
return FAILURE;
@@ -363,7 +363,7 @@ typedef struct thread_job {
363363
} thread_job;
364364

365365
typedef thread_job *thread_handle;
366-
366+
367367
typedef struct threadpool {
368368
thread_job *threads[MAX_NUMTHREAD];
369369
shared_interface *sharedp;
@@ -426,28 +426,27 @@ static inline int _fifo_connect(fifo_handle *lhs, fifo_handle *rhs);
426426

427427
static inline void threadpool_delete(threadpool_handle h)
428428
{
429-
int i;
430-
if(h){
431-
if(h->sharedp){
432-
if(h->sharedp->rv_init){
433-
rendezvous_delete(h->sharedp->rv_init);
434-
}
435-
if(h->sharedp->rv_cleanup){
436-
rendezvous_delete(h->sharedp->rv_cleanup);
437-
}
438-
439-
gbl_delete(h->sharedp->gbl);
429+
if(!h) return;
440430

441-
FREE(h->sharedp);
431+
if(h->sharedp){
432+
if(h->sharedp->rv_init){
433+
rendezvous_delete(h->sharedp->rv_init);
442434
}
443-
444-
for(i=0;i<h->cnt;i++){
445-
thread_delete(h->threads[i]);
435+
if(h->sharedp->rv_cleanup){
436+
rendezvous_delete(h->sharedp->rv_cleanup);
446437
}
447438

439+
gbl_delete(h->sharedp->gbl);
448440

449-
FREE(h);
441+
FREE(h->sharedp);
450442
}
443+
444+
for(int i=0;i<h->cnt;i++){
445+
thread_delete(h->threads[i]);
446+
}
447+
448+
449+
FREE(h);
451450
}
452451

453452
static inline threadpool_handle threadpool_create(void *argsp)
@@ -486,7 +485,7 @@ static inline int threadpool_add(threadpool_handle h,thread_handle threadp)
486485

487486
for(i=0;i<h->cnt;i++){
488487
DASSERT(h->threads[i] != threadp,return FAILURE);
489-
488+
490489
}
491490

492491
memcpy(tmp,threadp->name,MAX_THREADNAME);
@@ -553,7 +552,7 @@ static inline thread_handle create_base_thread(threadpool_handle h_pool, const c
553552
nh->param_priv = params;
554553

555554
strncpy(nh->name, name,MAX_THREADNAME);
556-
555+
557556
ASSERT(threadpool_add(h_pool,nh) == SUCCESS ,goto error);
558557

559558
return nh;
@@ -647,27 +646,27 @@ static inline void thread_sync_cleanup(thread_handle h)
647646
if(h->hOutPut != NULL) fifo_flush(h->hOutPut);
648647
if(h->hInPut != NULL) fifo_flush(h->hInPut);
649648

650-
649+
651650
/* Make sure the other threads aren't waiting for us */
652651
rendezvous_force(sharedp->rv_init);
653-
652+
654653
/* Meet up with other threads before cleaning up */
655654
rendezvous_meet(sharedp->rv_cleanup);
656-
655+
657656
DBG("%s thread died\n",h->name);
658657
}
659658

660659
static inline void thread_delete(thread_handle h)
661660
{
662-
if(h){
663-
if(h->started){
664-
ERR("BUG: deleting uninitialized thread %s\n",h->name);
665-
}
661+
if(!h) return;
666662

667-
FREE(h->param_priv);
668-
669-
FREE(h);
663+
if(h->started){
664+
ERR("BUG: deleting uninitialized thread %s\n",h->name);
670665
}
666+
667+
FREE(h->param_priv);
668+
669+
FREE(h);
671670
}
672671

673672
static inline int _fifo_connect(fifo_handle *lhs, fifo_handle *rhs)
@@ -689,7 +688,7 @@ static inline int _fifo_connect(fifo_handle *lhs, fifo_handle *rhs)
689688
}
690689

691690

692-
static inline int thread_joint(thread_handle lhs, thread_handle rhs)
691+
static inline int thread_joint(thread_handle lhs, thread_handle rhs)
693692
{
694693
ASSERT(lhs,return FAILURE);
695694
ASSERT(rhs,return FAILURE);
@@ -700,7 +699,7 @@ static inline int thread_joint(thread_handle lhs, thread_handle rhs)
700699
return SUCCESS;
701700
}
702701

703-
static inline int thread_chain(thread_handle lhs, thread_handle rhs)
702+
static inline int thread_chain(thread_handle lhs, thread_handle rhs)
704703
{
705704
ASSERT(lhs,return FAILURE);
706705
ASSERT(rhs,return FAILURE);
@@ -710,7 +709,7 @@ static inline int thread_chain(thread_handle lhs, thread_handle rhs)
710709
return SUCCESS;
711710
}
712711

713-
static inline int thread_extend(thread_handle lhs, thread_handle rhs)
712+
static inline int thread_extend(thread_handle lhs, thread_handle rhs)
714713
{
715714
ASSERT(lhs,return FAILURE);
716715
ASSERT(rhs,return FAILURE);
@@ -720,7 +719,7 @@ static inline int thread_extend(thread_handle lhs, thread_handle rhs)
720719
return SUCCESS;
721720
}
722721

723-
static inline int thread_close(thread_handle lhs, thread_handle rhs)
722+
static inline int thread_close(thread_handle lhs, thread_handle rhs)
724723
{
725724
ASSERT(lhs,return FAILURE);
726725
ASSERT(rhs,return FAILURE);
@@ -743,4 +742,4 @@ static inline int thread_close(thread_handle lhs, thread_handle rhs)
743742
#define getpwnam __NEVER_EVER_USE_THREAD_UNSAFE_FUNCTION_IN_MULTI_THREADED_PROGRAM_YOU_SUCKER__
744743

745744

746-
#endif /* _IPC_H */
745+
#endif /* _IPC_H */

0 commit comments

Comments
 (0)