Skip to content

Commit

Permalink
add lib_fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
ubuntu14 committed May 30, 2016
1 parent feecae1 commit 92b0dc1
Show file tree
Hide file tree
Showing 29 changed files with 1,538 additions and 66 deletions.
7 changes: 7 additions & 0 deletions app/redis_tools/redis_builder.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#command broadcast dangerous
del false true
set false false
get false false
incr false false

#all false true
6 changes: 3 additions & 3 deletions lib_acl/include/fiber/filber.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ extern "C" {

#include "stdlib/acl_define.h"

typedef struct FIBER {
typedef struct ACL_FIBER {
unsigned int id;
} FIBER;
} ACL_FIBER;

ACL_API FIBER *acl_fiber_create(void);
ACL_API ACL_FIBER *acl_fiber_create(void);

#ifdef __cplusplus
}
Expand Down
2 changes: 2 additions & 0 deletions lib_acl/samples/thread/ypipe/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include ../Makefile.in
PROG = ypipe
79 changes: 79 additions & 0 deletions lib_acl/samples/thread/ypipe/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "lib_acl.h"
#include <time.h>

static int __max = 100000000;
static int __base = 10;
static char __dummy[256];

static void *thread_producer(void *arg)
{
ACL_YPIPE *ypipe = (ACL_YPIPE*) arg;
int i, j, n = __max / __base;

for (i = 0; i < n; i++) {
for (j = 0; j < __base; j++)
acl_ypipe_write(ypipe, __dummy);
acl_ypipe_flush(ypipe);
}

return NULL;
}

static void *thread_consumer(void *arg)
{
ACL_YPIPE *ypipe = (ACL_YPIPE*) arg;
int i;

for (i = 0; i < __max; i++) {
char *ptr = (char*) acl_ypipe_read(ypipe);
if (ptr == NULL) {
//printf("ptr NULL, i: %d\r\n", i);
}
}

printf("i: %d\r\n", i);
return NULL;
}

static void usage(const char *procname)
{
printf("usage: %s -h [help] -n max -b base\r\n", procname);
}

