[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" |
---|
| 19 | #include "thread.hfa" |
---|
[a1538cd] | 20 | #include "time.hfa" |
---|
[bb662027] | 21 | |
---|
[a1538cd] | 22 | #include "cfathread.h" |
---|
| 23 | |
---|
| 24 | struct cfathread_object { |
---|
| 25 | $thread self; |
---|
| 26 | void * (*themain)( void * ); |
---|
| 27 | void * arg; |
---|
| 28 | void * ret; |
---|
[bb662027] | 29 | }; |
---|
[a1538cd] | 30 | void main(cfathread_object & this); |
---|
| 31 | void ^?{}(cfathread_object & mutex this); |
---|
| 32 | |
---|
| 33 | static inline $thread * get_thread( cfathread_object & this ) { return &this.self; } |
---|
| 34 | |
---|
| 35 | typedef ThreadCancelled(cfathread_object) cfathread_exception; |
---|
| 36 | typedef ThreadCancelled_vtable(cfathread_object) cfathread_vtable; |
---|
| 37 | |
---|
| 38 | void defaultResumptionHandler(ThreadCancelled(cfathread_object) & except) { |
---|
| 39 | abort | "A thread was cancelled"; |
---|
| 40 | } |
---|
[bb662027] | 41 | |
---|
[a1538cd] | 42 | cfathread_vtable _cfathread_vtable_instance; |
---|
| 43 | |
---|
| 44 | cfathread_vtable const & get_exception_vtable(cfathread_exception *) { |
---|
| 45 | return _cfathread_vtable_instance; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | static void ?{}( cfathread_object & this, cluster & cl, void *(*themain)( void * ), void * arg ) { |
---|
[bb662027] | 49 | this.themain = themain; |
---|
[a1538cd] | 50 | this.arg = arg; |
---|
| 51 | ((thread&)this){"C-thread", cl}; |
---|
| 52 | __thrd_start(this, main); |
---|
[bb662027] | 53 | } |
---|
| 54 | |
---|
[a1538cd] | 55 | void ^?{}(cfathread_object & mutex this) { |
---|
| 56 | ^(this.self){}; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | void main( cfathread_object & this ) { |
---|
| 60 | __attribute__((unused)) void * const thrd_obj = (void*)&this; |
---|
| 61 | __attribute__((unused)) void * const thrd_hdl = (void*)active_thread(); |
---|
| 62 | /* paranoid */ verify( thrd_obj == thrd_hdl ); |
---|
| 63 | |
---|
| 64 | this.ret = this.themain( this.arg ); |
---|
[bb662027] | 65 | } |
---|
| 66 | |
---|
| 67 | processor * procs = 0p; |
---|
| 68 | int proc_cnt = 1; |
---|
| 69 | |
---|
| 70 | extern "C" { |
---|
[a1538cd] | 71 | int cfathread_cluster_create(cfathread_cluster_t * cl) __attribute__((nonnull(1))) { |
---|
| 72 | *cl = new(); |
---|
| 73 | return 0; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | cfathread_cluster_t cfathread_cluster_self(void) { |
---|
| 77 | return active_cluster(); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | int cfathread_cluster_add_worker(cfathread_cluster_t cl, pthread_t* tid, void (*init_routine) (void *), void * arg) { |
---|
| 81 | // processor * proc = new("C-processor", *cl, init_routine, arg); |
---|
| 82 | processor * proc = alloc(); |
---|
| 83 | (*proc){ "C-processor", *cl, init_routine, arg }; |
---|
| 84 | if(tid) *tid = proc->kernel_thread; |
---|
| 85 | return 0; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | int cfathread_cluster_pause (cfathread_cluster_t) { |
---|
| 89 | abort | "Pausing clusters is not supported"; |
---|
| 90 | exit(1); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | int cfathread_cluster_resume(cfathread_cluster_t) { |
---|
| 94 | abort | "Resuming clusters is not supported"; |
---|
| 95 | exit(1); |
---|
| 96 | } |
---|
| 97 | |
---|
[bb662027] | 98 | //-------------------- |
---|
[a1538cd] | 99 | // Thread attributes |
---|
| 100 | int cfathread_attr_init(cfathread_attr_t *attr) __attribute__((nonnull (1))) { |
---|
| 101 | attr->cl = active_cluster(); |
---|
| 102 | return 0; |
---|
[bb662027] | 103 | } |
---|
| 104 | |
---|
[a1538cd] | 105 | //-------------------- |
---|
| 106 | // Thread |
---|
[9e27f69] | 107 | int cfathread_create( cfathread_t * handle, const cfathread_attr_t * attr, void *(*main)( void * ), void * arg ) __attribute__((nonnull (1))) { |
---|
[a1538cd] | 108 | cluster * cl = attr ? attr->cl : active_cluster(); |
---|
| 109 | cfathread_t thrd = alloc(); |
---|
| 110 | (*thrd){ *cl, main, arg }; |
---|
| 111 | *handle = thrd; |
---|
| 112 | return 0; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | int cfathread_join( cfathread_t thrd, void ** retval ) { |
---|
| 116 | void * ret = join( *thrd ).ret; |
---|
[bb662027] | 117 | delete( thrd ); |
---|
[a1538cd] | 118 | if(retval) { |
---|
| 119 | *retval = ret; |
---|
| 120 | } |
---|
| 121 | return 0; |
---|
| 122 | } |
---|
| 123 | |
---|
[9e27f69] | 124 | int cfathread_get_errno(void) { |
---|
| 125 | return errno; |
---|
| 126 | } |
---|
| 127 | |
---|
[a1538cd] | 128 | cfathread_t cfathread_self(void) { |
---|
| 129 | return (cfathread_t)active_thread(); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | int cfathread_usleep(useconds_t usecs) { |
---|
| 133 | sleep(usecs`us); |
---|
| 134 | return 0; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | int cfathread_sleep(unsigned int secs) { |
---|
| 138 | sleep(secs`s); |
---|
| 139 | return 0; |
---|
[bb662027] | 140 | } |
---|
| 141 | |
---|
| 142 | void cfathread_park( void ) { |
---|
[e235429] | 143 | park(); |
---|
[bb662027] | 144 | } |
---|
| 145 | |
---|
[a1538cd] | 146 | void cfathread_unpark( cfathread_t thrd ) { |
---|
[e235429] | 147 | unpark( *thrd ); |
---|
[bb662027] | 148 | } |
---|
| 149 | |
---|
| 150 | void cfathread_yield( void ) { |
---|
| 151 | yield(); |
---|
| 152 | } |
---|
| 153 | |
---|
[a1538cd] | 154 | typedef struct cfathread_mutex * cfathread_mutex_t; |
---|
| 155 | |
---|
| 156 | //-------------------- |
---|
| 157 | // Mutex |
---|
| 158 | struct cfathread_mutex { |
---|
| 159 | single_acquisition_lock impl; |
---|
| 160 | }; |
---|
| 161 | int cfathread_mutex_init(cfathread_mutex_t *restrict mut, const cfathread_mutexattr_t *restrict) __attribute__((nonnull (1))) { *mut = new(); return 0; } |
---|
| 162 | int cfathread_mutex_destroy(cfathread_mutex_t *mut) __attribute__((nonnull (1))) { delete( *mut ); return 0; } |
---|
| 163 | int cfathread_mutex_lock (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { lock ( (*mut)->impl ); return 0; } |
---|
| 164 | int cfathread_mutex_trylock(cfathread_mutex_t *mut) __attribute__((nonnull (1))) { try_lock( (*mut)->impl ); return 0; } |
---|
| 165 | int cfathread_mutex_unlock (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { unlock ( (*mut)->impl ); return 0; } |
---|
| 166 | |
---|
| 167 | //-------------------- |
---|
| 168 | // Condition |
---|
| 169 | struct cfathread_condition { |
---|
| 170 | condition_variable(single_acquisition_lock) impl; |
---|
| 171 | }; |
---|
| 172 | int cfathread_cond_init(cfathread_cond_t *restrict cond, const cfathread_condattr_t *restrict) __attribute__((nonnull (1))) { *cond = new(); return 0; } |
---|
| 173 | int cfathread_cond_signal(cfathread_cond_t *cond) __attribute__((nonnull (1))) { notify_one( (*cond)->impl ); return 0; } |
---|
| 174 | 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; } |
---|
| 175 | int cfathread_cond_timedwait(cfathread_cond_t *restrict cond, cfathread_mutex_t *restrict mut, const struct timespec *restrict abstime) __attribute__((nonnull (1,2,3))) { |
---|
| 176 | Time t = { *abstime }; |
---|
| 177 | if( wait( (*cond)->impl, (*mut)->impl, t ) ) { |
---|
| 178 | return 0; |
---|
| 179 | } |
---|
| 180 | errno = ETIMEDOUT; |
---|
| 181 | return ETIMEDOUT; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | #include <iofwd.hfa> |
---|
| 186 | |
---|
| 187 | extern "C" { |
---|
| 188 | #include <unistd.h> |
---|
| 189 | #include <sys/types.h> |
---|
| 190 | #include <sys/socket.h> |
---|
| 191 | |
---|
[bb662027] | 192 | //-------------------- |
---|
[a1538cd] | 193 | // IO operations |
---|
| 194 | int cfathread_socket(int domain, int type, int protocol) { |
---|
| 195 | return socket(domain, type, protocol); |
---|
| 196 | } |
---|
| 197 | int cfathread_bind(int socket, const struct sockaddr *address, socklen_t address_len) { |
---|
| 198 | return bind(socket, address, address_len); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | int cfathread_listen(int socket, int backlog) { |
---|
| 202 | return listen(socket, backlog); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | int cfathread_accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len) { |
---|
| 206 | return cfa_accept4(socket, address, address_len, 0, CFA_IO_LAZY); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | int cfathread_connect(int socket, const struct sockaddr *address, socklen_t address_len) { |
---|
| 210 | return cfa_connect(socket, address, address_len, CFA_IO_LAZY); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | int cfathread_dup(int fildes) { |
---|
| 214 | return dup(fildes); |
---|
| 215 | } |
---|
[bb662027] | 216 | |
---|
[a1538cd] | 217 | int cfathread_close(int fildes) { |
---|
| 218 | return cfa_close(fildes, CFA_IO_LAZY); |
---|
[bb662027] | 219 | } |
---|
[a1538cd] | 220 | |
---|
| 221 | ssize_t cfathread_sendmsg(int socket, const struct msghdr *message, int flags) { |
---|
| 222 | return cfa_sendmsg(socket, message, flags, CFA_IO_LAZY); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | ssize_t cfathread_write(int fildes, const void *buf, size_t nbyte) { |
---|
| 226 | return cfa_write(fildes, buf, nbyte, CFA_IO_LAZY); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | ssize_t cfathread_recvfrom(int socket, void *restrict buffer, size_t length, int flags, struct sockaddr *restrict address, socklen_t *restrict address_len) { |
---|
| 230 | struct iovec iov; |
---|
| 231 | iov.iov_base = buffer; |
---|
| 232 | iov.iov_len = length; |
---|
| 233 | |
---|
| 234 | struct msghdr msg; |
---|
| 235 | msg.msg_name = address; |
---|
| 236 | msg.msg_namelen = address_len ? (socklen_t)*address_len : (socklen_t)0; |
---|
| 237 | msg.msg_iov = &iov; |
---|
| 238 | msg.msg_iovlen = 1; |
---|
| 239 | msg.msg_control = 0p; |
---|
| 240 | msg.msg_controllen = 0; |
---|
| 241 | |
---|
| 242 | ssize_t ret = cfa_recvmsg(socket, &msg, flags, CFA_IO_LAZY); |
---|
| 243 | |
---|
| 244 | if(address_len) *address_len = msg.msg_namelen; |
---|
| 245 | return ret; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | ssize_t cfathread_read(int fildes, void *buf, size_t nbyte) { |
---|
| 249 | return cfa_read(fildes, buf, nbyte, CFA_IO_LAZY); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | void cfathread_suspendFD(int) { |
---|
| 253 | abort | "Suspending File Descriptors is not supported"; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | void cfathread_resumeFD (int) { |
---|
| 257 | abort | "Resuming File Descriptors is not supported"; |
---|
| 258 | } |
---|
| 259 | |
---|
[45444c3] | 260 | } |
---|