projects
/
model-checker.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
model: factor out a 'switch_from_master()' function
[model-checker.git]
/
threads.cc
diff --git
a/threads.cc
b/threads.cc
index 7f515159f78052727180b1be6d82ba1df7e9ebac..a8f282a8b32efdb96d1d248570fc8b7574af92f7 100644
(file)
--- a/
threads.cc
+++ b/
threads.cc
@@
-4,9
+4,9
@@
#include <string.h>
#include <string.h>
-#include
"libthreads.h"
+#include
<threads.h>
#include "common.h"
#include "common.h"
-#include "threads.h"
+#include "threads
-model
.h"
/* global "model" object */
#include "model.h"
/* global "model" object */
#include "model.h"
@@
-23,7
+23,13
@@
static void stack_free(void *stack)
snapshot_free(stack);
}
snapshot_free(stack);
}
-/** Return the currently executing thread. */
+/**
+ * @brief Get the current Thread
+ *
+ * Must be called from a user context
+ *
+ * @return The currently executing thread
+ */
Thread * thread_current(void)
{
ASSERT(model);
Thread * thread_current(void)
{
ASSERT(model);
@@
-34,8
+40,6
@@
Thread * thread_current(void)
* Provides a startup wrapper for each thread, allowing some initial
* model-checking data to be recorded. This method also gets around makecontext
* not being 64-bit clean
* Provides a startup wrapper for each thread, allowing some initial
* model-checking data to be recorded. This method also gets around makecontext
* not being 64-bit clean
- * @todo We should make the START event always immediately follow the
- * CREATE event, so we don't get redundant traces...
*/
void thread_startup()
{
*/
void thread_startup()
{
@@
-86,6
+90,7
@@
int Thread::create_context()
*/
int Thread::swap(Thread *t, ucontext_t *ctxt)
{
*/
int Thread::swap(Thread *t, ucontext_t *ctxt)
{
+ t->set_state(THREAD_READY);
return swapcontext(&t->context, ctxt);
}
return swapcontext(&t->context, ctxt);
}
@@
-99,6
+104,7
@@
int Thread::swap(Thread *t, ucontext_t *ctxt)
*/
int Thread::swap(ucontext_t *ctxt, Thread *t)
{
*/
int Thread::swap(ucontext_t *ctxt, Thread *t)
{
+ t->set_state(THREAD_RUNNING);
return swapcontext(ctxt, &t->context);
}
return swapcontext(ctxt, &t->context);
}
@@
-106,12
+112,11
@@
int Thread::swap(ucontext_t *ctxt, Thread *t)
/** Terminate a thread and free its stack. */
void Thread::complete()
{
/** Terminate a thread and free its stack. */
void Thread::complete()
{
- if (!is_complete()) {
- DEBUG("completed thread %d\n", id_to_int(get_id()));
- state = THREAD_COMPLETED;
- if (stack)
- stack_free(stack);
- }
+ ASSERT(!is_complete());
+ DEBUG("completed thread %d\n", id_to_int(get_id()));
+ state = THREAD_COMPLETED;
+ if (stack)
+ stack_free(stack);
}
/**
}
/**
@@
-161,22
+166,33
@@
Thread::Thread(thrd_t *t, void (*func)(void *), void *a) :
/* Initialize state */
ret = create_context();
if (ret)
/* Initialize state */
ret = create_context();
if (ret)
-
printf
("Error in create_context\n");
+
model_print
("Error in create_context\n");
id = model->get_next_id();
id = model->get_next_id();
-
*user_thread = id
;
+
user_thread->priv = this
;
parent = thread_current();
}
/** Destructor */
Thread::~Thread()
{
parent = thread_current();
}
/** Destructor */
Thread::~Thread()
{
- complete();
+ if (!is_complete())
+ complete();
model->remove_thread(this);
}
/** @return The thread_id_t corresponding to this Thread object. */
model->remove_thread(this);
}
/** @return The thread_id_t corresponding to this Thread object. */
-thread_id_t Thread::get_id()
+thread_id_t Thread::get_id()
const
{
return id;
}
{
return id;
}
+
+/**
+ * Set a thread's THREAD_* state (@see thread_state)
+ * @param s The state to enter
+ */
+void Thread::set_state(thread_state s)
+{
+ ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
+ state = s;
+}