source: libcfa/src/concurrency/kernel.hfa@ 3251b66

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 3251b66 was 71c8b7e, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Semaphore P() now returned whether or not it block

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[8118303]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//
[75a17f1]7// kernel --
[8118303]8//
9// Author : Thierry Delisle
[75f3522]10// Created On : Tue Jan 17 12:27:26 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[e3fea42]12// Last Modified On : Tue Feb 4 12:29:26 2020
13// Update Count : 22
[8118303]14//
15
[6b0b624]16#pragma once
[8118303]17
[c84e80a]18#include <stdbool.h>
[92976d9]19#include <stdint.h>
[8118303]20
[bd98b58]21#include "invoke.h"
[73abe95]22#include "time_t.hfa"
[d76bd79]23#include "coroutine.hfa"
[bd98b58]24
[8def349]25extern "C" {
26#include <pthread.h>
[6b4cdd3]27#include <semaphore.h>
[8def349]28}
29
[db6f06a]30//-----------------------------------------------------------------------------
31// Locks
[bdeba0b]32struct semaphore {
[ea7d2b0]33 __spinlock_t lock;
[bdeba0b]34 int count;
[ac2b598]35 __queue_t($thread) waiting;
[9c31349]36};
37
[242a902]38void ?{}(semaphore & this, int count = 1);
39void ^?{}(semaphore & this);
[71c8b7e]40bool P (semaphore & this);
[f0ce5f4]41bool V (semaphore & this);
[d384787]42bool V (semaphore & this, unsigned count);
[9c31349]43
[db6f06a]44
[bd98b58]45//-----------------------------------------------------------------------------
[de94a60]46// Processor
[de6319f]47extern struct cluster * mainCluster;
[bd98b58]48
[e60e0dc]49// Processor
[094476d]50coroutine processorCtx_t {
51 struct processor * proc;
52};
53
[e60e0dc]54// Wrapper around kernel threads
[c84e80a]55struct processor {
[e60e0dc]56 // Main state
[025278e]57 // Coroutine ctx who does keeps the state of the processor
[094476d]58 struct processorCtx_t runner;
[025278e]59
60 // Cluster from which to get threads
[de94a60]61 struct cluster * cltr;
[025278e]62
[de6319f]63 // Name of the processor
64 const char * name;
65
[025278e]66 // Handle to pthreads
67 pthread_t kernel_thread;
[2ac095d]68
[e60e0dc]69 // RunThread data
[025278e]70 // Action to do after a thread is ran
[ac2b598]71 $thread * destroyer;
[c81ebf9]72
[e60e0dc]73 // Preemption data
[025278e]74 // Node which is added in the discrete event simulaiton
75 struct alarm_node_t * preemption_alarm;
76
77 // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
78 bool pending_preemption;
[c81ebf9]79
[92e7631]80 // Idle lock (kernel semaphore)
81 __bin_sem_t idle;
[85b1deb]82
83 // Termination
84 // Set to true to notify the processor should terminate
85 volatile bool do_terminate;
86
[92e7631]87 // Termination synchronisation (user semaphore)
[85b1deb]88 semaphore terminated;
[de94a60]89
[27f5f71]90 // pthread Stack
91 void * stack;
92
[de94a60]93 // Link lists fields
[ea8b2f7]94 struct __dbg_node_proc {
[de94a60]95 struct processor * next;
96 struct processor * prev;
97 } node;
[14a61b5]98
[e60e0dc]99#ifdef __CFA_DEBUG__
[025278e]100 // Last function to enable preemption on this processor
[cdbfab0]101 const char * last_enable;
[e60e0dc]102#endif
[c84e80a]103};
104
[e3fea42]105void ?{}(processor & this, const char name[], struct cluster & cltr);
[242a902]106void ^?{}(processor & this);
[c84e80a]107
[de6319f]108static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
[de94a60]109static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
[e3fea42]110static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
[de6319f]111
[c7a900a]112static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]113
[92976d9]114//-----------------------------------------------------------------------------
115// I/O
[61dd73d]116struct __io_data;
[92976d9]117
[b6f2b213]118#define CFA_CLUSTER_IO_POLLER_USER_THREAD 1 << 0
119// #define CFA_CLUSTER_IO_POLLER_KERNEL_SIDE 1 << 1
120
[de94a60]121//-----------------------------------------------------------------------------
122// Cluster
123struct cluster {
124 // Ready queue locks
125 __spinlock_t ready_queue_lock;
126
127 // Ready queue for threads
[ac2b598]128 __queue_t($thread) ready_queue;
[de94a60]129
130 // Name of the cluster
131 const char * name;
132
133 // Preemption rate on this cluster
134 Duration preemption_rate;
135
136 // List of processors
[92e7631]137 __spinlock_t idle_lock;
[de94a60]138 __dllist_t(struct processor) procs;
139 __dllist_t(struct processor) idles;
[d4e68a6]140 unsigned int nprocessors;
[de94a60]141
[d4e68a6]142 // List of threads
[a1a17a74]143 __spinlock_t thread_list_lock;
[ac2b598]144 __dllist_t(struct $thread) threads;
[d4e68a6]145 unsigned int nthreads;
[a1a17a74]146
[de94a60]147 // Link lists fields
[ea8b2f7]148 struct __dbg_node_cltr {
[de94a60]149 cluster * next;
150 cluster * prev;
151 } node;
[92976d9]152
[61dd73d]153 struct __io_data * io;
[038be32]154
155 #if !defined(__CFA_NO_STATISTICS__)
156 bool print_stats;
157 #endif
[de94a60]158};
159extern Duration default_preemption();
160
[b6f2b213]161void ?{} (cluster & this, const char name[], Duration preemption_rate, int flags);
[de94a60]162void ^?{}(cluster & this);
163
[b6f2b213]164static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption(), 0}; }
165static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate, 0}; }
166static inline void ?{} (cluster & this, const char name[]) { this{name, default_preemption(), 0}; }
167static inline void ?{} (cluster & this, int flags) { this{"Anonymous Cluster", default_preemption(), flags}; }
168static inline void ?{} (cluster & this, Duration preemption_rate, int flags) { this{"Anonymous Cluster", preemption_rate, flags}; }
169static inline void ?{} (cluster & this, const char name[], int flags) { this{name, default_preemption(), flags}; }
[de94a60]170
[c7a900a]171static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]172
[0f2c555]173static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
174static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; }
[d4e68a6]175
[038be32]176#if !defined(__CFA_NO_STATISTICS__)
177 static inline void print_stats_at_exit( cluster & this ) {
178 this.print_stats = true;
179 }
180#endif
181
[8118303]182// Local Variables: //
[6b0b624]183// mode: c //
184// tab-width: 4 //
[8118303]185// End: //
Note: See TracBrowser for help on using the repository browser.