sharedarrays.c 4.16 KB
Newer Older
Éric Thiébaut's avatar
Éric Thiébaut committed
1
2
3
4
5
6
7
8
/*
 * sharedarrays.c --
 *
 * Implement multi-dimensional arrays whose contents can be shared between
 * processes.
 *
 *-----------------------------------------------------------------------------
 *
9
10
 * This file if part of TAO real-time software licensed under the MIT license
 * (https://git-cral.univ-lyon1.fr/tao/tao-rt).
Éric Thiébaut's avatar
Éric Thiébaut committed
11
 *
12
 * Copyright (C) 2018-2021, Éric Thiébaut.
Éric Thiébaut's avatar
Éric Thiébaut committed
13
14
 */

Éric Thiébaut's avatar
Éric Thiébaut committed
15
#include "common.h"
Éric Thiébaut's avatar
Éric Thiébaut committed
16
#include "tao-private.h"
Éric Thiébaut's avatar
Éric Thiébaut committed
17

18
tao_eltype_t
19
tao_get_shared_array_eltype(const tao_shared_array_t* arr)
Éric Thiébaut's avatar
Éric Thiébaut committed
20
{
Éric Thiébaut's avatar
Éric Thiébaut committed
21
    return (arr != NULL ? arr->eltype : -1);
Éric Thiébaut's avatar
Éric Thiébaut committed
22
23
}

24
long
25
tao_get_shared_array_length(const tao_shared_array_t* arr)
Éric Thiébaut's avatar
Éric Thiébaut committed
26
{
Éric Thiébaut's avatar
Éric Thiébaut committed
27
    return (arr != NULL ? arr->nelem : 0);
Éric Thiébaut's avatar
Éric Thiébaut committed
28
29
30
}

int
31
tao_get_shared_array_ndims(const tao_shared_array_t* arr)
Éric Thiébaut's avatar
Éric Thiébaut committed
32
{
Éric Thiébaut's avatar
Éric Thiébaut committed
33
    return (arr != NULL ? arr->ndims : 0);
Éric Thiébaut's avatar
Éric Thiébaut committed
34
35
}

36
long
Éric Thiébaut's avatar
Éric Thiébaut committed
37
tao_get_shared_array_dim(const tao_shared_array_t* arr, int d)
Éric Thiébaut's avatar
Éric Thiébaut committed
38
{
Éric Thiébaut's avatar
Éric Thiébaut committed
39
    return (arr != NULL ?
40
41
            (TAO_UNLIKELY(d < 1) ? 0 :
             (TAO_UNLIKELY(d > TAO_MAX_NDIMS) ? 1 :
42
              arr->dims[d-1])) : 0);
Éric Thiébaut's avatar
Éric Thiébaut committed
43
44
45
}

void*
46
tao_get_shared_array_data(const tao_shared_array_t* arr)
Éric Thiébaut's avatar
Éric Thiébaut committed
47
{
Éric Thiébaut's avatar
Éric Thiébaut committed
48
    return (arr != NULL ? (void*)((uint8_t*)arr + arr->offset) : (void*)0);
49
50
51
}

int64_t
52
tao_get_shared_array_counter(const tao_shared_array_t* arr)
53
{
Éric Thiébaut's avatar
Éric Thiébaut committed
54
    return (arr != NULL ? arr->counter : -1);
55
56
57
}

void
58
tao_set_shared_array_counter(tao_shared_array_t* arr, int64_t cnt)
59
{
Éric Thiébaut's avatar
Éric Thiébaut committed
60
    if (arr != NULL) {
61
62
        arr->counter = cnt;
    }
63
64
65
}

void
66
67
tao_get_shared_array_timestamp(const tao_shared_array_t* arr,
                               int64_t* ts_sec, int64_t* ts_nsec)
68
{
Éric Thiébaut's avatar
Éric Thiébaut committed
69
    if (arr != NULL) {
70
71
72
73
74
75
        *ts_sec = arr->ts_sec;
        *ts_nsec = arr->ts_nsec;
    } else {
        *ts_sec = -1;
        *ts_nsec = 0;
    }
76
77
78
}

void
79
80
tao_set_shared_array_timestamp(tao_shared_array_t* arr,
                               int64_t ts_sec, int64_t ts_nsec)
