Skip to content

Commit

Permalink
update makefile & readme
Browse files Browse the repository at this point in the history
  • Loading branch information
piaodazhu committed Oct 20, 2022
1 parent 7fc1e29 commit 2fed649
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 23 deletions.
22 changes: 8 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

CC = gcc
ECHO = echo

Expand All @@ -11,33 +10,28 @@ BIN = socket_server_example socket_client_example epoll_example sleep_example se
FLAG = -lpthread -O3 -ldl -I $(ROOT_DIR)/src
SSLFLAG = -lssl -lcrypto -D DYCO_SSL_OK

HASSSL := $(shell ldconfig -p | grep libssl)
HASCRYPTO := $(shell ldconfig -p | grep libcrypto)
HASRDS := $(shell ldconfig -p | grep libhiredis)
HASSSL := $(shell if [ -d /usr/local/include/openssl ] || [ -d /usr/include/openssl ]; then echo 1; fi)
HASRDS := $(shell if [ -d /usr/local/include/hiredis ] || [ -d /usr/include/hiredis ]; then echo 1; fi)

ifdef HASRDS
BIN += network_example
FLAG += $(SSLFLAG)
else
@echo "[Warning] libhiredis not found. redis cli example wont be build."
BIN += network_example
endif

ifdef HASSSL
ifdef HASCRYPTO
BIN += ssl_server_example ssl_client_example
else
@echo "[Warning] libssl or libcrypto not found."
endif
BIN += ssl_server_example ssl_client_example
FLAG += $(SSLFLAG)
endif

CUR_SOURCE = ${wildcard *.c}
CUR_OBJS = ${patsubst %.c, %.o, %(CUR_SOURCE)}

export CC BIN_DIR OBJS_DIR ROOT_DIR FLAG BIN ECHO

all : $(SUB_DIR) $(BIN)
all : PREPARE $(SUB_DIR) $(BIN)
.PHONY : all

PREPARE:
mkdir -p $(OBJS_DIR) $(BIN_DIR)

$(SUB_DIR) : ECHO
make -C $@
Expand Down
219 changes: 215 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,226 @@ You can give me a 🌟, or recommend it to others if you found dyco-coroutine he

# Build

TBD
```bash
# optional
$ sudo apt install libssl-dev
$ sudo apt install libhiredis-dev

# build
$ cd dyco-coroutine
$ make

# run
$ ./bin/xxx_example
```

# Get Started

TBD
```c
#include "dyco_coroutine.h"
#include <arpa/inet.h>

# Examples
// Pass the arguments by pointer.
struct foobarargs {
int arg1;
char *arg2;
};

TBD
void foobar(void *arg)
{
// Get the arguments by pointer.
struct foobarargs *args = arg;

int cid, fd, client, ret, status;

// coroID can be obtained
cid = dyco_coroutine_coroID();

// Get Udata if necessary
dyco_semaphore *sem;
dyco_coroutine_getUdata(cid, &sem);

// Create other coroutines if necessary
dyco_coroutine_create(foobar, NULL);

// Use dyco_coroutine_sleep() instead of sleep()
dyco_coroutine_sleep(1000);

// Use dyco_xx socket API if COROUTINE_HOOK is undefined
fd = dyco_socket(AF_INET, SOCK_STREAM, 0);
client = dyco_accept(fd, xxx, yyy);
ret = dyco_recv(client, xxx, yyy, 0);
ret = dyco_recv(client, xxx, yyy, 0);
dyco_close(client);

// Use normal socket API if COROUTINE_HOOK is defined
fd = socket(AF_INET, SOCK_STREAM, 0);
client = accept(fd, xxx, yyy);
ret = recv(client, xxx, yyy, 0);
ret = recv(client, xxx, yyy, 0);
close(client);

ret = fork();
if (ret == 0) {
exec(...)
}
else if (ret < 0) {
return;
}

// Wait child for 3000 ms. Set timeout to -1 to wait until child process is finished
ret = dyco_signal_waitchild(ret, &status, 3000);

// Use dyco_coroutine_sleep() instead of epoll_wait()
dyco_epoll_wait(...)

return;
}

int main()
{
// Optional: Pass the arguments by pointer.
struct foobarargs *args = calloc(1, sizeof(struct foobarargs));

// Create the corotine
int cid = dyco_coroutine_create(foobar, args);

// Optional: Set stack if necessary
char st[4096];
dyco_coroutine_setStack(cid, st, 4096);

// Optional: Create semaphore, channel, waitgroup or pubsubchannel
dyco_semaphore *sem = dyco_semaphore_create(3);

// Optional: Set Udata if necessary
dyco_coroutine_setUdata(cid, sem);

// Run
dyco_schedule_run();

return 0;
}
```
# User APIs
## Coroutine
Some basic coroutine method is defined.
```c
int dyco_coroutine_create(proc_coroutine func, void *arg);
void dyco_coroutine_free(dyco_coroutine *co);
void dyco_coroutine_sleep(uint32_t msecs);
int dyco_coroutine_waitRead(int fd, int timeout);
int dyco_coroutine_waitWrite(int fd, int timeout);
int dyco_coroutine_waitRW(int fd, int timeout);
int dyco_coroutine_coroID();
int dyco_coroutine_setStack(int cid, void *stackptr, size_t stacksize);
int dyco_coroutine_getStack(int cid, void **stackptr, size_t *stacksize);
int dyco_coroutine_setUdata(int cid, void *udata);
int dyco_coroutine_getUdata(int cid, void **udata);
int dyco_coroutine_getSchedCount(int cid);
```

