source: libcfa/src/concurrency/kernel_private.hfa @ 9f575ea

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 9f575ea was 9f575ea, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

First attempt at park/unpark

  • Property mode set to 100644
File size: 4.2 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// kernel_private.hfa --
8//
9// Author           : Thierry Delisle
10// Created On       : Mon Feb 13 12:27:26 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Nov 30 19:25:02 2019
13// Update Count     : 8
14//
15
16#pragma once
17
18#include "kernel.hfa"
19#include "thread.hfa"
20
21#include "alarm.hfa"
22
23
24//-----------------------------------------------------------------------------
25// Scheduler
26
27extern "C" {
28        void disable_interrupts() OPTIONAL_THREAD;
29        void enable_interrupts_noPoll();
30        void enable_interrupts( __cfaabi_dbg_ctx_param );
31}
32
33void ScheduleThread( thread_desc * ) __attribute__((nonnull (1)));
34static inline void WakeThread( thread_desc * thrd, bool must_yield ) {
35        if( !thrd ) return;
36
37        enum coroutine_state new_state = must_yield ? Reschedule : Rerun;
38
39        disable_interrupts();
40        static_assert(sizeof(thrd->state) == sizeof(int));
41        enum coroutine_state old_state = (enum coroutine_state)__atomic_exchange_n((volatile int *)&thrd->state, (int)new_state, __ATOMIC_SEQ_CST);
42        switch(old_state) {
43                case Active:
44                        // Wake won the race, the thread will reschedule/rerun itself
45                        break;
46                case Inactive:
47                        // Wake lost the race,
48                        thrd->state = Inactive;
49                        ScheduleThread( thrd );
50                        break;
51                case Rerun:
52                case Reschedule:
53                        abort("More than one thread attempted to schedule thread %p\n", thrd);
54                        break;
55                case Halted:
56                case Start:
57                case Primed:
58                default:
59                        // This makes no sense, something is wrong abort
60                        abort();
61        }
62        enable_interrupts( __cfaabi_dbg_ctx );
63}
64thread_desc * nextThread(cluster * this);
65
66//Block current thread and release/wake-up the following resources
67void BlockInternal(void);
68void BlockInternal(__spinlock_t * lock);
69void BlockInternal(thread_desc * thrd);
70void BlockInternal(__spinlock_t * lock, thread_desc * thrd);
71void BlockInternal(__spinlock_t * locks [], unsigned short count);
72void BlockInternal(__spinlock_t * locks [], unsigned short count, thread_desc * thrds [], unsigned short thrd_count);
73void BlockInternal(__finish_callback_fptr_t callback);
74void LeaveThread(__spinlock_t * lock, thread_desc * thrd);
75
76//-----------------------------------------------------------------------------
77// Processor
78void main(processorCtx_t *);
79
80void * create_pthread( pthread_t *, void * (*)(void *), void * );
81
82static inline void wake_fast(processor * this) {
83        __cfaabi_dbg_print_safe("Kernel : Waking up processor %p\n", this);
84        post( this->idleLock );
85}
86
87static inline void wake(processor * this) {
88        disable_interrupts();
89        wake_fast(this);
90        enable_interrupts( __cfaabi_dbg_ctx );
91}
92
93struct event_kernel_t {
94        alarm_list_t alarms;
95        __spinlock_t lock;
96};
97
98extern event_kernel_t * event_kernel;
99
100struct __cfa_kernel_preemption_state_t {
101        bool enabled;
102        bool in_progress;
103        unsigned short disable_count;
104};
105
106extern volatile thread_local __cfa_kernel_preemption_state_t preemption_state __attribute__ ((tls_model ( "initial-exec" )));
107
108//-----------------------------------------------------------------------------
109// Threads
110extern "C" {
111      void CtxInvokeThread(void (*main)(void *), void * this);
112}
113
114extern void ThreadCtxSwitch(coroutine_desc * src, coroutine_desc * dst);
115
116__cfaabi_dbg_debug_do(
117        extern void __cfaabi_dbg_thread_register  ( thread_desc * thrd );
118        extern void __cfaabi_dbg_thread_unregister( thread_desc * thrd );
119)
120
121//-----------------------------------------------------------------------------
122// Utils
123#define KERNEL_STORAGE(T,X) static char storage_##X[sizeof(T)]
124
125static inline uint32_t tls_rand() {
126        kernelTLS.rand_seed ^= kernelTLS.rand_seed << 6;
127        kernelTLS.rand_seed ^= kernelTLS.rand_seed >> 21;
128        kernelTLS.rand_seed ^= kernelTLS.rand_seed << 7;
129        return kernelTLS.rand_seed;
130}
131
132
133void doregister( struct cluster & cltr );
134void unregister( struct cluster & cltr );
135
136void doregister( struct cluster * cltr, struct thread_desc & thrd );
137void unregister( struct cluster * cltr, struct thread_desc & thrd );
138
139void doregister( struct cluster * cltr, struct processor * proc );
140void unregister( struct cluster * cltr, struct processor * proc );
141
142// Local Variables: //
143// mode: c //
144// tab-width: 4 //
145// End: //
Note: See TracBrowser for help on using the repository browser.