This file is indexed.

/usr/lib/gcc/x86_64-linux-gnu/6/include/d/rt/monitor_.d is in libgphobos-6-dev 6.4.0-17ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
 * Contains the implementation for object monitors.
 *
 * Copyright: Copyright Digital Mars 2000 - 2015.
 * License:   $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
 * Authors:   Walter Bright, Sean Kelly, Martin Nowak
 */

/* NOTE: This file has been patched from the original DMD distribution to
 * work with the GDC compiler.
 */
module rt.monitor_;

import core.atomic, core.stdc.stdlib, core.stdc.string;

// NOTE: The dtor callback feature is only supported for monitors that are not
//       supplied by the user.  The assumption is that any object with a user-
//       supplied monitor may have special storage or lifetime requirements and
//       that as a result, storing references to local objects within Monitor
//       may not be safe or desirable.  Thus, devt is only valid if impl is
//       null.

extern (C) void _d_setSameMutex(shared Object ownee, shared Object owner) nothrow
in
{
    assert(ownee.__monitor is null);
}
body
{
    auto m = ensureMonitor(cast(Object) owner);
    auto i = m.impl;
    if (i is null)
    {
        atomicOp!("+=")(m.refs, cast(size_t) 1);
        ownee.__monitor = owner.__monitor;
        return;
    }
    // If m.impl is set (ie. if this is a user-created monitor), assume
    // the monitor is garbage collected and simply copy the reference.
    ownee.__monitor = owner.__monitor;
}

extern (C) void _d_monitordelete(Object h, bool det)
{
    auto m = getMonitor(h);
    if (m is null)
        return;

    if (m.impl)
    {
        // let the GC collect the monitor
        setMonitor(h, null);
    }
    else if (!atomicOp!("-=")(m.refs, cast(size_t) 1))
    {
        // refcount == 0 means unshared => no synchronization required
        disposeEvent(cast(Monitor*) m, h);
        deleteMonitor(cast(Monitor*) m);
        setMonitor(h, null);
    }
}

extern (C) void _d_monitorenter(Object h)
{
    auto m = cast(Monitor*) ensureMonitor(h);
    auto i = m.impl;
    if (i is null)
        lockMutex(&m.mtx);
    else
        i.lock();
}

extern (C) void _d_monitorexit(Object h)
{
    auto m = cast(Monitor*) getMonitor(h);
    auto i = m.impl;
    if (i is null)
        unlockMutex(&m.mtx);
    else
        i.unlock();
}

extern (C) void rt_attachDisposeEvent(Object h, DEvent e)
{
    synchronized (h)
    {
        auto m = cast(Monitor*) getMonitor(h);
        assert(m.impl is null);

        foreach (ref v; m.devt)
        {
            if (v is null || v == e)
            {
                v = e;
                return;
            }
        }

        auto len = m.devt.length + 4; // grow by 4 elements
        auto pos = m.devt.length; // insert position
        auto p = realloc(m.devt.ptr, DEvent.sizeof * len);
        import core.exception : onOutOfMemoryError;

        if (!p)
            onOutOfMemoryError();
        m.devt = (cast(DEvent*) p)[0 .. len];
        m.devt[pos + 1 .. len] = null;
        m.devt[pos] = e;
    }
}

extern (C) void rt_detachDisposeEvent(Object h, DEvent e)
{
    synchronized (h)
    {
        auto m = cast(Monitor*) getMonitor(h);
        assert(m.impl is null);

        foreach (p, v; m.devt)
        {
            if (v == e)
            {
                memmove(&m.devt[p], &m.devt[p + 1], (m.devt.length - p - 1) * DEvent.sizeof);
                m.devt[$ - 1] = null;
                return;
            }
        }
    }
}

nothrow:

extern (C) void _d_monitor_staticctor()
{
    version (Posix)
    {
        pthread_mutexattr_init(&gattr);
        pthread_mutexattr_settype(&gattr, PTHREAD_MUTEX_RECURSIVE);
    }
    initMutex(&gmtx);
}

extern (C) void _d_monitor_staticdtor()
{
    destroyMutex(&gmtx);
    version (Posix)
        pthread_mutexattr_destroy(&gattr);
}

package:

// This is what the monitor reference in Object points to
alias IMonitor = Object.Monitor;
alias DEvent = void delegate(Object);

