source: doc/theses/thierry_delisle_PhD/code/readQ_example/thrdlib/pthread.hpp @ 20be782

ADTast-experimentalpthread-emulation
Last change on this file since 20be782 was 72b1800, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Some previous in progress work.
Commited only so it stops showing in my git status.

  • 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//--------------------
59// Basic types
60struct pthread_runner_t {
61        pthread_t handle;
62        __bin_sem_t sem;
63};
64typedef pthread_runner_t * thread_t;
65
66static_assert(sizeof(thread_t) == sizeof(void*), "thread_t musst be of same size as void*");
67
68extern "C" {
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}
Note: See TracBrowser for help on using the repository browser.