• Stars
    star
    264
  • Rank 155,103 (Top 4 %)
  • Language
    C
  • License
    MIT License
  • Created over 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tina is a teeny tiny, header only, coroutine and job library.

Tina

Tina is a teeny tiny, header only, coroutine and fiber library!

Coroutines are little lightweight userspace threading primitives, and they are super handy. OS threads are great if you want to use multiple CPUs, but syncronizing them is tricky and cumbersome. If you just want to run more than one bit of code at a time, coroutines are much easier to use. This makes them great for lightweight uses like implementing state machines directly as easy to read code, running a script in a video game to control a cutscene, or amortizing the cost of an long algorithm over time. Unlike OS threads, having thousands or millions of coroutines is not a problem. You are really only limited by RAM.

Tina is feature packed!

  • Super simple API: Basically just init(), resume() and yield() for assymetric coroutines
  • Fully symmetric coroutines (fibers) are supported too!
  • Bring your own memory (or let Tina malloc() for you)
  • Fast assembly language implementations
  • Cross platform, supporting several of the most common modern ABIs
    • System V for amd64: Mac, Linux, BSD, etc (and probably PS4)
    • Win64: Windows (and probably Xbox)
    • ARM aarch32 and aarch64: Unixes (and probably Mac / iOS / Android / Switch)
  • Supports GCC / Clang using inline assembly, and MSVC using embedded machine code
  • Minimal assembly footprint required to support a new ABI (armv7 is like a dozen instructions)
  • Minimal code footprint. Currently ~200 sloc

Limitations:

  • Obsolete or uncommon ABIs aren't supported (ex: 32 bit x86, MIPS, etc. Pull requests are welcome)
  • WASM explicitly forbids stackful coroutines :(
  • No RISCV support... yet ;)
  • Minimal built-in stack overflow protection: Bring your own memory means you need to bring your own guard pages and security too

Tina Jobs

