This repository was archived by the owner on Nov 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.hppy
More file actions
181 lines (150 loc) · 4.37 KB
/
context.hppy
File metadata and controls
181 lines (150 loc) · 4.37 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright (c) 2011, 2012 Timo Savola
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
#ifndef CONCRETE_CONTEXT_HPP
#define CONCRETE_CONTEXT_HPP
#include <cstddef>
#include <concrete/arena.hpp>
#include <concrete/objects/none.hpp>
#include <concrete/objects/object-partial.hpp>
#include <concrete/objects/type.hpp>
#include <concrete/portable.hpp>
#include <concrete/resource.hpp>
#include <concrete/util/noncopyable.hpp>
namespace concrete {
class EventCallback;
class EventLoop;
class EventTrigger;
class Execution;
class ScopedContext;
/**
* Python interpreter state. Instances are isolated from one another, so one
* process can have multiple interpreters running--either in separate threads
* or interleaved in common threads. A thread must not make nested calls to
* different Context instances because the active instance is passed around via
* thread-local data (without support for nesting).
*/
class Context: Noncopyable {
friend class EventCallback;
friend class ScopedContext;
public:
struct Data;
/**
* Get the active instance of the current thread.
*/
static Context &Active() throw ();
/**
* @internal debugging helper.
*/
static unsigned int NonthrowingExecutionAddress() throw ();
/**
* Create a new state.
* @param loop is owned by the caller and must live over the lifetime
* of the created Context instance
*/
explicit Context(EventLoop &loop);
/**
* Load state from a snapshot.
* @param loop is owned by the caller and must live over the lifetime
* of the created Context instance
* @param data will be copied
* @param size of the data in bytes
*/
Context(EventLoop &loop, void *data, size_t size);
/**
* Is this the active instance of the current thread?
*/
bool is_active() const throw () { return m_active; }
/**
* Access the I/O event interface.
*/
EventLoop &event_loop() throw () { return m_event_loop; }
/**
* Access the persistent memory.
*/
Arena &arena() throw () { return m_arena; }
/**
* Access the transient resources.
*/
ResourceManager &resource_manager() throw () { return m_resource_manager; }
/**
* Direct access to internal object vector.
* @return borrowed short-term reference
*/
Data *data();
/**
* Direct access to internal object vector.
* @return borrowed short-term reference or NULL
*/
const Data *nonthrowing_data() throw ();
/**
* Uncounted reference to the NoneObject instance.
* @return borrowed short-term NoneObject reference
*/
const Pointer &none_pointer() const throw () { return m_none_pointer; }
/**
* Look up an internal function or constant.
*/
Object load_builtin_name(const Object &name);
/**
* Look up an internal module.
*/
Object import_builtin_module(const Object &name);
/**
* Add a new executor. It will be scheduled at some point.
* @param execution reference is stolen
*/
void add_execution(const Execution &execution);
/**
* Get the active executor of this interpreter.
* @return borrowed short-term reference
*/
Portable<Execution> &execution() throw ();
/**
* Suspend the active executor until an I/O event occurs.
* @param trigger specifies the event which causes execution to resume
*/
void suspend_until(const EventTrigger &trigger);
/**
* Are there any unfinished executors (and the interpreter is in normal
* state)?
*/
bool executable() throw ();
/**
* Schedule an executor.
* @return true if there is more to execute immediately; otherwise the
* caller should wait for an I/O event first
*/
bool execute();
private:
EventLoop &m_event_loop;
Arena m_arena;
ResourceManager m_resource_manager;
Pointer m_none_pointer;
bool m_active;
};
/**
* Context activator.
*/
class ScopedContext: Noncopyable {
public:
/**
* Set the active Context instance of the current thread.
* @param context is owned by the caller and must live over the lifetime
* of the created ScopedContext instance
*/
explicit ScopedContext(Context &context) throw ();
/**
* Clear the active Context instance of the current thread.
*/
~ScopedContext() throw ();
private:
Context &m_context;
};
} // namespace
#endif