gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS Misc > All this yammering about Threads...

#102384 - OOPMan - Wed Sep 13, 2006 2:36 pm

Right, so, it seems like we don't have a proper, universal thread library for homebrew NDS development...

I searched through the forums and noticed a few threads related topics. The general line was that people had done some thread-related work but not created a complete library...

So I did a google for "thread library embedded systems" and got some promising results...

Top of the results was ProtoThreads

To quote from the website...

Quote:
Protothreads are extremely lightweight stackless threads designed for severely memory constrained systems, such as small embedded systems or wireless sensor network nodes. Protothreads provide linear code execution for event-driven systems implemented in C. Protothreads can be used with or without an underlying operating system to provide blocking event-handlers. Protothreads provide sequential flow of control without complex state machines or full multi-threading.

While protothreads originally were created for memory-constrained embedded systems, it has found many uses as a general purpose library too. Examples include multimedia streaming server software, grid computing research software, and MPEG decoding software for Internet TVs.

Read more...

Main features:
Very small RAM overhead - only two bytes per protothread and no extra stacks
Highly portable - the protothreads library is 100% pure C and no architecture specific assembly code
Can be used with or without an OS
Provides blocking wait without full multi-threading or stack-switching
Freely available under a BSD-like open source license

Example applications:
Memory constrained systems
Event-driven protocol stacks
Small embedded systems
Sensor network nodes
Portable C applications

For example usages, see the Examples page.

The protothreads library is released under an open source BSD-style license that freely allows for both non-commercial and commercial usage. The only requirement is that credit is given. Download the full source code here.

Protothreads were created by Adam Dunkels with support from Oliver Schmidt <ol.sc@web.de>


So, anyway, I'm downloading ProtoThreads and I'll see whether it passes through devkitARM without a peep...

Has anyone else come across any good threading libraries that would be appropriate to use on the NDS?

Why re-invent the wheel, after all :-)
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#102386 - kevinc - Wed Sep 13, 2006 3:10 pm

AFAIK that's not threading, it simply runs several short pieces of code sequentially. There's no parallelism there, each piece of code must return voluntarily before the next can run. That's different from true threading, where the thread lib runs pieces of each routine transparently.

For example, if you have a GUI routine and an X routine in two threads, you'd expect that if the X routine gets stuck in an infinite loop, the GUI routine still runs (or even kills the rogue thread). That wouldn't be possible with protothreads.

There's (also AFAIK) another difference to true threading. For example, if some kind of hardware access requires blocking time (for example, writing to FAT), while the hardware is waiting, the thread lib can switch to another thread and move on; with protothreading, the wait time remains.

/ my (uninformed) 0.02 ?
_________________
http://akzeac.blogspot.com

#102395 - OOPMan - Wed Sep 13, 2006 4:25 pm

Well, it is listed as proto-threading...

Even on a single processor thread execution is never actually truly parallel. The various threads are just all executing sequentially but switching very quickly...

I guess the question is, can the NDS hardware support true threading?

Given that some people have managed to get threading kind of working, the answer wuld be yes.

