-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.cpp
More file actions
38 lines (30 loc) · 1014 Bytes
/
thread.cpp
File metadata and controls
38 lines (30 loc) · 1014 Bytes
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
//
// thread.cpp
// TPSO1
//
// Created by Jean-Luc Nacif Coelho on 8/20/14.
// Copyright (c) 2014 GrupoSO. All rights reserved.
//
#include "thread.h"
#include "util.h"
#include <cstdlib>
#include <ucontext.h>
Thread::Thread(void(*callback)(), int param, int threadId, int priority) : main_context(*__main_context__) {
this->m_callback = callback;
this->m_param = param;
this->m_init_priority = priority;
this->m_priority = priority;
this->stack = (Stack) malloc (STANDARD_STACK_SIZE);
this->m_id = threadId;
this->m_context.uc_link = __main_context__;
this->m_context.uc_stack.ss_sp = this->stack;
this->m_context.uc_stack.ss_size = STANDARD_STACK_SIZE;
getcontext(&this->m_context);
makecontext(&this->m_context, this->m_callback(), 1, this->m_param);
}
Thread::~Thread() {
free(this->stack);
}
void MarkForDeath() { this->m_death = true; }
void Thread::save() { getcontext(&this->m_context); }
void Thread::restore() { setcontext(&this->m_context); }