Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
136 lines (108 loc) · 2.57 KB

File metadata and controls

136 lines (108 loc) · 2.57 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
libutils - a collection of usable tools
Copyright (c) 2015 Tor Krill <tor@openproducts.com>
This file is part of libutils
libutils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Thread.h"
#include "Exceptions.h"
#include <sys/signal.h>
#include <string>
#include <cstring>
namespace Utils{
Thread::Thread(bool detached): thread(0), detached(detached),state( UnInitialized)
{
int s = pthread_attr_init(&attr);
if( s != 0 ){
throw ErrnoException("Failed to init thread attribute");
}
if( this->detached ){
s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if( s != 0 ){
throw ErrnoException("Failed to set detached state on thread attribute");
}
}
}
void* Thread::BootstrapThread(void* obj)
{
auto* q=static_cast<Thread*>(obj);
if (q)
{
q->state = Initialized;
q->PreRun();
q->state = Running;
q->Run();
q->state = HasRun;
q->PostRun();
q->state = Terminated;
}
return nullptr;
}
bool Thread::isRunning()
{
return this->state == Running;
}
void Thread::Start()
{
pthread_create( &this->thread, &this->attr, Thread::BootstrapThread, this);
}
void Thread::Kill()
{
this->Signal(SIGKILL);
}
void Thread::Yield()
{
sched_yield();
}
static void* trampolin(void* arg)
{
auto* fn = static_cast<Thread::Function*>(arg);
if( fn )
{
(*fn)();
}
return nullptr;
}
void Thread::Async(Function *fn)
{
pthread_t tid = 0;
pthread_attr_t attr;
if( pthread_attr_init(&attr) != 0 )
{
throw ErrnoException("Failed to initialize thread attributes");
}
if( pthread_attr_setdetachstate( & attr, PTHREAD_CREATE_DETACHED ) != 0 )
{
throw ErrnoException("Failed to set thread attribute detached");
}
if( pthread_create( &tid, &attr, trampolin, fn) != 0 )
{
throw ErrnoException("Failed to create thread");
}
}
void Thread::Signal(int signum)
{
pthread_kill( this->thread, signum );
}
int Thread::Join()
{
return pthread_join( this->thread, nullptr);
}
Thread::State Thread::RunState()
{
return this->state;
}
Thread::~Thread()
{
pthread_attr_destroy(&this->attr);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.