Tina Jobs is a simple fiber based job system built on top of Tina. (Based on the ideas here: https://gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine)

Tina Jobs Features:

  • Jobs may yield to other jobs or abort before they finish. Each is run on a separate coroutine
  • Bring your own memory and threading
  • No dynamic allocations required at runtime
  • Multiple queues: You control when to run them and how
    • Parallel queues: Run a single queue from many worker threads
    • Serial queues: Run a queue from a single thread or poll it from somewhere
  • Simple priority model by linking queues together
  • Queue switching allows moving a job between queues
    • Ex: Load a texture on a parallel worker thread, but submit it on a serial graphics thread
  • Reasonable throughput: Though not a primary goal, even a Raspberry Pi can handle millions of jobs/sec!
  • Minimal code footprint: Currently ~300 sloc, which should make it easy to modify and extend

Limitations:

  • Not designed for extreme concurrency or throughput
    • Single lock per scheduler, doesn't implement work stealing, etc.
  • No dynamic allocations at runtime means you have to cap the maximum job/fiber counts at init time
  • API stability: I'm still making occasional changes and simplifications

What are coroutines anyway?

Functions are a simple and useful concept in structured programming. You give them some data, they process it, and return some data back to their caller. Coroutines on the other hand yield instead of returning, and can be resumed so they continue right where they left off.

There is a lot of confusing terminology around threading. So here's my best attempt at clarifying some of it.

  • Thread: A thread of execution. A flow of instructions as they are executed and their state (ex: CPU registers + stack).
  • Hardware Thread: The hardware pipeline that actually executes a thread. Usually a CPU core, but features like hyperthreading can provide multiple hardware threads per core.
  • OS Thread: OS threads are usually what people mean when simply saying "thread". It's a thread that is scheduled and managed by the OS. Usually using CPU interrupts to switch threads automatically without requiring any code changes to support multi-tasking. (ex: Windows threads, pthreads, etc)
  • Fiber: A lightweight thread implemented in user space code. Much simpler and faster than OS threads, but the fiber must explicitly yield to other fibers. (ex: Windows fibers, POSIX contexts, Tina coroutines)

So what's the difference between coroutines, fibers, generators, continuations, contexts, etc? Well... not much, and also a lot depending on who you talk to. Many aren't rigorously defined, so they tend to be used interchangeably. Some implementations operate at a language level by saving local variables. Some work by saving CPU registers. Some implementations have their own stack, while others work only within a single stack frame. Some implementations are asymmetric and can only yield back to the coroutine that resumed them, while others are symmetric and can switch to any other coroutine arbitrarily. Sometimes the terminology is simply a matter of what it's used for. For example generators are basically coroutines used like iterators.

Tina's coroutines (or fibers, or whatever you want to call them) each have their own stack, and they work by using ABI specific assembly code to save and restore the CPU registers. They can also be used in either a symmetric or asymmetric fashion which is handy. There are other coroutine/fiber libraries that provide a fast assembly implementation of course, but as far as I know Tina is the only one with a simple header only implementation (1). I'm not always a huge fan of header only libs, but avoiding a mess of assembler files in a cross platform project is quite nice! By supporting a few of the most common ABIs, Tina should run on all of the current desktop, mobile, and console platforms available in 2021. \o/

(1) Here's a new library that is very similar to Tina: https://github.com/edubart/minicoro

More Repositories

1

Chipmunk2D

A fast and lightweight 2D game physics library.
C
2,189
star
2

debugger.lua

A dependency free, embeddable debugger for Lua in a single file (.lua or .h)
Lua
766
star
3

Cocos2DShaderCookbook

A introductory guide to shaders with Cocos2D 3.1.
Objective-C
115
star
4

SpacePatrol

A high performance, large scale, deformable terrain example for Chipmunk Pro.
Objective-C
82
star
5

CocoRoids

Example game Cocos2D v3 using CCPhysics and SpriteBuilder.
Objective-C
63
star
6

CCPhysicsColorMatch

A simple CCPhysics based color matching game.
Objective-C
49
star
7

CloudBomber

Chipmunk Pro Autogeometry demo built on top of Cocos2D 2.0.
Objective-C
46
star
8

CCController

Use XBox, PlayStation (and other HID controllers) with Apple's GameController.framework.
Objective-C
32
star
9

ChipmunkShowcase

Chipmunk Pro Showcase app for iOS
Objective-C
28
star
10

UnofficialCocos2DTemplate

Cocos2D v3.4 template that does not require SpriteBuilder
Objective-C
26
star
11

veridian-expanse

Source code for the asteroid mining game I've been building.
C
22
star
12

ChipmunkColorMatch

A simple color matching game example for Chipmunk, Chipmunk Pro, Cocos2D and UIKit.
Objective-C
17
star
13

GalacticGuardian.spritebuilder

Open source Cocos2D/SpriteBuilder twin stick shooter.
Objective-C
15
star
14

LiFFT

The little FFT library
C
14
star
15

CausticCavern.spritebuilder

SpriteBuilder demo using some of the new 3.1 features.
Objective-C
14
star
16

critical-match

A hybrid platform/color matching game for NES.
C++
12
star
17

mini-lz4

A little lz4 implementation I made for fun.
Assembly
9
star
18

InterglacticTransmissing

Interglactic Transmissing for NES
C
8
star
19

TwilightGolf

Open sourcing an old iPhone game we made.
C
8
star
20

love-debugger

A wrapper for debugger.lua for the Lรถve game engine.
Lua
8
star
21

Gemeralds

Pinball game using CocosBuilder and Chipmunk Pro to make the collision geometry.
Objective-C
7
star
22

neslib-template

Template NES project in C using neslib.
C++
6
star
23

HotReloadingInC

C
5
star
24

AngryChipmunks

A simple Cocos2D/ChipmunkPro example project.
Objective-C
5
star
25

slembcke.github.io

Website
JavaScript
4
star
26

streamingtest

Playing around with memory mapped bandwith with lz4
C
4
star
27

Disasteroids2

Simple CCPhysics prototype game using GPL spritelib graphics.
Objective-C
3
star
28

flatpak-marathon

Flatpak build for Aleph One + Marathon 1, 2, and 3
Makefile
3
star
29

GCControllerTester

Extremely simple app that displays the values of an iOS 7 GCController (extended profile only).
Objective-C
3
star
30

elua.lua

ERB inspired templates for Lua.
Lua
2
star
31

pixler

C library for creating NES games.
Assembly
2
star
32

libpng

Temporary libpng Xcode project repository for Cocos2D.
C
2
star
33

PuzzleSnake

A little js / svg puzzle game
C
2
star
34

Super-City-Mayor

C++
2
star
35

SlapstickNES

Global Game Jam NES project template
C
2
star
36

ImageConvert

Extremely simple image conversion CLI tool for OS X.
Objective-C
1
star
37

MultiTouchObjectiveChipmunk

A simple Cocos2D 2.1 project demonstrating multi-touch physics with Objective-Chipmunk.
Objective-C
1
star
38

vscode-m68k

Gnu as m68k syntax highlighting for VSCode.
1
star
39

Blockade

Board game for the NES made during the 2021 Global Game Jam
C++
1
star
40

ggj22

Global Game Jam '22
Lua
1
star
41

AdventOfCode2020

Lua
1
star
42

iPhoneSnap2D

A simple ChipmunkPro/UIKit example of a physics board game.
Objective-C
1
star
43

tree-perf

Comparing performance of various AABB tree algorithms
C
1
star
44

over-crispd

Gene editing game for the NES inspired by Overcooked. Made for the Global Game Jam 2019.
C
1
star
45

ConcaveSprite

A simple example showing how to use Chipmunk Pro autogeometry to generate concave polygon shapes from Cocos2D sprites automatically.
Objective-C
1
star