summaryrefslogtreecommitdiffstats
path: root/workqueue.h
blob: 3f9f3208d52074beeec6fad31d363bf126a013d3 (plain)
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
#ifndef WORKQUEUE_H
#define WORKQUEUE_H

#include "db.h"
#include "trie.h"

typedef enum WorkQueueOp { NONE, ASSERT, RETRACT, RUN_WHEN, RUN_SUBSCRIBE, EVAL } WorkQueueOp;
typedef struct WorkQueueItem {
    WorkQueueOp op;

    // Clause pointers are the responsibility of the user of the
    // workqueue to keep alive (and to free once a work item is
    // processed). The workqueue does not itself copy or own or free
    // the Clause* you give it.
    union {
        struct {
            Clause* clause;
            // Caller is also responsible for freeing sourceFileName
            // on dequeue.
            char* sourceFileName;
            int sourceLineNumber;
        } assert;
        struct { Clause* pattern; } retract;
        struct {
            // The StatementRefs may be invalidated while this Run is
            // still in the workqueue -- if either is invalidated,
            // then the Run is invalidated.
            StatementRef when;
            Clause* whenPattern;
            StatementRef stmt;
        } runWhen;
        struct {
            // The subscribeRef may be invalidated while this Run is
            // still in the workqueue -- if so, then the Run is invalidated.
            StatementRef subscribeRef;
            Clause* subscribePattern;
            Clause* notifyClause;
        } runSubscribe;
        struct {
            char* code;
        } eval;
    };
} WorkQueueItem;

typedef struct WorkQueue WorkQueue;

// Global module initialization. Must call first.
void workQueueInit();

// Constructs a new work queue.
WorkQueue* workQueueNew();

// Removes the bottom item from work queue:
WorkQueueItem workQueueTake(WorkQueue* q);

// Adds an item to the bottom of work queue:
void workQueuePush(WorkQueue* q, WorkQueueItem item);

// Removes the top item from work queue:
WorkQueueItem workQueueSteal(WorkQueue* q);

int unsafe_workQueueCopy(WorkQueueItem* into, int maxn,
                         WorkQueue* q);

#endif