servalproject/serval-dna — pull request #68 by rom1v (2013-08-11 23:17:31+00:00)
Title: Rhizome parallelization

I recently [found](https://groups.google.com/forum/#!topic/serval-project-developers/kynmhNjv_To) that mdp packets and Rhizome stuff were executed on the same thread. Consequently, audio communications were broken during Rhizome synchronization.

I propose some code making all the Rhizome stuff executed in a separate POSIX thread (only in overlay mode, not necessary elsewhere).

With this code, I successfully exchange Rhizome bundles without impacting audio communications.

### Implementation

Here are the principles of my implementation (for more information, read the messages of the firsts commits).

The main idea is to use two fdqueue instances: one "main" and one "rhizome". In overlay mode, a new thread is started, which calls `fd_poll()` on the rhizome fdqueue.

An alarm (`sched_ent`) has now a field `fdqueue`, specifying on which fdqueue the alarm must be scheduled/watched. Thus, the main thread can schedule an alarm to be executed on the rhizome thread (and vice-versa).

Of course, the alarm data (its context) cannot be stack-stored (there is one stack per thread) and these alarms must be allocated dynamically, which implies quite a lot of changes. Commits with message beggining by "Alarm" create a wrapper to be scheduled on an fdqueue. Commits with message beggining by "Schedule" allocate parameters (if needed) and schedule an alarm to be executed on the other thread.

### Tests

All tests pass, except `FileTransferUnreliableBigMDP`.
This test sometimes [fails](https://github.com/servalproject/serval-dna/issues/67) even without my changes. If I change the filesize from 1MB to 500kB (in `setup_bugfile_common()` in `tests/rhizomeprotocol`), it pass.