source: doc/theses/thierry_delisle_PhD/code/readQ_example/thrdlib/thread_pthread.h @ d035cf7

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since d035cf7 was d035cf7, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Added first attempt at libfibre implementation

  • Property mode set to 100644
File size: 2.2 KB
Line 
1#pragma once
2
3#include <pthread.h>
4#include <errno.h>
5#include <cstring>
6#include <cstdio>
7#include <iostream>
8
9#define CHECKED(x) { int err = x; if( err != 0 ) { std::cerr << "KERNEL ERROR: Operation \"" #x "\" return error " << err << " - " << strerror(err) << std::endl; std::abort(); } }
10
11struct __bin_sem_t {
12        pthread_mutex_t         lock;
13        pthread_cond_t          cond;
14        int                     val;
15
16        __bin_sem_t() {
17                // Create the mutex with error checking
18                pthread_mutexattr_t mattr;
19                pthread_mutexattr_init( &mattr );
20                pthread_mutexattr_settype( &mattr, PTHREAD_MUTEX_ERRORCHECK_NP);
21                pthread_mutex_init(&lock, &mattr);
22
23                pthread_cond_init (&cond, nullptr);
24                val = 0;
25        }
26
27        ~__bin_sem_t() {
28                CHECKED( pthread_mutex_destroy(&lock) );
29                CHECKED( pthread_cond_destroy (&cond) );
30        }
31
32        void wait() {
33                CHECKED( pthread_mutex_lock(&lock) );
34                        while(val < 1) {
35                                pthread_cond_wait(&cond, &lock);
36                        }
37                        val -= 1;
38                CHECKED( pthread_mutex_unlock(&lock) );
39        }
40
41        bool post() {
42                bool needs_signal = false;
43
44                CHECKED( pthread_mutex_lock(&lock) );
45                        if(val < 1) {
46                                val += 1;
47                                pthread_cond_signal(&cond);
48                                needs_signal = true;
49                        }
50                CHECKED( pthread_mutex_unlock(&lock) );
51
52                return needs_signal;
53        }
54};
55
56#undef CHECKED
57
58#if defined(__cforall) || defined(__cpluplus)
59extern "C" {
60#endif
61        //--------------------
62        // Basic types
63        struct pthread_runner_t {
64                pthread_t handle;
65                __bin_sem_t sem;
66        };
67        typedef pthread_runner_t * thread_t;
68
69        //--------------------
70        // Basic thread support
71        thread_t thrdlib_create( void (*main)( thread_t ) ) {
72                thread_t thrd = new pthread_runner_t();
73                int r = pthread_create( &thrd->handle, nullptr, (void *(*)(void *))main, thrd );
74                if( r != 0 ) std::abort();
75                return thrd;
76        }
77
78        void thrdlib_join( thread_t handle ) {
79                void * ret;
80                int r = pthread_join( handle->handle, &ret );
81                if( r != 0 ) std::abort();
82                delete handle;
83        }
84
85        void thrdlib_park( thread_t handle ) {
86                handle->sem.wait();
87        }
88
89        void thrdlib_unpark( thread_t handle ) {
90                handle->sem.post();
91        }
92
93        void thrdlib_yield( void ) {
94                int r = pthread_yield();
95                if( r != 0 ) std::abort();
96        }
97
98        //--------------------
99        // Basic kernel features
100        void thrdlib_init( int ) {}
101        void thrdlib_clean( void ) {}
102
103
104#if defined(__cforall) || defined(__cpluplus)
105}
106#endif
Note: See TracBrowser for help on using the repository browser.