thread_local_alloc.c 11.3 KB
Newer Older
1
/*
eg's avatar
.  
eg committed
2 3 4 5 6 7 8 9 10 11 12
 * Copyright (c) 2000-2005 by Hewlett-Packard Company.  All rights reserved.
 *
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
 *
 * Permission is hereby granted to use or copy this program
 * for any purpose,  provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 */
13

eg's avatar
.  
eg committed
14 15
#include "private/gc_priv.h"

16 17 18 19 20
#if defined(THREAD_LOCAL_ALLOC)

#ifndef THREADS
# error "invalid config - THREAD_LOCAL_ALLOC requires GC_THREADS"
#endif
eg's avatar
.  
eg committed
21 22 23

#include "private/thread_local_alloc.h"

24
#include <stdlib.h>
eg's avatar
.  
eg committed
25 26

#if defined(USE_COMPILER_TLS)
27
  __thread GC_ATTR_TLS_FAST
eg's avatar
.  
eg committed
28
#elif defined(USE_WIN32_COMPILER_TLS)
29
  __declspec(thread) GC_ATTR_TLS_FAST
eg's avatar
.  
eg committed
30 31 32 33 34
#endif
GC_key_t GC_thread_key;

static GC_bool keys_initialized;

35 36
/* Return a single nonempty freelist fl to the global one pointed to    */
/* by gfl.                                                              */
37 38 39 40 41 42

static void return_single_freelist(void *fl, void **gfl)
{
    if (*gfl == 0) {
      *gfl = fl;
    } else {
43 44
      void *q, **qptr;

45 46
      GC_ASSERT(GC_size(fl) == GC_size(*gfl));
      /* Concatenate: */
47 48 49 50 51 52
        qptr = &(obj_link(fl));
        while ((word)(q = *qptr) >= HBLKSIZE)
          qptr = &(obj_link(q));
        GC_ASSERT(0 == q);
        *qptr = *gfl;
        *gfl = fl;
53 54 55
    }
}

