source: libcfa/src/concurrency/clib/cfathread.cfa @ 420b498

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 420b498 was 420b498, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Moved cfathread to Colby's new lock.

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