int main(int argc, char *argv[])
{
acl_pthread_attr_t attr;
acl_pthread_t t1, t2;
ACL_YPIPE *ypipe = acl_ypipe_new();
int ch;

while ((ch = getopt(argc, argv, "hn:b:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 'n':
__max = atoi(optarg);
break;
case 'b':
__base = atoi(optarg);
break;
default:
break;
}
}

memset(__dummy, 'x', sizeof(__dummy));
__dummy[sizeof(__dummy) - 1] = 0;

acl_pthread_attr_init(&attr);
acl_pthread_create(&t2, &attr, thread_consumer, ypipe);
acl_pthread_create(&t1, &attr, thread_producer, ypipe);
acl_pthread_join(t2, NULL);
acl_pthread_join(t1, NULL);

acl_ypipe_free(ypipe, NULL);
printf("over\r\n");

return 0;
}
4 changes: 2 additions & 2 deletions lib_acl/src/fiber/fiber.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#endif
#include "fiber/filber.h"

FIBER *acl_fiber_create(void)
ACL_FIBER *acl_fiber_create(void)
{
FIBER *fiber = (FIBER*) acl_mycalloc(1, sizeof(FIBER));
ACL_FIBER *fiber = (ACL_FIBER *) acl_mycalloc(1, sizeof(ACL_FIBER));
return fiber;
}
45 changes: 29 additions & 16 deletions lib_acl/src/stdlib/acl_atomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

#endif

#if defined(ACL_WINDOWS) || defined(ACL_LINUX)
# define HAS_ATOMIC
#else
# undef HAS_ATOMIC
#endif

struct ACL_ATOMIC {
void *value;
#ifndef ACL_WINDOWS
#ifndef HAS_ATOMIC
acl_pthread_mutex_t lock;
#endif
};
Expand All @@ -19,7 +25,7 @@ ACL_ATOMIC *acl_atomic_new(void)
{
ACL_ATOMIC *self = (ACL_ATOMIC*) acl_mymalloc(sizeof(ACL_ATOMIC));

#ifndef ACL_WINDOWS
#ifndef HAS_ATOMIC
acl_pthread_mutex_init(&self->lock, NULL);
#endif
self->value = NULL;
Expand All @@ -28,53 +34,60 @@ ACL_ATOMIC *acl_atomic_new(void)

void acl_atomic_free(ACL_ATOMIC *self)
{
#ifdef ACL_WINDOWS
self->value = NULL;
#else
#ifndef HAS_ATOMIC
acl_pthread_mutex_destroy(&self->lock);
acl_myfree(self);
#endif
}

void acl_atomic_set(ACL_ATOMIC *self, void *value)
{
#ifdef ACL_WINDOWS
InterlockedExchangePointer((volatile PVOID*)&self->value, value);
#else
#ifndef HAS_ATOMIC
acl_pthread_mutex_lock(&self->lock);
self->value = value;
acl_pthread_mutex_unlock(&self->lock);
#elif defined(ACL_WINDOWS)
InterlockedExchangePointer((volatile PVOID*) &self->value, value);
#elif defined(ACL_LINUX)
(void) __sync_lock_test_and_set(&self->value, value);
#endif
}

void *acl_atomic_cas(ACL_ATOMIC *self, void *cmp, void *value)
{
#ifdef ACL_WINDOWS
return InterlockedCompareExchangePointer(
(volatile PVOID*)&self->value, value, cmp);
#else
void *old = self->value;
#ifndef HAS_ATOMIC
void *old;

acl_pthread_mutex_lock(&self->lock);
old = self->value;
if (self->value == cmp)
self->value = value;
acl_pthread_mutex_unlock(&self->lock);

return old;
#elif defined(ACL_WINDOWS)
return InterlockedCompareExchangePointer(
(volatile PVOID*)&self->value, value, cmp);
#elif defined(ACL_LINUX)
return __sync_val_compare_and_swap(&self->value, cmp, value);
#endif
}

void * acl_atomic_xchg(ACL_ATOMIC *self, void *value)
{
#ifdef ACL_WINDOWS
return InterlockedExchangePointer((volatile PVOID*)&self->value, value);
#else
#ifndef HAS_ATOMIC
void *old;

void *old = self->value;
acl_pthread_mutex_lock(&self->lock);
old = self->value;
self->value = value;
acl_pthread_mutex_unlock(&self->lock);

return old;
#elif defined(ACL_WINDOWS)
return InterlockedExchangePointer((volatile PVOID*)&self->value, value);
#elif defined(ACL_LINUX)
return __sync_lock_test_and_set(&self->value, value);
#endif
}
15 changes: 7 additions & 8 deletions lib_acl/src/stdlib/common/acl_ypipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ struct ACL_YPIPE {

ACL_YPIPE *acl_ypipe_new(void)
{
ACL_YPIPE *self = (ACL_YPIPE *) acl_mymalloc(sizeof(ACL_YPIPE));
ACL_YPIPE *self = (ACL_YPIPE *) acl_mycalloc(1, sizeof(ACL_YPIPE));
void **item;

memset(self, 0, sizeof(ACL_YPIPE));
self->queue = acl_yqueue_new();
acl_yqueue_push(self->queue);
item = acl_yqueue_back(self->queue);;
item = acl_yqueue_back(self->queue);;
self->w = item;
self->f = item;
self->r = item;
Expand All @@ -46,16 +45,16 @@ void acl_ypipe_free(ACL_YPIPE *self, void(*free_data_fun)(void*))

void *acl_ypipe_read(ACL_YPIPE *self)
{
void **value;
void *value;

if (!acl_ypipe_check_read(self))
return NULL;

value = acl_yqueue_front(self->queue);
value = *acl_yqueue_front(self->queue);
acl_yqueue_pop(self->queue);
self->reads++;

return *value;
return value;
}

void acl_ypipe_write(ACL_YPIPE *self, void *data)
Expand All @@ -74,7 +73,7 @@ int acl_ypipe_flush(ACL_YPIPE *self)
if (acl_atomic_cas(self->c, self->w, self->f) != self->w) {
acl_atomic_set(self->c, self->f);
self->w = self->f;
return -1;
return 1;
}

self->w = self->f;
Expand All @@ -85,7 +84,7 @@ int acl_ypipe_check_read(ACL_YPIPE *self)
{
void **front = acl_yqueue_front(self->queue);

if (front != self->r &&self->r && *self->r)
if (front != self->r && self->r && *self->r)
return 1;

self->r = (void **) acl_atomic_cas(self->c, front, NULL);
Expand Down
10 changes: 5 additions & 5 deletions lib_acl/src/stdlib/common/acl_yqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,20 @@ void acl_yqueue_push(ACL_YQUEUE *self)

self->pushs++;
self->back_chunk = self->end_chunk;
self->back_pos = self->end_pos;
self->back_pos = self->end_pos;

self->end_pos++;
if (self->end_pos != CHUNK_SIZE)
return;

sc = (chunk_t *)acl_atomic_xchg(self->spare_chunk, NULL);
sc = (chunk_t *) acl_atomic_xchg(self->spare_chunk, NULL);

if (sc) {
self->end_chunk->next = sc;
sc->prev = self->end_chunk;
} else {
self->end_chunk->next =
(chunk_t*) acl_mymalloc(sizeof(chunk_t));
(chunk_t *) acl_mymalloc(sizeof(chunk_t));
memset(self->end_chunk->next, 0, sizeof(chunk_t));
acl_assert(self->end_chunk);
self->end_chunk->next->prev = self->end_chunk;
Expand All @@ -146,13 +146,13 @@ void acl_yqueue_pop(ACL_YQUEUE *self)

if (self->begin_pos == CHUNK_SIZE) {
chunk_t *cs;

chunk_t *o = self->begin_chunk;

self->begin_chunk = self->begin_chunk->next;
self->begin_chunk->prev = NULL;
self->begin_pos = 0;
memset(o, 0, sizeof(chunk_t));
cs = (chunk_t *)acl_atomic_xchg(self->spare_chunk, o);
cs = (chunk_t *) acl_atomic_xchg(self->spare_chunk, o);
if (cs)
acl_myfree(cs);
}
Expand Down
3 changes: 3 additions & 0 deletions lib_acl_cpp/changes.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
�޸���ʷ�б���

-----------------------------------------------------------------------
429) 2016.5.30
429.1) safety: string �����ָ����������˰�ȫ�Լ��

428) 2016.5.10
428.1) bugfix: http_request ���get_body�����У����� ps Ϊ�ֲ����������������
��Ȼ��ʹ�ö����±���
Expand Down
33 changes: 16 additions & 17 deletions lib_acl_cpp/src/mime/internal/mime_state_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,36 +611,35 @@ static int mime_state_head(MIME_STATE *state, const char *s, int n)
static int mime_bound_body(MIME_STATE *state, const char * const boundary,
MIME_NODE *node, const char *s, int n, int *finish)
{
const unsigned char *cp = (const unsigned char*) s;
const unsigned char *cp = (const unsigned char*) s;
const unsigned char *end = (const unsigned char*) s + n;
size_t bound_len = strlen(boundary);
register off_t curr_off = state->curr_off;
register off_t last_cr_pos = node->last_cr_pos;
off_t last_lf_pos = node->last_lf_pos;
const char *bound_ptr = node->bound_ptr;
unsigned char ch = 0;
size_t bound_len = strlen(boundary);
off_t curr_off = state->curr_off;
off_t last_cr_pos = node->last_cr_pos;
off_t last_lf_pos = node->last_lf_pos;
const char *bound_ptr = node->bound_ptr;

for (; cp < end; cp++) {

// 记录下 \r\n 的位置
if (*cp == '\n') {
if (*cp == '\r')
last_cr_pos = curr_off;
else if (*cp == '\n')
last_lf_pos = curr_off;
if (ch == '\r')
last_cr_pos = curr_off - 1;
}

ch = *cp;
curr_off++;

if (bound_ptr == NULL) {
if (ch == *boundary)
bound_ptr = boundary;
else
if (*cp != *boundary)
continue;
}

if (ch != *bound_ptr) {
bound_ptr = boundary;
}

if (*cp != *bound_ptr) {
bound_ptr = NULL;
} else if (*++bound_ptr == 0) {

/* 说明完全匹配 */
*finish = 1;

Expand Down
Loading

0 comments on commit 92b0dc1

Please sign in to comment.