Skip to content

Commit

Permalink
feat(melang): support running as a daemon process
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Nov 4, 2023
1 parent 5783c75 commit eebd86e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@



Melang is a script Language of step-sharing scheduling coroutine in single thread or multi-thread. It is only support on UNIX/Linux.
Melang is a script Language of time-sharing scheduling coroutine in single thread or multi-thread. It is only support on UNIX/Linux.



Expand Down
2 changes: 1 addition & 1 deletion docs/coroutine.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Coroutine

Coroutine in Melang is step-sharing scheduled in a single thread.
Coroutine in Melang is time-sharing scheduled in a single thread.

If you use the execution file which is generated by the repository on Github, script files in a same melang command will be executed in one process one thread.

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Thank you for using Melang.
Melang is a script language with these features below:

- basic programming structures
- step-sharing scheduling coroutine in single thread
- time-sharing scheduling coroutine in single thread
- reflection
- injection
- reactive programming
Expand Down
44 changes: 44 additions & 0 deletions melang.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "mln_conf.h"
#include "mln_iothread.h"
#include "mln_rbtree.h"
#include "mln_tools.h"

typedef struct mln_fd_node_s {
pthread_t tid;
Expand Down Expand Up @@ -46,6 +47,10 @@ static mln_fd_node_t *head = NULL;
static mln_fd_node_t *tail = NULL;
static pthread_mutex_t lock;
__thread mln_fd_node_t *t_node;
static mln_conf_item_t daemon_conf;
#if !defined(WIN32)
static int daemon_flag = 0;
#endif

int main(int argc, char *argv[])
{
Expand All @@ -64,6 +69,36 @@ int main(int argc, char *argv[])
fprintf(stderr, "init log failed.\n");
return -1;
}
#if !defined(WIN32)
if (daemon_flag) {
mln_conf_t *cf;
mln_conf_cmd_t *cc;
mln_conf_domain_t *cd;
if ((cf = mln_conf()) == NULL) {
mln_log(error, "Configuration error.\n");
return -1;
}
if ((cd = cf->search(cf, "main")) == NULL) {
mln_log(error, "Configuration error.\n");
return -1;
}
if ((cc = cd->search(cd, "daemon")) == NULL) {
if ((cc = cd->insert(cd, "daemon")) == NULL) {
mln_log(error, "insert configuration command 'daemon' failed.\n");
return -1;
}
}
daemon_conf.type = CONF_BOOL;
daemon_conf.val.b = 1;
if (cc->update(cc, &daemon_conf, 1) < 0) {
mln_log(error, "update configuration command 'daemon' failed.\n");
return -1;
}
if (mln_daemon() < 0) {
return -1;
}
}
#endif

rbattr.pool = NULL;
rbattr.pool_alloc = NULL;
Expand Down Expand Up @@ -99,8 +134,17 @@ static void mln_params_check(int argc, char *argv[])
printf("\tmelang <script-file> ...\n");
printf("\t-v\t\t\tshow version\n");
printf("\t-t=number_of_threads\tspecify the number of threads. Default is 1\n");
#if !defined(WIN32)
printf("\t-d\t\t\trunning as a daemon process\n");
#endif
} else if (!strncmp(argv[i], "-t=", 3)) {
/* do nothing */
} else if (!strcmp(argv[i], "-d")) {
#if defined(WIN32)
fprintf(stderr, "-d is not supported on Windows\n");
#else
daemon_flag = 1;
#endif
} else {
if ((fd = open(argv[i], O_RDONLY)) < 0) {
fprintf(stderr, "File %s cannot be read, %s\n", argv[i], strerror(errno));
Expand Down

0 comments on commit eebd86e

Please sign in to comment.