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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
#include "byebug.h"
/* Threads table class */
static VALUE cThreadsTable;
/* If not Qnil, holds the next thread that must be run */
VALUE next_thread = Qnil;
/* To allow thread syncronization, we must stop threads when debugging */
static VALUE locker = Qnil;
static int
t_tbl_mark_keyvalue(st_data_t key, st_data_t value, st_data_t tbl)
{
UNUSED(tbl);
rb_gc_mark((VALUE)key);
if (!value)
return ST_CONTINUE;
rb_gc_mark((VALUE)value);
return ST_CONTINUE;
}
static void
t_tbl_mark(void *data)
{
threads_table_t *t_tbl = (threads_table_t *)data;
st_table *tbl = t_tbl->tbl;
st_foreach(tbl, t_tbl_mark_keyvalue, (st_data_t)tbl);
}
static void
t_tbl_free(void *data)
{
threads_table_t *t_tbl = (threads_table_t *)data;
st_free_table(t_tbl->tbl);
xfree(t_tbl);
}
/*
* Creates a numeric hash whose keys are the currently active threads and
* whose values are their associated contexts.
*/
VALUE
create_threads_table(void)
{
threads_table_t *t_tbl;
t_tbl = ALLOC(threads_table_t);
t_tbl->tbl = st_init_numtable();
return Data_Wrap_Struct(cThreadsTable, t_tbl_mark, t_tbl_free, t_tbl);
}
/*
* Checks a single entry in the threads table.
*
* If it has no associated context or the key doesn't correspond to a living
* thread, the entry is removed from the thread's list.
*/
static int
check_thread_i(st_data_t key, st_data_t value, st_data_t data)
{
UNUSED(data);
if (!value)
return ST_DELETE;
if (!is_living_thread((VALUE)key))
return ST_DELETE;
return ST_CONTINUE;
}
/*
* Checks whether a thread is either in the running or sleeping state.
*/
int
is_living_thread(VALUE thread)
{
VALUE status = rb_funcall(thread, rb_intern("status"), 0);
if (NIL_P(status) || status == Qfalse)
return 0;
if (rb_str_cmp(status, rb_str_new2("run")) == 0
|| rb_str_cmp(status, rb_str_new2("sleep")) == 0)
return 1;
return 0;
}
/*
* Checks threads table for dead/finished threads.
*/
static void
cleanup_dead_threads(void)
{
threads_table_t *t_tbl;
Data_Get_Struct(threads, threads_table_t, t_tbl);
st_foreach(t_tbl->tbl, check_thread_i, 0);
}
/*
* Looks up a context in the threads table. If not present, it creates it.
*/
void
thread_context_lookup(VALUE thread, VALUE *context)
{
threads_table_t *t_tbl;
Data_Get_Struct(threads, threads_table_t, t_tbl);
if (!st_lookup(t_tbl->tbl, thread, context) || !*context)
{
*context = byebug_context_create(thread);
st_insert(t_tbl->tbl, thread, *context);
}
}
/*
* Holds thread execution while another thread is active.
*
* Thanks to this, all threads are "frozen" while the user is typing commands.
*/
void
acquire_lock(debug_context_t *dc)
{
while ((!NIL_P(locker) && locker != rb_thread_current())
|| CTX_FL_TEST(dc, CTX_FL_SUSPEND))
{
byebug_add_to_locked(rb_thread_current());
rb_thread_stop();
if (CTX_FL_TEST(dc, CTX_FL_SUSPEND))
CTX_FL_SET(dc, CTX_FL_WAS_RUNNING);
}
locker = rb_thread_current();
}
/*
* Releases our global lock and passes execution on to another thread, either
* the thread specified by +next_thread+ or any other thread if +next_thread+
* is nil.
*/
void
release_lock(void)
{
VALUE thread;
cleanup_dead_threads();
locker = Qnil;
if (NIL_P(next_thread))
thread = byebug_pop_from_locked();
else
{
byebug_remove_from_locked(next_thread);
thread = next_thread;
next_thread = Qnil;
}
if (!NIL_P(thread) && is_living_thread(thread))
rb_thread_run(thread);
}
/*
* call-seq:
* Byebug.unlock -> nil
*
* Unlocks global switch so other threads can run.
*/
static VALUE
Unlock(VALUE self)
{
UNUSED(self);
release_lock();
return locker;
}
/*
* call-seq:
* Byebug.lock -> Thread.current
*
* Locks global switch to reserve execution to current thread exclusively.
*/
static VALUE
Lock(VALUE self)
{
debug_context_t *dc;
VALUE context;
UNUSED(self);
if (!is_living_thread(rb_thread_current()))
rb_raise(rb_eRuntimeError, "Current thread is dead!");
thread_context_lookup(rb_thread_current(), &context);
Data_Get_Struct(context, debug_context_t, dc);
acquire_lock(dc);
return locker;
}
/*
*
* Document-class: ThreadsTable
*
* == Sumary
*
* Hash table holding currently active threads and their associated contexts
*/
void
Init_threads_table(VALUE mByebug)
{
cThreadsTable = rb_define_class_under(mByebug, "ThreadsTable", rb_cObject);
rb_define_module_function(mByebug, "unlock", Unlock, 0);
rb_define_module_function(mByebug, "lock", Lock, 0);
}
|