## Scheduler

```c
int dyco_schedule_run();
int dyco_schedule_create(size_t stack_size, uint64_t loopwait_timeout);
void dyco_schedule_free(dyco_schedule *sched);

int dyco_schedule_schedID();
int dyco_schedule_setUdata(void *udata);
int dyco_schedule_getUdata(void **udata);
int dyco_schedule_getCoroCount();
```
## Scheduler Call
```c
int dyco_schedcall_sigprocmask(int __how, sigset_t *__set, sigset_t *__oset);
void dyco_schedcall_stop();
void dyco_schedcall_abort();
```

## epoll
```c
int dyco_epoll_init();
void dyco_epoll_destroy();
int dyco_epoll_add(int fd, struct epoll_event *ev);
int dyco_epoll_del(int fd, struct epoll_event *ev);
int dyco_epoll_wait(struct epoll_event *events, int maxevents, int timeout);
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
```
## Signal
```c
int dyco_signal_waitchild(const pid_t child, int *status, int timeout);
int dyco_signal_init(sigset_t *mask);
void dyco_signal_destroy();
int dyco_signal_wait(struct signalfd_siginfo *sinfo, int timeout);
```


## Half Duplex Channel

```c
dyco_channel* dyco_channel_create(size_t size);
void dyco_channel_destroy(dyco_channel **chan);
ssize_t dyco_channel_send(dyco_channel *chan, void *buf, size_t size, int timeout);
ssize_t dyco_channel_recv(dyco_channel *chan, void *buf, size_t maxsize, int timeout);
```
## Publish-subscribe Channel
```c
dyco_pubsubchannel* dyco_pubsub_create(size_t size);
void dyco_pubsub_destroy(dyco_pubsubchannel **pschan);
ssize_t dyco_pubsub_publish(dyco_pubsubchannel *pschan, void *buf, size_t size);
ssize_t dyco_pubsub_subscribe(dyco_pubsubchannel *pschan, void *buf, size_t maxsize, int timeout);
```

## Waitgroup
```c
dyco_waitgroup* dyco_waitgroup_create(int suggest_size);
void dyco_waitgroup_destroy(dyco_waitgroup **group);
int dyco_waitgroup_add(dyco_waitgroup* group, int cid);
int dyco_waitgroup_done(dyco_waitgroup* group);
int dyco_waitgroup_wait(dyco_waitgroup* group, int target, int timeout);
```
## Semaphore
```c
dyco_semaphore* dyco_semaphore_create(size_t value);
void dyco_semaphore_destroy(dyco_semaphore **sem);
int dyco_semaphore_wait(dyco_semaphore *sem, int timeout);
int dyco_semaphore_signal(dyco_semaphore *sem);
```

## Socket
```c
int dyco_socket(int domain, int type, int protocol);
int dyco_close(int fd);
int dyco_accept(int fd, struct sockaddr *addr, socklen_t *len);
int dyco_connect(int fd, struct sockaddr *name, socklen_t namelen);
ssize_t dyco_send(int fd, const void *buf, size_t len, int flags);
ssize_t dyco_recv(int fd, void *buf, size_t len, int flags);
ssize_t dyco_sendto(int fd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t dyco_recvfrom(int fd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
```
## SSL
```c
int dyco_SSL_accept(SSL *ssl);
int dyco_SSL_connect(SSL *ssl);
int dyco_SSL_read(SSL *ssl, void *buf, int num);
int dyco_SSL_write(SSL *ssl, const void *buf, int num);
```

# About Coroutine

Expand Down
4 changes: 1 addition & 3 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


CUR_SOURCE = ${wildcard *.c}

CUR_OBJS = ${patsubst %.c, %.o, $(CUR_SOURCE)}
Expand All @@ -10,7 +8,7 @@ $(SUB_DIR) : ECHO
make -C $@

$(CUR_OBJS) : %.o : %.c
$(CC) -c $^ -o $(OBJS_DIR)/$@ $(FLAG)
- $(CC) -c $^ -o $(OBJS_DIR)/$@ $(FLAG)

ECHO :
@echo $(SUB_DIR)
2 changes: 0 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


CUR_SOURCE = ${wildcard *.c}

CUR_OBJS = ${patsubst %.c, %.o, $(CUR_SOURCE)}
Expand Down

0 comments on commit 2fed649

Please sign in to comment.