| 1 |
Design concepts for Mowgli |
| 2 |
-------------------------- |
| 3 |
|
| 4 |
1. Object orientation |
| 5 |
|
| 6 |
First and foremost, Mowgli is a base development framework that you can build |
| 7 |
higher level development frameworks ontop of. To facilitate this in a way that |
| 8 |
most people can understand, we use a somewhat LISP-like form of OO. |
| 9 |
|
| 10 |
What does this mean? |
| 11 |
|
| 12 |
In LISP, it is common for objects to be made up of smaller, more primitive |
| 13 |
objects that help to facilitate the implementation of the object. What this |
| 14 |
means is that, for example, we have mowgli_heap_t (which is a private object): |
| 15 |
|
| 16 |
struct mowgli_heap_ |
| 17 |
{ |
| 18 |
unsigned int elem_size; |
| 19 |
unsigned int mowgli_heap_elems; |
| 20 |
unsigned int free_elems; |
| 21 |
|
| 22 |
unsigned int alloc_size; |
| 23 |
|
| 24 |
unsigned int flags; |
| 25 |
|
| 26 |
mowgli_list_t blocks; /* list of non-empty blocks */ |
| 27 |
|
| 28 |
mowgli_allocation_policy_t *allocator; |
| 29 |
mowgli_boolean_t use_mmap; |
| 30 |
|
| 31 |
mowgli_mutex_t mutex; |
| 32 |
|
| 33 |
mowgli_block_t *empty_block; /* a single entirely free block, |
| 34 |
* or NULL */ |
| 35 |
}; |
| 36 |
|
| 37 |
This is an object which keeps state. It also depends on other objects, such as |
| 38 |
allocation policies and lists. There is a need for a destructor, it has member |
| 39 |
objects which have destructors, etc. |
| 40 |
|
| 41 |
C allows you to make a pointer out of anything -- block-level variables, for |
| 42 |
example can be "dereferenced" using the & operator. This means that if the heap |
| 43 |
above did not require initialization, you could just write something like: |
| 44 |
|
| 45 |
{ |
| 46 |
mowgli_heap_t heap; |
| 47 |
void *ptr; |
| 48 |
|
| 49 |
ptr = mowgli_heap_alloc(&heap); |
| 50 |
mowgli_heap_free(&heap, ptr); |
| 51 |
} |
| 52 |
|
| 53 |
However, we have a need to initialize this object, so we provide a constructor |
| 54 |
for it, mowgli_heap_create(). We also need to tear down the object |
| 55 |
specifically, so we provide mowgli_heap_destroy(). |
| 56 |
|
| 57 |
While it is easy to provide a separate initializer function, we feel this would |
| 58 |
be a bad idea for objects which keep state, because odds are likely the |
| 59 |
destructor would not be called. There is also a stack memory limit -- which is |
| 60 |
very small on embedded systems, where Mowgli is intended to be used. |
| 61 |
|
| 62 |
There is also the possibility that the library may depend on the reference |
| 63 |
remaining past the time it was used, such as an internal dependency (caching, |
| 64 |
for example). So we want to enforce that a destructor is called when needed, or |
| 65 |
at least make sure we don't crash because the object is no longer on the stack. |
| 66 |
|
| 67 |
[Yes, I know you can put objects in BSS by making them 'static.' There is |
| 68 |
limited space for BSS on many systems, so putting 100MB worth of objects there |
| 69 |
is kind of insane and makes your program not look so great either.] |
| 70 |
|
| 71 |
2. Threads are evil. |
| 72 |
|
| 73 |
"The one-sentence answer is that humans are not smart enough to write |
| 74 |
thread-safe code." |
| 75 |
-- Rasmus Lerdorf, inventor of PHP |
| 76 |
|
| 77 |
While PHP itself is a disaster of a programming language (for reasons I am not |
| 78 |
going to go into here), Rasmus Lerdorf is entirely correct in the above quote. |
| 79 |
While Mowgli is itself thread-safe, we do not recommend using threads as they |
| 80 |
promote inferior design patterns. There are better, safer ways to achieve |
| 81 |
concurrency using helpers and work queues with message-passing. |
| 82 |
|
| 83 |
Mowgli's thread library implements a subset of a thread library. Specifically, |
| 84 |
it implements the fastest possible mutex implementation, and global critical |
| 85 |
sections ala a global lock. It does not implement anything else other than |
| 86 |
that, except for maybe abstraction of TLS data in the future. But that TLS |
| 87 |
support would just be to further advance Mowgli's thread-safety so that it is |
| 88 |
more performant. |
| 89 |
|
| 90 |
Threads are a gigantic hack. Depending on what you want, there is usually |
| 91 |
something better for any pattern which involves threads: |
| 92 |
|
| 93 |
Pattern: Threads which do work and then sleep for a long time. |
| 94 |
|
| 95 |
Answer: Use mowgli_timer_add() on an eventloop object. This allows you to |
| 96 |
queue deferred work as a first-class citizen of your app's eventloop. |
| 97 |
|
| 98 |
Pattern: Multiple worker threads for I/O. |
| 99 |
|
| 100 |
Answer: Using concurrency in an I/O bound app is entirely pointless, but you |
| 101 |
can pass FDs to a helper using IPC. Pass the FD and then pass instructions on |
| 102 |
what to do with it. |
| 103 |
|
| 104 |
Pattern: Work that needs to be done concurrently. |
| 105 |
|
| 106 |
Answer: Helpers with message-passing and specific regions of memory shared |
| 107 |
between helpers, ala mmap() or VirtualAlloc(). |
| 108 |
|
| 109 |
Pattern: Work that needs to be done cooperatively. |
| 110 |
|
| 111 |
Answer: Producer-consumer models can be implemented as first-class citizens |
| 112 |
using the eventloop using pipes as a synchronization primitive. Alternatively, |
| 113 |
use a coroutine library, such as the work-in-progress mowgli.coroutine. |
| 114 |
|
| 115 |
Pretty much anything you can do with threads, you can do better with something, |
| 116 |
*anything* else. |
| 117 |
|
| 118 |
Please note the Mowgli team will generally not accept intrusive patches to |
| 119 |
add any additional threading support aside from fixes and locking bugs. No |
| 120 |
atomics, no making anything in the library use threads. Don't waste your time. |
| 121 |
|