Skip to content

Commit

Permalink
[libthread] update
Browse files Browse the repository at this point in the history
  • Loading branch information
gozfree committed Aug 21, 2015
1 parent 7d4ecf1 commit 44a124c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
3 changes: 2 additions & 1 deletion libthread/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ OBJS_UNIT_TEST = test_${LIBNAME}.o
CFLAGS := -g -Wall -Werror -fPIC
CFLAGS += -I$(OUTPUT)/include
SHARED := -shared
LDFLAGS := -lpthread
LDFLAGS := -pthread
LDFLAGS += -L$(OUTPUT)/lib
LDFLAGS += -llog

.PHONY : all clean

Expand Down
46 changes: 40 additions & 6 deletions libthread/libthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,62 @@
* created: 2015-08-15 22:57
* updated: 2015-08-15 22:57
*****************************************************************************/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <stdarg.h>
#include <libgzf.h>
#include <liblog.h>
#include "libthread.h"



static void *__thread_func(void *arg)
{
struct thread *t = (struct thread *)arg;
if (t->func) {
printf("%s:%d t->tid = %ld\n", __func__, __LINE__, t->tid);
logd("thread %s is created, thread_id = %ld, thread_self = %ld\n",
t->name, t->tid, pthread_self());
t->is_run = 1;
t->func(t, t->arg);
t->is_run = 0;
logd("thread %s exits\n", t->name);
}

return NULL;
}

struct thread *thread_create(void *(*func)(struct thread *, void *), void *arg)
struct thread *thread_create(const char *name, void *(*func)(struct thread *, void *), void *arg)
{
struct thread *t = CALLOC(1, struct thread);
if (!t) {
printf("malloc thread failed(%d): %s\n", errno, strerror(errno));
loge("malloc thread failed(%d): %s\n", errno, strerror(errno));
goto err;
}
pthread_mutex_init(&t->mutex, NULL);
if (0 != pthread_cond_init(&t->cond, NULL)) {
printf("pthread_cond_init failed(%d): %s\n", errno, strerror(errno));
loge("pthread_cond_init failed(%d): %s\n", errno, strerror(errno));
goto err;
}
t->arg = arg;
t->func = func;
t->is_run = 0;
if (name) {
t->name = strdup(name);
}
if (0 != pthread_create(&t->tid, NULL, __thread_func, t)) {
printf("pthread_create failed(%d): %s\n", errno, strerror(errno));
loge("pthread_create failed(%d): %s\n", errno, strerror(errno));
goto err;
}
printf("%s:%d t->tid = %ld\n", __func__, __LINE__, t->tid);
if (name) {
if (0 != pthread_setname_np(t->tid, name)) {
loge("pthread_setname_np failed(%d): %s\n", errno, strerror(errno));
goto err;
}
}

return t;

err:
Expand All @@ -58,6 +76,9 @@ void thread_destroy(struct thread *t)
return;
}
pthread_mutex_destroy(&t->mutex);
if (t->name) {
free(t->name);
}
free(t);
}

Expand All @@ -80,3 +101,16 @@ int thread_mutex_unlock(struct thread *t)
{
return pthread_mutex_unlock(&t->mutex);
}


void thread_print_info(struct thread *t)
{
pthread_mutex_lock(&t->mutex);
logi("========\n");
logi("thread name = %s\n", t->name);
logi("thread id = %ld\n", t->tid);
logi("thread status=%s\n", t->is_run?"running":"stopped");
logi("========\n");
pthread_mutex_unlock(&t->mutex);

}
8 changes: 7 additions & 1 deletion libthread/libthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ struct thread {
pthread_t tid;
pthread_mutex_t mutex;
pthread_cond_t cond;
char *name; //only for debug, useless
int is_run;
void *(*func)(struct thread *, void *);
void *arg;
} thread_t;

struct thread *thread_create(void *(*func)(struct thread *, void *), void *arg);
struct thread *thread_create(const char *name, void *(*func)(struct thread *, void *), void *arg);
void thread_destroy(struct thread *t);
int thread_cond_wait(struct thread *t);
int thread_cond_signal(struct thread *t);
int thread_mutex_lock(struct thread *t);
int thread_mutex_unlock(struct thread *t);
int thread_sem_lock(struct thread *t);
int thread_sem_unloock(struct thread *t);

void thread_print_info(struct thread *t);


#ifdef __cpulspuls
Expand Down
11 changes: 7 additions & 4 deletions libthread/test_libthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

static void *thread(struct thread *t, void *arg)
{
printf("into thread, thread_id = %ld\n", pthread_self());
thread_mutex_lock(t);
thread_cond_wait(t);
thread_mutex_unlock(t);
Expand All @@ -24,10 +23,14 @@ static void *thread(struct thread *t, void *arg)

void foo()
{
struct thread *t1 = thread_create(thread, NULL);
// struct thread *t2 = thread_create(thread, NULL);
struct thread *t1 = thread_create("t1", thread, NULL);
struct thread *t2 = thread_create("t2", thread, NULL);
struct thread *t3 = thread_create(NULL, thread, NULL);
printf("%s: t1->tid = %ld\n", __func__, t1->tid);
// printf("%s: t2->tid = %ld\n", __func__, t2->tid);
printf("%s: t2->tid = %ld\n", __func__, t2->tid);
thread_print_info(t1);
thread_print_info(t2);
thread_print_info(t3);

}

Expand Down

0 comments on commit 44a124c

Please sign in to comment.