Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::resources::ThreadResource Class Referencefinal

A wrapper for a Thread which exposes Thread operations as Resource operations. More...

#include <scheduler_resources.h>

Inheritance diagram for MaxOS::processes::resources::ThreadResource:

Public Member Functions

 ThreadResource (const string &name, size_t flags, resource_type_t type)
 
void close (size_t flags) final
 Closes the thread resource, marking the thread to be removed by the scheduler.
 
int read (void *buffer, size_t size, size_t flags) final
 Reads data from the thread resource.
 
int write (const void *buffer, size_t size, size_t flags) final
 Writes data to the thread resource.
 

Public Attributes

Thread * thread
 The thread that this resource handles & exposes.
 
uint64_t exit_code
 The exit code of the thread once it has exited.
 

Detailed Description

A wrapper for a Thread which exposes Thread operations as Resource operations.

See also
Thread
Resource
Note
The thread pointer will be freed by scheduler during close()

Definition at line 59 of file scheduler_resources.h.

Constructor & Destructor Documentation

◆ ThreadResource()

ThreadResource::ThreadResource ( const string &  name,
size_t  flags,
resource_type_t  type 
)

Definition at line 179 of file scheduler_resources.cpp.

180: Resource(name, flags, type)
181{
182
183}
KPI::Resource Resource
Alias to make the libsyscore Resource accessible here.
Definition resource.h:26

Member Function Documentation

◆ close()

void ThreadResource::close ( size_t  flags)
final

Closes the thread resource, marking the thread to be removed by the scheduler.

Parameters
flagsN/A

Definition at line 190 of file scheduler_resources.cpp.

190 {
191
192 thread->thread_state = ThreadState::STOPPED;
193}
volatile thread_state_t thread_state
The current state of the thread.
Definition process.h:72
Thread * thread
The thread that this resource handles & exposes.

References thread, and MaxOS::processes::Thread::thread_state.

◆ read()

int ThreadResource::read ( void *  buffer,
size_t  size,
size_t  flags 
)
final

Reads data from the thread resource.

Parameters
bufferThe buffer to read into
sizeThe number of bytes to read
flagsThe flags to pass to the reading
Returns
The number of bytes successfully read or -1 on error

Definition at line 204 of file scheduler_resources.cpp.

204 {
205
206 // Thread not found
207 if(!thread && flags != (size_t)ThreadFlags::READ_STATS)
208 return -1;
209
210 switch ((ThreadFlags)flags) {
211
212 case ThreadFlags::READ_STATS: {
213
214 // Check bounds
215 if(size < sizeof(thread_stats_t))
216 return -1;
217
218 // Get the info
219 thread_stats_t stats = {};
220
221 if(thread) {
222 stats.tid = thread->tid;
223 stats.pid = thread->parent_pid;
224 stats.ticks = thread->ticks;
225 stats.state = (size_t)thread->thread_state;
226 stats.wakeup_time_ms = thread->wakeup_time;
227 }
228 stats.exit_code = exit_code;
229
230 // Copy across
231 memcpy(buffer, &stats, sizeof(thread_stats_t));
232 return sizeof(thread_stats_t);
233
234 }
235
236 default:
237 return -1;
238
239 }
240
241}
uint64_t parent_pid
The parent process ID.
Definition process.h:69
uint64_t tid
The thread ID.
Definition process.h:68
volatile size_t ticks
The number of ticks the thread has run for.
Definition process.h:74
volatile size_t wakeup_time
The time at which the thread should wake up (if sleeping)
Definition process.h:75
uint64_t exit_code
The exit code of the thread once it has exited.

References exit_code, MaxOS::processes::Thread::parent_pid, thread, MaxOS::processes::Thread::thread_state, MaxOS::processes::Thread::ticks, MaxOS::processes::Thread::tid, and MaxOS::processes::Thread::wakeup_time.

◆ write()

int ThreadResource::write ( const void *  buffer,
size_t  size,
size_t  flags 
)
final

Writes data to the thread resource.

Parameters
bufferThe buffer to write from
sizeThe number of bytes to write
flagsThe flags to pass to the writing
Returns
The number of bytes successfully written or -1 on error

Definition at line 252 of file scheduler_resources.cpp.

252 {
253
254 // Thread not found
255 if(!thread)
256 return -1;
257
258 switch ((ThreadFlags)flags) {
259
260 case ThreadFlags::WRITE_SLEEP_TIME: {
261
262 // Set the sleep time
263 uint64_t sleep_time = *((uint64_t*)buffer);
264 thread->sleep(sleep_time);
265 return size;
266 }
267
268 case ThreadFlags::WRITE_KILL: {
269
270 // Kill the thread
271 exit_code = *((uint64_t*)buffer);
272 thread->thread_state = ThreadState::STOPPED;
273 thread->yield();
274 return size;
275 }
276
277 case ThreadFlags::WRITE_YIELD: {
278 thread->yield();
279 return size;
280 }
281
282 case ThreadFlags::WRITE_ENABLE_PORT_IO: {
283 uint64_t port = *((uint64_t*)buffer);
284 thread->enable_port(port);
285 return size;
286 }
287
288 case ThreadFlags::WRITE_DISABLE_PORT_IO: {
289 uint64_t port = *((uint64_t*)buffer);
290 thread->disable_port(port);
291 return size;
292 }
293
294 default:
295 return -1;
296 }
297
298}
void enable_port(uint64_t port)
Sets bits in the threads TSS to allow a port for I/O.
Definition process.cpp:247
void yield()
Pass execution to the next ready thread.
Definition process.cpp:103
void disable_port(uint64_t port)
Sets bits in the threads TSS to disallow a port for I/O.
Definition process.cpp:258
void sleep(size_t milliseconds)
Sleeps the thread for a certain amount of time.
Definition process.cpp:92

References MaxOS::processes::Thread::disable_port(), MaxOS::processes::Thread::enable_port(), exit_code, MaxOS::processes::Thread::sleep(), thread, MaxOS::processes::Thread::thread_state, and MaxOS::processes::Thread::yield().

Member Data Documentation

◆ exit_code

uint64_t MaxOS::processes::resources::ThreadResource::exit_code

The exit code of the thread once it has exited.

Definition at line 66 of file scheduler_resources.h.

Referenced by read(), and write().

◆ thread

Thread* MaxOS::processes::resources::ThreadResource::thread

The thread that this resource handles & exposes.

Definition at line 65 of file scheduler_resources.h.

Referenced by close(), read(), and write().


The documentation for this class was generated from the following files: