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