source: src/libcfa/concurrency/kernel.c @ 68e6031

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 68e6031 was c84e80a, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Kernel now supports [0-9] cfa threads on a single core, using round-robin scheduling and no preemption

  • Property mode set to 100644
File size: 4.3 KB
Line 
1//                              -*- Mode: CFA -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// kernel.c --
9//
10// Author           : Thierry Delisle
11// Created On       : Tue Jan 17 12:27:26 2016
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count     : 0
15//
16
17//Header
18#include "kernel"
19
20//C Includes
21#include <stddef.h>
22
23//CFA Includes
24#include "libhdr.h"
25#include "threads"
26
27//Private includes
28#define __CFA_INVOKE_PRIVATE__
29#include "invoke.h"
30
31processor systemProcessorStorage = {};
32processor * systemProcessor = &systemProcessorStorage;
33
34void ?{}(processor * this) {
35        this->cor = NULL;
36        this->thread_index = 0;
37        this->thread_count = 10;
38        this->terminated = false;
39
40        for(int i = 0; i < 10; i++) {
41                this->threads[i] = NULL;
42        }
43
44        LIB_DEBUG_PRINTF("Processor : ctor for core %p (core spots %d)\n", this, this->thread_count);
45}
46
47void ^?{}(processor * this) {
48
49}
50
51//-----------------------------------------------------------------------------
52// Processor coroutine
53struct proc_coroutine {
54        processor * proc;
55        coroutine c;
56};
57
58void ?{}(coroutine * this, processor * proc) {
59        this{};
60}
61
62DECL_COROUTINE(proc_coroutine)
63
64void ?{}(proc_coroutine * this, processor * proc) {
65        (&this->c){proc};
66        this->proc = proc;
67        proc->cor = this;
68}
69
70void ^?{}(proc_coroutine * this) {
71        ^(&this->c){};
72}
73
74void CtxInvokeProcessor(processor * proc) {
75        proc_coroutine proc_cor_storage = {proc};
76        resume( &proc_cor_storage );
77}
78
79//-----------------------------------------------------------------------------
80// Processor running routines
81void main(proc_coroutine * cor);
82thread_h * nextThread(processor * this);
83void runThread(processor * this, thread_h * dst);
84void spin(processor * this, unsigned int * spin_count);
85
86void main(proc_coroutine * cor) {
87        processor * this;
88        this = cor->proc;
89
90        thread_h * readyThread = NULL;
91        for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) {
92               
93                readyThread = nextThread(this);
94
95                if(readyThread) {
96                        runThread(this, readyThread);
97                        spin_count = 0;
98                } else {
99                        spin(this, &spin_count);
100                }               
101        }
102
103        LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this);
104}
105
106thread_h * nextThread(processor * this) {
107        for(int i = 0; i < this->thread_count; i++) {
108                this->thread_index = (this->thread_index + 1) % this->thread_count;     
109               
110                thread_h * thrd = this->threads[this->thread_index];
111                if(thrd) return thrd;
112        }
113
114        return NULL;
115}
116
117void runThread(processor * this, thread_h * dst) {
118        coroutine * proc_ctx = get_coroutine(this->cor);
119        coroutine * thrd_ctx = get_coroutine(dst);
120        thrd_ctx->last = proc_ctx;
121
122        // context switch to specified coroutine
123        // Which is now the current_coroutine
124        LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
125        current_coroutine = thrd_ctx;
126        CtxSwitch( proc_ctx->stack.context, thrd_ctx->stack.context );
127        current_coroutine = proc_ctx;
128        LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
129
130        // when CtxSwitch returns we are back in the processor coroutine
131}
132
133void spin(processor * this, unsigned int * spin_count) {
134        (*spin_count)++;
135}
136
137//-----------------------------------------------------------------------------
138// Kernel runner (Temporary)
139
140void scheduler_add( struct thread_h * thrd ) {
141        LIB_DEBUG_PRINTF("Kernel : scheduling %p on core %p (%d spots)\n", thrd, systemProcessor, systemProcessor->thread_count);
142        for(int i = 0; i < systemProcessor->thread_count; i++) {
143                if(systemProcessor->threads[i] == NULL) {
144                        systemProcessor->threads[i] = thrd;
145                        return;
146                }
147        }
148        assert(false);
149}
150
151void scheduler_remove( struct thread_h * thrd ) {
152        LIB_DEBUG_PRINTF("Kernel : unscheduling %p from core %p\n", thrd, systemProcessor);
153        for(int i = 0; i < systemProcessor->thread_count; i++) {
154                if(systemProcessor->threads[i] == thrd) {
155                        systemProcessor->threads[i] = NULL;
156                        break;
157                }
158        }
159        for(int i = 0; i < systemProcessor->thread_count; i++) {
160                if(systemProcessor->threads[i] != NULL) {
161                        return;
162                }
163        }
164        LIB_DEBUG_PRINTF("Kernel : terminating core %p\n\n\n", systemProcessor);       
165        systemProcessor->terminated = true;
166}
167
168void kernel_run( void ) {
169        CtxInvokeProcessor(systemProcessor);
170}
171
172// Local Variables: //
173// mode: c //
174// tab-width: 4 //
175// End: //
Note: See TracBrowser for help on using the repository browser.