| 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 | // threads --
|
|---|
| 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 | #ifndef KERNEL_H
|
|---|
| 18 | #define KERNEL_H
|
|---|
| 19 |
|
|---|
| 20 | #include <stdbool.h>
|
|---|
| 21 |
|
|---|
| 22 | #include "invoke.h"
|
|---|
| 23 |
|
|---|
| 24 | extern "C" {
|
|---|
| 25 | #include <pthread.h>
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | //-----------------------------------------------------------------------------
|
|---|
| 29 | // Cluster
|
|---|
| 30 | struct cluster {
|
|---|
| 31 | simple_thread_list ready_queue;
|
|---|
| 32 | // pthread_spinlock_t lock;
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | void ?{}(cluster * this);
|
|---|
| 36 | void ^?{}(cluster * this);
|
|---|
| 37 |
|
|---|
| 38 | //-----------------------------------------------------------------------------
|
|---|
| 39 | // Processor
|
|---|
| 40 | enum ProcessorAction {
|
|---|
| 41 | Reschedule,
|
|---|
| 42 | NoAction
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 | struct processor {
|
|---|
| 46 | struct processorCtx_t * runner;
|
|---|
| 47 | cluster * cltr;
|
|---|
| 48 | coroutine * current_coroutine;
|
|---|
| 49 | thread * current_thread;
|
|---|
| 50 | pthread_t kernel_thread;
|
|---|
| 51 | simple_lock lock;
|
|---|
| 52 | volatile bool terminated;
|
|---|
| 53 | ProcessorAction thread_action;
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | void ?{}(processor * this);
|
|---|
| 57 | void ?{}(processor * this, cluster * cltr);
|
|---|
| 58 | void ^?{}(processor * this);
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | //-----------------------------------------------------------------------------
|
|---|
| 62 | // Locks
|
|---|
| 63 |
|
|---|
| 64 | void ?{}(simple_lock * this);
|
|---|
| 65 | void ^?{}(simple_lock * this);
|
|---|
| 66 |
|
|---|
| 67 | void lock( simple_lock * );
|
|---|
| 68 | void unlock( simple_lock * );
|
|---|
| 69 |
|
|---|
| 70 | struct pthread_spinlock_guard {
|
|---|
| 71 | pthread_spinlock_t * lock;
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | static inline void ?{}( pthread_spinlock_guard * this, pthread_spinlock_t * lock ) {
|
|---|
| 75 | this->lock = lock;
|
|---|
| 76 | pthread_spin_lock( this->lock );
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | static inline void ^?{}( pthread_spinlock_guard * this ) {
|
|---|
| 80 | pthread_spin_unlock( this->lock );
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | // //Simple spinlock implementation from
|
|---|
| 84 | // //http://stackoverflow.com/questions/1383363/is-my-spin-lock-implementation-correct-and-optimal
|
|---|
| 85 | // //Not optimal but correct
|
|---|
| 86 | // #define VOL
|
|---|
| 87 |
|
|---|
| 88 | // struct simple_spinlock {
|
|---|
| 89 | // VOL int lock;
|
|---|
| 90 | // };
|
|---|
| 91 |
|
|---|
| 92 | // extern VOL int __sync_lock_test_and_set( VOL int *, VOL int);
|
|---|
| 93 | // extern void __sync_synchronize();
|
|---|
| 94 |
|
|---|
| 95 | // static inline void lock( simple_spinlock * this ) {
|
|---|
| 96 | // while (__sync_lock_test_and_set(&this->lock, 1)) {
|
|---|
| 97 | // // Do nothing. This GCC builtin instruction
|
|---|
| 98 | // // ensures memory barrier.
|
|---|
| 99 | // }
|
|---|
| 100 | // }
|
|---|
| 101 |
|
|---|
| 102 | // static inline void unlock( simple_spinlock * this ) {
|
|---|
| 103 | // __sync_synchronize(); // Memory barrier.
|
|---|
| 104 | // this->lock = 0;
|
|---|
| 105 | // }
|
|---|
| 106 |
|
|---|
| 107 | #endif //KERNEL_H
|
|---|
| 108 |
|
|---|
| 109 | // Local Variables: //
|
|---|
| 110 | // mode: c //
|
|---|
| 111 | // tab-width: 4 //
|
|---|
| 112 | // End: //
|
|---|