Still, given the lack of a common thread library, I'm guessing there's some problems overall :-(


Anyway, for small pieces of code, Protothreads might well be a better bet than nothing at all...
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#102399 - bjoerngiesler - Wed Sep 13, 2006 6:11 pm

OOPMan wrote:
I guess the question is, can the NDS hardware support true threading?


I'm sure it can, but do we need it? Threads give you less control over what your processor time gets used for, and more overhead (not to mention all the pitfalls with locking etc). Both undesirable, if you ask me, particularly on weak hardware. Much better to do your own scheduling.

The only *real* benefit of threads (besides being useful as a programming paradigm sometimes) is when you have a multiprocessor system and things actually *can* run in parallel. Which isn't true for the DS.
_________________
DSFTP homepage

#102400 - neonext - Wed Sep 13, 2006 6:20 pm

porting

#102403 - sajiimori - Wed Sep 13, 2006 6:47 pm

At work, we have real threads (that are interrupt based), full asymmetric coroutines (which have their own stack), and partial asymmetric coroutines (which don't have a stack).

"Proto threads" could be called "partial asymmetric coroutines."

Be careful with real threads -- they introduce a lot of complexity. We hardly ever use them.

#102411 - knight0fdragon - Wed Sep 13, 2006 9:38 pm

MightyMax did some threading and posted an example of it somewhere on these forums, but unless it is used for porting, I would recommend just using the IRQs
_________________
http://www.myspace.com/knight0fdragonds

MK DS FC: Dragon 330772 075464
AC WW FC: Anthony SamsClub 1933-3433-9458
MPFH: Dragon 0215 4231 1206

#102413 - OOPMan - Wed Sep 13, 2006 10:18 pm

Real threads are indeed troublesome creatures in many cases, but they still have their uses...

I read over MightyMax's thread. It's one thing to develop threads for one's own use, however, and quite another to create a general library with a good API...

IRQs alone will only take you so far (Deadlocks are not fun, I know from experience :-( )

Anyway, in my quest I uncovered another OSS library that seems to have more potential than ProtoThreads...

The library in question is MTClib

From the web-page:
Quote:
MTClib - MultiThreaded C

What is MTClib?

The MTC library is a multithreading library for C and C++ programs that is open source, cross platform and high performance.

Features
Pre-emptively scheduled threads with 32767 priority levels.
Priority inversion protection.
Mutexes.
Semaphores.
Very low overhead, millisecond resolution, callback timers.
Message passing.
Multiple readers, single writer locks.
Barriers.
Thread local heaps for lock free memory management.
Small footprint (typically < 10K code).
Fast context switching - under 2 microseconds on a 200 MHz Pentium.
Floating point context save (where supported by the target).
Fully instrumented source with logging capability.
100% ANSI C90 source with a single, small, platform/compiler adaptation module.
Lightweight ANSI C++ 99 class wrappers.
Platform independent.
Ideal for embedded systems.

What's in the package?

The package consists of the library sources, pre-built libraries, C/C++ header files, example C and C++ applications and projects or makefiles for the following target/compiler combinations:
32-bit DOS, DJGPP (version 2.03 or later) and gcc 4.0.
32-bit DOS, OpenWatcom C++ version 1.04.
16-bit DOS, Microsoft Visual C++ 1.52c or BorlandC++ 3.1.
16-bit DOS, OpenWatcom C++ version 1.04.
Win32, Microsoft Visual C++ 6.0.
Win32, OpenWatcom C++ 1.04.
Win32, BorlandC++ 5.5.
POSIX, Cygwin version 1.5.19 with gcc 3.4.
Mac OS 9 PowerPC target, CodeWarrior 8.
ARM 7, ARM SDT 2.51

Requirements
Supported target: e.g. x86 (e.g. DOS, Windows), PowerPC (Mac OS 9 and X) or ARM7.
A host system compatible with the chosen compiler e.g. Windows 98, Me, 2000, XP or Mac OS 9, X.
10 MB of free disk space.
Suitable ANSI C90 compiler.

Limitations of use
The MTC library is open source, shareware.
The library may be freely used by, or incorporated into products of, any individual for personal and private use or for their sole business without further restriction or charge.
Companies, organisations or governments must first obtain a license to use the library.
Any person, organisation or company intending to sell or redistribute for profit, products containing the library or any part thereof must first obtain a licence.
In no case may the library or any part thereof be re-badged, re-sold or used in a system to deprive anyone of life, liberty or livelihood.


What's interesting here is that they already support the ARM7. Given that most ARM architecture is supposed to be pretty uniform across the various lines and taking into account the fact that the source is supplied, it might well be possible to develop a proper MT library for the ARM9 using this as a base...

I'm going to take a look at it and see what comes of it...
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#102501 - OOPMan - Thu Sep 14, 2006 6:37 pm

Well, MTClib looks like it might well be portable...

Most of the library is basic C, with only a small amount that is platform specific. The developers have even provided a skeleton.c file which provides a framework for writing the necessary platform specific code...

A quick and dirty attempt to get it compiling under devkitARM wasn't ultra-successful though...

It choked while processing the ARM7 platform-specific code, specifically during a section containing some inline assembler code.

Well, I'm gonna tweak it about a little more and contact the developers and see if they have any ideas...
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...