source: libcfa/src/concurrency/clib/cfathread.cfa @ d27b6be

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since d27b6be was d27b6be, checked in by Thierry Delisle <tdelisle@…>, 3 years ago
  • Fixed TryLock? in blocking_lock implementation that did not return whether or not try_lock succeeded.
  • Fix cfathread try_lock interface.
  • Property mode set to 100644
File size: 9.4 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// clib/cfathread.cfa --
8//
9// Author           : Thierry Delisle
10// Created On       : Tue Sep 22 15:31:20 2020
11// Last Modified By :
12// Last Modified On :
13// Update Count     :
14//
15
16#include "fstream.hfa"
17#include "locks.hfa"
18#include "kernel.hfa"
19#include "stats.hfa"
20#include "thread.hfa"
21#include "time.hfa"
22
23#include "cfathread.h"
24
25extern void ?{}(processor &, const char[], cluster &, $thread *);
26extern "C" {
27      extern void __cfactx_invoke_thread(void (*main)(void *), void * this);
28}
29
30//================================================================================
31// Thread run y the C Interface
32
33struct cfathread_object {
34        $thread self;
35        void * (*themain)( void * );
36        void * arg;
37        void * ret;
38};
39void main(cfathread_object & this);
40void ^?{}(cfathread_object & mutex this);
41
42static inline $thread * get_thread( cfathread_object & this ) { return &this.self; }
43
44typedef ThreadCancelled(cfathread_object) cfathread_exception;
45typedef ThreadCancelled_vtable(cfathread_object) cfathread_vtable;
46
47void defaultResumptionHandler(ThreadCancelled(cfathread_object) & except) {
48        abort | "A thread was cancelled";
49}
50
51cfathread_vtable _cfathread_vtable_instance;
52
53cfathread_vtable const & get_exception_vtable(cfathread_exception *) {
54        return _cfathread_vtable_instance;
55}
56
57static void ?{}( cfathread_object & this, cluster & cl, void *(*themain)( void * ), void * arg ) {
58        this.themain = themain;
59        this.arg = arg;
60        ((thread&)this){"C-thread", cl};
61        __thrd_start(this, main);
62}
63
64void ^?{}(cfathread_object & mutex this) {
65        ^(this.self){};
66}
67
68void main( cfathread_object & this ) {
69        __attribute__((unused)) void * const thrd_obj = (void*)&this;
70        __attribute__((unused)) void * const thrd_hdl = (void*)active_thread();
71        /* paranoid */ verify( thrd_obj == thrd_hdl );
72
73        this.ret = this.themain( this.arg );
74}
75
76//================================================================================
77// Special Init Thread responsible for the initialization or processors
78struct __cfainit {
79        $thread self;
80        void (*init)( void * );
81        void * arg;
82};
83void main(__cfainit & this);
84void ^?{}(__cfainit & mutex this);
85
86static inline $thread * get_thread( __cfainit & this ) { return &this.self; }
87
88typedef ThreadCancelled(__cfainit) __cfainit_exception;
89typedef ThreadCancelled_vtable(__cfainit) __cfainit_vtable;
90
91void defaultResumptionHandler(ThreadCancelled(__cfainit) & except) {
92        abort | "The init thread was cancelled";
93}
94
95__cfainit_vtable ___cfainit_vtable_instance;
96
97__cfainit_vtable const & get_exception_vtable(__cfainit_exception *) {
98        return ___cfainit_vtable_instance;
99}
100
101static void ?{}( __cfainit & this, void (*init)( void * ), void * arg ) {
102        this.init = init;
103        this.arg = arg;
104        ((thread&)this){"Processir Init"};
105
106        // Don't use __thrd_start! just prep the context manually
107        $thread * this_thrd = get_thread(this);
108        void (*main_p)(__cfainit &) = main;
109
110        disable_interrupts();
111        __cfactx_start(main_p, get_coroutine(this), this, __cfactx_invoke_thread);
112
113        this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP];
114        /* paranoid */ verify( this_thrd->context.SP );
115
116        this_thrd->state = Ready;
117        enable_interrupts( __cfaabi_dbg_ctx );
118}
119
120void ^?{}(__cfainit & mutex this) {
121        ^(this.self){};
122}
123
124void main( __cfainit & this ) {
125        __attribute__((unused)) void * const thrd_obj = (void*)&this;
126        __attribute__((unused)) void * const thrd_hdl = (void*)active_thread();
127        /* paranoid */ verify( thrd_obj == thrd_hdl );
128
129        this.init( this.arg );
130}
131
132//================================================================================
133// Main Api
134extern "C" {
135        int cfathread_cluster_create(cfathread_cluster_t * cl) __attribute__((nonnull(1))) {
136                *cl = new();
137                return 0;
138        }
139
140        cfathread_cluster_t cfathread_cluster_self(void) {
141                return active_cluster();
142        }
143
144        int cfathread_cluster_print_stats( cfathread_cluster_t cl ) {
145                #if !defined(__CFA_NO_STATISTICS__)
146                        print_stats_at_exit( *cl, CFA_STATS_READY_Q | CFA_STATS_IO );
147                        print_stats_now( *cl, CFA_STATS_READY_Q | CFA_STATS_IO );
148                #endif
149                return 0;
150        }
151
152        int cfathread_cluster_add_worker(cfathread_cluster_t cl, pthread_t* tid, void (*init_routine) (void *), void * arg) {
153                __cfainit * it = 0p;
154                if(init_routine) {
155                        it = alloc();
156                        (*it){init_routine, arg};
157                }
158                processor * proc = alloc();
159                (*proc){ "C-processor", *cl, get_thread(*it) };
160
161                // Wait for the init thread to return before continuing
162                if(it) {
163                        ^(*it){};
164                        free(it);
165                }
166
167                if(tid) *tid = proc->kernel_thread;
168                return 0;
169        }
170
171        int cfathread_cluster_pause (cfathread_cluster_t) {
172                abort | "Pausing clusters is not supported";
173                exit(1);
174        }
175
176        int cfathread_cluster_resume(cfathread_cluster_t) {
177                abort | "Resuming clusters is not supported";
178                exit(1);
179        }
180
181        //--------------------
182        // Thread attributes
183        int cfathread_attr_init(cfathread_attr_t *attr) __attribute__((nonnull (1))) {
184                attr->cl = active_cluster();
185                return 0;
186        }
187
188        //--------------------
189        // Thread
190        int cfathread_create( cfathread_t * handle, const cfathread_attr_t * attr, void *(*main)( void * ), void * arg ) __attribute__((nonnull (1))) {
191                cluster * cl = attr ? attr->cl : active_cluster();
192                cfathread_t thrd = alloc();
193                (*thrd){ *cl, main, arg };
194                *handle = thrd;
195                return 0;
196        }
197
198        int cfathread_join( cfathread_t thrd, void ** retval ) {
199                void * ret = join( *thrd ).ret;
200                ^( *thrd ){};
201                free(thrd);
202                if(retval) {
203                        *retval = ret;
204                }
205                return 0;
206        }
207
208        int cfathread_get_errno(void) {
209                return errno;
210        }
211
212        cfathread_t cfathread_self(void) {
213                return (cfathread_t)active_thread();
214        }
215
216        int cfathread_usleep(useconds_t usecs) {
217                sleep(usecs`us);
218                return 0;
219        }
220
221        int cfathread_sleep(unsigned int secs) {
222                sleep(secs`s);
223                return 0;
224        }
225
226        void cfathread_park( void ) {
227                park();
228        }
229
230        void cfathread_unpark( cfathread_t thrd ) {
231                unpark( *thrd );
232        }
233
234        void cfathread_yield( void ) {
235                yield();
236        }
237
238        typedef struct cfathread_mutex * cfathread_mutex_t;
239
240        //--------------------
241        // Mutex
242        struct cfathread_mutex {
243                single_acquisition_lock impl;
244        };
245        int cfathread_mutex_init(cfathread_mutex_t *restrict mut, const cfathread_mutexattr_t *restrict) __attribute__((nonnull (1))) { *mut = new(); return 0; }
246        int cfathread_mutex_destroy(cfathread_mutex_t *mut) __attribute__((nonnull (1))) { delete( *mut ); return 0; }
247        int cfathread_mutex_lock   (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { lock( (*mut)->impl ); return 0; }
248        int cfathread_mutex_unlock (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { unlock( (*mut)->impl ); return 0; }
249        int cfathread_mutex_trylock(cfathread_mutex_t *mut) __attribute__((nonnull (1))) {
250                bool ret = try_lock( (*mut)->impl );
251                if( ret ) return 0;
252                else return EBUSY;
253        }
254
255        //--------------------
256        // Condition
257        struct cfathread_condition {
258                condition_variable(single_acquisition_lock) impl;
259        };
260        int cfathread_cond_init(cfathread_cond_t *restrict cond, const cfathread_condattr_t *restrict) __attribute__((nonnull (1))) { *cond = new(); return 0; }
261        int cfathread_cond_signal(cfathread_cond_t *cond) __attribute__((nonnull (1)))  { notify_one( (*cond)->impl ); return 0; }
262        int cfathread_cond_wait(cfathread_cond_t *restrict cond, cfathread_mutex_t *restrict mut) __attribute__((nonnull (1,2))) { wait( (*cond)->impl, (*mut)->impl ); return 0; }
263        int cfathread_cond_timedwait(cfathread_cond_t *restrict cond, cfathread_mutex_t *restrict mut, const struct timespec *restrict abstime) __attribute__((nonnull (1,2,3))) {
264                Time t = { *abstime };
265                if( wait( (*cond)->impl, (*mut)->impl, t ) ) {
266                        return 0;
267                }
268                errno = ETIMEDOUT;
269                return ETIMEDOUT;
270        }
271}
272
273#include <iofwd.hfa>
274
275extern "C" {
276        #include <unistd.h>
277        #include <sys/types.h>
278        #include <sys/socket.h>
279
280        //--------------------
281        // IO operations
282        int cfathread_socket(int domain, int type, int protocol) {
283                return socket(domain, type, protocol);
284        }
285        int cfathread_bind(int socket, const struct sockaddr *address, socklen_t address_len) {
286                return bind(socket, address, address_len);
287        }
288
289        int cfathread_listen(int socket, int backlog) {
290                return listen(socket, backlog);
291        }
292
293        int cfathread_accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len) {
294                return cfa_accept4(socket, address, address_len, 0, CFA_IO_LAZY);
295        }
296
297        int cfathread_connect(int socket, const struct sockaddr *address, socklen_t address_len) {
298                return cfa_connect(socket, address, address_len, CFA_IO_LAZY);
299        }
300
301        int cfathread_dup(int fildes) {
302                return dup(fildes);
303        }
304
305        int cfathread_close(int fildes) {
306                return cfa_close(fildes, CFA_IO_LAZY);
307        }
308
309        ssize_t cfathread_sendmsg(int socket, const struct msghdr *message, int flags) {
310                return cfa_sendmsg(socket, message, flags, CFA_IO_LAZY);
311        }
312
313        ssize_t cfathread_write(int fildes, const void *buf, size_t nbyte) {
314                return cfa_write(fildes, buf, nbyte, CFA_IO_LAZY);
315        }
316
317        ssize_t cfathread_recvfrom(int socket, void *restrict buffer, size_t length, int flags, struct sockaddr *restrict address, socklen_t *restrict address_len)  {
318                struct iovec iov;
319                iov.iov_base = buffer;
320                iov.iov_len = length;
321
322                struct msghdr msg;
323                msg.msg_name = address;
324                msg.msg_namelen = address_len ? (socklen_t)*address_len : (socklen_t)0;
325                msg.msg_iov = &iov;
326                msg.msg_iovlen = 1;
327                msg.msg_control = 0p;
328                msg.msg_controllen = 0;
329
330                ssize_t ret = cfa_recvmsg(socket, &msg, flags, CFA_IO_LAZY);
331
332                if(address_len) *address_len = msg.msg_namelen;
333                return ret;
334        }
335
336        ssize_t cfathread_read(int fildes, void *buf, size_t nbyte) {
337                return cfa_read(fildes, buf, nbyte, CFA_IO_LAZY);
338        }
339
340}
Note: See TracBrowser for help on using the repository browser.