| 1 | #ifndef __CIVL_CIVLPTHREAD__
|
|---|
| 2 | #define __CIVL_CIVLPTHREAD__
|
|---|
| 3 |
|
|---|
| 4 | #include <pthread.h>
|
|---|
| 5 | #include <civl-pthread.cvh>
|
|---|
| 6 | #include <civlc.cvh>
|
|---|
| 7 | #include <seq.cvh>
|
|---|
| 8 |
|
|---|
| 9 | /* pthread_t struct definition
|
|---|
| 10 | Description: The pthread_t is a struct containing a $proc variable as well as a thread attribute which defines
|
|---|
| 11 | its interactions with other threads. It encapsulates the $proc and allows attributes to apply to it.
|
|---|
| 12 | Fields: thr: the $proc variable that is the heart of the thread
|
|---|
| 13 | attr: see above
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | struct _pthread_t{
|
|---|
| 17 | $proc thr;
|
|---|
| 18 | const pthread_attr_t attr;
|
|---|
| 19 | _Bool terminated;
|
|---|
| 20 | void** exitValue;
|
|---|
| 21 | };
|
|---|
| 22 |
|
|---|
| 23 | /* pthread_attr_t struct definition
|
|---|
| 24 | Description: This struct corresponds to the pthread_attr_t which is the attribute of a pthread_t. It's fields
|
|---|
| 25 | define the way the pthread_t is able to interact (join/detach), (memory capacity), (scope) etc.
|
|---|
| 26 |
|
|---|
| 27 | Fields:
|
|---|
| 28 | int detachstate: Defines a threads ability to join with two values: PTHREAD_CREATE_DETACHED and PTHREAD_CREATE_JOINABLE
|
|---|
| 29 | int inheritsched: The inheritance scheduling policy of the thread
|
|---|
| 30 | int contentionscope: Defines the contention scope of the thread
|
|---|
| 31 | int schedpolicy: Determines the scheduling policy of the thread
|
|---|
| 32 |
|
|---|
| 33 | */
|
|---|
| 34 | struct _pthread_attr_t{
|
|---|
| 35 | int detachstate;
|
|---|
| 36 | int inheritsched;
|
|---|
| 37 | int contentionscope;
|
|---|
| 38 | int schedpolicy;
|
|---|
| 39 | //sched_param param;
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | struct _pthread_gpool_t{
|
|---|
| 43 | pthread_t threads[];
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | struct _pthread_pool_t{
|
|---|
| 47 | $pthread_gpool_t gpool;
|
|---|
| 48 | $proc tid;
|
|---|
| 49 | pthread_t * thread;
|
|---|
| 50 | };
|
|---|
| 51 |
|
|---|
| 52 | $atomic_f $pthread_gpool_t $pthread_gpool_create($scope scope){
|
|---|
| 53 | $pthread_gpool_t gpool=($pthread_gpool_t)$malloc(scope, sizeof(struct _pthread_gpool_t));
|
|---|
| 54 |
|
|---|
| 55 | $seq_init(&gpool->threads, 0, NULL);
|
|---|
| 56 | return gpool;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | $atomic_f void $pthread_gpool_destroy($pthread_gpool_t gpool){
|
|---|
| 60 | $free(gpool);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | $atomic_f $pthread_pool_t $pthread_pool_create_main($scope scope, $pthread_gpool_t gpool){
|
|---|
| 64 | pthread_t thread;
|
|---|
| 65 |
|
|---|
| 66 | thread.thr = $self;
|
|---|
| 67 | thread.attr.detachstate = PTHREAD_CREATE_JOINABLE;
|
|---|
| 68 | thread.attr.inheritsched = 0;
|
|---|
| 69 | thread.attr.contentionscope = 0;
|
|---|
| 70 | thread.attr.schedpolicy = 0;
|
|---|
| 71 | thread.terminated = $false;
|
|---|
| 72 | thread.exitValue = NULL;
|
|---|
| 73 | $pthread_gpool_add(gpool, &thread);
|
|---|
| 74 | return $pthread_pool_create(scope, gpool);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | $atomic_f $pthread_pool_t $pthread_pool_create($scope scope, $pthread_gpool_t gpool){
|
|---|
| 78 | $pthread_pool_t pool=($pthread_pool_t)$malloc(scope, sizeof(struct _pthread_pool_t));
|
|---|
| 79 |
|
|---|
| 80 | pool->gpool=gpool;
|
|---|
| 81 | pool->tid=$self;
|
|---|
| 82 |
|
|---|
| 83 | int nthreads=$seq_length(&gpool->threads);
|
|---|
| 84 |
|
|---|
| 85 | for(int i=0; i<nthreads; i++){
|
|---|
| 86 | pthread_t thread=gpool->threads[i];
|
|---|
| 87 |
|
|---|
| 88 | if(!thread.terminated && thread.thr==$self){
|
|---|
| 89 | pool->thread=&(gpool->threads[i]);
|
|---|
| 90 | break;
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | return pool;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | $atomic_f void $pthread_pool_destroy($pthread_pool_t pool){
|
|---|
| 97 | $free(pool);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | $atomic_f void $pthread_pool_terminates($pthread_pool_t pool, void* value){
|
|---|
| 101 | $pthread_gpool_t gpool=pool->gpool;
|
|---|
| 102 | int nthreads=$seq_length(&gpool->threads);
|
|---|
| 103 | int i;
|
|---|
| 104 |
|
|---|
| 105 | for(i=0; i<nthreads; i++){
|
|---|
| 106 | pthread_t *thread=&(gpool->threads[i]);
|
|---|
| 107 |
|
|---|
| 108 | if(!thread->terminated && thread->thr==pool->tid){
|
|---|
| 109 | thread->terminated = $true;
|
|---|
| 110 | break;
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | /*if(i<nthreads)
|
|---|
| 114 | $seq_remove(&gpool->threads, i, NULL, 1);*/
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | $atomic_f _Bool $pthread_pool_is_terminated($pthread_pool_t pool, $proc pid){
|
|---|
| 118 | if(pid==$proc_null)
|
|---|
| 119 | return $false;
|
|---|
| 120 |
|
|---|
| 121 | $pthread_gpool_t gpool=pool->gpool;
|
|---|
| 122 | int nthreads=$seq_length(&gpool->threads);
|
|---|
| 123 | int i;
|
|---|
| 124 |
|
|---|
| 125 | for(i=0; i<nthreads; i++){
|
|---|
| 126 | pthread_t thread=gpool->threads[i];
|
|---|
| 127 |
|
|---|
| 128 | if(!thread.terminated && thread.thr==pid){
|
|---|
| 129 | return thread.terminated;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | return $true;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 | $atomic_f void $pthread_exit(void *value_ptr, $pthread_pool_t pool){
|
|---|
| 138 | $pthread_pool_terminates(pool, value_ptr);
|
|---|
| 139 | $free(pool);
|
|---|
| 140 | $exit();
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | #endif
|
|---|