81
{
Éric Thiébaut's avatar
Éric Thiébaut committed
82
    if (arr != NULL) {
83
84
85
        arr->ts_sec = ts_sec;
        arr->ts_nsec = ts_nsec;
    }
86
87
}

Éric Thiébaut's avatar
Éric Thiébaut committed
88
tao_shared_array_t*
89
tao_create_1d_shared_array(const char* owner,
90
                           tao_eltype_t eltype,
91
                           long dim1, unsigned perms)
Éric Thiébaut's avatar
Éric Thiébaut committed
92
{
93
94
    long dims[1];
    dims[0] = dim1;
95
    return tao_create_shared_array(owner, eltype, 1, dims, perms);
Éric Thiébaut's avatar
Éric Thiébaut committed
96
97
98
}

tao_shared_array_t*
99
tao_create_2d_shared_array(const char* owner, tao_eltype_t eltype,
100
                           long dim1, long dim2, unsigned perms)
Éric Thiébaut's avatar
Éric Thiébaut committed
101
{
102
103
104
    long dims[2];
    dims[0] = dim1;
    dims[1] = dim2;
105
    return tao_create_shared_array(owner, eltype, 2, dims, perms);
Éric Thiébaut's avatar
Éric Thiébaut committed
106
107
108
}

tao_shared_array_t*
109
tao_create_3d_shared_array(const char* owner, tao_eltype_t eltype,
110
                           long dim1, long dim2, long dim3,
111
                           unsigned perms)
Éric Thiébaut's avatar
Éric Thiébaut committed
112
{
113
114
115
116
    long dims[3];
    dims[0] = dim1;
    dims[1] = dim2;
    dims[2] = dim3;
117
    return tao_create_shared_array(owner, eltype, 3, dims, perms);
Éric Thiébaut's avatar
Éric Thiébaut committed
118
119
120
}

tao_shared_array_t*
121
tao_create_shared_array(const char* owner, tao_eltype_t eltype,
122
                        int ndims, const long dims[], unsigned perms)
Éric Thiébaut's avatar
Éric Thiébaut committed
123
{
124
    size_t elsize = tao_size_of_eltype(eltype);
Éric Thiébaut's avatar
Éric Thiébaut committed
125
    if (elsize < 1) {
126
        tao_push_error(__func__, TAO_BAD_TYPE);
Éric Thiébaut's avatar
Éric Thiébaut committed
127
128
        return NULL;
    }
129
    long nelem = tao_count_elements(ndims, dims);
130
    if (nelem < 1) {
Éric Thiébaut's avatar
Éric Thiébaut committed
131
132
        return NULL;
    }
133
134
    size_t offset = TAO_ROUND_UP(sizeof(tao_shared_array_t), ALIGNMENT);
    size_t nbytes = offset + nelem*elsize;
135
    tao_shared_object_t* obj = tao_create_shared_object(owner,
136
                                                        TAO_SHARED_ARRAY,
Éric Thiébaut's avatar
Éric Thiébaut committed
137
138
139
140
141
                                                        nbytes, perms);
    if (obj == NULL) {
        return NULL;
    }
    tao_shared_array_t* arr = (tao_shared_array_t*)obj;
142
    arr->offset = offset;
Éric Thiébaut's avatar
Éric Thiébaut committed
143
144
145
    arr->nelem = nelem;
    arr->ndims = ndims;
    for (int d = 0; d < ndims; ++d) {
146
        arr->dims[d] = dims[d];
Éric Thiébaut's avatar
Éric Thiébaut committed
147
148
    }
    for (int d = ndims; d < TAO_MAX_NDIMS; ++d) {
149
        arr->dims[d] = 1;
Éric Thiébaut's avatar
Éric Thiébaut committed
150
    }
151
    arr->eltype = eltype;
152
    arr->counter = 0;
153
154
155
156
    tao_time_t t;
    if (tao_get_monotonic_time(&t) == TAO_OK) {
        arr->ts_sec = t.sec;
        arr->ts_nsec = t.nsec;
157
158
    } else {
        tao_report_errors();
159
    }
Éric Thiébaut's avatar
Éric Thiébaut committed
160
161
162
    return arr;
}

Éric Thiébaut's avatar
Éric Thiébaut committed
163
164
165
166
167
/* Generic shared object methods. */
#define TYPE  tao_shared_array_t
#define NAME  shared_array
#define MAGIC TAO_SHARED_ARRAY
#include "./sharedgeneric.c"