runtime_x64_sel4_rumprun_sysv.c 2.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright 2017 The Australian National University
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifdef __linux__
// RTLD_DEFAULT is not defined in POSIX. Linux gcc does not define it unless
// _GNU_SOURCE is also defined.
#define _GNU_SOURCE
#endif // __linux__

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
25
#include <assert.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

int tls_initialized = 0;

static pthread_key_t mu_tls;

void set_thread_local(void* thread) {
    if(tls_initialized == 0){
        tls_initialized = 1;
        int result = pthread_key_create(&mu_tls, NULL);
        if(result != 0){
            printf("Set_Thread_Local(): PThread key create failed with error code = %d\n", result);
            assert(0);
        }
    }
    int result = pthread_setspecific(mu_tls, thread);
    if(result != 0){
        printf("Set_Thread_Local(): PThread set specific failed with error code = %d\n", result);
        assert(0);
    }
}

void* muentry_get_thread_local() {
    if(tls_initialized == 0){
        printf("Get_Thread_Local(): PThread key MUST be initialized before first use!!\n");
        assert(0);
    }
    void * result = pthread_getspecific(mu_tls);
    if(result == NULL){
        printf("Get_Thread_Local(): NO pthread key found for current thread!!\n");
        assert(0);
    }

    return result;
}

int32_t mu_retval;

void muentry_set_retval(int32_t x) {
    mu_retval = x;
}

67 68 69 70
int32_t muentry_get_retval() {
    return mu_retval;
}

71 72
int32_t c_check_result() {
    return mu_retval;
73 74 75 76 77
}

char * alloc_mem(size_t size){
    return (char *) malloc(size);
}