Skip to content

Commit

Permalink
Added Context concept to docs. Updated interface (MODULE_CTX instead …
Browse files Browse the repository at this point in the history
…of CTX_MODULE macro as it broke internal naming convention.
  • Loading branch information
FedeDP committed Mar 10, 2018
1 parent f07a8bd commit 5641711
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Lib/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define DEFAULT_CTX "default"

/* Interface Macros */
#define CTX_MODULE(name, ctx) \
#define MODULE_CTX(name, ctx) \
static int init(void); \
static int check(void); \
static int evaluate(void); \
Expand All @@ -30,7 +30,7 @@
} \
void _dtor1_ ctx##_destroy_##name() { module_deregister(&self); }

#define MODULE(name) CTX_MODULE(name, default)
#define MODULE(name) MODULE_CTX(name, default)

/* Defines for easy API (with no need bothering with self and ctx) */
#define m_is(x) module_is(self, x)
Expand Down
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/3526dd92b6d84370b072bfadfc7da632)](https://www.codacy.com/app/FedeDP/libmodule?utm_source=github.com&utm_medium=referral&utm_content=FedeDP/libmodule&utm_campaign=Badge_Grade)
[![Documentation Status](https://readthedocs.org/projects/libmodule/badge/?version=latest)](http://libmodule.readthedocs.io/en/latest/?badge=latest)

Libmodule aims at let developers easily create modular C projects in a way that is both simple and elegant.
Libmodule aims to let developers easily create modular C projects in a way that is both simple and elegant.
You will write less code, focusing on what you really need.

## Is this an event loop or an actor lib?
Expand Down Expand Up @@ -55,8 +55,3 @@ This is a design choice that strongly differentiates libmodule from an actor lib
You may notice that recv method takes a message_t as parameter, that has "message" and "sender" fields.
These fields are unused right now, but they can be used to build a module's messaging system (with PubSub mechanism too) in the future, if any interest rises.

## I see there is a multi-context API. What is a context?

A context is a way to create subnets of modules. You can then loop on events from each context, and each context behaves independently from others.
This can be particularly useful when dealing with 2+ threads; ideally, each thread has its own module's context and thus its own events to be polled.

4 changes: 2 additions & 2 deletions Samples/MultiCtx/a.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static const char *myCtx = "FirstCtx";
* this module and its context as soon as program starts.
* Note that both module and context names are not passed as string here.
*/
CTX_MODULE(A, FirstCtx);
MODULE_CTX(A, FirstCtx);

static void recv_ready(message_t *msg, const void *userdata);

Expand Down Expand Up @@ -95,7 +95,7 @@ static void recv_ready(message_t *msg, const void *userdata) {
if (*counter % 3 == 0) {
m_unbecome();
}
if (counter == 10) {
if (*counter == 10) {
modules_ctx_quit(myCtx);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Samples/MultiCtx/b.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static const char *myCtx = "SecondCtx";
* this module and its context as soon as program starts.
* Note that both module and context names are not passed as string here.
*/
CTX_MODULE(B, SecondCtx);
MODULE_CTX(B, SecondCtx);

/*
* Initializes this module's state;
Expand Down
1 change: 1 addition & 0 deletions Samples/SharedSrc/mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <sys/timerfd.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>

static int A_init(void);
static int B_init(void);
Expand Down
4 changes: 2 additions & 2 deletions Samples/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Easy:
cd Easy; $(CC) *.c -L../../build -lmodule -I../../Lib -o sample.out -Wl,-rpath=../../build

SharedSrc:
cd SharedSrc; $(CC) *.c -L../../build -lmodule -I../../Lib -o sample.out -Wl,-rpath=../../build
cd SharedSrc; $(CC) *.c -L../../build -lmodule -I../../Lib -o sample.out -Wl,-rpath=../../build

MultiCtx:
cd MultiCtx; $(CC) *.c -L../../build -lmodule -lpthread -I../../Lib -o sample.out -Wl,-rpath=../../build
cd MultiCtx; $(CC) *.c -L../../build -lmodule -lpthread -I../../Lib -o sample.out -Wl,-rpath=../../build
15 changes: 13 additions & 2 deletions docs/src/concepts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,20 @@ Finally, this macro declares all of needed callbacks and returns an opaque handl
Submodule concept
-----------------

PLACEHOLDER
PLACEHOLDER (being worked on)

Context concept
---------------

PLACEHOLDER
A context is a way to create subnets of modules. You can loop on events from each context, and each context behaves independently from others. |br|
This can be particularly useful when dealing with 2+ threads; ideally, each thread has its own module's context and thus its own events to be polled. |br|
A context is automatically created for you first time a module that binds on that context is registered; so, multi-context API is very similar to single context one. |br|
To initialize a module binding it to its context, use MODULE_CTX macro:

.. code::
MODULE_CTX(test, myCtx)
This macro firstly creates a "myCtx" context, then a "test" module using same MODULE macro as before. |br|
Indeed, MODULE macro is only a particular case of MODULE_CTX macro, where myCtx is automatically setted to "default". |br|
This makes sense, as you can expect: single context API is a multi context API with only 1 context.

0 comments on commit 5641711

Please sign in to comment.