mirror of
https://github.com/gozfree/gear-lib.git
synced 2026-02-06 11:13:01 +08:00
[libqueue] fix arg check
[libthread] add GEAR_API
This commit is contained in:
@@ -31,19 +31,10 @@
|
||||
|
||||
#define QUEUE_MAX_DEPTH 200
|
||||
|
||||
struct iovec *item_get_data(struct queue *q, struct item *it)
|
||||
{
|
||||
if (q->alloc_hook) {
|
||||
return &it->opaque;
|
||||
} else {
|
||||
return &it->data;
|
||||
}
|
||||
}
|
||||
|
||||
struct item *item_alloc(struct queue *q, void *data, size_t len, void *arg)
|
||||
GEAR_API struct item *item_alloc(struct queue *q, void *data, size_t len, void *arg)
|
||||
{
|
||||
struct item *item;
|
||||
if (!q) {
|
||||
if (!q || !data || len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
item = CALLOC(1, struct item);
|
||||
@@ -62,12 +53,9 @@ struct item *item_alloc(struct queue *q, void *data, size_t len, void *arg)
|
||||
return item;
|
||||
}
|
||||
|
||||
void item_free(struct queue *q, struct item *item)
|
||||
GEAR_API void item_free(struct queue *q, struct item *item)
|
||||
{
|
||||
if (!q) {
|
||||
return;
|
||||
}
|
||||
if (!item) {
|
||||
if (!q || !item) {
|
||||
return;
|
||||
}
|
||||
if (q->free_hook) {
|
||||
@@ -79,7 +67,19 @@ void item_free(struct queue *q, struct item *item)
|
||||
free(item);
|
||||
}
|
||||
|
||||
int queue_set_mode(struct queue *q, enum queue_mode mode)
|
||||
GEAR_API struct iovec *item_get_data(struct queue *q, struct item *it)
|
||||
{
|
||||
if (!q || !it) {
|
||||
return NULL;
|
||||
}
|
||||
if (q->alloc_hook) {
|
||||
return &it->opaque;
|
||||
} else {
|
||||
return &it->data;
|
||||
}
|
||||
}
|
||||
|
||||
GEAR_API int queue_set_mode(struct queue *q, enum queue_mode mode)
|
||||
{
|
||||
if (!q) {
|
||||
return -1;
|
||||
@@ -88,7 +88,7 @@ int queue_set_mode(struct queue *q, enum queue_mode mode)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int queue_set_hook(struct queue *q, alloc_hook *alloc_cb, free_hook *free_cb)
|
||||
GEAR_API int queue_set_hook(struct queue *q, alloc_hook *alloc_cb, free_hook *free_cb)
|
||||
{
|
||||
if (!q) {
|
||||
return -1;
|
||||
@@ -98,21 +98,24 @@ int queue_set_hook(struct queue *q, alloc_hook *alloc_cb, free_hook *free_cb)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int queue_set_depth(struct queue *q, int depth)
|
||||
GEAR_API int queue_set_depth(struct queue *q, int depth)
|
||||
{
|
||||
if (!q) {
|
||||
if (!q || depth <= 0) {
|
||||
return -1;
|
||||
}
|
||||
q->max_depth = depth;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int queue_get_depth(struct queue *q)
|
||||
GEAR_API int queue_get_depth(struct queue *q)
|
||||
{
|
||||
if (!q) {
|
||||
return -1;
|
||||
}
|
||||
return q->depth;
|
||||
}
|
||||
|
||||
struct queue *queue_create()
|
||||
GEAR_API struct queue *queue_create()
|
||||
{
|
||||
struct queue *q = CALLOC(1, struct queue);
|
||||
if (!q) {
|
||||
@@ -132,7 +135,7 @@ struct queue *queue_create()
|
||||
return q;
|
||||
}
|
||||
|
||||
int queue_flush(struct queue *q)
|
||||
GEAR_API int queue_flush(struct queue *q)
|
||||
{
|
||||
struct item *item, *next;
|
||||
if (!q) {
|
||||
@@ -153,7 +156,7 @@ int queue_flush(struct queue *q)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void queue_destroy(struct queue *q)
|
||||
GEAR_API void queue_destroy(struct queue *q)
|
||||
{
|
||||
if (!q) {
|
||||
return;
|
||||
@@ -164,7 +167,7 @@ void queue_destroy(struct queue *q)
|
||||
free(q);
|
||||
}
|
||||
|
||||
void queue_pop_free(struct queue *q)
|
||||
static void queue_pop_free(struct queue *q)
|
||||
{
|
||||
struct item *tmp = queue_pop(q);
|
||||
if (tmp) {
|
||||
@@ -172,7 +175,7 @@ void queue_pop_free(struct queue *q)
|
||||
}
|
||||
}
|
||||
|
||||
int queue_push(struct queue *q, struct item *item)
|
||||
GEAR_API int queue_push(struct queue *q, struct item *item)
|
||||
{
|
||||
if (!q || !item) {
|
||||
printf("invalid paraments!\n");
|
||||
@@ -197,7 +200,7 @@ int queue_push(struct queue *q, struct item *item)
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct item *queue_pop(struct queue *q)
|
||||
GEAR_API struct item *queue_pop(struct queue *q)
|
||||
{
|
||||
struct item *item = NULL;
|
||||
if (!q) {
|
||||
@@ -246,7 +249,7 @@ struct item *queue_pop(struct queue *q)
|
||||
return item;
|
||||
}
|
||||
|
||||
struct queue_branch *queue_branch_new(struct queue *q, const char *name)
|
||||
GEAR_API struct queue_branch *queue_branch_new(struct queue *q, const char *name)
|
||||
{
|
||||
struct queue_branch *qb = CALLOC(1, struct queue_branch);
|
||||
if (!q || !qb || !name) {
|
||||
@@ -262,9 +265,12 @@ struct queue_branch *queue_branch_new(struct queue *q, const char *name)
|
||||
return qb;
|
||||
}
|
||||
|
||||
int queue_branch_del(struct queue *q, const char *name)
|
||||
GEAR_API int queue_branch_del(struct queue *q, const char *name)
|
||||
{
|
||||
struct queue_branch *qb, *next;
|
||||
if (!q || !name) {
|
||||
return -1;
|
||||
}
|
||||
#if defined (__linux__) || defined (__CYGWIN__) || defined (FREERTOS)
|
||||
list_for_each_entry_safe(qb, next, &q->branch, hook) {
|
||||
#else
|
||||
@@ -283,9 +289,12 @@ int queue_branch_del(struct queue *q, const char *name)
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct queue_branch *queue_branch_get(struct queue *q, const char *name)
|
||||
GEAR_API struct queue_branch *queue_branch_get(struct queue *q, const char *name)
|
||||
{
|
||||
struct queue_branch *qb, *next;
|
||||
if (!q || !name) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if defined (__linux__) || defined (__CYGWIN__) || defined (FREERTOS)
|
||||
list_for_each_entry_safe(qb, next, &q->branch, hook) {
|
||||
@@ -299,10 +308,13 @@ struct queue_branch *queue_branch_get(struct queue *q, const char *name)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int queue_branch_notify(struct queue *q)
|
||||
GEAR_API int queue_branch_notify(struct queue *q)
|
||||
{
|
||||
struct queue_branch *qb, *next;
|
||||
char notify = '1';
|
||||
if (!q) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if defined (__linux__) || defined (__CYGWIN__) || defined (FREERTOS)
|
||||
list_for_each_entry_safe(qb, next, &q->branch, hook) {
|
||||
@@ -317,11 +329,14 @@ int queue_branch_notify(struct queue *q)
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct item *queue_branch_pop(struct queue *q, const char *name)
|
||||
GEAR_API struct item *queue_branch_pop(struct queue *q, const char *name)
|
||||
{
|
||||
struct queue_branch *qb, *next;
|
||||
char notify = '1';
|
||||
|
||||
if (!q || !name) {
|
||||
return NULL;
|
||||
}
|
||||
#if defined (__linux__) || defined (__CYGWIN__) || defined (FREERTOS)
|
||||
list_for_each_entry_safe(qb, next, &q->branch, hook) {
|
||||
#elif defined (__WIN32__) || defined (WIN32) || defined (_MSC_VER)
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#define LIBQUEUE_VERSION "0.1.0"
|
||||
#define LIBQUEUE_VERSION "0.1.1"
|
||||
|
||||
/*
|
||||
* queue is multi-reader single-writer
|
||||
|
||||
@@ -39,11 +39,13 @@ static void *__thread_func(void *arg)
|
||||
printf("thread function is null\n");
|
||||
return NULL;
|
||||
}
|
||||
t->run = true;
|
||||
t->func(t, t->arg);
|
||||
t->run = false;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct thread *thread_create(void *(*func)(struct thread *, void *), void *arg, ...)
|
||||
GEAR_API struct thread *thread_create(void *(*func)(struct thread *, void *), void *arg, ...)
|
||||
{
|
||||
enum lock_type type;
|
||||
va_list ap;
|
||||
@@ -96,7 +98,6 @@ struct thread *thread_create(void *(*func)(struct thread *, void *), void *arg,
|
||||
|
||||
t->arg = arg;
|
||||
t->func = func;
|
||||
t->run = true;
|
||||
if (0 != pthread_create(&t->tid, &t->attr, __thread_func, t)) {
|
||||
printf("pthread_create failed(%d): %s\n", errno, strerror(errno));
|
||||
goto err;
|
||||
@@ -110,17 +111,19 @@ err:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void thread_join(struct thread *t)
|
||||
GEAR_API int thread_join(struct thread *t)
|
||||
{
|
||||
pthread_join(t->tid, NULL);
|
||||
if (!t) {
|
||||
return -1;
|
||||
}
|
||||
return pthread_join(t->tid, NULL);
|
||||
}
|
||||
|
||||
void thread_destroy(struct thread *t)
|
||||
GEAR_API void thread_destroy(struct thread *t)
|
||||
{
|
||||
if (!t) {
|
||||
return;
|
||||
}
|
||||
t->run = false;
|
||||
switch (t->type) {
|
||||
case THREAD_LOCK_SPIN:
|
||||
break;
|
||||
@@ -138,12 +141,11 @@ void thread_destroy(struct thread *t)
|
||||
default:
|
||||
break;
|
||||
}
|
||||
pthread_join(t->tid, NULL);
|
||||
pthread_attr_destroy(&t->attr);
|
||||
free(t);
|
||||
}
|
||||
|
||||
int thread_set_name(struct thread *t, const char *name)
|
||||
GEAR_API int thread_set_name(struct thread *t, const char *name)
|
||||
{
|
||||
#if defined (__linux__) || defined (__CYGWIN__)
|
||||
|
||||
@@ -158,7 +160,7 @@ int thread_set_name(struct thread *t, const char *name)
|
||||
#endif
|
||||
}
|
||||
|
||||
void thread_get_info(struct thread *t)
|
||||
GEAR_API void thread_get_info(struct thread *t)
|
||||
{
|
||||
#if defined (__linux__) || defined (__CYGWIN__)
|
||||
int i;
|
||||
@@ -211,7 +213,7 @@ void thread_get_info(struct thread *t)
|
||||
#endif
|
||||
}
|
||||
|
||||
int thread_lock(struct thread *t)
|
||||
GEAR_API int thread_lock(struct thread *t)
|
||||
{
|
||||
if (!t) {
|
||||
return -1;
|
||||
@@ -229,7 +231,7 @@ int thread_lock(struct thread *t)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int thread_unlock(struct thread *t)
|
||||
GEAR_API int thread_unlock(struct thread *t)
|
||||
{
|
||||
if (!t) {
|
||||
return -1;
|
||||
@@ -247,7 +249,7 @@ int thread_unlock(struct thread *t)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int thread_wait(struct thread *t, int64_t ms)
|
||||
GEAR_API int thread_wait(struct thread *t, int64_t ms)
|
||||
{
|
||||
if (!t) {
|
||||
return -1;
|
||||
@@ -266,7 +268,7 @@ int thread_wait(struct thread *t, int64_t ms)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int thread_signal(struct thread *t)
|
||||
GEAR_API int thread_signal(struct thread *t)
|
||||
{
|
||||
if (!t) {
|
||||
return -1;
|
||||
@@ -284,7 +286,7 @@ int thread_signal(struct thread *t)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int thread_signal_all(struct thread *t)
|
||||
GEAR_API int thread_signal_all(struct thread *t)
|
||||
{
|
||||
if (!t) {
|
||||
return -1;
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
#define _POSIX_RW_LOCKS
|
||||
#endif
|
||||
|
||||
#define LIBTHREAD_VERSION "0.1.1"
|
||||
#define LIBTHREAD_VERSION "0.1.2"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -124,7 +124,7 @@ typedef struct thread {
|
||||
} thread_t;
|
||||
|
||||
struct thread *thread_create(void *(*func)(struct thread *, void *), void *arg, ...);
|
||||
void thread_join(struct thread *t);
|
||||
int thread_join(struct thread *t);
|
||||
void thread_destroy(struct thread *t);
|
||||
void thread_get_info(struct thread *t);
|
||||
int thread_set_name(struct thread *t, const char *name);
|
||||
|
||||
Reference in New Issue
Block a user