version (GNU)
{
    import gcc.config;
    static if (GNU_Thread_Model == ThreadModel.Single)
        version = SingleThreaded;
    // Ignore ThreadModel, we don't want posix threads on windows and
    // will always use native threading instead.
}

version (SingleThreaded)
{
    alias Mutex = int;

    void initMutex(Mutex* mtx)
    {
    }

    void destroyMutex(Mutex* mtx)
    {
    }

    void lockMutex(Mutex* mtx)
    {
    }

    void unlockMutex(Mutex* mtx)
    {
    }
}
else version (Windows)
{
    version (CRuntime_DigitalMars)
    {
        pragma(lib, "snn.lib");
    }
    else version (CRuntime_Microsoft)
    {
        pragma(lib, "libcmt.lib");
        pragma(lib, "oldnames.lib");
    }
    import core.sys.windows.windows;

    alias Mutex = CRITICAL_SECTION;

    alias initMutex = InitializeCriticalSection;
    alias destroyMutex = DeleteCriticalSection;
    alias lockMutex = EnterCriticalSection;
    alias unlockMutex = LeaveCriticalSection;
}
else version (Posix)
{
    import core.sys.posix.pthread;

    alias Mutex = pthread_mutex_t;
    __gshared pthread_mutexattr_t gattr;

    void initMutex(pthread_mutex_t* mtx)
    {
        pthread_mutex_init(mtx, &gattr) && assert(0);
    }

    void destroyMutex(pthread_mutex_t* mtx)
    {
        pthread_mutex_destroy(mtx) && assert(0);
    }

    void lockMutex(pthread_mutex_t* mtx)
    {
        pthread_mutex_lock(mtx) && assert(0);
    }

    void unlockMutex(pthread_mutex_t* mtx)
    {
        pthread_mutex_unlock(mtx) && assert(0);
    }
}
else
{
    static assert(0, "Unsupported platform");
}

struct Monitor
{
    IMonitor impl; // for user-level monitors
    DEvent[] devt; // for internal monitors
    size_t refs; // reference count
    Mutex mtx;
}

private:

@property ref shared(Monitor*) monitor(Object h) pure nothrow
{
    return *cast(shared Monitor**)&h.__monitor;
}

private shared(Monitor)* getMonitor(Object h) pure
{
    return atomicLoad!(MemoryOrder.acq)(h.monitor);
}

void setMonitor(Object h, shared(Monitor)* m) pure
{
    atomicStore!(MemoryOrder.rel)(h.monitor, m);
}

__gshared Mutex gmtx;

shared(Monitor)* ensureMonitor(Object h)
{
    if (auto m = getMonitor(h))
        return m;

    auto m = cast(Monitor*) calloc(Monitor.sizeof, 1);
    assert(m);
    initMutex(&m.mtx);

    bool success;
    lockMutex(&gmtx);
    if (getMonitor(h) is null)
    {
        m.refs = 1;
        setMonitor(h, cast(shared) m);
        success = true;
    }
    unlockMutex(&gmtx);

    if (success)
    {
        // Set the finalize bit so that the monitor gets collected (Bugzilla 14573)
        import core.memory : GC;

        if (!(typeid(h).m_flags & TypeInfo_Class.ClassFlags.hasDtor))
            GC.setAttr(cast(void*) h, GC.BlkAttr.FINALIZE);
        return cast(shared(Monitor)*) m;
    }
    else // another thread succeeded instead
    {
        deleteMonitor(m);
        return getMonitor(h);
    }
}

void deleteMonitor(Monitor* m)
{
    destroyMutex(&m.mtx);
    free(m);
}

void disposeEvent(Monitor* m, Object h)
{
    foreach (v; m.devt)
    {
        if (v)
            v(h);
    }
    if (m.devt.ptr)
        free(m.devt.ptr);
}

// Bugzilla 14573
unittest
{
    import core.memory : GC;

    auto obj = new Object;
    assert(!(GC.getAttr(cast(void*) obj) & GC.BlkAttr.FINALIZE));
    ensureMonitor(obj);
    assert(getMonitor(obj) !is null);
    assert(GC.getAttr(cast(void*) obj) & GC.BlkAttr.FINALIZE);
}