-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtart.h
112 lines (83 loc) · 3.27 KB
/
tart.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
tart.h -- Tiny Actor Run-Time
"MIT License"
Copyright (c) 2013 Dale Schumacher, Tristan Slominski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#undef inline /*inline*/
#define TRACE(x) x /* enable tracing */
#define TRACE_MEMORY(x) /* enable tracing */
#define ALLOC(S) (calloc((S), 1))
#define NEW(T) ((T *)calloc(sizeof(T), 1))
#define FREE(p) ((p) = (free(p), NULL))
typedef void *Any;
typedef struct pair PAIR, *Pair;
typedef struct config CONFIG, *Config;
typedef struct behavior BEHAVIOR, *Behavior;
typedef struct actor ACTOR, *Actor;
typedef struct event EVENT, *Event;
typedef void (*Action)(Event e);
struct pair {
Any h;
Any t;
};
#define NIL ((Pair)0)
#define PR(h,t) pair_new((h),(t))
extern Pair pair_new(Any h, Any t);
extern Pair list_new();
extern int list_empty_p(Pair list);
extern Pair list_pop(Pair list);
extern Pair list_push(Pair list, Any item);
extern Pair queue_new();
extern int queue_empty_p(Pair q);
extern void queue_give(Pair q, Any item);
extern Any queue_take(Pair q);
extern Pair dict_new();
extern int dict_empty_p(Pair dict);
extern Any dict_lookup(Pair dict, Any key);
extern Pair dict_bind(Pair dict, Any key, Any value);
struct actor {
Behavior behavior; // current behavior
};
struct behavior {
Action action; // code
Any context; // data
};
struct event {
Config sponsor; // sponsor configuration
Actor actor; // target actor
Any message; // message to deliver
};
struct config {
Pair event_q; // queue of messages in-transit
Pair actors; // list of actors created
};
extern Actor actor_new(Behavior beh);
extern void actor_become(Actor a, Behavior beh);
extern Behavior behavior_new(Action act, Any data);
extern Event event_new(Config cfg, Actor a, Any msg);
extern Config config_new();
extern void config_enqueue(Config cfg, Event e);
extern void config_enlist(Config cfg, Actor a);
extern void config_send(Config cfg, Actor target, Any msg);
extern void config_create(Config cfg, Behavior beh);
extern int config_dispatch(Config cfg);
extern BEHAVIOR sink_behavior;
extern ACTOR sink_actor;