source: libcfa/src/concurrency/clib/cfathread.cfa@ 339e30a

ADT ast-experimental
Last change on this file since 339e30a was 95dab9e, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Changed real_pthread symbols (now cfaabi_pthread) to be protected in libcfathread

  • Property mode set to 100644
File size: 16.4 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
[bc4a433]16// #define EPOLL_FOR_SOCKETS
[4500e52]17
[a1538cd]18#include "fstream.hfa"
19#include "locks.hfa"
[bb662027]20#include "kernel.hfa"
[d971c8d]21#include "stats.hfa"
[bb662027]22#include "thread.hfa"
[a1538cd]23#include "time.hfa"
[75965a6]24#include "stdlib.hfa"
[bb662027]25
[a1538cd]26#include "cfathread.h"
27
[4500e52]28extern "C" {
29 #include <string.h>
30 #include <errno.h>
31}
32
[e84ab3d]33extern void ?{}(processor &, const char[], cluster &, thread$ *);
[a5e7233]34extern "C" {
35 extern void __cfactx_invoke_thread(void (*main)(void *), void * this);
[4500e52]36 extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
[a5e7233]37}
38
[c457dc41]39extern Time __kernel_get_time();
[4500e52]40extern unsigned register_proc_id( void );
41
42//================================================================================
43// Epoll support for sockets
44
45#if defined(EPOLL_FOR_SOCKETS)
46 extern "C" {
47 #include <sys/epoll.h>
48 #include <sys/resource.h>
49 }
50
51 static pthread_t master_poller;
52 static int master_epollfd = 0;
53 static size_t poller_cnt = 0;
54 static int * poller_fds = 0p;
55 static struct leaf_poller * pollers = 0p;
56
57 struct __attribute__((aligned)) fd_info_t {
58 int pollid;
59 size_t rearms;
60 };
61 rlim_t fd_limit = 0;
62 static fd_info_t * volatile * fd_map = 0p;
63
64 void * master_epoll( __attribute__((unused)) void * args ) {
65 unsigned id = register_proc_id();
66
67 enum { MAX_EVENTS = 5 };
68 struct epoll_event events[MAX_EVENTS];
69 for() {
70 int ret = epoll_wait(master_epollfd, events, MAX_EVENTS, -1);
71 if ( ret < 0 ) {
72 abort | "Master epoll error: " | strerror(errno);
73 }
74
75 for(i; ret) {
76 thread$ * thrd = (thread$ *)events[i].data.u64;
77 unpark( thrd );
78 }
79 }
80
81 return 0p;
82 }
83
84 static inline int epoll_rearm(int epollfd, int fd, uint32_t event) {
85 struct epoll_event eevent;
86 eevent.events = event | EPOLLET | EPOLLONESHOT;
87 eevent.data.u64 = (uint64_t)active_thread();
88
89 if(0 != epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &eevent))
90 {
91 if(errno == ENOENT) return -1;
92 abort | acquire | "epoll" | epollfd | "ctl rearm" | fd | "error: " | errno | strerror(errno);
93 }
94
95 park();
96 return 0;
97 }
98
99 thread leaf_poller {
100 int epollfd;
101 };
102
103 void ?{}(leaf_poller & this, int fd) { this.epollfd = fd; }
104
105 void main(leaf_poller & this) {
106 enum { MAX_EVENTS = 1024 };
107 struct epoll_event events[MAX_EVENTS];
108 const int max_retries = 5;
109 int retries = max_retries;
110
111 struct epoll_event event;
112 event.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
113 event.data.u64 = (uint64_t)&(thread&)this;
114
115 if(0 != epoll_ctl(master_epollfd, EPOLL_CTL_ADD, this.epollfd, &event))
116 {
117 abort | "master epoll ctl add leaf: " | errno | strerror(errno);
118 }
119
120 park();
121
122 for() {
123 yield();
124 int ret = epoll_wait(this.epollfd, events, MAX_EVENTS, 0);
125 if ( ret < 0 ) {
126 abort | "Leaf epoll error: " | errno | strerror(errno);
127 }
128
129 if(ret) {
130 for(i; ret) {
131 thread$ * thrd = (thread$ *)events[i].data.u64;
[75c7252]132 unpark( thrd, UNPARK_REMOTE );
[4500e52]133 }
134 }
135 else if(0 >= --retries) {
136 epoll_rearm(master_epollfd, this.epollfd, EPOLLIN);
137 }
138 }
139 }
140
141 void setup_epoll( void ) __attribute__(( constructor ));
142 void setup_epoll( void ) {
143 if(master_epollfd) abort | "Master epoll already setup";
144
145 master_epollfd = epoll_create1(0);
146 if(master_epollfd == -1) {
147 abort | "failed to create master epoll: " | errno | strerror(errno);
148 }
149
150 struct rlimit rlim;
151 if(int ret = getrlimit(RLIMIT_NOFILE, &rlim); 0 != ret) {
152 abort | "failed to get nofile limit: " | errno | strerror(errno);
153 }
154
155 fd_limit = rlim.rlim_cur;
156 fd_map = alloc(fd_limit);
157 for(i;fd_limit) {
158 fd_map[i] = 0p;
159 }
160
[75c7252]161 poller_cnt = 2;
[4500e52]162 poller_fds = alloc(poller_cnt);
163 pollers = alloc(poller_cnt);
164 for(i; poller_cnt) {
165 poller_fds[i] = epoll_create1(0);
166 if(poller_fds[i] == -1) {
167 abort | "failed to create leaf epoll [" | i | "]: " | errno | strerror(errno);
168 }
169
170 (pollers[i]){ poller_fds[i] };
171 }
172
173 pthread_attr_t attr;
[95dab9e]174 if (int ret = __cfaabi_pthread_attr_init(&attr); 0 != ret) {
[4500e52]175 abort | "failed to create master epoll thread attr: " | ret | strerror(ret);
176 }
177
[95dab9e]178 if (int ret = __cfaabi_pthread_create(&master_poller, &attr, master_epoll, 0p); 0 != ret) {
[4500e52]179 abort | "failed to create master epoll thread: " | ret | strerror(ret);
180 }
181 }
182
183 static inline int epoll_wait(int fd, uint32_t event) {
184 if(fd_map[fd] >= 1p) {
185 fd_map[fd]->rearms++;
186 epoll_rearm(poller_fds[fd_map[fd]->pollid], fd, event);
187 return 0;
188 }
189
190 for() {
191 fd_info_t * expected = 0p;
192 fd_info_t * sentinel = 1p;
193 if(__atomic_compare_exchange_n( &(fd_map[fd]), &expected, sentinel, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED)) {
194 struct epoll_event eevent;
195 eevent.events = event | EPOLLET | EPOLLONESHOT;
196 eevent.data.u64 = (uint64_t)active_thread();
197
[75965a6]198 int id = prng() % poller_cnt;
[4500e52]199 if(0 != epoll_ctl(poller_fds[id], EPOLL_CTL_ADD, fd, &eevent))
200 {
201 abort | "epoll ctl add" | poller_fds[id] | fd | fd_map[fd] | expected | "error: " | errno | strerror(errno);
202 }
203
204 fd_info_t * ninfo = alloc();
205 ninfo->pollid = id;
206 ninfo->rearms = 0;
207 __atomic_store_n( &fd_map[fd], ninfo, __ATOMIC_SEQ_CST);
208
209 park();
210 return 0;
211 }
212
213 if(expected >= 0) {
214 fd_map[fd]->rearms++;
215 epoll_rearm(poller_fds[fd_map[fd]->pollid], fd, event);
216 return 0;
217 }
218
219 Pause();
220 }
221 }
222#endif
[c457dc41]223
[a5e7233]224//================================================================================
[4500e52]225// Thread run by the C Interface
[a5e7233]226
[a1538cd]227struct cfathread_object {
[e84ab3d]228 thread$ self;
[a1538cd]229 void * (*themain)( void * );
230 void * arg;
231 void * ret;
[bb662027]232};
[a1538cd]233void main(cfathread_object & this);
234void ^?{}(cfathread_object & mutex this);
235
[e84ab3d]236static inline thread$ * get_thread( cfathread_object & this ) { return &this.self; }
[a1538cd]237
238typedef ThreadCancelled(cfathread_object) cfathread_exception;
[c715e5f]239typedef vtable(ThreadCancelled(cfathread_object)) cfathread_vtable;
[a1538cd]240
241void defaultResumptionHandler(ThreadCancelled(cfathread_object) & except) {
242 abort | "A thread was cancelled";
243}
[bb662027]244
[a1538cd]245cfathread_vtable _cfathread_vtable_instance;
246
[8edbe40]247cfathread_vtable & const _default_vtable = _cfathread_vtable_instance;
248
[a1538cd]249cfathread_vtable const & get_exception_vtable(cfathread_exception *) {
250 return _cfathread_vtable_instance;
251}
252
253static void ?{}( cfathread_object & this, cluster & cl, void *(*themain)( void * ), void * arg ) {
[bb662027]254 this.themain = themain;
[a1538cd]255 this.arg = arg;
[4150779]256 (this.self){"C-thread", cl};
[a1538cd]257 __thrd_start(this, main);
[bb662027]258}
259
[a1538cd]260void ^?{}(cfathread_object & mutex this) {
261 ^(this.self){};
262}
263
264void main( cfathread_object & this ) {
265 __attribute__((unused)) void * const thrd_obj = (void*)&this;
266 __attribute__((unused)) void * const thrd_hdl = (void*)active_thread();
267 /* paranoid */ verify( thrd_obj == thrd_hdl );
268
269 this.ret = this.themain( this.arg );
[bb662027]270}
271
[a5e7233]272//================================================================================
273// Special Init Thread responsible for the initialization or processors
274struct __cfainit {
[e84ab3d]275 thread$ self;
[a5e7233]276 void (*init)( void * );
277 void * arg;
278};
279void main(__cfainit & this);
280void ^?{}(__cfainit & mutex this);
281
[e84ab3d]282static inline thread$ * get_thread( __cfainit & this ) { return &this.self; }
[a5e7233]283
284typedef ThreadCancelled(__cfainit) __cfainit_exception;
[c715e5f]285typedef vtable(ThreadCancelled(__cfainit)) __cfainit_vtable;
[a5e7233]286
287void defaultResumptionHandler(ThreadCancelled(__cfainit) & except) {
288 abort | "The init thread was cancelled";
289}
290
291__cfainit_vtable ___cfainit_vtable_instance;
292
293__cfainit_vtable const & get_exception_vtable(__cfainit_exception *) {
294 return ___cfainit_vtable_instance;
295}
296
297static void ?{}( __cfainit & this, void (*init)( void * ), void * arg ) {
298 this.init = init;
299 this.arg = arg;
[4150779]300 (this.self){"Processir Init"};
[a5e7233]301
302 // Don't use __thrd_start! just prep the context manually
[e84ab3d]303 thread$ * this_thrd = get_thread(this);
[a5e7233]304 void (*main_p)(__cfainit &) = main;
305
306 disable_interrupts();
307 __cfactx_start(main_p, get_coroutine(this), this, __cfactx_invoke_thread);
308
309 this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP];
310 /* paranoid */ verify( this_thrd->context.SP );
311
312 this_thrd->state = Ready;
[a3821fa]313 enable_interrupts();
[a5e7233]314}
315
316void ^?{}(__cfainit & mutex this) {
317 ^(this.self){};
318}
319
320void main( __cfainit & this ) {
321 __attribute__((unused)) void * const thrd_obj = (void*)&this;
322 __attribute__((unused)) void * const thrd_hdl = (void*)active_thread();
323 /* paranoid */ verify( thrd_obj == thrd_hdl );
324
325 this.init( this.arg );
326}
[bb662027]327
[c18bf9e]328#pragma GCC visibility push(default)
329
[a5e7233]330//================================================================================
331// Main Api
[bb662027]332extern "C" {
[c18bf9e]333 int cfathread_cluster_create(cfathread_cluster_t * cl) __attribute__((nonnull(1))) libcfa_public {
[a1538cd]334 *cl = new();
335 return 0;
336 }
337
[c18bf9e]338 cfathread_cluster_t cfathread_cluster_self(void) libcfa_public {
[a1538cd]339 return active_cluster();
340 }
341
[c18bf9e]342 int cfathread_cluster_print_stats( cfathread_cluster_t cl ) libcfa_public {
[d971c8d]343 #if !defined(__CFA_NO_STATISTICS__)
344 print_stats_at_exit( *cl, CFA_STATS_READY_Q | CFA_STATS_IO );
345 print_stats_now( *cl, CFA_STATS_READY_Q | CFA_STATS_IO );
346 #endif
347 return 0;
348 }
349
[a1538cd]350 int cfathread_cluster_add_worker(cfathread_cluster_t cl, pthread_t* tid, void (*init_routine) (void *), void * arg) {
[a5e7233]351 __cfainit * it = 0p;
352 if(init_routine) {
353 it = alloc();
354 (*it){init_routine, arg};
355 }
[a1538cd]356 processor * proc = alloc();
[a5e7233]357 (*proc){ "C-processor", *cl, get_thread(*it) };
358
359 // Wait for the init thread to return before continuing
360 if(it) {
361 ^(*it){};
362 free(it);
363 }
364
[a1538cd]365 if(tid) *tid = proc->kernel_thread;
366 return 0;
367 }
368
369 int cfathread_cluster_pause (cfathread_cluster_t) {
370 abort | "Pausing clusters is not supported";
371 exit(1);
372 }
373
374 int cfathread_cluster_resume(cfathread_cluster_t) {
375 abort | "Resuming clusters is not supported";
376 exit(1);
377 }
378
[bb662027]379 //--------------------
[a1538cd]380 // Thread attributes
381 int cfathread_attr_init(cfathread_attr_t *attr) __attribute__((nonnull (1))) {
382 attr->cl = active_cluster();
383 return 0;
[bb662027]384 }
385
[a1538cd]386 //--------------------
387 // Thread
[9e27f69]388 int cfathread_create( cfathread_t * handle, const cfathread_attr_t * attr, void *(*main)( void * ), void * arg ) __attribute__((nonnull (1))) {
[a1538cd]389 cluster * cl = attr ? attr->cl : active_cluster();
390 cfathread_t thrd = alloc();
391 (*thrd){ *cl, main, arg };
392 *handle = thrd;
393 return 0;
394 }
395
396 int cfathread_join( cfathread_t thrd, void ** retval ) {
397 void * ret = join( *thrd ).ret;
[f03e11d]398 ^( *thrd ){};
399 free(thrd);
[a1538cd]400 if(retval) {
401 *retval = ret;
402 }
403 return 0;
404 }
405
[9e27f69]406 int cfathread_get_errno(void) {
407 return errno;
408 }
409
[a1538cd]410 cfathread_t cfathread_self(void) {
411 return (cfathread_t)active_thread();
412 }
413
414 int cfathread_usleep(useconds_t usecs) {
415 sleep(usecs`us);
416 return 0;
417 }
418
419 int cfathread_sleep(unsigned int secs) {
420 sleep(secs`s);
421 return 0;
[bb662027]422 }
423
424 void cfathread_park( void ) {
[e235429]425 park();
[bb662027]426 }
427
[a1538cd]428 void cfathread_unpark( cfathread_t thrd ) {
[e235429]429 unpark( *thrd );
[bb662027]430 }
431
432 void cfathread_yield( void ) {
433 yield();
434 }
435
[a1538cd]436 typedef struct cfathread_mutex * cfathread_mutex_t;
437
438 //--------------------
439 // Mutex
440 struct cfathread_mutex {
[420b498]441 linear_backoff_then_block_lock impl;
[a1538cd]442 };
443 int cfathread_mutex_init(cfathread_mutex_t *restrict mut, const cfathread_mutexattr_t *restrict) __attribute__((nonnull (1))) { *mut = new(); return 0; }
444 int cfathread_mutex_destroy(cfathread_mutex_t *mut) __attribute__((nonnull (1))) { delete( *mut ); return 0; }
[d27b6be]445 int cfathread_mutex_lock (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { lock( (*mut)->impl ); return 0; }
446 int cfathread_mutex_unlock (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { unlock( (*mut)->impl ); return 0; }
447 int cfathread_mutex_trylock(cfathread_mutex_t *mut) __attribute__((nonnull (1))) {
448 bool ret = try_lock( (*mut)->impl );
449 if( ret ) return 0;
450 else return EBUSY;
451 }
[a1538cd]452
453 //--------------------
454 // Condition
455 struct cfathread_condition {
[420b498]456 condition_variable(linear_backoff_then_block_lock) impl;
[a1538cd]457 };
458 int cfathread_cond_init(cfathread_cond_t *restrict cond, const cfathread_condattr_t *restrict) __attribute__((nonnull (1))) { *cond = new(); return 0; }
459 int cfathread_cond_signal(cfathread_cond_t *cond) __attribute__((nonnull (1))) { notify_one( (*cond)->impl ); return 0; }
460 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; }
461 int cfathread_cond_timedwait(cfathread_cond_t *restrict cond, cfathread_mutex_t *restrict mut, const struct timespec *restrict abstime) __attribute__((nonnull (1,2,3))) {
462 Time t = { *abstime };
[c457dc41]463 timespec curr;
464 clock_gettime( CLOCK_REALTIME, &curr );
465 Time c = { curr };
466 if( wait( (*cond)->impl, (*mut)->impl, t - c ) ) {
[a1538cd]467 return 0;
468 }
469 errno = ETIMEDOUT;
470 return ETIMEDOUT;
471 }
472}
473
474#include <iofwd.hfa>
475
476extern "C" {
477 #include <unistd.h>
478 #include <sys/types.h>
479 #include <sys/socket.h>
480
[bb662027]481 //--------------------
[a1538cd]482 // IO operations
483 int cfathread_socket(int domain, int type, int protocol) {
[4500e52]484 return socket(domain, type
485 #if defined(EPOLL_FOR_SOCKETS)
486 | SOCK_NONBLOCK
487 #endif
488 , protocol);
[a1538cd]489 }
490 int cfathread_bind(int socket, const struct sockaddr *address, socklen_t address_len) {
491 return bind(socket, address, address_len);
492 }
493
494 int cfathread_listen(int socket, int backlog) {
495 return listen(socket, backlog);
496 }
497
498 int cfathread_accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len) {
[4500e52]499 #if defined(EPOLL_FOR_SOCKETS)
500 int ret;
501 for() {
502 yield();
503 ret = accept4(socket, address, address_len, SOCK_NONBLOCK);
504 if(ret >= 0) break;
505 if(errno != EAGAIN && errno != EWOULDBLOCK) break;
506
507 epoll_wait(socket, EPOLLIN);
508 }
509 return ret;
510 #else
511 return cfa_accept4(socket, address, address_len, 0, CFA_IO_LAZY);
512 #endif
[a1538cd]513 }
514
515 int cfathread_connect(int socket, const struct sockaddr *address, socklen_t address_len) {
[4500e52]516 #if defined(EPOLL_FOR_SOCKETS)
517 int ret;
518 for() {
519 ret = connect(socket, address, address_len);
520 if(ret >= 0) break;
521 if(errno != EAGAIN && errno != EWOULDBLOCK) break;
522
523 epoll_wait(socket, EPOLLIN);
524 }
525 return ret;
526 #else
527 return cfa_connect(socket, address, address_len, CFA_IO_LAZY);
528 #endif
[a1538cd]529 }
530
531 int cfathread_dup(int fildes) {
532 return dup(fildes);
533 }
[bb662027]534
[a1538cd]535 int cfathread_close(int fildes) {
536 return cfa_close(fildes, CFA_IO_LAZY);
[bb662027]537 }
[a1538cd]538
539 ssize_t cfathread_sendmsg(int socket, const struct msghdr *message, int flags) {
[4500e52]540 #if defined(EPOLL_FOR_SOCKETS)
541 ssize_t ret;
542 __STATS__( false, io.ops.sockwrite++; )
543 for() {
544 ret = sendmsg(socket, message, flags);
545 if(ret >= 0) break;
546 if(errno != EAGAIN && errno != EWOULDBLOCK) break;
547
548 __STATS__( false, io.ops.epllwrite++; )
549 epoll_wait(socket, EPOLLOUT);
550 }
551 #else
552 ssize_t ret = cfa_sendmsg(socket, message, flags, CFA_IO_LAZY);
553 #endif
554 return ret;
[a1538cd]555 }
556
557 ssize_t cfathread_write(int fildes, const void *buf, size_t nbyte) {
[86dc95d]558 // Use send rather then write for socket since it's faster
[4500e52]559 #if defined(EPOLL_FOR_SOCKETS)
560 ssize_t ret;
561 // __STATS__( false, io.ops.sockwrite++; )
562 for() {
563 ret = send(fildes, buf, nbyte, 0);
564 if(ret >= 0) break;
565 if(errno != EAGAIN && errno != EWOULDBLOCK) break;
566
567 // __STATS__( false, io.ops.epllwrite++; )
568 epoll_wait(fildes, EPOLLOUT);
569 }
570 #else
571 ssize_t ret = cfa_send(fildes, buf, nbyte, 0, CFA_IO_LAZY);
572 #endif
573 return ret;
[a1538cd]574 }
575
576 ssize_t cfathread_recvfrom(int socket, void *restrict buffer, size_t length, int flags, struct sockaddr *restrict address, socklen_t *restrict address_len) {
577 struct iovec iov;
578 iov.iov_base = buffer;
579 iov.iov_len = length;
580
581 struct msghdr msg;
582 msg.msg_name = address;
583 msg.msg_namelen = address_len ? (socklen_t)*address_len : (socklen_t)0;
584 msg.msg_iov = &iov;
585 msg.msg_iovlen = 1;
586 msg.msg_control = 0p;
587 msg.msg_controllen = 0;
588
[4500e52]589 #if defined(EPOLL_FOR_SOCKETS)
590 ssize_t ret;
591 yield();
592 for() {
593 ret = recvmsg(socket, &msg, flags);
594 if(ret >= 0) break;
595 if(errno != EAGAIN && errno != EWOULDBLOCK) break;
596
597 epoll_wait(socket, EPOLLIN);
598 }
599 #else
600 ssize_t ret = cfa_recvmsg(socket, &msg, flags, CFA_IO_LAZY);
601 #endif
[a1538cd]602
603 if(address_len) *address_len = msg.msg_namelen;
604 return ret;
605 }
606
607 ssize_t cfathread_read(int fildes, void *buf, size_t nbyte) {
[86dc95d]608 // Use recv rather then read for socket since it's faster
[4500e52]609 #if defined(EPOLL_FOR_SOCKETS)
610 ssize_t ret;
611 __STATS__( false, io.ops.sockread++; )
612 yield();
613 for() {
614 ret = recv(fildes, buf, nbyte, 0);
615 if(ret >= 0) break;
616 if(errno != EAGAIN && errno != EWOULDBLOCK) break;
617
618 __STATS__( false, io.ops.epllread++; )
619 epoll_wait(fildes, EPOLLIN);
620 }
621 #else
622 ssize_t ret = cfa_recv(fildes, buf, nbyte, 0, CFA_IO_LAZY);
623 #endif
624 return ret;
[a1538cd]625 }
626
[45444c3]627}
Note: See TracBrowser for help on using the repository browser.