eg's avatar
.  
eg committed
56
/* Recover the contents of the freelist array fl into the global one gfl.*/
57
/* We hold the allocator lock.                                          */
eg's avatar
.  
eg committed
58 59 60 61 62
static void return_freelists(void **fl, void **gfl)
{
    int i;

    for (i = 1; i < TINY_FREELISTS; ++i) {
63 64 65 66 67 68
        if ((word)(fl[i]) >= HBLKSIZE) {
          return_single_freelist(fl[i], &gfl[i]);
        }
        /* Clear fl[i], since the thread structure may hang around.     */
        /* Do it in a way that is likely to trap if we access it.       */
        fl[i] = (ptr_t)HBLKSIZE;
eg's avatar
.  
eg committed
69
    }
70
    /* The 0 granule freelist really contains 1 granule objects.        */
71 72 73 74
#   ifdef GC_GCJ_SUPPORT
      if (fl[0] == ERROR_FL) return;
#   endif
    if ((word)(fl[0]) >= HBLKSIZE) {
75
        return_single_freelist(fl[0], &gfl[1]);
76
    }
eg's avatar
.  
eg committed
77 78
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#ifdef USE_PTHREAD_SPECIFIC
  /* Re-set the TLS value on thread cleanup to allow thread-local       */
  /* allocations to happen in the TLS destructors.                      */
  /* GC_unregister_my_thread (and similar routines) will finally set    */
  /* the GC_thread_key to NULL preventing this destructor from being    */
  /* called repeatedly.                                                 */
  static void reset_thread_key(void* v) {
    pthread_setspecific(GC_thread_key, v);
  }
#else
# define reset_thread_key 0
#endif

/* Each thread structure must be initialized.   */
/* This call must be made from the new thread.  */
GC_INNER void GC_init_thread_local(GC_tlfs p)
eg's avatar
.  
eg committed
95
{
96
    int i, j, res;
eg's avatar
.  
eg committed
97

98
    GC_ASSERT(I_HOLD_LOCK());
99 100 101 102 103
    if (!EXPECT(keys_initialized, TRUE)) {
        GC_ASSERT((word)&GC_thread_key % sizeof(word) == 0);
        res = GC_key_create(&GC_thread_key, reset_thread_key);
        if (COVERT_DATAFLOW(res) != 0) {
            ABORT("Failed to create key for local allocator");
eg's avatar
.  
eg committed
104
        }
105
        keys_initialized = TRUE;
eg's avatar
.  
eg committed
106
    }
107 108 109
    res = GC_setspecific(GC_thread_key, p);
    if (COVERT_DATAFLOW(res) != 0) {
        ABORT("Failed to set thread specific allocation pointers");
eg's avatar
.  
eg committed
110
    }
111 112 113 114 115 116 117 118 119 120 121
    for (j = 0; j < TINY_FREELISTS; ++j) {
        for (i = 0; i < THREAD_FREELISTS_KINDS; ++i) {
            p -> _freelists[i][j] = (void *)(word)1;
        }
#       ifdef GC_GCJ_SUPPORT
            p -> gcj_freelists[j] = (void *)(word)1;
#       endif
    }
    /* The size 0 free lists are handled like the regular free lists,   */
    /* to ensure that the explicit deallocation works.  However,        */
    /* allocation of a size 0 "gcj" object is always an error.          */
eg's avatar
.  
eg committed
122
#   ifdef GC_GCJ_SUPPORT
123
        p -> gcj_freelists[0] = ERROR_FL;
eg's avatar
.  
eg committed
124 125 126
#   endif
}

127 128
/* We hold the allocator lock.  */
GC_INNER void GC_destroy_thread_local(GC_tlfs p)
eg's avatar
.  
eg committed
129
{
130 131 132 133 134 135 136 137 138
    int k;

    /* We currently only do this from the thread itself.        */
    GC_STATIC_ASSERT(THREAD_FREELISTS_KINDS <= MAXOBJKINDS);
    for (k = 0; k < THREAD_FREELISTS_KINDS; ++k) {
        if (k == (int)GC_n_kinds)
            break; /* kind is not created */
        return_freelists(p -> _freelists[k], GC_obj_kinds[k].ok_freelist);
    }
eg's avatar
.  
eg committed
139
#   ifdef GC_GCJ_SUPPORT
140
        return_freelists(p -> gcj_freelists, (void **)GC_gcjobjfreelist);
eg's avatar
.  
eg committed
141 142 143
#   endif
}

144 145 146
#ifdef GC_ASSERTIONS
  /* Defined in pthread_support.c or win32_threads.c. */
  GC_bool GC_is_thread_tsd_valid(void *tsd);
eg's avatar
.  
eg committed
147 148
#endif

149
GC_API GC_ATTR_MALLOC void * GC_CALL GC_malloc_kind(size_t bytes, int knd)
eg's avatar
.  
eg committed
150
{
151
    size_t granules;
eg's avatar
.  
eg committed
152 153 154
    void *tsd;
    void *result;

155 156 157
#   if MAXOBJKINDS > THREAD_FREELISTS_KINDS
      if (EXPECT(knd >= THREAD_FREELISTS_KINDS, FALSE)) {
        return GC_malloc_kind_global(bytes, knd);
eg's avatar
.  
eg committed
158 159
      }
#   endif
160
#   if !defined(USE_PTHREAD_SPECIFIC) && !defined(USE_WIN32_SPECIFIC)
161
    {
162
      GC_key_t k = GC_thread_key;
163 164 165 166 167

      if (EXPECT(0 == k, FALSE)) {
        /* We haven't yet run GC_init_parallel.  That means     */
        /* we also aren't locking, so this is fairly cheap.     */
        return GC_malloc_kind_global(bytes, knd);
168 169
      }
      tsd = GC_getspecific(k);
170
    }
171
#   else
172 173
      if (!EXPECT(keys_initialized, TRUE))
        return GC_malloc_kind_global(bytes, knd);
174 175
      tsd = GC_getspecific(GC_thread_key);
#   endif
176 177 178
#   if !defined(USE_COMPILER_TLS) && !defined(USE_WIN32_COMPILER_TLS)
      if (EXPECT(0 == tsd, FALSE)) {
        return GC_malloc_kind_global(bytes, knd);
179 180 181
      }
#   endif
    GC_ASSERT(GC_is_initialized);
182 183 184 185 186 187 188 189 190 191 192 193
    GC_ASSERT(GC_is_thread_tsd_valid(tsd));
    granules = ROUNDED_UP_GRANULES(bytes);
    GC_FAST_MALLOC_GRANS(result, granules,
                         ((GC_tlfs)tsd) -> _freelists[knd], DIRECT_GRANULES,
                         knd, GC_malloc_kind_global(bytes, knd),
                         (void)(knd == PTRFREE ? NULL
                                               : (obj_link(result) = 0)));
#   ifdef LOG_ALLOCS
      GC_log_printf("GC_malloc_kind(%lu, %d) returned %p, recent GC #%lu\n",
                    (unsigned long)bytes, knd, result,
                    (unsigned long)GC_gc_no);
#   endif
eg's avatar
.  
eg committed
194 195 196 197 198
    return result;
}

#ifdef GC_GCJ_SUPPORT

199
# include "atomic_ops.h" /* for AO_compiler_barrier() */
eg's avatar
.  
eg committed
200

201
# include "include/gc_gcj.h"
202

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
/* Gcj-style allocation without locks is extremely tricky.  The         */
/* fundamental issue is that we may end up marking a free list, which   */
/* has freelist links instead of "vtable" pointers.  That is usually    */
/* OK, since the next object on the free list will be cleared, and      */
/* will thus be interpreted as containing a zero descriptor.  That's    */
/* fine if the object has not yet been initialized.  But there are      */
/* interesting potential races.                                         */
/* In the case of incremental collection, this seems hopeless, since    */
/* the marker may run asynchronously, and may pick up the pointer to    */
/* the next freelist entry (which it thinks is a vtable pointer), get   */
/* suspended for a while, and then see an allocated object instead      */
/* of the vtable.  This may be avoidable with either a handshake with   */
/* the collector or, probably more easily, by moving the free list      */
/* links to the second word of each object.  The latter isn't a         */
/* universal win, since on architecture like Itanium, nonzero offsets   */
/* are not necessarily free.  And there may be cache fill order issues. */
/* For now, we punt with incremental GC.  This probably means that      */
/* incremental GC should be enabled before we fork a second thread.     */
/* Unlike the other thread local allocation calls, we assume that the   */
/* collector has been explicitly initialized.                           */
GC_API GC_ATTR_MALLOC void * GC_CALL GC_gcj_malloc(size_t bytes,
                                    void * ptr_to_struct_containing_descr)
eg's avatar
.  
eg committed
225
{
226
  if (EXPECT(GC_incremental, FALSE)) {
227 228
    return GC_core_gcj_malloc(bytes, ptr_to_struct_containing_descr);
  } else {
eg's avatar
.  
eg committed
229 230
    size_t granules = ROUNDED_UP_GRANULES(bytes);
    void *result;
231
    void **tiny_fl = ((GC_tlfs)GC_getspecific(GC_thread_key))
232
                                        -> gcj_freelists;
eg's avatar
.  
eg committed
233
    GC_ASSERT(GC_gcj_malloc_initialized);
234
    GC_FAST_MALLOC_GRANS(result, granules, tiny_fl, DIRECT_GRANULES,
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
                         GC_gcj_kind,
                         GC_core_gcj_malloc(bytes,
                                            ptr_to_struct_containing_descr),
                         {AO_compiler_barrier();
                          *(void **)result = ptr_to_struct_containing_descr;});
        /* This forces the initialization of the "method ptr".          */
        /* This is necessary to ensure some very subtle properties      */
        /* required if a GC is run in the middle of such an allocation. */
        /* Here we implicitly also assume atomicity for the free list.  */
        /* and method pointer assignments.                              */
        /* We must update the freelist before we store the pointer.     */
        /* Otherwise a GC at this point would see a corrupted           */
        /* free list.                                                   */
        /* A real memory barrier is not needed, since the               */
        /* action of stopping this thread will cause prior writes       */
        /* to complete.                                                 */
        /* We assert that any concurrent marker will stop us.           */
        /* Thus it is impossible for a mark procedure to see the        */
        /* allocation of the next object, but to see this object        */
        /* still containing a free list pointer.  Otherwise the         */
        /* marker, by misinterpreting the freelist link as a vtable     */
        /* pointer, might find a random "mark descriptor" in the next   */
        /* object.                                                      */
eg's avatar
.  
eg committed
258
    return result;
259
  }
eg's avatar
.  
eg committed
260 261 262 263
}

#endif /* GC_GCJ_SUPPORT */

264 265 266 267 268
/* The thread support layer must arrange to mark thread-local   */
/* free lists explicitly, since the link field is often         */
/* invisible to the marker.  It knows how to find all threads;  */
/* we take care of an individual thread freelist structure.     */
GC_INNER void GC_mark_thread_local_fls_for(GC_tlfs p)
eg's avatar
.  
eg committed
269 270
{
    ptr_t q;
271 272
    int i, j;

273
    for (j = 0; j < TINY_FREELISTS; ++j) {
274 275 276 277 278
      for (i = 0; i < THREAD_FREELISTS_KINDS; ++i) {
        q = p -> _freelists[i][j];
        if ((word)q > HBLKSIZE)
          GC_set_fl_marks(q);
      }
eg's avatar
.  
eg committed
279
#     ifdef GC_GCJ_SUPPORT
280
        if (EXPECT(j > 0, TRUE)) {
281
          q = p -> gcj_freelists[j];
282 283 284 285
          if ((word)q > HBLKSIZE)
            GC_set_fl_marks(q);
        }
#     endif
eg's avatar
.  
eg committed
286 287 288 289
    }
}

#if defined(GC_ASSERTIONS)
290
    /* Check that all thread-local free-lists in p are completely marked. */
eg's avatar
.  
eg committed
291 292
    void GC_check_tls_for(GC_tlfs p)
    {
293 294 295 296 297 298 299 300 301 302
        int i, j;

        for (j = 1; j < TINY_FREELISTS; ++j) {
          for (i = 0; i < THREAD_FREELISTS_KINDS; ++i) {
            GC_check_fl_marks(&p->_freelists[i][j]);
          }
#         ifdef GC_GCJ_SUPPORT
            GC_check_fl_marks(&p->gcj_freelists[j]);
#         endif
        }
eg's avatar
.  
eg committed
303 304 305
    }
#endif /* GC_ASSERTIONS */

306
#endif /* THREAD_LOCAL_ALLOC */